el-text-editor 0.0.69 → 0.0.70

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.
@@ -641,6 +641,8 @@ class ElTextEditorComponent {
641
641
  let editor = document.getElementById('editor');
642
642
  if (editor) {
643
643
  this.moveCaretToEnd(editor);
644
+ this.onEditorClick(editor);
645
+ this.onKeyboardMove(editor);
644
646
  }
645
647
  this.placeHolderRemove.emit();
646
648
  }
@@ -666,6 +668,8 @@ class ElTextEditorComponent {
666
668
  this.setupMutationObserver(editor);
667
669
  // Move the caret to the end after initial content setup
668
670
  this.moveCaretToEnd(editor);
671
+ this.onEditorClick(editor);
672
+ this.onKeyboardMove(editor);
669
673
  // Track caret position on mouse click
670
674
  editor.addEventListener('click', () => {
671
675
  if (editor) { // Ensure editor is not null
@@ -695,6 +699,16 @@ class ElTextEditorComponent {
695
699
  });
696
700
  observer.observe(editor, { childList: true, subtree: true, characterData: true });
697
701
  }
702
+ // private moveCaretToEnd(editor: HTMLElement) {
703
+ // const selection = window.getSelection();
704
+ // const range = document.createRange();
705
+ // if (editor.lastChild) {
706
+ // range.selectNodeContents(editor);
707
+ // range.collapse(false); // Collapse to the end
708
+ // selection?.removeAllRanges();
709
+ // selection?.addRange(range);
710
+ // }
711
+ // }
698
712
  moveCaretToEnd(editor) {
699
713
  const selection = window.getSelection();
700
714
  const range = document.createRange();
@@ -705,6 +719,39 @@ class ElTextEditorComponent {
705
719
  selection?.addRange(range);
706
720
  }
707
721
  }
722
+ onKeyboardMove(editor) {
723
+ editor.addEventListener('keydown', (event) => {
724
+ const selection = window.getSelection();
725
+ const range = document.createRange();
726
+ if (selection && selection.rangeCount > 0) {
727
+ const currentRange = selection.getRangeAt(0);
728
+ const currentCaretPosition = currentRange.startContainer;
729
+ if (event.key === 'ArrowRight' || event.key === 'ArrowLeft' || event.key === 'ArrowUp' || event.key === 'ArrowDown') {
730
+ // You can handle specific behavior for different arrow keys here
731
+ // For example, move caret within a specific node or across nodes
732
+ }
733
+ }
734
+ });
735
+ }
736
+ onEditorClick(editor) {
737
+ editor.addEventListener('click', (event) => {
738
+ const selection = window.getSelection();
739
+ const range = document.createRange();
740
+ const clickedNode = event.target;
741
+ if (clickedNode && clickedNode !== editor) {
742
+ // Place the caret at the clicked position
743
+ range.setStart(clickedNode, 0);
744
+ range.setEnd(clickedNode, clickedNode?.length);
745
+ }
746
+ else {
747
+ // Move caret to the end if clicked inside the editor but not on text node
748
+ range.selectNodeContents(editor);
749
+ range.collapse(false);
750
+ }
751
+ selection?.removeAllRanges();
752
+ selection?.addRange(range);
753
+ });
754
+ }
708
755
  // init_Func(html: any) {
709
756
  // if (html) {
710
757
  // let editor = document.getElementById('editor');