dragon-editor 3.1.0 → 3.2.0

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.
@@ -1,14 +1,20 @@
1
1
  import { _setCursor, _setCursorData, _clenupCursor, _soltingCursorDataOnElement } from "./cursor.mjs";
2
2
  import { _getParentElementIfNodeIsText, _findContentEditableElement } from "./element.mjs";
3
- import { _getBlockType, _createTextBlock, _createListItemBlock } from "./block.mjs";
3
+ import { _getBlockType, _createTextBlock, _createListItemBlock, _generateId } from "./block.mjs";
4
4
  import { _setNodeStyle } from "./style.mjs";
5
5
  let keyEventCount = 0;
6
6
  let keyEventFn;
7
7
  export function _elementKeyEvent(event, store) {
8
8
  _setCursorData(store);
9
+ const { type: blockType } = _getBlockType(event.target);
9
10
  switch (event.code) {
10
11
  case "Enter":
11
- event.preventDefault();
12
+ if (blockType !== "code") {
13
+ event.preventDefault();
14
+ }
15
+ if (blockType === "code" && event.shiftKey === true) {
16
+ event.preventDefault();
17
+ }
12
18
  if (keyEventCount === 0) {
13
19
  _clenupCursor(store);
14
20
  setTimeout(() => {
@@ -43,10 +49,22 @@ export function _elementKeyEvent(event, store) {
43
49
  case "Space":
44
50
  break;
45
51
  case "ArrowUp":
46
- moveToBlockEvent(event, store, "up");
52
+ if (keyEventCount === 0) {
53
+ moveToBlockEvent(event, store, "up");
54
+ }
55
+ keyEventCount += 1;
56
+ setTimeout(() => {
57
+ keyEventCount = 0;
58
+ }, 250);
47
59
  break;
48
60
  case "ArrowDown":
49
- moveToBlockEvent(event, store, "down");
61
+ if (keyEventCount === 0) {
62
+ moveToBlockEvent(event, store, "down");
63
+ }
64
+ keyEventCount += 1;
65
+ setTimeout(() => {
66
+ keyEventCount = 0;
67
+ }, 250);
50
68
  break;
51
69
  }
52
70
  clearTimeout(keyEventFn);
@@ -68,7 +86,8 @@ function elementEnterEvent(event, store) {
68
86
  case "list":
69
87
  listBlockEnterEvent(event, store, $element);
70
88
  break;
71
- default:
89
+ case "code":
90
+ break;
72
91
  }
73
92
  }
74
93
  function elementShiftEnterEvent(event, store) {
@@ -81,7 +100,9 @@ function elementShiftEnterEvent(event, store) {
81
100
  case "list":
82
101
  listBlockEnterEvent(event, store, $element);
83
102
  break;
84
- default:
103
+ case "code":
104
+ codeBlockShiftEnterEvent($element);
105
+ break;
85
106
  }
86
107
  }
87
108
  function elementBackspaceEvent(e, store) {
@@ -601,6 +622,11 @@ function defaultBlockShiftEnterEvent(store, $element) {
601
622
  _setCursor($textBlock.childNodes[startNodeIdx + 2], 0);
602
623
  }
603
624
  }
625
+ function codeBlockShiftEnterEvent($element) {
626
+ const $newTextBlock = _createTextBlock();
627
+ $element.insertAdjacentElement("afterend", $newTextBlock);
628
+ $newTextBlock.focus();
629
+ }
604
630
  function defaultBlockBackspaceEvent(e, store, $element) {
605
631
  const $textBlock = $element;
606
632
  const childList = store.$content.querySelectorAll(".de-block");
@@ -650,7 +676,7 @@ function defaultBlockBackspaceEvent(e, store, $element) {
650
676
  }
651
677
  }
652
678
  } else {
653
- if (store.cursorData.startOffset === 0 && ($textBlock.childNodes[0] === store.cursorData.startNode || $textBlock.childNodes[0] === $target)) {
679
+ if (store.cursorData.type === "Caret" && store.cursorData.startOffset === 0 && ($textBlock.childNodes[0] === store.cursorData.startNode || $textBlock.childNodes[0] === $target)) {
654
680
  e.preventDefault();
655
681
  const $preBlock = $textBlock.previousElementSibling;
656
682
  const { type: preBlockType } = _getBlockType($preBlock);
@@ -719,7 +745,7 @@ function listBlockBackspaceEvent(e, store, $element) {
719
745
  }
720
746
  }
721
747
  } else {
722
- if (store.cursorData.startOffset === 0 && ($targetItem.childNodes[0] === store.cursorData.startNode || $targetItem.childNodes[0] === $target)) {
748
+ if (store.cursorData.type === "Caret" && store.cursorData.startOffset === 0 && ($targetItem.childNodes[0] === store.cursorData.startNode || $targetItem.childNodes[0] === $target)) {
723
749
  e.preventDefault();
724
750
  const $preBlock = $listBlock.previousElementSibling;
725
751
  const { type: preBlockType } = _getBlockType($preBlock);
@@ -757,7 +783,7 @@ function listBlockBackspaceEvent(e, store, $element) {
757
783
  }
758
784
  }
759
785
  } else {
760
- if (store.cursorData.startOffset === 0 && ($targetItem.childNodes[0] === store.cursorData.startNode || $targetItem.childNodes[0] === $target)) {
786
+ if (store.cursorData.type === "Caret" && store.cursorData.startOffset === 0 && ($targetItem.childNodes[0] === store.cursorData.startNode || $targetItem.childNodes[0] === $target)) {
761
787
  e.preventDefault();
762
788
  const $preBlock = $listBlock.previousElementSibling;
763
789
  const { type: preBlockType } = _getBlockType($preBlock);
@@ -790,7 +816,7 @@ function listBlockBackspaceEvent(e, store, $element) {
790
816
  _setCursor($preBlock, 0);
791
817
  }
792
818
  } else {
793
- if (store.cursorData.startOffset === 0 && ($targetItem.childNodes[0] === store.cursorData.startNode || $targetItem.childNodes[0] === $target)) {
819
+ if (store.cursorData.type === "Caret" && store.cursorData.startOffset === 0 && ($targetItem.childNodes[0] === store.cursorData.startNode || $targetItem.childNodes[0] === $target)) {
794
820
  e.preventDefault();
795
821
  const $preBlock = liList[liIdx - 1];
796
822
  if ($preBlock.hasChildNodes() === true) {
@@ -833,8 +859,14 @@ function moveToBlockEvent(e, store, keyType) {
833
859
  if ($editableElement !== null) {
834
860
  const { $element, type } = _getBlockType($editableElement);
835
861
  let $target;
862
+ if (store.cursorData.startIdx > store.cursorData.endIdx) {
863
+ _setCursor(store.cursorData.startNode, store.cursorData.endIdx);
864
+ } else {
865
+ _setCursor(store.cursorData.startNode, store.cursorData.startIdx);
866
+ }
836
867
  switch (type) {
837
868
  case "list":
869
+ e.preventDefault();
838
870
  if (keyType === "up") {
839
871
  $target = $editableElement.previousElementSibling;
840
872
  } else {
@@ -848,7 +880,11 @@ function moveToBlockEvent(e, store, keyType) {
848
880
  }
849
881
  }
850
882
  break;
883
+ case "code":
884
+ $target = null;
885
+ break;
851
886
  default:
887
+ e.preventDefault();
852
888
  if (keyType === "up") {
853
889
  $target = $element.previousElementSibling;
854
890
  } else {
@@ -856,25 +892,28 @@ function moveToBlockEvent(e, store, keyType) {
856
892
  }
857
893
  }
858
894
  if ($target !== null) {
859
- if ($target.classList.contains("de-block") == true) {
860
- const { $element: $element2, type: targetType } = _getBlockType($target);
861
- switch (targetType) {
862
- case "list":
863
- const $targetItem = $element2.querySelectorAll(".de-item");
864
- let $item;
865
- if (keyType === "up") {
866
- $item = $targetItem[$targetItem.length - 1];
867
- } else {
868
- $item = $targetItem[0];
869
- }
870
- _setCursor($item, 0);
871
- break;
872
- default:
873
- _setCursor($element2, 0);
895
+ _clenupCursor(store);
896
+ setTimeout(() => {
897
+ if ($target.classList.contains("de-block") == true) {
898
+ const { $element: $element2, type: targetType } = _getBlockType($target);
899
+ switch (targetType) {
900
+ case "list":
901
+ const $targetItem = $element2.querySelectorAll(".de-item");
902
+ let $item;
903
+ if (keyType === "up") {
904
+ $item = $targetItem[$targetItem.length - 1];
905
+ } else {
906
+ $item = $targetItem[0];
907
+ }
908
+ _setCursor($item, 0);
909
+ break;
910
+ default:
911
+ _setCursor($element2, 0);
912
+ }
913
+ } else {
914
+ _setCursor($target, 0);
874
915
  }
875
- } else {
876
- _setCursor($target, 0);
877
- }
916
+ });
878
917
  }
879
918
  }
880
919
  }
@@ -910,7 +949,27 @@ export function _hotKeyEvent(event, store) {
910
949
  }
911
950
  }
912
951
  }
913
- export function copyEvent(event, store) {
952
+ export function _copyEvent(event, store) {
914
953
  }
915
- export function pasteEvent(event, store) {
954
+ export async function _pasteEvent(event, store, emit) {
955
+ event.preventDefault();
956
+ const text = await navigator.clipboard.readText();
957
+ const $block = event.target.closest(".de-block");
958
+ if ($block !== null) {
959
+ if (text === "") {
960
+ const clipboardItems = await navigator.clipboard.read();
961
+ const imageItem = clipboardItems[0].types.find((type) => type.startsWith("image/"));
962
+ if (imageItem !== void 0) {
963
+ const blob = await clipboardItems[0].getType(imageItem);
964
+ const file = new File([blob], `${_generateId()}.${imageItem.split("/")[1]}`);
965
+ emit("addPasteImage", file);
966
+ }
967
+ } else {
968
+ const selection = window.getSelection();
969
+ const textNode = document.createTextNode(text);
970
+ selection.deleteFromDocument();
971
+ selection.getRangeAt(0).insertNode(textNode);
972
+ _setCursor(textNode, textNode.length);
973
+ }
974
+ }
916
975
  }