Open
Conversation
|
@einarlove is attempting to deploy a commit to the Sanity Sandbox Team on Vercel. A member of the Team first needs to authorize it. |
Author
|
To use this myself, I've created my own forked package while I wait. If you want to test this directly, you can also replace your dependency: - "sanity-plugin-media": "^4.1.1",
+ "sanity-plugin-media": "npm:@einarlove/sanity-plugin-media@^4.1.1-einarlove.0", |
This plugin hasn't been updated in a while, has it? Does anyone know why? |
|
Would this be merged soon? |
I asked for information on Discord |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #200
Description
Adds the ability to automatically tag assets when they are uploaded through image field. This helps keep the media library organised without relying on users to manually tag assets.
When opening the Media Browser from a field with
mediaTagsconfigured, the browser now automatically filters to show only assets that have those tags, making it easier to find relevant assets.Usage
Add a
mediaTagsoption to any image or file field:When a user uploads an image through this field, the asset will automatically be tagged with "employee" and "portrait".
When a user opens the Media Browser from this field, the browser will automatically filter to show only assets tagged with both "employee" and "portrait".
How it works
flowchart TD Upload[User uploads asset] --> Wrapper[AutoTagInputWrapper] Wrapper --> CheckType{Is image or file field?} CheckType -->|No| RenderDefault[Render default input] CheckType -->|Yes| CheckTags{Has mediaTags option?} CheckTags -->|No| RenderDefault CheckTags -->|Yes| WatchChanges[Watch for asset._ref changes] WatchChanges --> DetectNew{New asset detected?} DetectNew -->|No| RenderDefault DetectNew -->|Yes| ApplyTags[applyMediaTags] ApplyTags --> ResolveLoop[For each tag name] ResolveLoop --> FindTag[Query existing tag by name] FindTag --> TagExists{Tag exists?} TagExists -->|Yes| UseExisting[Use existing tag] TagExists -->|No| CheckCreate{createTagsOnUpload?} CheckCreate -->|Yes| CreateTag[Create new tag document] CheckCreate -->|No| Skip[Skip tag] UseExisting --> CollectRefs[Collect tag references] CreateTag --> CollectRefs Skip --> CollectRefs CollectRefs --> HasRefs{Has valid tags?} HasRefs -->|No| Done[Done] HasRefs -->|Yes| PatchAsset[Patch asset with tag references] PatchAsset --> DoneWorks with both the Media Browser and the native Sanity upload button (including drag-and-drop).
Changes
src/utils/applyMediaTags.ts- Finds or creates tags and patches the assetsrc/components/AutoTagInputWrapper/index.tsx- Global input wrapper that detects new asset uploadsform.components.inputcreateTagsOnUpload(default:true) - Controls whether non-existent tags should be created automaticallyMediaTagsFieldOptions- For TypeScript users to type the field optionsmediaTagswhen opening from a fieldAuto-filtering on browser open
When the Media Browser is opened from an asset field with
mediaTagsconfigured:referencesoperatorNew files:
src/components/Browser/prefilterByMediaTags.ts- Tag resolution and facet seeding logicsrc/components/Browser/useBrowserInit.ts- Initialization hook (assets, tags, real-time sync)Plugin configuration
Notes
mediaTagsconfigured