-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Decorations API #5437
base: next
Are you sure you want to change the base?
WIP: Decorations API #5437
Conversation
bdbch
commented
Aug 3, 2024
•
edited by nperez0111
Loading
edited by nperez0111
- Decorations API
- Positions
- Attach Decorations to specific Nodes
- Attach Decorations to the cursor
- Manually set positions of a decoration
- We should use a DecorationManager that
- Handles the state for decorations
- Communicates with a DecorationPlugin to handle creation and rendering of Prosemirror Decorations
- Decoration Plugin
- Syncs it’s state with the decoration manager state
- uses the attached positions of decorations to determine what type of decoration is needed
- DecorationManager.create => editor.decorations.create
- The create api is similar to how we create extensions
- render: handles rendering and allows return of html or react / vue components
- binding events, f.e. onUpdate, onTransaction, etc.
- The create api is similar to how we create extensions
- TiptapDecoration
- attachTo(x)
- Can take a position, a range or a node
- The type of decoration is inferred from this attachTo value
- attachTo(x)
- Positions
|
❌ Deploy Preview for tiptap-embed failed. Why did it fail? →
|
Some ideas for how we might wanna structure this: const editor = new Editor()
editor.on('transaction', () => {
const removeDecoration = editor.addDecorationWidget({ ...options })
removeDecoration()
editor.getDecorationSet('myDecoration').forEach(({ from, to }) => {
editor.removeDecoration(from, to, 'myDecoration')
})
})
DecorationSet.create({
onTransaction,
shouldShow,
onCreate,
onRemove,
onUpdate,
name: 'myDecoration',
onEvent: (eventName, meta)=>{
if (eventName === 'eventName') {
// I want this decoration active
editor.addDecorationSet('myDecoration', decorationSet)
return;
}
}
onTransaction: tr => {
if (!tr.isOneIWant) {
editor.clearDecorationSet('myDecoration')
return
}
this.editor.addDecorationWidget()
},
})
class DecorationManager {
allDecorationSets: Record<string, DecorationSet> = {}
addDecorationSet(key: string, decorationSet: DecorationSet) {
this.allDecorationSets[key] = decorationSet
}
removeDecorationSet(key: string) {
this.allDecorationSets[key].destroy()
this.allDecorationSets[key] = undefined
}
listDecorationSets() {
return ['myDecoration', 'anotherDecorationSet']
}
getDecorationSet(key: string) {
}
triggerEvent(eventName: string) {
// re-run decoration sets
}
getProsemirrorPlugin() {
return new Plugin({
key: new PluginKey('decorationManager'),
state: {
init: () => this,
apply: (tr, plugin) => {
const decorationManager = plugin.getState(tr)
// map through transactions for all decorations that still exist
decorationManager.allDecorationSets.forEach(decorationSet => {
decorationSet.onTransaction(tr)
})
},
},
})
}
}
editor.triggerDecoration('eventName', meta)
editor.addDecorationSet('myDecoration', decorationSet)
editor.removeDecorationSet('myDecoration') |
b86ad19
to
eb0e379
Compare
6a9af9d
to
3290131
Compare
3290131
to
36489ba
Compare