X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/f1709cec5b085835c7e5d36eaa2d2b35d0b13f22..e51396f5e4a7b44b55bd5e9806c296b85b44a1d1:/src/editor/modules/documentCanvas/canvas/canvas.js diff --git a/src/editor/modules/documentCanvas/canvas/canvas.js b/src/editor/modules/documentCanvas/canvas/canvas.js index 3bf6b51..2f25cb3 100644 --- a/src/editor/modules/documentCanvas/canvas/canvas.js +++ b/src/editor/modules/documentCanvas/canvas/canvas.js @@ -11,7 +11,8 @@ define([ 'modules/documentCanvas/canvas/genericElement', 'modules/documentCanvas/canvas/nullElement', 'modules/documentCanvas/canvas/gutter', -], function($, _, Backbone, logging, documentElement, keyboard, utils, wlxmlListener, ElementsRegister, genericElement, nullElement, gutter) { +'libs/text!./canvas.html' +], function($, _, Backbone, logging, documentElement, keyboard, utils, wlxmlListener, ElementsRegister, genericElement, nullElement, gutter, canvasTemplate) { 'use strict'; /* global document:false, window:false, Node:false, gettext */ @@ -59,7 +60,8 @@ $.extend(TextHandler.prototype, { }); -var Canvas = function(wlxmlDocument, elements) { +var Canvas = function(wlxmlDocument, elements, metadata) { + this.metadata = metadata || {}; this.elementsRegister = new ElementsRegister(documentElement.DocumentNodeElement, nullElement); elements = [ @@ -75,13 +77,13 @@ var Canvas = function(wlxmlDocument, elements) { }.bind(this)); this.eventBus = _.extend({}, Backbone.Events); - this.wrapper = $('
'); + this.dom = $(canvasTemplate); + this.rootWrapper = this.dom.find('.root-wrapper'); this.gutter = gutter.create(); this.gutterView = new gutter.GutterView(this.gutter); - this.rootWrapper = $('
').addClass('canvas-wrapper').attr('contenteditable', true); - this.wrapper.find('.crow').append(this.rootWrapper, this.gutterView.dom); + this.dom.find('.view-row').append(this.gutterView.dom); this.wlxmlListener = wlxmlListener.create(this); this.loadWlxmlDocument(wlxmlDocument); @@ -92,7 +94,7 @@ var Canvas = function(wlxmlDocument, elements) { $.extend(Canvas.prototype, Backbone.Events, { getElementOffset: function(element) { - return element.dom.offset().top - this.wrapper.offset().top; + return element.dom.offset().top - this.dom.offset().top; }, loadWlxmlDocument: function(wlxmlDocument) { @@ -136,8 +138,10 @@ $.extend(Canvas.prototype, Backbone.Events, { }, reloadRoot: function() { + if(this.rootElement) { + this.rootElement.detach(); + } this.rootElement = this.createElement(this.wlxmlDocument.root); - this.rootWrapper.empty(); this.rootWrapper.append(this.rootElement.dom); }, @@ -155,16 +159,28 @@ $.extend(Canvas.prototype, Backbone.Events, { var mouseDown; this.rootWrapper.on('mousedown', '[document-node-element], [document-text-element]', function(e) { mouseDown = e.target; + canvas.rootWrapper.find('[contenteditable]').attr('contenteditable', null); }); this.rootWrapper.on('click', '[document-node-element], [document-text-element]', function(e) { + var position, element; e.stopPropagation(); if(e.originalEvent.detail === 3) { e.preventDefault(); canvas._moveCaretToTextElement(canvas.getDocumentElement(e.currentTarget), 'whole'); } else { if(mouseDown === e.target) { - canvas.setCurrentElement(canvas.getDocumentElement(e.currentTarget), {caretTo: false}); + element = canvas.getDocumentElement(e.target); + if(element && element.wlxmlNode.nodeType === Node.ELEMENT_NODE) { + if(element.getVerticallyFirstTextElement && !element.getVerticallyFirstTextElement({considerChildren: false})) { + canvas.setCurrentElement(element); + return; + } + } + if(window.getSelection().isCollapsed) { + position = utils.caretPositionFromPoint(e.clientX, e.clientY); + canvas.setCurrentElement(canvas.getDocumentElement(position.textNode), {caretTo: position.offset}); + } } } }); @@ -196,7 +212,7 @@ $.extend(Canvas.prototype, Backbone.Events, { /* globals MutationObserver */ var observer = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { - if(documentElement.DocumentTextElement.isContentContainer(mutation.target)) { + if(canvas.dom[0].contains(mutation.target) && documentElement.DocumentTextElement.isContentContainer(mutation.target)) { observer.disconnect(); if(mutation.target.data === '') { mutation.target.data = utils.unicode.ZWS; @@ -205,7 +221,7 @@ $.extend(Canvas.prototype, Backbone.Events, { mutation.target.data = mutation.target.data.replace(utils.unicode.ZWS, ''); canvas._moveCaretToTextElement(canvas.getDocumentElement(mutation.target), 'end'); } - observer.observe(canvas.wrapper[0], config); + observer.observe(canvas.dom[0], config); var textElement = canvas.getDocumentElement(mutation.target), toSet = mutation.target.data !== utils.unicode.ZWS ? mutation.target.data : ''; @@ -249,7 +265,7 @@ $.extend(Canvas.prototype, Backbone.Events, { }, view: function() { - return this.wrapper; + return this.dom; }, doc: function() { @@ -345,7 +361,7 @@ $.extend(Canvas.prototype, Backbone.Events, { if(byBrowser && byBrowser.parent().sameNode(nodeToLand)) { return byBrowser; } - return e.getVerticallyFirstTextElement(); + return _.isFunction(e.getVerticallyFirstTextElement) ? e.getVerticallyFirstTextElement({considerChildren: false}) : null; }.bind(this); var _markAsCurrent = function(element) { if(element instanceof documentElement.DocumentTextElement) { @@ -373,7 +389,7 @@ $.extend(Canvas.prototype, Backbone.Events, { if(textElementToLand) { _markAsCurrent(textElementToLand); - if(params.caretTo || !textElementToLand.sameNode(this.getCursor().getPosition().element)) { + if((params.caretTo || params.caretTo === 0) || !textElementToLand.sameNode(this.getCursor().getPosition().element)) { this._moveCaretToTextElement(textElementToLand, params.caretTo); // as method on element? } } else { @@ -405,23 +421,16 @@ $.extend(Canvas.prototype, Backbone.Events, { } var selection = document.getSelection(); + $(node).parent().attr('contenteditable', true); selection.removeAllRanges(); selection.addRange(range); - this.rootWrapper.focus(); // FF requires this for caret to be put where range colllapses, Chrome doesn't. + $(node).parent().focus(); // FF requires this for caret to be put where range colllapses, Chrome doesn't. }, setCursorPosition: function(position) { if(position.element) { this._moveCaretToTextElement(position.element, position.offset); } - }, - - toggleGrid: function() { - this.rootWrapper.toggleClass('grid-on'); - this.trigger('changed'); - }, - isGridToggled: function() { - return this.rootWrapper.hasClass('grid-on'); } }); @@ -461,6 +470,9 @@ $.extend(Selection.prototype, { return doc.createFragment(doc.CaretFragment, {node: anchorNode, offset: this.nativeSelection.anchorOffset}); } if(this.type === 'textSelection') { + if(!anchorNode || !focusNode) { + return; + } if(anchorNode.isSiblingOf(focusNode)) { return doc.createFragment(doc.TextRangeFragment, { node1: anchorNode, @@ -613,8 +625,8 @@ $.extend(Cursor.prototype, { }); return { - fromXMLDocument: function(wlxmlDocument, elements) { - return new Canvas(wlxmlDocument, elements); + fromXMLDocument: function(wlxmlDocument, elements, metadata) { + return new Canvas(wlxmlDocument, elements, metadata); } };