X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/1d25d2c941f5ffdc6e0824724db9a34960dc5450..9c680e17e94d8299a55ac811bca76e73339d02f0:/modules/documentCanvas/canvas/canvas.js diff --git a/modules/documentCanvas/canvas/canvas.js b/modules/documentCanvas/canvas/canvas.js index c4bfc09..cf46d5e 100644 --- a/modules/documentCanvas/canvas/canvas.js +++ b/modules/documentCanvas/canvas/canvas.js @@ -22,7 +22,8 @@ $.extend(Canvas.prototype, { var currentTag = $(this); if(currentTag.attr('wlxml-tag')) return; - var toret = $('
').attr('wlxml-tag', currentTag.prop('tagName').toLowerCase()); + var toret = $('
') + .attr('wlxml-tag', currentTag.prop('tagName').toLowerCase()); //toret.attr('id', 'xxxxxxxx-xxxx-xxxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {var r = Math.random()*16|0,v=c=='x'?r:r&0x3|0x8;return v.toString(16);})); for(var i = 0; i < this.attributes.length; i++) { var attr = this.attributes.item(i); @@ -57,9 +58,15 @@ $.extend(Canvas.prototype, { this.data = $.trim(this.data); if(this.data.length === 0 && oldLength > 0 && el.parent().contents().length === 1) this.data = ' '; - if(this.data.length === 0) + if(this.data.length === 0) { $(this).remove(); + return true; // continue + } } + + var wrapper = documentElement.DocumentTextElement.create({text: this.data}); + $(this).before(wrapper.dom()); + $(this).remove(); }); this.d = wrapper.children(0); @@ -72,7 +79,7 @@ $.extend(Canvas.prototype, { doc: function() { if(this.d === null) return null; - return documentElement.wrap(this.d.get(0), this); //{wlxmlTag: this.d.prop('tagName')}; + return documentElement.DocumentNodeElement.fromHTMLElement(this.d.get(0), this); //{wlxmlTag: this.d.prop('tagName')}; }, wrapText: function(params) { @@ -117,10 +124,15 @@ $.extend(Canvas.prototype, { }, getDocumentElement: function(from) { if(from instanceof HTMLElement || from instanceof Text) { - return documentElement.wrap(from, this); + return documentElement.DocumentElement.fromHTMLElement(from, this); } }, - list: {} + getCursor: function() { + return new Cursor(this); + }, + + list: {}, + }); $.extend(Canvas.prototype.list, { @@ -253,6 +265,89 @@ $.extend(Canvas.prototype.list, { } }); + +var Cursor = function(canvas) { + this.canvas = canvas; +}; + +$.extend(Cursor.prototype, { + isSelecting: function() { + var selection = window.getSelection(); + return !selection.isCollapsed; + }, + isSelectingWithinElement: function() { + return this.isSelecting() && this.getSelectionStart().element.sameNode(this.getSelectionEnd().element); + }, + isSelectingSiblings: function() { + return this.isSelecting() && this.getSelectionStart().element.parent().sameNode(this.getSelectionEnd().element.parent()); + }, + getPosition: function() { + return this.getSelectionAnchor(); + }, + getSelectionStart: function() { + return this.getSelectionBoundry('start'); + }, + getSelectionEnd: function() { + return this.getSelectionBoundry('end'); + }, + getSelectionAnchor: function() { + return this.getSelectionBoundry('anchor'); + }, + getSelectionBoundry: function(which) { + var selection = window.getSelection(), + anchorElement = this.canvas.getDocumentElement(selection.anchorNode), + focusElement = this.canvas.getDocumentElement(selection.focusNode); + + if(which === 'anchor') { + return { + element: anchorElement, + offset: selection.anchorOffset + }; + } + + var element, + offset; + + if(anchorElement.parent().sameNode(focusElement.parent())) { + var parent = anchorElement.parent(), + anchorFirst = parent.childIndex(anchorElement) < parent.childIndex(focusElement); + if(anchorFirst) { + if(which === 'start') { + element = anchorElement; + offset = selection.anchorOffset; + } + else if(which === 'end') { + element = focusElement, + offset = selection.focusOffset; + } + } else { + if(which === 'start') { + element = focusElement, + offset = selection.focusOffset + } + else if(which === 'end') { + element = anchorElement; + offset = selection.anchorOffset; + } + } + } else { + // TODO: Handle order + if(which === 'start') { + element = anchorElement; + offset = selection.anchorOffset + } else { + element = focusElement; + offset = selection.focusOffset + } + } + + return { + element: element, + offset: offset + } + } +}) + return { fromXML: function(xml) { return new Canvas(xml);