From 6757e2f4fe80bfdc197d4963c464bf2b3a47f66b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Wed, 27 Nov 2013 16:10:11 +0100 Subject: [PATCH] pre d: setattr, settext, split as ContextTransformations; detach in old format; pre auto mapper --- .../modules/documentCanvas/canvas/canvas.js | 7 +- .../modules/documentCanvas/canvas/keyboard.js | 5 +- .../documentCanvas/canvas/wlxmlListener.js | 1 + .../documentToolbar/documentToolbar.js | 6 + src/editor/modules/nodePane/nodePane.js | 6 +- src/smartxml/smartxml.js | 159 +++++++++++++++++- 6 files changed, 172 insertions(+), 12 deletions(-) diff --git a/src/editor/modules/documentCanvas/canvas/canvas.js b/src/editor/modules/documentCanvas/canvas/canvas.js index b6ddf71..e181195 100644 --- a/src/editor/modules/documentCanvas/canvas/canvas.js +++ b/src/editor/modules/documentCanvas/canvas/canvas.js @@ -38,7 +38,8 @@ $.extend(TextHandler.prototype, { } }, setText: function(text, node) { - this.canvas.wlxmlDocument.transform('setText', {node:node, text: text}); + //this.canvas.wlxmlDocument.transform('setText', {node:node, text: text}); + node.transform('setText', {text: text}); } @@ -104,7 +105,7 @@ $.extend(Canvas.prototype, { mutation.target.data = mutation.target.data.replace(utils.unicode.ZWS, ''); canvas._moveCaretToTextElement(canvas.getDocumentElement(mutation.target), 'end'); } - observer.observe(canvas.d[0], config); + observer.observe(canvas.wrapper[0], config); canvas.publisher('contentChanged'); var textElement = canvas.getDocumentElement(mutation.target), @@ -119,7 +120,7 @@ $.extend(Canvas.prototype, { }); }); var config = { attributes: false, childList: false, characterData: true, subtree: true, characterDataOldValue: true}; - observer.observe(this.d[0], config); + observer.observe(this.wrapper[0], config); this.wrapper.on('mouseover', '[document-node-element], [document-text-element]', function(e) { diff --git a/src/editor/modules/documentCanvas/canvas/keyboard.js b/src/editor/modules/documentCanvas/canvas/keyboard.js index 16f2f68..dfe3f66 100644 --- a/src/editor/modules/documentCanvas/canvas/keyboard.js +++ b/src/editor/modules/documentCanvas/canvas/keyboard.js @@ -74,7 +74,8 @@ handlers.push({key: KEYS.ENTER, return false; // top level element is unsplittable } - var nodes = position.element.data('wlxmlNode').split({offset: position.offset}), + //var nodes = position.element.data('wlxmlNode').split({offset: position.offset}), + var nodes = position.element.data('wlxmlNode').transform('split', {offset: position.offset}), newEmpty, goto, gotoOptions; @@ -85,7 +86,7 @@ handlers.push({key: KEYS.ENTER, newEmpty = nodes.second; if(newEmpty) { - goto = newEmpty.append({text: ''}); + //goto = newEmpty.append({text: ''}); gotoOptions = {}; } else { goto = nodes.second; diff --git a/src/editor/modules/documentCanvas/canvas/wlxmlListener.js b/src/editor/modules/documentCanvas/canvas/wlxmlListener.js index 80da027..ffabfec 100644 --- a/src/editor/modules/documentCanvas/canvas/wlxmlListener.js +++ b/src/editor/modules/documentCanvas/canvas/wlxmlListener.js @@ -68,6 +68,7 @@ var handlers = { canvasNode.detach(); }, nodeTextChange: function(event) { + console.log('wlxmlListener: ' + event.meta.node.getText()); var canvasElement = utils.findCanvasElement(event.meta.node), toSet = event.meta.node.getText(); if(toSet === '') { diff --git a/src/editor/modules/documentToolbar/documentToolbar.js b/src/editor/modules/documentToolbar/documentToolbar.js index 7fbf6c1..75fcc72 100644 --- a/src/editor/modules/documentToolbar/documentToolbar.js +++ b/src/editor/modules/documentToolbar/documentToolbar.js @@ -36,6 +36,12 @@ return function(sandbox) { params.meta = meta; } + if(command === 'undo' || command === 'redo') { + params.callback = function(disable) { + btn.attr('disabled', !disable); + } + } + sandbox.publish('command', command, params); }); }, diff --git a/src/editor/modules/nodePane/nodePane.js b/src/editor/modules/nodePane/nodePane.js index 7473c2e..dbdd951 100644 --- a/src/editor/modules/nodePane/nodePane.js +++ b/src/editor/modules/nodePane/nodePane.js @@ -18,7 +18,11 @@ return function(sandbox) { var attr = target.attr('class').split('-')[3] === 'tagSelect' ? 'Tag' : 'Class', value = target.val().replace(/-/g, '.'); - currentNode['set' + attr](value); + if(attr === 'Class') { + //currentNode.document.transform('setClass', {node: currentNode, klass: value}); + currentNode.transform('setAttr', {name: 'class', value: value}); + } + //currentNode['set' + attr](value); }); return { diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index 7b59b18..1379153 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -43,6 +43,16 @@ var DocumentNode = function(nativeNode, document) { }; $.extend(DocumentNode.prototype, { + + transform: function(name, args) { + var Transformation = contextTransformations[name], + transformation; + if(Transformation) { + transformation = new Transformation(this.document, this, args); + } + return this.document.transform(transformation); + }, + _setNativeNode: function(nativeNode) { this.nativeNode = nativeNode; this._$ = $(nativeNode); @@ -643,14 +653,21 @@ $.extend(Document.prototype, Backbone.Events, { transform: function(transformationName, args) { console.log('transform'); - var Transformation = transformations[transformationName], - transformation; - if(Transformation) { - transformation = new Transformation(args); - transformation.run(); + var Transformation, transformation, toret; + if(typeof transformationName === 'string') { + Transformation = transformations[transformationName]; + if(Transformation) { + transformation = new Transformation(args); + } + } else { + transformation = transformationName; + } + if(transformation) { + toret = transformation.run(); this.undoStack.push(transformation); console.log('clearing redo stack'); this.redoStack = []; + return toret; } else { throw new Error('Transformation ' + transformationName + ' doesn\'t exist!'); } @@ -806,6 +823,111 @@ var createTransformation = function(desc) { return NodeTransformation; } +/// +var Transformation = function(args) { + this.args = args; +}; +$.extend(Transformation.prototype, { + run: function() { + throw new Error('not implemented'); + }, + undo: function() { + throw new Error('not implemented'); + }, +}); + +var createGenericTransformation = function(desc) { + var GenericTransformation = function(document, args) { + //document.getNodeByPath(contextPath).call(this, args); + this.args = args; + this.document = document; + }; + $.extend(GenericTransformation.prototype, { + run: function() { + var changeRoot = desc.getChangeRoot ? desc.getChangeRoot.call(this) : this.document.root; + this.snapshot = changeRoot.clone(); + this.changeRootPath = changeRoot.getPath(); + return desc.impl.call(this.context, this.args); // a argumenty do metody? + }, + undo: function() { + this.document.getNodeByPath(this.changeRootPath).replaceWith(this.snapshot); + }, + }); + + return GenericTransformation; +}; + +// var T = createGenericTransformation({impl: function() {}}); +// var t = T(doc, {a:1,b:2,c3:3}); + + +var createContextTransformation = function(desc) { + // mozna sie pozbyc przez przeniesienie object/context na koniec argumentow konstruktora generic transformation + var GenericTransformation = createGenericTransformation(desc); + + var ContextTransformation = function(document, object, args) { + var contextPath = object.getPath(); + + GenericTransformation.call(this, document, args); + + Object.defineProperty(this, 'context', { + get: function() { + return document.getNodeByPath(contextPath); + } + }); + } + ContextTransformation.prototype = Object.create(GenericTransformation.prototype); + return ContextTransformation; +} +// var T = createContextTransformation({impl: function() {}}); +// var t = T(doc, node, {a:1,b:2,c3:3}); +/// + +var contextTransformations = {}; +contextTransformations['setText'] = createContextTransformation({ + impl: function(args) { + this.setText(args.text); + }, + getChangeRoot: function() { + return this.context; + } +}); + +contextTransformations['setAttr'] = createContextTransformation({ + impl: function(args) { + this.setAttr(args.name, args.value); + }, + getChangeRoot: function() { + return this.context; + } +}); + +contextTransformations['split'] = createContextTransformation({ + impl: function(args) { + return this.split({offset: args.offset}); + }//, + // getChangeRoot: function() { + // return this.context.parent().parent(); + // } +}); + +// var TRANSFORMATION2 = function(f, getChangeRoot, undo) { +// var context = this, + + +// var transformation = createContextTransformation({ +// impl: f, +// getChangeRoot: getChangeRoot, + +// }); + +// var toret = function() { +// var +// f.apply(context, createArgs ? createArgs(arguments) : arguments) +// }; +// return toret; +// } + transformations['detach2'] = createTransformation({ // impl: function() { // //this.setAttr('class', 'cite'); // @@ -817,7 +939,7 @@ transformations['detach2'] = createTransformation({ }); -transformations['setText'] = createTransformation({ +transformations['setText-old'] = createTransformation({ impl: function(args) { this.setText(args.text) }, @@ -827,6 +949,15 @@ transformations['setText'] = createTransformation({ }); +transformations['setClass-old'] = createTransformation({ + impl: function(args) { + this.setClass(args.klass); + }, + getRoot: function(node) { + return node; + } +}) + //3. detach z pełnym własnym redo var Detach3NodeTransformation = function(args) { @@ -867,6 +998,22 @@ $.extend(Detach3NodeTransformation.prototype, { }); transformations['detach3'] = Detach3NodeTransformation; + +var registerTransformationsFromObject = function(object) { + _.pairs(object).filter(function(pair) { + var property = pair[1]; + return typeof property === 'function' && property._isTransformation; + }) + .forEach(function(pair) { + var name = pair[0], + method = pair[1]; + object.registerTransformation(name, createContextTransformation(method)); + }); +}; +registerTransformationsFromObject(ElementNode.prototype); +registerTransformationsFromObject(TextNode.prototype); +registerTransformationsFromObject(Document.prototype); + return { documentFromXML: function(xml) { return new Document(xml); -- 2.20.1