Skip to content

Instantly share code, notes, and snippets.

@burg
Created November 13, 2015 19:41
Show Gist options
  • Save burg/15c30881b975e3399af2 to your computer and use it in GitHub Desktop.
Save burg/15c30881b975e3399af2 to your computer and use it in GitHub Desktop.
CodeMirror handleKillKey
_handleKillKey(codeMirror)
{
let handleTextChanges = (cm, changes) => {
console.assert(this._expectTextChanges);
this._expectTextChanges = false;
console.log(changes);
console.log("changes OFF, blur ON, move ON");
let killedText;
console.assert(changes.length === 1);
let change = changes[0];
console.assert(change.origin === "+delete");
if (change.to.line === change.from.line + 1)
killedText = "\n";
else {
console.assert(change.removed.length === 1);
killedText = change.removed[0];
}
console.log("killed text: " + killedText);
console.log("starts new sequence: " + !this._appendToKillSequence);
cm.off("changes", handleTextChanges);
cm.on("blur", handleBlur);
cm.on("cursorActivity", handleCursorOrSelectionMovement);
this._appendToKillSequence = true;
};
let handleBlur = (cm) => {
this._appendToKillSequence = false;
console.log("blurred");
console.log("blur OFF, move OFF");
cm.off("blur", handleBlur);
cm.off("cursorActivity", handleCursorOrSelectionMovement);
};
let handleCursorOrSelectionMovement = (cm) => {
if (this._expectTextChanges)
return;
this._appendToKillSequence = false;
console.log("selection or cursor moved");
console.log("blur OFF, move OFF");
cm.off("blur", handleBlur);
cm.off("cursorActivity", handleCursorOrSelectionMovement);
};
let keyHandled = (cm, name, event) => {
console.log("keyHandled");
cm.off("keyHandled", keyHandled);
};
console.log("Killing line");
this._expectTextChanges = true;
console.log("changes ON, blur OFF, move OFF");
codeMirror.on("keyHandled", keyHandled);
codeMirror.on("changes", handleTextChanges);
codeMirror.operation()
codeMirror.execCommand("killLine");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment