I have a properties editor that allows displaying and changing the attributes of a given, selected node. This is a seperate ‘panel’ which I effectively only want to to (re)draw whenever:
Here are some rough code. The detail need you to sculpt it.
const pluginKey = new PluginKey('plugin_my-panel');
new Plugin<HTMLElement>({
key: pluginKey,
state: {
init() { return null; },
apply(tr, value) {
if (!value) return tr.getMeta(pluginKey);
return value;
},
},
props: {
handleDOMEvents: {
// sometimes you may need determine whether re-render the panel through event
mousedown() {return false;},
keyup() {return false;},
},
},
view(view) {
const panel = document.createElement('div');
setTimeout(() => view.dispatch(view.state.tr.setMeta(pluginKey, panel)));
return {
update(view, prevState) {
// note: this method will be called every time when the selection or doc of the editor changed
// so you need to write your own logic to determine when re-render the panel
renderPanel(view);
},
};
},
});
function renderPanel(view: EditorView) {
const { state } = view;
const panel = pluginKey.getState(state);
// find your target node
const node = findTargetNode();
panel.innerHTML = JSON.stringify(node.attrs)
}
function findTargetNode(){ ... }
0, 1 and 2 are all Vue3 components that are rooted in a container Vue3 application shell. Following your line of thought of using a plugin; would it be possible for that plugin to emit a custom DOM event when redraw is required? The application shell would then listen for that event and delegate it to observers.
O, 1 and 2 are sibling components - and I don’t want to add any additional state mgmt library for event distribution.
With this and the UniqueID plugin of tiptap UniqueID – Tiptap Editor, I could effectively determine when the properties editor needed to be redrawn (i.e. set nodesChanged = true and a watch on that property, to recalculate stuff)