2013-05-07 13:42:47 +00:00
|
|
|
/*\
|
2013-11-08 08:47:00 +00:00
|
|
|
title: $:/core/modules/widgets/edit-text-codemirror.js
|
2013-05-07 13:42:47 +00:00
|
|
|
type: application/javascript
|
2013-11-08 08:47:00 +00:00
|
|
|
module-type: widget
|
2013-05-07 13:42:47 +00:00
|
|
|
|
2013-10-25 08:48:57 +00:00
|
|
|
Extend the edit-text widget to use CodeMirror
|
2013-05-07 13:42:47 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2014-01-13 23:34:11 +00:00
|
|
|
var CODEMIRROR_OPTIONS = "$:/config/CodeMirror", configOptions;
|
2014-01-24 21:45:37 +00:00
|
|
|
/*
|
|
|
|
e.g. to allow vim key bindings
|
|
|
|
{
|
|
|
|
"require": [
|
|
|
|
"$:/plugins/tiddlywiki/codemirror/addon/dialog.js",
|
|
|
|
"$:/plugins/tiddlywiki/codemirror/addon/searchcursor.js",
|
|
|
|
"$:/plugins/tiddlywiki/codemirror/keymap/vim.js"
|
|
|
|
],
|
|
|
|
"configuration": {
|
|
|
|
"keyMap": "vim",
|
|
|
|
"showCursorWhenSelecting": true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2013-05-07 13:42:47 +00:00
|
|
|
|
2013-11-08 08:47:00 +00:00
|
|
|
var EditTextWidget = require("$:/core/modules/widgets/edit-text.js")["edit-text"];
|
2013-05-07 13:42:47 +00:00
|
|
|
|
2014-01-11 15:23:49 +00:00
|
|
|
if($tw.browser) {
|
|
|
|
require("$:/plugins/tiddlywiki/codemirror/codemirror.js");
|
2014-01-13 23:34:11 +00:00
|
|
|
|
|
|
|
configOptions = $tw.wiki.getTiddlerData(CODEMIRROR_OPTIONS,{});
|
|
|
|
|
|
|
|
if(configOptions) {
|
|
|
|
if(configOptions["require"]) {
|
2014-01-25 14:07:10 +00:00
|
|
|
if($tw.utils.isArray(configOptions["require"])) {
|
2014-01-24 21:45:37 +00:00
|
|
|
for (var index=0; index < configOptions["require"].length; index++) {
|
|
|
|
require(configOptions["require"][index]);
|
2014-01-13 23:34:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
require(configOptions["require"]);
|
|
|
|
}
|
2014-01-11 15:23:49 +00:00
|
|
|
}
|
2014-01-24 21:45:37 +00:00
|
|
|
EditTextWidget.configuration = configOptions["configuration"];
|
2014-01-11 15:23:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-07 13:42:47 +00:00
|
|
|
/*
|
2013-10-25 08:48:57 +00:00
|
|
|
The edit-text widget calls this method just after inserting its dom nodes
|
2013-05-07 13:42:47 +00:00
|
|
|
*/
|
2013-10-25 08:48:57 +00:00
|
|
|
EditTextWidget.prototype.postRender = function() {
|
|
|
|
var self = this,
|
2014-01-13 23:34:11 +00:00
|
|
|
cm, config, cv, cm_opts = {
|
2013-10-25 08:48:57 +00:00
|
|
|
lineWrapping: true,
|
|
|
|
lineNumbers: true
|
2014-01-11 15:23:49 +00:00
|
|
|
};
|
2014-01-13 23:34:11 +00:00
|
|
|
|
2014-01-11 15:23:49 +00:00
|
|
|
if($tw.browser && window.CodeMirror && this.editTag === "textarea") {
|
2014-01-24 21:45:37 +00:00
|
|
|
if(EditTextWidget.configuration) {
|
|
|
|
for (cv in EditTextWidget.configuration) { cm_opts[cv] = EditTextWidget.configuration[cv]; }
|
2014-01-11 15:23:49 +00:00
|
|
|
}
|
|
|
|
cm = CodeMirror.fromTextArea(this.domNodes[0], cm_opts);
|
2013-10-25 08:48:57 +00:00
|
|
|
cm.on("change",function() {
|
|
|
|
self.saveChanges(cm.getValue());
|
|
|
|
});
|
2013-05-07 13:42:47 +00:00
|
|
|
} else {
|
2013-10-25 08:48:57 +00:00
|
|
|
cm = undefined;
|
2013-05-07 13:42:47 +00:00
|
|
|
}
|
2013-10-25 08:48:57 +00:00
|
|
|
this.codemirrorInstance = cm;
|
2013-05-07 13:42:47 +00:00
|
|
|
};
|
|
|
|
|
2013-10-25 08:48:57 +00:00
|
|
|
EditTextWidget.prototype.updateEditor = function(text) {
|
|
|
|
// Replace the edit value if the tiddler we're editing has changed
|
|
|
|
if(this.codemirrorInstance) {
|
|
|
|
if(!this.codemirrorInstance.hasFocus()) {
|
|
|
|
this.codemirrorInstance.setValue(text);
|
|
|
|
}
|
|
|
|
} else {
|
2013-10-30 14:28:20 +00:00
|
|
|
this.updateEditorDomNode(this.getEditInfo().value);
|
2013-05-07 13:42:47 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|