Is there a hook to listen for selection change on the proseMirror view or state?
At the moment, I am listening for a mouseUp event on the container dom element and then reading the selection from the editorState after a delay of 150ms.
I would like to remove the need for a delay and use a custom handler.
Is there the equivalent of handleSelection on the props.
For example:handleSelection: (view, dispatch, selection) => true
Where you can set the selection manually using setSelection with a transaction?
Observe transactions and compare their selection to the previous one (or use selectionSet if you want to know whether it was explicitly set or just moved to follow changes)
The plugin is supposed to update a decorationSet when the document is clicked.
Currently, the console.log fires when you start dragging a new selection and that is where the issue lies. Is there a way within the transaction to see the events?
Or for there to be methods that can override the default:
handleCollapsedSelection
handleCollapsedSelection
export default () =>
new Plugin({
state: {
init(config, editorState) {
const { doc } = editorState;
return initDecorationSet(doc);
},
apply(transaction, decorationSet) {
const { docChanged, curSelection } = transaction;
if (!docChanged && selectionIsCollapsed(curSelection)) {
// This fires when the mouse is still mid selection
console.log('This should only be called when the selection is a click event not a drag event')
return updateDecorationSet(decorationSet, curSelection);
}
return decorationSet;
},
},
props: {
decorations(state) {
return this.getState(state);
},
},
});
You may be able to use the "pointer"meta property, which is set to true on transactions that were caused (directly) by the pointer device. If thatâs there, and the selection is empty, it was probably a click.
Ah I didnât know about the âpointerâ meta property - It looks like this is still present when dragging a selection which makes it unsuitable in this scenario.
When I use handleDOMEvents it looks like it never gets called⌠which is also a little odd. Am I missing something?
I donât have much experience with collab, but I suspect that transactions that originate from non-local sources are marked with some type of metadata. Try outputting transactions to the console and look for metadata that only exists for transactions generated non-locally.
Not really, by default. You should be able to rather easily add a metadata property to the transactions created for remote changes, though, and check for that.