insert-text-at-cursor
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -52,4 +52,7 @@ var browserSupportsTextareaTextNodes; | ||
} else { | ||
// To make a change we just need a Range, not a Selection | ||
var range = document.createRange(); | ||
var textNode = document.createTextNode(text); | ||
if (canManipulateViaTextNodes(input)) { | ||
var textNode = document.createTextNode(text); | ||
var node = input.firstChild; // If textarea is empty, just insert the text | ||
@@ -63,6 +66,4 @@ | ||
var startNode = null; | ||
var endNode = null; // To make a change we just need a Range, not a Selection | ||
var endNode = null; | ||
var range = document.createRange(); | ||
while (node && (startNode === null || endNode === null)) { | ||
@@ -87,10 +88,16 @@ var nodeLength = node.nodeValue.length; // if start of the selection falls into current node | ||
range.deleteContents(); | ||
} // Finally insert a new node. The browser will automatically | ||
// split start and end nodes into two if necessary | ||
} | ||
} | ||
} // If the node is a textarea and the range doesn't span outside the element | ||
// | ||
// Get the commonAncestorContainer of the selected range and test its type | ||
// If the node is of type `#text` it means that we're still working with text nodes within our textarea element | ||
// otherwise, if it's of type `#document` for example it means our selection spans outside the textarea. | ||
range.insertNode(textNode); | ||
} | ||
if (canManipulateViaTextNodes(input) && range.commonAncestorContainer.nodeName === "#text") { | ||
// Finally insert a new node. The browser will automatically split start and end nodes into two if necessary | ||
range.insertNode(textNode); | ||
} else { | ||
// For the text input the only way is to replace the whole value :( | ||
// If the node is not a textarea or the range spans outside a textarea the only way is to replace the whole value | ||
var value = input.value; | ||
@@ -97,0 +104,0 @@ input.value = value.slice(0, start) + text + value.slice(end); |
@@ -58,4 +58,7 @@ (function (global, factory) { | ||
} else { | ||
// To make a change we just need a Range, not a Selection | ||
var range = document.createRange(); | ||
var textNode = document.createTextNode(text); | ||
if (canManipulateViaTextNodes(input)) { | ||
var textNode = document.createTextNode(text); | ||
var node = input.firstChild; // If textarea is empty, just insert the text | ||
@@ -69,6 +72,4 @@ | ||
var startNode = null; | ||
var endNode = null; // To make a change we just need a Range, not a Selection | ||
var endNode = null; | ||
var range = document.createRange(); | ||
while (node && (startNode === null || endNode === null)) { | ||
@@ -93,10 +94,16 @@ var nodeLength = node.nodeValue.length; // if start of the selection falls into current node | ||
range.deleteContents(); | ||
} // Finally insert a new node. The browser will automatically | ||
// split start and end nodes into two if necessary | ||
} | ||
} | ||
} // If the node is a textarea and the range doesn't span outside the element | ||
// | ||
// Get the commonAncestorContainer of the selected range and test its type | ||
// If the node is of type `#text` it means that we're still working with text nodes within our textarea element | ||
// otherwise, if it's of type `#document` for example it means our selection spans outside the textarea. | ||
range.insertNode(textNode); | ||
} | ||
if (canManipulateViaTextNodes(input) && range.commonAncestorContainer.nodeName === "#text") { | ||
// Finally insert a new node. The browser will automatically split start and end nodes into two if necessary | ||
range.insertNode(textNode); | ||
} else { | ||
// For the text input the only way is to replace the whole value :( | ||
// If the node is not a textarea or the range spans outside a textarea the only way is to replace the whole value | ||
var value = input.value; | ||
@@ -103,0 +110,0 @@ input.value = value.slice(0, start) + text + value.slice(end); |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).insertTextAtCursor=t()}(this,function(){"use strict";var m;return function(e,t){if(e.focus(),document.selection){var n=document.selection.createRange();return n.text=t,n.collapse(!1),void n.select()}if(!document.execCommand("insertText",!1,t)){var i=e.selectionStart,o=e.selectionEnd;if("function"==typeof e.setRangeText)e.setRangeText(t);else if(function(e){if("TEXTAREA"!==e.nodeName)return!1;if(void 0===m){var t=document.createElement("textarea");t.value=1,m=!!t.firstChild}return m}(e)){var l=document.createTextNode(t),r=e.firstChild;if(r){for(var a=0,c=null,u=null,s=document.createRange();r&&(null===c||null===u);){var d=r.nodeValue.length;a<=i&&i<=a+d&&s.setStart(c=r,i-a),a<=o&&o<=a+d&&s.setEnd(u=r,o-a),a+=d,r=r.nextSibling}i!==o&&s.deleteContents(),s.insertNode(l)}else e.appendChild(l)}else{var f=e.value;e.value=f.slice(0,i)+t+f.slice(o)}e.setSelectionRange(i+t.length,i+t.length);var v=document.createEvent("UIEvent");v.initEvent("input",!0,!1),e.dispatchEvent(v)}}}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).insertTextAtCursor=t()}(this,function(){"use strict";var n;function m(e){if("TEXTAREA"!==e.nodeName)return!1;if(void 0===n){var t=document.createElement("textarea");t.value=1,n=!!t.firstChild}return n}return function(e,t){if(e.focus(),document.selection){var n=document.selection.createRange();return n.text=t,n.collapse(!1),void n.select()}if(!document.execCommand("insertText",!1,t)){var i=e.selectionStart,o=e.selectionEnd;if("function"==typeof e.setRangeText)e.setRangeText(t);else{var r=document.createRange(),l=document.createTextNode(t);if(m(e)){var a=e.firstChild;if(a){for(var c=0,s=null,u=null;a&&(null===s||null===u);){var d=a.nodeValue.length;c<=i&&i<=c+d&&r.setStart(s=a,i-c),c<=o&&o<=c+d&&r.setEnd(u=a,o-c),c+=d,a=a.nextSibling}i!==o&&r.deleteContents()}else e.appendChild(l)}if(m(e)&&"#text"===r.commonAncestorContainer.nodeName)r.insertNode(l);else{var f=e.value;e.value=f.slice(0,i)+t+f.slice(o)}}e.setSelectionRange(i+t.length,i+t.length);var v=document.createEvent("UIEvent");v.initEvent("input",!0,!1),e.dispatchEvent(v)}}}); | ||
//# sourceMappingURL=index.umd.min.js.map |
27
index.js
@@ -49,4 +49,7 @@ let browserSupportsTextareaTextNodes; | ||
} else { | ||
// To make a change we just need a Range, not a Selection | ||
const range = document.createRange(); | ||
const textNode = document.createTextNode(text); | ||
if (canManipulateViaTextNodes(input)) { | ||
const textNode = document.createTextNode(text); | ||
let node = input.firstChild; | ||
@@ -63,5 +66,2 @@ | ||
// To make a change we just need a Range, not a Selection | ||
const range = document.createRange(); | ||
while (node && (startNode === null || endNode === null)) { | ||
@@ -88,9 +88,18 @@ const nodeLength = node.nodeValue.length; | ||
} | ||
} | ||
} | ||
// Finally insert a new node. The browser will automatically | ||
// split start and end nodes into two if necessary | ||
range.insertNode(textNode); | ||
} | ||
// If the node is a textarea and the range doesn't span outside the element | ||
// | ||
// Get the commonAncestorContainer of the selected range and test its type | ||
// If the node is of type `#text` it means that we're still working with text nodes within our textarea element | ||
// otherwise, if it's of type `#document` for example it means our selection spans outside the textarea. | ||
if ( | ||
canManipulateViaTextNodes(input) && | ||
range.commonAncestorContainer.nodeName === "#text" | ||
) { | ||
// Finally insert a new node. The browser will automatically split start and end nodes into two if necessary | ||
range.insertNode(textNode); | ||
} else { | ||
// For the text input the only way is to replace the whole value :( | ||
// If the node is not a textarea or the range spans outside a textarea the only way is to replace the whole value | ||
const value = input.value; | ||
@@ -97,0 +106,0 @@ input.value = value.slice(0, start) + text + value.slice(end); |
{ | ||
"name": "insert-text-at-cursor", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Cross-browser lib for inserting text at selection in a textarea / input", | ||
@@ -5,0 +5,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40104
289