X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/24ac4f5092661ce053f33d460306bf81d6a3dc8a..9da575320a658b0f543c8ff4ac0ebe43fdd975e1:/src/editor/modules/documentCanvas/canvas/canvas.js?ds=inline diff --git a/src/editor/modules/documentCanvas/canvas/canvas.js b/src/editor/modules/documentCanvas/canvas/canvas.js index 8b94782..48f03de 100644 --- a/src/editor/modules/documentCanvas/canvas/canvas.js +++ b/src/editor/modules/documentCanvas/canvas/canvas.js @@ -4,14 +4,16 @@ define([ 'libs/backbone', 'modules/documentCanvas/canvas/documentElement', 'modules/documentCanvas/canvas/keyboard', -'modules/documentCanvas/canvas/utils' -], function($, _, Backbone, documentElement, keyboard, utils) { +'modules/documentCanvas/canvas/utils', +'modules/documentCanvas/canvas/wlxmlListener' +], function($, _, Backbone, documentElement, keyboard, utils, wlxmlListener) { 'use strict'; var Canvas = function(wlxmlDocument, publisher) { this.eventBus = _.extend({}, Backbone.Events); this.wrapper = $('
').addClass('canvas-wrapper').attr('contenteditable', true); + this.wlxmlListener = wlxmlListener.create(this); this.loadWlxmlDocument(wlxmlDocument); this.publisher = publisher ? publisher : function() {}; }; @@ -19,10 +21,10 @@ var Canvas = function(wlxmlDocument, publisher) { $.extend(Canvas.prototype, { loadWlxmlDocument: function(wlxmlDocument) { - wlxmlDocument = wlxmlDocument || this.wlxmlDocument; if(!wlxmlDocument) { return false; } + var canvasDOM = this.generateCanvasDOM(wlxmlDocument.root); this.wrapper.empty(); @@ -30,17 +32,7 @@ $.extend(Canvas.prototype, { this.d = this.wrapper.children(0); this.setupEventHandling(); - var canvas = this; - - if(this.wlxmlDocument !== wlxmlDocument) { - wlxmlDocument.on('change', function(event) { - var canvasNode = utils.findCanvasElement(event.meta.node); - if(event.type === 'nodeAttrChange' && event.meta.attr === 'class') { - canvasNode.setWlxmlClass(event.meta.newVal); - } - - }); - } + this.wlxmlListener.listenTo(wlxmlDocument); this.wlxmlDocument = wlxmlDocument; }, @@ -72,6 +64,11 @@ $.extend(Canvas.prototype, { } observer.observe(canvas.d[0], config); canvas.publisher('contentChanged'); + + var textElement = canvas.getDocumentElement(mutation.target), + toSet = mutation.target.data !== utils.unicode.ZWS ? mutation.target.data : ''; + + textElement.data('wlxmlNode').setText(toSet); } }); }); @@ -115,6 +112,11 @@ $.extend(Canvas.prototype, { return documentElement.DocumentNodeElement.fromHTMLElement(this.d.get(0), this); //{wlxmlTag: this.d.prop('tagName')}; }, + toggleElementHighlight: function(node, toggle) { + var element = utils.findCanvasElement(node); + element.toggleHighlight(toggle); + }, + createNodeElement: function(params) { return documentElement.DocumentNodeElement.create(params, this); }, @@ -235,6 +237,10 @@ $.extend(Canvas.prototype, { setCurrentElement: function(element, params) { + if(!(element instanceof documentElement.DocumentElement)) { + element = utils.findCanvasElement(element); + } + params = _.extend({caretTo: 'end'}, params); var findFirstDirectTextChild = function(e, nodeToLand) { var byBrowser = this.getCursor().getPosition().element; @@ -273,7 +279,7 @@ $.extend(Canvas.prototype, { if(params.caretTo || !textElementToLand.sameNode(this.getCursor().getPosition().element)) this._moveCaretToTextElement(textElementToLand, params.caretTo); // as method on element? if(!(textElementToLand.sameNode(currentTextElement))) - this.publisher('currentTextElementSet', textElementToLand); + this.publisher('currentTextElementSet', textElementToLand.data('wlxmlNode')); } else { document.getSelection().removeAllRanges(); }