/*\ title: $:/plugins/tiddlywiki/codemirror/engine.js type: application/javascript module-type: library Text editor engine based on a CodeMirror instance \*/ (function(){ /*jslint node: true, browser: true */ /*global $tw: false */ "use strict"; var CODEMIRROR_OPTIONS = "$:/config/CodeMirror", HEIGHT_VALUE_TITLE = "$:/config/TextEditor/EditorHeight/Height" // Install CodeMirror if($tw.browser && !window.CodeMirror) { window.CodeMirror = require("$:/plugins/tiddlywiki/codemirror/lib/codemirror.js"); // Install required CodeMirror plugins var configOptions = $tw.wiki.getTiddlerData(CODEMIRROR_OPTIONS,{}), req = configOptions.require; if(req) { if($tw.utils.isArray(req)) { for(var index=0; index 0) { var anchorPos = this.cm.indexFromPos(selections[0].anchor), headPos = this.cm.indexFromPos(selections[0].head); } var operation = { text: this.cm.getValue(), selStart: Math.min(anchorPos,headPos), selEnd: Math.max(anchorPos,headPos), cutStart: null, cutEnd: null, replacement: null, newSelStart: null, newSelEnd: null }; operation.selection = operation.text.substring(operation.selStart,operation.selEnd); return operation; }; /* Execute a text operation */ CodeMirrorEngine.prototype.executeTextOperation = function(operation) { // Perform the required changes to the text area and the underlying tiddler var newText = operation.text; if(operation.replacement !== null) { this.cm.replaceRange(operation.replacement,this.cm.posFromIndex(operation.cutStart),this.cm.posFromIndex(operation.cutEnd)); this.cm.setSelection(this.cm.posFromIndex(operation.newSelStart),this.cm.posFromIndex(operation.newSelEnd)); newText = operation.text.substring(0,operation.cutStart) + operation.replacement + operation.text.substring(operation.cutEnd); } this.cm.focus(); return newText; }; exports.CodeMirrorEngine = CodeMirrorEngine; })();