From d7d5165847f3bc9856bff55f29f5b59468b3e110 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Bolila?= Date: Mon, 13 Jan 2014 23:34:11 +0000 Subject: [PATCH] all configuration and required libs in the config tiddler --- .../tiddlywiki/codemirror/codemirroreditor.js | 50 ++++++++++++------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/plugins/tiddlywiki/codemirror/codemirroreditor.js b/plugins/tiddlywiki/codemirror/codemirroreditor.js index 588506e28..f6e288bdb 100644 --- a/plugins/tiddlywiki/codemirror/codemirroreditor.js +++ b/plugins/tiddlywiki/codemirror/codemirroreditor.js @@ -12,25 +12,41 @@ Extend the edit-text widget to use CodeMirror /*global $tw: false */ "use strict"; -var CODEMIRROR_KEYMAP = "$:/CodeMirrorKeymap", - kbinding, kbindingsAllowed = /^(vim|emacs)$/i; +var CODEMIRROR_OPTIONS = "$:/config/CodeMirror", configOptions; +/* 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"); - kbinding = ($tw.wiki.getTiddlerText(CODEMIRROR_KEYMAP) || '').match(kbindingsAllowed); - if(kbinding) { - require("$:/plugins/tiddlywiki/codemirror/addon/dialog.js"); - require("$:/plugins/tiddlywiki/codemirror/addon/searchcursor.js"); - if(kbinding[0].toLowerCase() === 'emacs') { - require("$:/plugins/tiddlywiki/codemirror/keymap/emacs.js"); - EditTextWidget._codemirrorMode = 'emacs'; - } - else if(kbinding[0].toLowerCase() === 'vim') { - require("$:/plugins/tiddlywiki/codemirror/keymap/vim.js"); - EditTextWidget._codemirrorMode = 'vim'; + + configOptions = $tw.wiki.getTiddlerData(CODEMIRROR_OPTIONS,{}); + + if(configOptions) { + if(configOptions["require"]) { + if(typeof configOptions["require"] === 'object' && + Object.prototype.toString.call(configOptions["require"]) == '[object Array]') { + for (var idx=0; idx < configOptions["require"].length; idx++) { + require(configOptions["require"][idx]); + } + } + else { + require(configOptions["require"]); + } } + EditTextWidget._configuration = configOptions["configuration"]; } } @@ -39,14 +55,14 @@ The edit-text widget calls this method just after inserting its dom nodes */ EditTextWidget.prototype.postRender = function() { var self = this, - cm, cm_opts = { + cm, config, cv, cm_opts = { lineWrapping: true, lineNumbers: true }; + if($tw.browser && window.CodeMirror && this.editTag === "textarea") { - if(EditTextWidget._codemirrorMode) { - cm_opts["keyMap"] = EditTextWidget._codemirrorMode; - cm_opts["showCursorWhenSelecting"] = true; + 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() {