el-text-editor 0.0.92 → 0.0.94
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm2020/lib/el-text-editor.component.mjs +83 -43
- package/fesm2015/el-text-editor.mjs +84 -42
- package/fesm2015/el-text-editor.mjs.map +1 -1
- package/fesm2020/el-text-editor.mjs +82 -42
- package/fesm2020/el-text-editor.mjs.map +1 -1
- package/lib/el-text-editor.component.d.ts +1 -0
- package/package.json +1 -1
@@ -1738,13 +1738,11 @@ class ElTextEditorComponent {
|
|
1738
1738
|
// this.savedSelection = null;
|
1739
1739
|
// }
|
1740
1740
|
setTextColor(event) {
|
1741
|
-
console.log("clicked");
|
1742
1741
|
if (!this.savedSelection) {
|
1743
1742
|
console.warn("No saved selection available!");
|
1744
1743
|
return;
|
1745
1744
|
}
|
1746
1745
|
const color = event.target.value;
|
1747
|
-
console.log("color", color);
|
1748
1746
|
this.selectedTextColor = color;
|
1749
1747
|
const selection = window.getSelection();
|
1750
1748
|
if (!selection || selection.rangeCount === 0) {
|
@@ -1753,60 +1751,104 @@ class ElTextEditorComponent {
|
|
1753
1751
|
}
|
1754
1752
|
selection.removeAllRanges();
|
1755
1753
|
selection.addRange(this.savedSelection);
|
1756
|
-
const range = this.savedSelection;
|
1757
|
-
const
|
1758
|
-
|
1759
|
-
|
1760
|
-
|
1754
|
+
const range = this.savedSelection.cloneRange();
|
1755
|
+
const wrapTextNode = (textNode, start, end) => {
|
1756
|
+
const span = document.createElement("span");
|
1757
|
+
span.style.color = color;
|
1758
|
+
const selectedText = textNode.textContent.slice(start, end);
|
1759
|
+
span.textContent = selectedText;
|
1760
|
+
const beforeText = textNode.textContent.slice(0, start);
|
1761
|
+
const afterText = textNode.textContent.slice(end);
|
1762
|
+
const fragment = document.createDocumentFragment();
|
1763
|
+
if (beforeText)
|
1764
|
+
fragment.appendChild(document.createTextNode(beforeText));
|
1765
|
+
fragment.appendChild(span);
|
1766
|
+
if (afterText)
|
1767
|
+
fragment.appendChild(document.createTextNode(afterText));
|
1768
|
+
textNode.replaceWith(fragment);
|
1769
|
+
};
|
1770
|
+
const processNode = (node) => {
|
1771
|
+
if (node.nodeType === Node.TEXT_NODE) {
|
1772
|
+
wrapTextNode(node, 0, (node.textContent || "").length);
|
1761
1773
|
}
|
1762
|
-
|
1763
|
-
|
1774
|
+
else if (node.nodeType === Node.ELEMENT_NODE) {
|
1775
|
+
const element = node;
|
1776
|
+
if (["LI", "P", "DIV", "TABLE", "TD", "TH"].includes(element.tagName)) {
|
1777
|
+
element.style.color = color; // Apply directly for block and table elements
|
1778
|
+
}
|
1764
1779
|
}
|
1765
|
-
return null;
|
1766
1780
|
};
|
1767
|
-
const
|
1768
|
-
|
1769
|
-
|
1770
|
-
|
1771
|
-
|
1772
|
-
return;
|
1781
|
+
const startContainer = range.startContainer;
|
1782
|
+
const endContainer = range.endContainer;
|
1783
|
+
if (startContainer === endContainer &&
|
1784
|
+
startContainer.nodeType === Node.TEXT_NODE) {
|
1785
|
+
wrapTextNode(startContainer, range.startOffset, range.endOffset);
|
1773
1786
|
}
|
1774
|
-
|
1775
|
-
|
1776
|
-
|
1777
|
-
|
1778
|
-
|
1779
|
-
|
1780
|
-
|
1781
|
-
|
1787
|
+
else {
|
1788
|
+
const treeWalker = document.createTreeWalker(range.commonAncestorContainer, NodeFilter.SHOW_TEXT, {
|
1789
|
+
acceptNode: (node) => {
|
1790
|
+
return range.intersectsNode(node)
|
1791
|
+
? NodeFilter.FILTER_ACCEPT
|
1792
|
+
: NodeFilter.FILTER_REJECT;
|
1793
|
+
},
|
1794
|
+
});
|
1782
1795
|
const textNodes = [];
|
1783
|
-
const walker = document.createTreeWalker(parentElement, NodeFilter.SHOW_TEXT, null);
|
1784
1796
|
let node;
|
1785
|
-
while ((node =
|
1797
|
+
while ((node = treeWalker.nextNode())) {
|
1786
1798
|
textNodes.push(node);
|
1787
1799
|
}
|
1788
1800
|
for (const textNode of textNodes) {
|
1789
|
-
|
1790
|
-
|
1791
|
-
|
1792
|
-
|
1793
|
-
|
1794
|
-
|
1795
|
-
|
1796
|
-
|
1797
|
-
const fragment = document.createDocumentFragment();
|
1798
|
-
if (beforeText)
|
1799
|
-
fragment.appendChild(document.createTextNode(beforeText));
|
1800
|
-
fragment.appendChild(span);
|
1801
|
-
if (afterText)
|
1802
|
-
fragment.appendChild(document.createTextNode(afterText));
|
1803
|
-
textNode.replaceWith(fragment);
|
1804
|
-
break; // Apply only once per selection
|
1801
|
+
if (textNode === startContainer) {
|
1802
|
+
wrapTextNode(textNode, range.startOffset, textNode.textContent.length);
|
1803
|
+
}
|
1804
|
+
else if (textNode === endContainer) {
|
1805
|
+
wrapTextNode(textNode, 0, range.endOffset);
|
1806
|
+
}
|
1807
|
+
else {
|
1808
|
+
wrapTextNode(textNode, 0, textNode.textContent.length);
|
1805
1809
|
}
|
1806
1810
|
}
|
1807
1811
|
}
|
1808
1812
|
this.savedSelection = null;
|
1809
1813
|
}
|
1814
|
+
// Helper method to recursively apply color to all text nodes in a fragment
|
1815
|
+
applyColorToContents(node, color) {
|
1816
|
+
var _a;
|
1817
|
+
if (node.nodeType === Node.TEXT_NODE && ((_a = node.textContent) === null || _a === void 0 ? void 0 : _a.trim())) {
|
1818
|
+
const span = document.createElement("span");
|
1819
|
+
span.style.color = color;
|
1820
|
+
span.textContent = node.textContent;
|
1821
|
+
if (node.parentNode) {
|
1822
|
+
node.parentNode.replaceChild(span, node);
|
1823
|
+
}
|
1824
|
+
return;
|
1825
|
+
}
|
1826
|
+
if (node.nodeType === Node.ELEMENT_NODE) {
|
1827
|
+
if (node instanceof HTMLLIElement) {
|
1828
|
+
const childNodes = Array.from(node.childNodes);
|
1829
|
+
childNodes.forEach((child) => {
|
1830
|
+
var _a;
|
1831
|
+
if (child.nodeType === Node.TEXT_NODE && ((_a = child.textContent) === null || _a === void 0 ? void 0 : _a.trim())) {
|
1832
|
+
const span = document.createElement("span");
|
1833
|
+
span.style.color = color;
|
1834
|
+
span.textContent = child.textContent;
|
1835
|
+
child.replaceWith(span);
|
1836
|
+
}
|
1837
|
+
else if (child.nodeType === Node.ELEMENT_NODE) {
|
1838
|
+
this.applyColorToContents(child, color);
|
1839
|
+
}
|
1840
|
+
});
|
1841
|
+
}
|
1842
|
+
else {
|
1843
|
+
const childNodes = Array.from(node.childNodes);
|
1844
|
+
childNodes.forEach((child) => this.applyColorToContents(child, color));
|
1845
|
+
}
|
1846
|
+
}
|
1847
|
+
if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
|
1848
|
+
const childNodes = Array.from(node.childNodes);
|
1849
|
+
childNodes.forEach((child) => this.applyColorToContents(child, color));
|
1850
|
+
}
|
1851
|
+
}
|
1810
1852
|
setBackgroundColor(event) {
|
1811
1853
|
if (this.savedSelection) {
|
1812
1854
|
const color = event.target.value;
|