canvas api: dividing text element at the text boundries
[fnpeditor.git] / modules / documentCanvas / canvas / documentElement.js
index c6c9f22..b516af1 100644 (file)
@@ -1,8 +1,9 @@
 define([
 'libs/jquery-1.9.1.min',
 'libs/underscore-min',
-'modules/documentCanvas/classAttributes'
-], function($, _, classAttributes) {
+'modules/documentCanvas/classAttributes',
+'modules/documentCanvas/canvas/utils'
+], function($, _, classAttributes, utils) {
     
 'use strict';
 
@@ -154,7 +155,7 @@ $.extend(DocumentNodeElement.prototype, {
         var dom = this.dom(),
             args = Array.prototype.slice.call(arguments, 0);
         if(args.length === 2 && args[1] === undefined)
-            return dom.removeData(args[1]);
+            return dom.removeData(args[0]);
         return dom.data.apply(dom, arguments);
     },
     toXML: function(level) {
@@ -227,7 +228,7 @@ $.extend(DocumentNodeElement.prototype, {
     },
     append: function(params) {
         if(params.tag !== 'span')
-            this.data('orig-ends', undefined);
+            this.data('orig-end', undefined);
         return manipulate(this, params, 'append');
     },
     before: function(params) {
@@ -324,7 +325,7 @@ $.extend(DocumentTextElement, {
     createDOM: function(params) {
         return $('<div>')
             .attr('wlxml-text', '')
-            .text(params.text);
+            .text(params.text || utils.unicode.ZWS);
     },
 
     create: function(params, canvas) {
@@ -333,6 +334,9 @@ $.extend(DocumentTextElement, {
 
     fromHTMLElement: function(htmlElement, canvas) {
         return new this(htmlElement, canvas);
+    },
+    isContentContainer: function(htmlElement) {
+        return htmlElement.nodeType === Node.TEXT_NODE && $(htmlElement).parent().is('[wlxml-text]');
     }
 });
 
@@ -353,7 +357,11 @@ $.extend(DocumentTextElement.prototype, {
         this.dom().contents()[0].data = text;
     },
     getText: function() {
-        return this.dom().text();
+        return this.dom().text().replace(utils.unicode.ZWS, '');
+    },
+    isEmpty: function() {
+        // Having at least Zero Width Space is guaranteed be Content Observer
+        return this.dom().contents()[0].data === utils.unicode.ZWS;
     },
     after: function(params) {
         if(params instanceof DocumentTextElement || params.text)
@@ -458,9 +466,11 @@ $.extend(DocumentTextElement.prototype, {
     },
     divide: function(params) {
         var myText = this.getText();
-        
-        if(params.offset <= 0 || params.offset >= myText.length)
-            return;
+
+        if(params.offset === myText.length)
+            return this.after(params);
+        if(params.offset === 0)
+            return this.before(params);
 
         var lhsText = myText.substr(0, params.offset),
             rhsText = myText.substr(params.offset),