Updating and maintaining textMarked code

I just want to make sure I’m approaching my problem correctly. I’m setting up an educational application, in which I want much of the code at any given time to be read-only. The various text ranges need to be kept separate as code is updated, even when multiple sequential ranges are read-only.

I’ll be adding lines in code at initialization, and from time to time, however. So, here’s what I’ve found so far:

  1. Although I’d like to be able to restrict the user from adding or deleting new lines, this doesn’t appear to be possible. I can use keystroke events to prevent the user from removing a line, but especially when typing quickly, they don’t appear to work consistently; and there seems to be no way to prevent the user from pasting multiple-line text. (In fact, there seems to be no reasonable way to restrict use of the clipboard at all).
  2. When I try to divide up all text between ranges, so that all characters in any given line are in exactly one range, this also appears to be impossible. I’ve tried various combinations of inclusiveLeft and inclusiveRight, but it seems that it’s always possible to enter characters outside of established ranges, especially at the end of the document. So I will have to correct the ranges after the fact.
  3. When use replaceRange or setValue to add text, I will need to adjust all the existing ranges to conform to the changes. According to this post:
    Way to update content covered by text marker and keep text marker attached to document?
    …it seems that I will need to recreate all textMark ranges after updating the contents. Since I will be making multiple changes to the text at a time, the easiest way to do this will be to use setValue rather than multiple replaceRanges, since replaceRange will not automatically update existing textMark ranges.
  4. Also, when I run replaceRange rather than setValue to set up the initial text on startup, replaceRange appeared to terminate code execution rather than returning to the calling routine…? I am probably screwing something up, because I assume this isn’t how it normally works?

So in summary, I will use other events to correct user changes to the textMark ranges after the fact, rather than prevent them from messing them up, and I’ll recreate all the ranges every time I add or remove text from the contents. I’ll use setValue rather than replaceRange to update the contents.

Does this sound like the correct approach? Thank you!

Use the beforeChange event.

I have no idea what goal you are trying to accomplish here. If you don’t want people entering text between ranges, why not create a single range?

No you don’t. All ranges outside of the replaced area will be updated.

Nope, it does not do that. You might be passing it invalid parameters causing it to throw an exception. Learn to use the browser’s developer console to see the error message.

Thanks very much Marijn! That sounds great.

I need to keep different sections of content separate so my system can evaluate each section separately and give accurate feedback to the user about their content. If I can use the beforeChange event to keep the user from changing line breaks, it will be much simpler to keep these clean. If the user manages to mess up the ranges, I can fix them.

Sounds like the beforeChange event is not a good place to fix these issues, since it’s not used to change content. I’ll try to check and repair the marked ranges in a keyboard event, unless you have a better suggestion.