1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-14 13:54:50 +00:00
TiddlyWiki5/plugins/tiddlywiki/codemirror/codemirroreditor.js

89 lines
2.1 KiB
JavaScript
Raw Normal View History

/*\
title: $:/core/modules/widgets/edit-text-codemirror.js
type: application/javascript
module-type: widget
Extend the edit-text widget to use CodeMirror
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
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
}
}
*/
var EditTextWidget = require("$:/core/modules/widgets/edit-text.js")["edit-text"];
if($tw.browser) {
require("$:/plugins/tiddlywiki/codemirror/codemirror.js");
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]);
}
}
else {
require(configOptions["require"]);
}
}
2014-01-24 21:45:37 +00:00
EditTextWidget.configuration = configOptions["configuration"];
}
}
/*
The edit-text widget calls this method just after inserting its dom nodes
*/
EditTextWidget.prototype.postRender = function() {
var self = this,
cm, config, cv, cm_opts = {
lineWrapping: true,
lineNumbers: true
};
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]; }
}
cm = CodeMirror.fromTextArea(this.domNodes[0], cm_opts);
cm.on("change",function() {
self.saveChanges(cm.getValue());
});
} else {
cm = undefined;
}
this.codemirrorInstance = cm;
};
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 {
this.updateEditorDomNode(this.getEditInfo().value);
}
};
})();