X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/c6526aebeee1d516b8bd7c793fecda2fcb80bbdf..2d4b18135a37d60f45c008271dbbd5c038bd76d1:/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 b234dd8..08df11f 100644 --- a/src/editor/modules/documentCanvas/canvas/canvas.js +++ b/src/editor/modules/documentCanvas/canvas/canvas.js @@ -159,16 +159,21 @@ $.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; 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}); + if(window.getSelection().isCollapsed) { + position = utils.caretPositionFromPoint(e.clientX, e.clientY); + canvas.setCurrentElement(canvas.getDocumentElement(position.textNode), {caretTo: position.offset}); + } } } }); @@ -200,7 +205,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; @@ -349,7 +354,7 @@ $.extend(Canvas.prototype, Backbone.Events, { if(byBrowser && byBrowser.parent().sameNode(nodeToLand)) { return byBrowser; } - return e.getVerticallyFirstTextElement({considerChildren: false}); + return _.isFunction(e.getVerticallyFirstTextElement) ? e.getVerticallyFirstTextElement({considerChildren: false}) : null; }.bind(this); var _markAsCurrent = function(element) { if(element instanceof documentElement.DocumentTextElement) { @@ -377,7 +382,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 { @@ -409,23 +414,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'); } }); @@ -465,6 +463,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,