2013-05-07 13:42:47 +00:00
|
|
|
/*\
|
2013-10-25 08:48:57 +00:00
|
|
|
title: $:/core/modules/new_widgets/edit-text-codemirror.js
|
2013-05-07 13:42:47 +00:00
|
|
|
type: application/javascript
|
2013-10-25 08:48:57 +00:00
|
|
|
module-type: new_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";
|
|
|
|
|
|
|
|
if($tw.browser) {
|
2013-10-25 08:48:57 +00:00
|
|
|
require("$:/plugins/tiddlywiki/codemirror/codemirror.js");
|
2013-05-07 13:42:47 +00:00
|
|
|
}
|
|
|
|
|
2013-10-25 08:48:57 +00:00
|
|
|
var EditTextWidget = require("$:/core/modules/new_widgets/edit-text.js")["edit-text"];
|
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,
|
|
|
|
cm;
|
|
|
|
if($tw.browser && window.CodeMirror && this.editTag === "textarea") {
|
|
|
|
cm = CodeMirror.fromTextArea(this.domNodes[0],{
|
|
|
|
lineWrapping: true,
|
|
|
|
lineNumbers: true
|
|
|
|
});
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|