dragon-editor 3.0.0 → 3.0.2

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/README.md CHANGED
@@ -127,7 +127,7 @@ Done!
127
127
  const $editor = ref<any>();
128
128
 
129
129
  function setData() {
130
- $editor.value.getContentData([...]);
130
+ $editor.value.setContentData([...]);
131
131
  }
132
132
  </script>
133
133
  ```
package/dist/module.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "dragon-editor",
3
3
  "configKey": "dragon-editor",
4
- "version": "3.0.0"
4
+ "version": "3.0.2"
5
5
  }
@@ -156,15 +156,16 @@ function setDecoration(type: string) {
156
156
  _setNodeStyle(`de-${type}`, editorStore);
157
157
  }
158
158
 
159
- function getContentData() {
159
+ function getContentData(): DEContentData {
160
160
  if (editorStore.$content !== null) {
161
161
  return _getContentData(editorStore.$content);
162
162
  } else {
163
163
  console.error("[DragonEditor] Con't find content Element.");
164
+ return [];
164
165
  }
165
166
  }
166
167
 
167
- function setContentData(data: any[]) {
168
+ function setContentData(data: DEContentData) {
168
169
  _setContentData(data, editorStore);
169
170
  }
170
171
 
@@ -22,7 +22,7 @@
22
22
 
23
23
  <script setup lang="ts">
24
24
  const props = defineProps<{
25
- content: any[];
25
+ content: DEContentData;
26
26
  }>();
27
27
  </script>
28
28
 
@@ -22,3 +22,30 @@ interface DEArrangeCursorData {
22
22
  endNodeIdx: number;
23
23
  endOffset: number;
24
24
  }
25
+
26
+ interface DETextBlock {
27
+ type: "text";
28
+ textContent: string;
29
+ }
30
+
31
+ interface DEHeadingBlock {
32
+ type: "heading";
33
+ level: number;
34
+ id: string;
35
+ textContent: string;
36
+ }
37
+
38
+ interface DEUListBlock {
39
+ type: "ul";
40
+ child: string[];
41
+ }
42
+
43
+ interface DEOListBlock {
44
+ type: "ol";
45
+ pattern: "a" | "i" | "1" | "A" | "I";
46
+ child: string[];
47
+ }
48
+
49
+ type DEContent = DETextBlock | DEHeadingBlock | DEUListBlock | DEOListBlock;
50
+
51
+ type DEContentData = DEContent[];
@@ -1,2 +1,2 @@
1
- export declare function _getContentData($content: HTMLDivElement): any[];
1
+ export declare function _getContentData($content: HTMLDivElement): DEContentData;
2
2
  export declare function _setContentData(data: any[], store: any): void;
@@ -82,10 +82,21 @@ function elementBackspaceEvent(e, store) {
82
82
  case "heading":
83
83
  defaultBlockBackspaceEvent(e, store, $element);
84
84
  break;
85
+ case "list":
86
+ listBlockBackspaceEvent(e, store, $element);
87
+ break;
85
88
  default:
86
89
  console.log("// TODO : \uB2E4\uB978 \uD0C0\uC785 \uBE14\uB7ED \uBC31\uC2A4\uD398\uC774\uC2A4 \uC774\uBCA4\uD2B8", type);
87
90
  }
88
91
  }
92
+ function elementTabEvent(e, store) {
93
+ const { $element, type } = _getBlockType(e.target);
94
+ switch (type) {
95
+ case "text":
96
+ defaultTabEvent(e.shiftKey, $element);
97
+ break;
98
+ }
99
+ }
89
100
  function defaultBlockEnterEvent(store, $element) {
90
101
  const $textBlock = $element;
91
102
  if (store.cursorData.type === "Caret") {
@@ -579,21 +590,12 @@ function defaultBlockShiftEnterEvent(store, $element) {
579
590
  _setCursor($textBlock.childNodes[startNodeIdx + 2], 0);
580
591
  }
581
592
  }
582
- function elementTabEvent(e, store) {
583
- const { $element, type } = _getBlockType(e.target);
584
- switch (type) {
585
- case "text":
586
- defaultTabEvent(e.shiftKey, $element);
587
- break;
588
- }
589
- }
590
593
  function defaultBlockBackspaceEvent(e, store, $element) {
591
594
  const $textBlock = $element;
592
- const childList = store.wrap.querySelectorAll(".de-block");
593
- const childLength = childList.length;
595
+ const childList = store.$content.querySelectorAll(".de-block");
594
596
  const $target = _getParentElementIfNodeIsText(store.cursorData.startNode, $textBlock);
595
597
  let elementIdx = -1;
596
- for (let i = 0; childLength > i; i += 1) {
598
+ for (let i = 0; childList.length > i; i += 1) {
597
599
  if (childList[i] === $element) {
598
600
  elementIdx = i;
599
601
  break;
@@ -618,39 +620,35 @@ function defaultBlockBackspaceEvent(e, store, $element) {
618
620
  if ($textBlock.hasChildNodes() === false) {
619
621
  e.preventDefault();
620
622
  const $preBlock = $textBlock.previousElementSibling;
621
- if ($preBlock !== null) {
622
- const { type: preBlockType } = _getBlockType($textBlock);
623
- $textBlock.remove();
624
- if (preBlockType === "text") {
625
- if ($preBlock.hasChildNodes() === true) {
626
- const textBlockChildList = $preBlock.childNodes;
627
- const textBlockTargetChild = textBlockChildList[textBlockChildList.length - 1];
628
- _setCursor(textBlockTargetChild, textBlockTargetChild.textContent.length);
629
- } else {
630
- _setCursor($preBlock, 0);
631
- }
623
+ const { type: preBlockType } = _getBlockType($preBlock);
624
+ $textBlock.remove();
625
+ if (preBlockType === "text" || preBlockType === "heading") {
626
+ if ($preBlock.hasChildNodes() === true) {
627
+ const textBlockChildList = $preBlock.childNodes;
628
+ const textBlockTargetChild = textBlockChildList[textBlockChildList.length - 1];
629
+ _setCursor(textBlockTargetChild, textBlockTargetChild.textContent.length);
630
+ } else {
631
+ _setCursor($preBlock, 0);
632
632
  }
633
633
  }
634
634
  } else {
635
635
  if (store.cursorData.startOffset === 0 && ($textBlock.childNodes[0] === store.cursorData.startNode || $textBlock.childNodes[0] === $target)) {
636
636
  e.preventDefault();
637
637
  const $preBlock = $textBlock.previousElementSibling;
638
- if ($preBlock !== null) {
639
- const { type: preBlockType } = _getBlockType($preBlock);
640
- if (preBlockType === "text") {
641
- if ($preBlock.hasChildNodes() === true) {
642
- const textBlockChildList = $preBlock.childNodes;
643
- const textBlockTargetChildIdx = textBlockChildList.length - 1;
644
- const textBlockTargetCursorIdx = textBlockChildList[textBlockTargetChildIdx].textContent.length;
645
- const thisBlockHTML = $textBlock.innerHTML;
646
- $preBlock.innerHTML = $preBlock.innerHTML + thisBlockHTML;
647
- _setCursor($preBlock.childNodes[textBlockTargetChildIdx], textBlockTargetCursorIdx);
648
- } else {
649
- $preBlock.innerHTML = $textBlock.innerHTML;
650
- _setCursor($preBlock, 0);
651
- }
652
- $textBlock.remove();
638
+ const { type: preBlockType } = _getBlockType($preBlock);
639
+ if (preBlockType === "text" || preBlockType === "heading") {
640
+ if ($preBlock.hasChildNodes() === true) {
641
+ const textBlockChildList = $preBlock.childNodes;
642
+ const textBlockTargetChildIdx = textBlockChildList.length - 1;
643
+ const textBlockTargetCursorIdx = textBlockChildList[textBlockTargetChildIdx].textContent.length;
644
+ const thisBlockHTML = $textBlock.innerHTML;
645
+ $preBlock.innerHTML = $preBlock.innerHTML + thisBlockHTML;
646
+ _setCursor($preBlock.childNodes[textBlockTargetChildIdx], textBlockTargetCursorIdx);
647
+ } else {
648
+ $preBlock.innerHTML = $textBlock.innerHTML;
649
+ _setCursor($preBlock, 0);
653
650
  }
651
+ $textBlock.remove();
654
652
  }
655
653
  }
656
654
  }
@@ -675,6 +673,125 @@ function defaultBlockBackspaceEvent(e, store, $element) {
675
673
  }
676
674
  _setCursorData(store);
677
675
  }
676
+ function listBlockBackspaceEvent(e, store, $element) {
677
+ const $listBlock = $element;
678
+ const $targetItem = _findContentEditableElement(store.cursorData.startNode);
679
+ const liList = $listBlock.querySelectorAll(".de-item");
680
+ const $target = _getParentElementIfNodeIsText(store.cursorData.startNode, $targetItem);
681
+ let liIdx = -1;
682
+ for (let i = 0; liList.length > i; i += 1) {
683
+ if (liList[i] === $targetItem) {
684
+ liIdx = i;
685
+ break;
686
+ }
687
+ }
688
+ if (liList.length === 1) {
689
+ if ($targetItem.textContent === "") {
690
+ e.preventDefault();
691
+ const $preBlock = $listBlock.previousElementSibling;
692
+ const { type: preBlockType } = _getBlockType($preBlock);
693
+ $listBlock.remove();
694
+ if (preBlockType === "text" || preBlockType === "heading") {
695
+ if ($preBlock.hasChildNodes() === true) {
696
+ const textBlockChildList = $preBlock.childNodes;
697
+ const textBlockTargetChild = textBlockChildList[textBlockChildList.length - 1];
698
+ _setCursor(textBlockTargetChild, textBlockTargetChild.textContent.length);
699
+ } else {
700
+ _setCursor($preBlock, 0);
701
+ }
702
+ }
703
+ } else {
704
+ if (store.cursorData.startOffset === 0 && ($targetItem.childNodes[0] === store.cursorData.startNode || $targetItem.childNodes[0] === $target)) {
705
+ e.preventDefault();
706
+ const $preBlock = $listBlock.previousElementSibling;
707
+ const { type: preBlockType } = _getBlockType($preBlock);
708
+ if (preBlockType === "text" || preBlockType === "heading") {
709
+ if ($preBlock.hasChildNodes() === true) {
710
+ const textBlockChildList = $preBlock.childNodes;
711
+ const textBlockTargetChildIdx = textBlockChildList.length - 1;
712
+ const textBlockTargetCursorIdx = textBlockChildList[textBlockTargetChildIdx].textContent.length;
713
+ const thisBlockHTML = $targetItem.innerHTML;
714
+ $preBlock.innerHTML = $preBlock.innerHTML + thisBlockHTML;
715
+ _setCursor($preBlock.childNodes[textBlockTargetChildIdx], textBlockTargetCursorIdx);
716
+ } else {
717
+ $preBlock.innerHTML = $targetItem.innerHTML;
718
+ _setCursor($preBlock, 0);
719
+ }
720
+ $targetItem.remove();
721
+ $listBlock.remove();
722
+ }
723
+ }
724
+ }
725
+ } else {
726
+ if (liIdx === 0) {
727
+ if ($targetItem.textContent === "") {
728
+ e.preventDefault();
729
+ const $preBlock = $listBlock.previousElementSibling;
730
+ const { type: preBlockType } = _getBlockType($preBlock);
731
+ $targetItem.remove();
732
+ if (preBlockType === "text" || preBlockType === "heading") {
733
+ if ($preBlock.hasChildNodes() === true) {
734
+ const textBlockChildList = $preBlock.childNodes;
735
+ const textBlockTargetChild = textBlockChildList[textBlockChildList.length - 1];
736
+ _setCursor(textBlockTargetChild, textBlockTargetChild.textContent.length);
737
+ } else {
738
+ _setCursor($preBlock, 0);
739
+ }
740
+ }
741
+ } else {
742
+ if (store.cursorData.startOffset === 0 && ($targetItem.childNodes[0] === store.cursorData.startNode || $targetItem.childNodes[0] === $target)) {
743
+ e.preventDefault();
744
+ const $preBlock = $listBlock.previousElementSibling;
745
+ const { type: preBlockType } = _getBlockType($preBlock);
746
+ if (preBlockType === "text" || preBlockType === "heading") {
747
+ if ($preBlock.hasChildNodes() === true) {
748
+ const textBlockChildList = $preBlock.childNodes;
749
+ const textBlockTargetChildIdx = textBlockChildList.length - 1;
750
+ const textBlockTargetCursorIdx = textBlockChildList[textBlockTargetChildIdx].textContent.length;
751
+ const thisBlockHTML = $targetItem.innerHTML;
752
+ $preBlock.innerHTML = $preBlock.innerHTML + thisBlockHTML;
753
+ _setCursor($preBlock.childNodes[textBlockTargetChildIdx], textBlockTargetCursorIdx);
754
+ } else {
755
+ $preBlock.innerHTML = $targetItem.innerHTML;
756
+ _setCursor($preBlock, 0);
757
+ }
758
+ $targetItem.remove();
759
+ }
760
+ }
761
+ }
762
+ } else {
763
+ if ($targetItem.textContent === "") {
764
+ e.preventDefault();
765
+ const $preBlock = liList[liIdx - 1];
766
+ $targetItem.remove();
767
+ if ($preBlock.hasChildNodes() === true) {
768
+ const textBlockChildList = $preBlock.childNodes;
769
+ const textBlockTargetChild = textBlockChildList[textBlockChildList.length - 1];
770
+ _setCursor(textBlockTargetChild, textBlockTargetChild.textContent.length);
771
+ } else {
772
+ _setCursor($preBlock, 0);
773
+ }
774
+ } else {
775
+ if (store.cursorData.startOffset === 0 && ($targetItem.childNodes[0] === store.cursorData.startNode || $targetItem.childNodes[0] === $target)) {
776
+ e.preventDefault();
777
+ const $preBlock = liList[liIdx - 1];
778
+ if ($preBlock.hasChildNodes() === true) {
779
+ const textBlockChildList = $preBlock.childNodes;
780
+ const textBlockTargetChildIdx = textBlockChildList.length - 1;
781
+ const textBlockTargetCursorIdx = textBlockChildList[textBlockTargetChildIdx].textContent.length;
782
+ const thisBlockHTML = $targetItem.innerHTML;
783
+ $preBlock.innerHTML = $preBlock.innerHTML + thisBlockHTML;
784
+ _setCursor($preBlock.childNodes[textBlockTargetChildIdx], textBlockTargetCursorIdx);
785
+ } else {
786
+ $preBlock.innerHTML = $targetItem.innerHTML;
787
+ _setCursor($preBlock, 0);
788
+ }
789
+ $targetItem.remove();
790
+ }
791
+ }
792
+ }
793
+ }
794
+ }
678
795
  function defaultTabEvent(useShiftKey, $element) {
679
796
  const $target = $element;
680
797
  let value = $target.dataset["depth"] === void 0 ? 0 : parseInt($target.dataset["depth"]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dragon-editor",
3
- "version": "3.0.0",
3
+ "version": "3.0.2",
4
4
  "description": "Javascript WYSIWYG editor in Nuxt3!",
5
5
  "repository": {
6
6
  "type": "git",