Roo/lib/Selection.js
authorAlan <alan@roojs.com>
Wed, 29 Dec 2021 05:16:26 +0000 (13:16 +0800)
committerAlan <alan@roojs.com>
Wed, 29 Dec 2021 05:16:26 +0000 (13:16 +0800)
Roo/lib/Selection.js [new file with mode: 0644]

diff --git a/Roo/lib/Selection.js b/Roo/lib/Selection.js
new file mode 100644 (file)
index 0000000..d23d34a
--- /dev/null
@@ -0,0 +1,82 @@
+/**
+ * @class Roo.lib.Selection
+ * @constructor
+ * This is a toolkit, normally used to copy features into a Dom Selection element
+ * Roo.lib.Selection.wrap(x);
+ *
+ *
+ *
+ */
+Roo.lib.Selection = function() { };
+
+/**
+ * Wrap a Dom Range object, to give it new features...
+ * @static
+ * @param {Range} the range to wrap
+ */
+Roo.lib.Selection.wrap = function(r, doc) {
+    Roo.apply(r, Roo.lib.Selection.prototype);
+    r.ownerDocument = r; // usefull so we dont have to keep referening to it.
+};
+/**
+ * find a parent node eg. LI / OL
+ * @param {string|Array} node name or array of nodenames
+ * @return {DomElement|false}
+ */
+Roo.apply(Roo.lib.Selection.prototype,
+{
+    /**
+     * the owner document
+     */
+    ownerDocument : false,
+    
+    getRangeAt : function(n)
+    {
+        return Roo.lib.Range.wrap(Selection.prototype.getRangeAt.call(this,n));
+    },
+    
+    /**
+     * insert node at selection 
+     * @param {DomElement|string} node
+     * @param {string} cursor (after|in|none) where to place the cursor after inserting.
+     */
+    insertNode: function(node, cursor)
+    {
+        if (typeof(node) == 'string') {
+            node = this.ownerDocument.createElement(node);
+            if (cursor == 'in') {
+                node.innerHTML = '&nbsp;';
+            }
+        }
+        
+        var range = this.getRangeAt(0);
+        
+        if (this.type != 'Caret') {
+            range.deleteContents();
+        }
+        range.insertNode(node);
+        if (cursor == 'none') {
+            return;
+        }
+        var sn = node.childNodes[0]; // select the contents.
+        if (cursor == 'after') {
+            sn = node.insertAdjacentHTML('afterend', '&nbsp;');
+        }
+        this.cursorStart(sn);
+    },
+    cursorStart : function(n)
+    {
+        var range = this.getRangeAt(0);
+        range = range.cloneRange();
+        range.selectNode(sn);
+        
+        range.collapse(false);
+         
+        this.removeAllRanges();
+        this.addRange(range);
+    }
+    
+    
+    
+    
+});
\ No newline at end of file