@aquera/nile-elements 1.1.8-beta-1.2 → 1.1.8-beta-1.3
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/dist/index.js +2 -2
- package/dist/nile-rich-text-editor/utils.cjs.js +1 -1
- package/dist/nile-rich-text-editor/utils.cjs.js.map +1 -1
- package/dist/nile-rich-text-editor/utils.esm.js +1 -1
- package/dist/src/nile-rich-text-editor/utils.js +91 -40
- package/dist/src/nile-rich-text-editor/utils.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/nile-rich-text-editor/utils.ts +92 -42
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "Webcomponent nile-elements following open-wc recommendations",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "nile-elements",
|
|
6
|
-
"version": "1.1.8-beta-1.
|
|
6
|
+
"version": "1.1.8-beta-1.3",
|
|
7
7
|
"main": "dist/src/index.js",
|
|
8
8
|
"type": "module",
|
|
9
9
|
"module": "dist/src/index.js",
|
|
@@ -583,39 +583,110 @@ export function toggleList(root: HTMLElement, kind: 'ul' | 'ol') {
|
|
|
583
583
|
const sel = document.getSelection();
|
|
584
584
|
if (!sel || sel.rangeCount === 0) return;
|
|
585
585
|
const range = sel.getRangeAt(0);
|
|
586
|
+
function styleList(list: HTMLElement) {
|
|
587
|
+
list.style.paddingInlineStart = '0';
|
|
588
|
+
list.style.margin = '0';
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
function isEmptyBlock(el: HTMLElement) {
|
|
592
|
+
return !el.textContent?.trim() && !el.querySelector('img,br');
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
function setCursorInside(el: HTMLElement) {
|
|
596
|
+
const selection = document.getSelection();
|
|
597
|
+
if (!selection) return;
|
|
598
|
+
const r = document.createRange();
|
|
599
|
+
r.selectNodeContents(el);
|
|
600
|
+
r.collapse(true);
|
|
601
|
+
selection.removeAllRanges();
|
|
602
|
+
selection.addRange(r);
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
function closestBlock(node: Node | null): HTMLElement | null {
|
|
606
|
+
while (node && node !== root) {
|
|
607
|
+
if (node instanceof HTMLElement) {
|
|
608
|
+
const display = getComputedStyle(node).display;
|
|
609
|
+
if (
|
|
610
|
+
node.tagName.match(/^(P|DIV|H[1-6]|LI)$/) ||
|
|
611
|
+
display === 'block' ||
|
|
612
|
+
display === 'list-item'
|
|
613
|
+
)
|
|
614
|
+
return node;
|
|
615
|
+
}
|
|
616
|
+
node = node.parentNode;
|
|
617
|
+
}
|
|
618
|
+
return null;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
function unwrapList(list: HTMLElement) {
|
|
622
|
+
const frag = document.createDocumentFragment();
|
|
623
|
+
(Array.from(list.querySelectorAll('li')) as HTMLElement[]).forEach((li) => {
|
|
624
|
+
const p = document.createElement('p');
|
|
625
|
+
if (li.style.textAlign) p.style.textAlign = li.style.textAlign;
|
|
626
|
+
while (li.firstChild) p.appendChild(li.firstChild);
|
|
627
|
+
frag.appendChild(p);
|
|
628
|
+
});
|
|
629
|
+
list.replaceWith(frag);
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
function unwrapListItem(block: HTMLElement) {
|
|
633
|
+
const li = block.closest('li');
|
|
634
|
+
if (!li) return;
|
|
635
|
+
const list = li.parentElement as HTMLElement;
|
|
636
|
+
const p = document.createElement('p');
|
|
637
|
+
if (li.style.textAlign) p.style.textAlign = li.style.textAlign;
|
|
638
|
+
while (li.firstChild) p.appendChild(li.firstChild);
|
|
639
|
+
list.parentNode?.insertBefore(p, list.nextSibling);
|
|
640
|
+
li.remove();
|
|
641
|
+
if (list.childNodes.length === 0) list.remove();
|
|
642
|
+
setCursorInside(p);
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
function switchListType(list: HTMLElement, newType: 'ul' | 'ol') {
|
|
647
|
+
const newList = document.createElement(newType);
|
|
648
|
+
styleList(newList);
|
|
649
|
+
while (list.firstChild) newList.appendChild(list.firstChild);
|
|
650
|
+
list.replaceWith(newList);
|
|
651
|
+
}
|
|
586
652
|
if (range.collapsed) {
|
|
587
|
-
const block = closestBlock(range.startContainer
|
|
588
|
-
|
|
653
|
+
const block = closestBlock(range.startContainer);
|
|
654
|
+
const insideList = block?.closest('ul,ol') as HTMLElement | null;
|
|
655
|
+
if (insideList && insideList.tagName.toLowerCase() === kind) {
|
|
656
|
+
unwrapListItem(block as HTMLElement);
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
if (insideList && insideList.tagName.toLowerCase() !== kind) {
|
|
661
|
+
switchListType(insideList, kind);
|
|
662
|
+
return;
|
|
663
|
+
}
|
|
664
|
+
if (!block || block === root || block.childNodes.length === 0 || isEmptyBlock(block)) {
|
|
589
665
|
const list = document.createElement(kind);
|
|
590
666
|
const li = document.createElement('li');
|
|
591
667
|
li.appendChild(document.createElement('br'));
|
|
592
668
|
list.appendChild(li);
|
|
669
|
+
styleList(list);
|
|
593
670
|
range.insertNode(list);
|
|
594
|
-
|
|
595
|
-
r.selectNodeContents(li);
|
|
596
|
-
r.collapse(true);
|
|
597
|
-
sel.removeAllRanges();
|
|
598
|
-
sel.addRange(r);
|
|
671
|
+
setCursorInside(li);
|
|
599
672
|
return;
|
|
600
673
|
}
|
|
601
674
|
const list = document.createElement(kind);
|
|
675
|
+
styleList(list);
|
|
602
676
|
const li = document.createElement('li');
|
|
677
|
+
if (block.style.textAlign) li.style.textAlign = block.style.textAlign;
|
|
603
678
|
while (block.firstChild) li.appendChild(block.firstChild);
|
|
604
679
|
list.appendChild(li);
|
|
605
680
|
block.replaceWith(list);
|
|
606
|
-
|
|
607
|
-
r.selectNodeContents(li);
|
|
608
|
-
r.collapse(true);
|
|
609
|
-
sel.removeAllRanges();
|
|
610
|
-
sel.addRange(r);
|
|
681
|
+
setCursorInside(li);
|
|
611
682
|
return;
|
|
612
683
|
}
|
|
684
|
+
|
|
613
685
|
const blocks: HTMLElement[] = [];
|
|
614
686
|
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT, {
|
|
615
687
|
acceptNode: (n) => {
|
|
616
688
|
if (!(n instanceof HTMLElement)) return NodeFilter.FILTER_SKIP;
|
|
617
689
|
if (!/^(P|DIV|LI|H[1-6])$/.test(n.tagName)) return NodeFilter.FILTER_SKIP;
|
|
618
|
-
|
|
619
690
|
const r = document.createRange();
|
|
620
691
|
r.selectNodeContents(n);
|
|
621
692
|
const intersects =
|
|
@@ -624,12 +695,11 @@ export function toggleList(root: HTMLElement, kind: 'ul' | 'ol') {
|
|
|
624
695
|
return intersects ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;
|
|
625
696
|
},
|
|
626
697
|
});
|
|
627
|
-
|
|
628
698
|
let n: Node | null;
|
|
629
699
|
while ((n = walker.nextNode())) blocks.push(n as HTMLElement);
|
|
630
700
|
|
|
631
701
|
if (blocks.length === 0) {
|
|
632
|
-
const block = closestBlock(range.startContainer
|
|
702
|
+
const block = closestBlock(range.startContainer);
|
|
633
703
|
if (block) blocks.push(block);
|
|
634
704
|
}
|
|
635
705
|
|
|
@@ -637,37 +707,15 @@ export function toggleList(root: HTMLElement, kind: 'ul' | 'ol') {
|
|
|
637
707
|
if (firstLi) {
|
|
638
708
|
const currentList = firstLi.closest('ul,ol') as HTMLElement | null;
|
|
639
709
|
if (!currentList) return;
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
(currentList as HTMLElement).style.textAlign ||
|
|
643
|
-
'';
|
|
644
|
-
|
|
645
|
-
if (currentList.tagName.toLowerCase() === kind) {
|
|
646
|
-
const frag = document.createDocumentFragment();
|
|
647
|
-
for (const li of Array.from(currentList.querySelectorAll('li'))) {
|
|
648
|
-
const p = document.createElement('p');
|
|
649
|
-
p.style.textAlign = (li as HTMLElement).style.textAlign || currentAlign || '';
|
|
650
|
-
while (li.firstChild) p.appendChild(li.firstChild);
|
|
651
|
-
frag.appendChild(p);
|
|
652
|
-
}
|
|
653
|
-
currentList.replaceWith(frag);
|
|
654
|
-
} else {
|
|
655
|
-
const newList = document.createElement(kind);
|
|
656
|
-
while (currentList.firstChild) {
|
|
657
|
-
const li = currentList.firstChild as HTMLElement;
|
|
658
|
-
li.style.textAlign = li.style.textAlign || currentAlign || '';
|
|
659
|
-
newList.appendChild(li);
|
|
660
|
-
}
|
|
661
|
-
currentList.replaceWith(newList);
|
|
662
|
-
}
|
|
710
|
+
if (currentList.tagName.toLowerCase() === kind) unwrapList(currentList);
|
|
711
|
+
else switchListType(currentList, kind);
|
|
663
712
|
return;
|
|
664
713
|
}
|
|
665
|
-
|
|
666
714
|
const newList = document.createElement(kind);
|
|
715
|
+
styleList(newList);
|
|
667
716
|
for (const block of blocks) {
|
|
668
717
|
const li = document.createElement('li');
|
|
669
|
-
|
|
670
|
-
if (align) li.style.textAlign = align;
|
|
718
|
+
if (block.style.textAlign) li.style.textAlign = block.style.textAlign;
|
|
671
719
|
while (block.firstChild) li.appendChild(block.firstChild);
|
|
672
720
|
newList.appendChild(li);
|
|
673
721
|
}
|
|
@@ -677,6 +725,8 @@ export function toggleList(root: HTMLElement, kind: 'ul' | 'ol') {
|
|
|
677
725
|
|
|
678
726
|
|
|
679
727
|
|
|
728
|
+
|
|
729
|
+
|
|
680
730
|
export function insertOrEditLink(root: HTMLElement, href?: string) {
|
|
681
731
|
const sel = document.getSelection();
|
|
682
732
|
if (!sel || sel.rangeCount === 0) return;
|
|
@@ -712,7 +762,7 @@ export function insertOrEditLink(root: HTMLElement, href?: string) {
|
|
|
712
762
|
const r = document.createRange();
|
|
713
763
|
r.setStartAfter(link);
|
|
714
764
|
r.collapse(true);
|
|
715
|
-
sel
|
|
765
|
+
sel?.removeAllRanges();
|
|
716
766
|
sel.addRange(r);
|
|
717
767
|
return;
|
|
718
768
|
}
|