In the underlining command example, the provided code allows adding an underline; this is accomplished using the update
method:
underlines = underlines.update({
add: [underlineMark.range(e.value.from, e.value.to)]
})
I’m unsure how best to use update
to look at the previous state when computing the next state. For example, if anything in the range is already underlined, I’d like to remove the underline; otherwise, I’d like to (as the example currently does) add an underline. While I see that it’s easy to reference the values from the effect (e.value.from/to
), I don’t know how to easily access existing values, then modify them.
Is the best approach to use between
? Perhaps:
// Look for any existing underline in this effect's range.
let is_underlined = false;
underlines.between(e.value.from, e.value.to, () =>
{ is_underlined = true; return true; });
// Remove previous underlines; add another if there were no previous underlines.
underlines = underlines.update({
filterFrom: e.value.from,
filterTo: e.value.to,
add: is_underlined ? [] : [underlineMark.range(e.value.from, e.value.to)]
});