From d16481c5240a30c36a551f083e2e4dbe9ee5089d Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Wed, 11 Jul 2012 15:52:15 +0100 Subject: [PATCH] Refactored editor logic to handle refreshes more efficiently --- core/modules/macros/edit/edit.js | 12 ++------ .../macros/edit/editors/bitmapeditor.js | 5 ---- .../modules/macros/edit/editors/texteditor.js | 28 +++++++++++++------ 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/core/modules/macros/edit/edit.js b/core/modules/macros/edit/edit.js index 6d429c18e..27671cd02 100644 --- a/core/modules/macros/edit/edit.js +++ b/core/modules/macros/edit/edit.js @@ -50,16 +50,8 @@ exports.refreshInDom = function(changes) { var t; // Only refresh if a dependency is triggered if(this.dependencies.hasChanged(changes,this.tiddlerTitle)) { - // Only refresh if the editor lets us - if(this.editor.isRefreshable()) { - // Remove the previous child - var parent = this.child.domNode.parentNode, - nextSibling = this.child.domNode.nextSibling; - parent.removeChild(this.child.domNode); - // Execute the macro - this.execute(this.parents,this.tiddlerTitle); - // Render to the DOM - this.child.renderInDom(parent,nextSibling); + if(this.editor.refreshInDom) { + this.editor.refreshInDom(); } } else { // Refresh any children diff --git a/core/modules/macros/edit/editors/bitmapeditor.js b/core/modules/macros/edit/editors/bitmapeditor.js index 29a332f8e..e00fbaf6e 100644 --- a/core/modules/macros/edit/editors/bitmapeditor.js +++ b/core/modules/macros/edit/editors/bitmapeditor.js @@ -156,11 +156,6 @@ BitmapEditor.prototype.saveChanges = function() { } }; -BitmapEditor.prototype.isRefreshable = function() { - // Don't ever refresh the bitmap editor - return false; -}; - exports["image/jpg"] = BitmapEditor; exports["image/jpeg"] = BitmapEditor; exports["image/png"] = BitmapEditor; diff --git a/core/modules/macros/edit/editors/texteditor.js b/core/modules/macros/edit/editors/texteditor.js index 547cfd8fe..eb9fbe671 100644 --- a/core/modules/macros/edit/editors/texteditor.js +++ b/core/modules/macros/edit/editors/texteditor.js @@ -18,7 +18,10 @@ function TextEditor(macroNode) { this.macroNode = macroNode; } -TextEditor.prototype.getChild = function() { +/* +Get the tiddler being editted, field name and current value +*/ +TextEditor.prototype.getEditText = function() { // Get the current tiddler and the field name var tiddler = this.macroNode.wiki.getTiddler(this.macroNode.tiddlerTitle), field = this.macroNode.hasParameter("field") ? this.macroNode.params.field : "title", @@ -40,19 +43,24 @@ TextEditor.prototype.getChild = function() { break; } } + return {tiddler: tiddler, field: field, value: value}; +}; + +TextEditor.prototype.getChild = function() { + var edit = this.getEditText(); var attributes = { "class": ["tw-edit-field"] }, tagName, content = []; // Make a textarea for text fields and an input box for other fields - if(field === "text") { + if(edit.field === "text") { tagName = "textarea"; - content.push($tw.Tree.Text(value)); + content.push($tw.Tree.Text(edit.value)); } else { tagName = "input"; attributes.type = "text"; - attributes.value = value; + attributes.value = edit.value; } // Wrap the editor control in a div return $tw.Tree.Element("div",{},[$tw.Tree.Element(tagName,attributes,content)],{ @@ -92,7 +100,7 @@ TextEditor.prototype.fixHeight = function() { var prevWrapperHeight = wrapper.style.height; wrapper.style.height = textarea.style.height + "px"; textarea.style.overflow = "hidden"; - textarea.style.height = "1px"; +// textarea.style.height = "1px"; textarea.style.height = Math.max(textarea.scrollHeight,MIN_TEXT_AREA_HEIGHT) + "px"; wrapper.style.height = prevWrapperHeight; } @@ -102,9 +110,13 @@ TextEditor.prototype.postRenderInDom = function() { this.fixHeight(); }; -TextEditor.prototype.isRefreshable = function() { - // Don't refresh the editor if it contains the caret or selection - return document.activeElement !== this.macroNode.child.children[0].domNode; +TextEditor.prototype.refreshInDom = function() { + if(document.activeElement !== this.macroNode.child.children[0].domNode) { + var edit = this.getEditText(); + this.macroNode.child.children[0].domNode.value = edit.value; + } + // Fix the height if needed + this.fixHeight(); }; exports["text/x-tiddlywiki"] = TextEditor;