Note that there are some explanatory texts on larger screens.

plurals
  1. POCode Mirror: Get Text Around Changes
    primarykey
    data
    text
    <p>Using Code Mirror, I need to map change event data to a list of tuples containing:</p> <p><code>(Text before change, Change text, Text after change)</code></p> <p>I am currently listening on the <code>change</code> event and can handle input changes, but have to code specific solutions for deletions, selection movements, and undos.</p> <p>Is there a more reliable approach that works with the standard events? </p> <h1>Edit</h1> <p><em>Adding current work</em>.</p> <p>Thinking a bit more, I only care about the first (in terms of position) change in a document. Here is what I currently do:</p> <pre><code>var docStart = {'line': 0, 'ch': 0}, docEnd = {'line': Infinity, 'ch': Infinity}; // Just assume that we always have a single change and it is first // for this example. cm.on('change', function(cm, change) { var start, end, text; switch (change.origin) { case '+delete': start = change.from; end = change.from; text = ''; break; case 'undo': start = change.from; end = change.from; text = change.text.join('\n'); break; case 'redo': start = change.from; end = {'line': change.to.line, 'ch': change.to.ch + 1}; text = ''; break; default: start = change.from; end = {'line': change.to.line, 'ch': change.to.ch + 1}; text = change.text.join('\n'); break; } var pre = cm.doc.getRange(docStart, start); var post = cm.doc.getRange(end, docEnd); [pre, text, post]; // output }; </code></pre> <p>This is not correct. Not all event types are handled and many cases like line terminators also are not handled correctly or constantly. An alternative would be greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload