Hello – thank you for this library: it works well, and learning how to write an extension has been very fun and pleasant.
- I’m using CodeMirror via GitHub - uiwjs/react-codemirror: CodeMirror 6 component for React. @codemirror https://uiwjs.github.io/react-codemirror/ in my app to let a user edit some CSS.
- This CSS text can be edited in other ways by other views, potentially while CodeMirror is on-screen.
- To keep the CSS text in sync across all of these views, I’m storing the text in an external store, which CodeMirror subscribes and publishes changes to – done via react-codemirror’s
value
andonChange
prop, this roundtrip happens on every CodeMirror update (https://github.com/uiwjs/react-codemirror/blob/fd6a8dbc731d87344f7c9016bbf151006a5b89a2/core/src/useCodeMirror.ts#L67)
This all functions fine for me so far – but I’m trying to add a new feature:
- User highlights a CSS color literal, which adds a Decoration to that color text’s range
- User edits the CSS color in some non-CodeMirror view, which edits the external store’s text
- This triggers a full-text replace https://github.com/uiwjs/react-codemirror/blob/fd6a8dbc731d87344f7c9016bbf151006a5b89a2/core/src/useCodeMirror.ts#L164-L167 which clears out the Decoration !
Is there a way to maintain these Decorations after this replace? It seems tough, since the full-text replace could be a complete rewrite of the content, which would render the Decorations meaningless – but I’m curious if you see a good solution that lets me keep the source of truth outside of CodeMirror. (If not, I can move it into CodeMirror, just not ideal for other parts of app.)
Update: I found a decent workaround: I’m inserting magic keywords into the actual CSS text that signal the start and end of a CSS color (e.g. /* start_color */#abcdef/* end_color */
), and using a view plugin to replace that text – including magic keywords – with a widget. Magic keywords are preserved across full-text replaces, so things work as expected.
I’d love to get rid of these magic keywords and use internal state if possible, but I get that I might be asking CodeMirror to infer too much.