UNPKG

usxeditor

Version:

USX editor react component.

111 lines (110 loc) 4.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.selectNextEditableTextNode = exports.setSelectionStart = exports.toTextNodeOffset = exports.previousEditableTextNode = exports.nextEditableTextNode = exports.getParentEditableText = exports.previousElementWithClassName = exports.nextElementWithClassName = exports.isTextNode = void 0; function isTextNode(node) { return node.nodeType === 3; } exports.isTextNode = isTextNode; // element must be an element with the specified class name function nextElementWithClassName(element, className) { // TODO: PERFORMANCE REVIEW : while this is an easy implementation, manually traversing the DOM may // be quicker for large documents. var results = document.getElementsByClassName(className); var array = Array.from(results); var index = array.findIndex(function (x) { return x === element; }); if (index < array.length - 1) return array[index + 1]; return null; } exports.nextElementWithClassName = nextElementWithClassName; // element must be an element with the specified class name function previousElementWithClassName(element, className) { // TODO: PERFORMANCE REVIEW : while this is an easy implementation, manually traversing the DOM may // be quicker for large documents. var results = document.getElementsByClassName(className); var array = Array.from(results); var index = array.findIndex(function (x) { return x === element; }); if (index > 0) return array[index - 1]; return null; } exports.previousElementWithClassName = previousElementWithClassName; function getParentEditableText(node) { var _a; if (node === null) return null; if ((_a = node.className) === null || _a === void 0 ? void 0 : _a.includes("EditableText")) return node; return getParentEditableText(node.parentElement); } exports.getParentEditableText = getParentEditableText; function nextEditableTextNode(textNode) { var next = textNode.nextSibling; while (next !== null) { if (isTextNode(next)) return next; next = next.nextSibling; } var parent = getParentEditableText(textNode); if (!parent) return null; next = nextElementWithClassName(parent, 'EditableText'); if (!next) return null; return next.firstChild; } exports.nextEditableTextNode = nextEditableTextNode; function previousEditableTextNode(textNode) { var previous = textNode.previousSibling; while (previous !== null) { if (isTextNode(previous)) return previous; previous = previous.previousSibling; } var parent = getParentEditableText(textNode); if (!parent) return null; previous = previousElementWithClassName(parent, 'EditableText'); if (!previous) return null; return previous.lastChild; } exports.previousEditableTextNode = previousEditableTextNode; function toTextNodeOffset(node, offset) { if (!node) return 0; if (isTextNode(node)) return offset; var length = 0; for (var i = 0; i < offset; i++) length += node === null || node === void 0 ? void 0 : node.childNodes[i].textContent.length; return length; } exports.toTextNodeOffset = toTextNodeOffset; function setSelectionStart(node, offset) { var range = document.createRange(); range.setStart(node, offset); var selection = document.getSelection(); if ((selection === null || selection === void 0 ? void 0 : selection.anchorNode) === node && (selection === null || selection === void 0 ? void 0 : selection.anchorOffset) === offset) return; selection === null || selection === void 0 ? void 0 : selection.removeAllRanges(); selection === null || selection === void 0 ? void 0 : selection.addRange(range); } exports.setSelectionStart = setSelectionStart; function selectNextEditableTextNode(forward) { var _a, _b, _c; if (forward) { var newTextNode = nextEditableTextNode((_a = document.getSelection()) === null || _a === void 0 ? void 0 : _a.anchorNode); if (!newTextNode) return; setSelectionStart(newTextNode, 0); } else { var newTextNode = previousEditableTextNode((_b = document.getSelection()) === null || _b === void 0 ? void 0 : _b.anchorNode); if (!newTextNode) return; var maxLength = ((_c = newTextNode.textContent) === null || _c === void 0 ? void 0 : _c.length) || 0; setSelectionStart(newTextNode, maxLength); } } exports.selectNextEditableTextNode = selectNextEditableTextNode;