Hello,
i’ve never used older version of codemirror, starting straight with v6, and i’d like to clarify a few concepts/keywords for my use-case…
I have a simple editor with plaintex (custom DSL with no currently existing CM6 language extension). I’d like to add a simple higlighting to a single range of a text in editor’s content.
Type of highlight i expect is similar to “selection” or a gray-ish background highlight for pre-formatted text here on discourse for anything between backticks `like this
`
This is not to be done on keypress in editor (thus my assumption i dont need to define the Command/binding/keymaps that’s in the latter part of the examples) but driven by external HTML event/other component…
E.g. i have text in editor:
container outer {
leaf hello;
leaf there;
}
and want to define JS/TS function to trigger/set custom mark decoration for all the text in range of “from” → “to”. e.g. range of 20 -> 31
would “decorate” the text leaf hello;
.
Looking into decoration examples on CM page, my current understanding is, i need to use view
and run a transaction that will add/remove/overwrite my decoration…
// reference to my existing editor/view
const view = ...;
// i have no textual "changes" to be done,
// so i guess i need to apply some "effect" here?
const transaction = view.state.update({
effects: {...}
});
view.dispatch(transaction);
At this point however, i am completely lost as for actual transaction payload…
What concepts/object types have to be included in the transaction itself that would lead to a specific text range highlight? Please note that following is not a criticism, but my observations that may not be complete/correct, and i’d like a pointers/push to right directions…
-
There’s a mention of
RangeSet
but example page then does not instantiate any explicitly with this type defintion/import. -
do i need code similar to this, when i dont want to edit/update any editor’s text in the transaction doing the highlight?
const addUnderline = StateEffect.define<{from: number, to: number}>({ map: ({from, to}, change) => ({from: change.mapPos(from), to: change.mapPos(to)}) })
-
What’s the use-case of a
StateField
object, reps. what does it represent? (from architectural point of view) -
The
const underlineField = StateField.define<DecorationSet>({
part if confusing for me → do i need to define create/update/provide callbacks? Reps. when do i do, and when not? It’s a bit confusing to me what are the actualunderlines
in the update callback.
thanks for all/any feedback, and apologies for the wall of text!
j.