@yurikilian/lex4 1.5.9 → 1.6.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.
@@ -3,7 +3,7 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
3
3
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
4
  import { jsx, jsxs, Fragment } from "react/jsx-runtime";
5
5
  import React, { createContext, useContext, useMemo, useReducer, useState, useRef, useCallback, useEffect, forwardRef, createElement, useImperativeHandle } from "react";
6
- import { $getRoot, $createRangeSelectionFromDom, $getSelection, $isRangeSelection, $isTextNode, FORMAT_ELEMENT_COMMAND, FORMAT_TEXT_COMMAND, OUTDENT_CONTENT_COMMAND, INDENT_CONTENT_COMMAND, $createParagraphNode, $applyNodeReplacement, DecoratorNode, KEY_BACKSPACE_COMMAND, COMMAND_PRIORITY_LOW, KEY_DELETE_COMMAND, $isNodeSelection, $getNodeByKey, $selectAll, SELECTION_CHANGE_COMMAND, KEY_TAB_COMMAND, $isElementNode, $isParagraphNode, $setSelection, FOCUS_COMMAND, $splitNode, $getNearestNodeFromDOMNode, CONTROLLED_TEXT_INSERTION_COMMAND, KEY_DOWN_COMMAND, PASTE_COMMAND, KEY_ENTER_COMMAND, COMMAND_PRIORITY_HIGH, COMMAND_PRIORITY_CRITICAL, $insertNodes, $createLineBreakNode, $createTextNode, createCommand, COMMAND_PRIORITY_EDITOR } from "lexical";
6
+ import { $getRoot, $createRangeSelectionFromDom, $getSelection, $isRangeSelection, $isTextNode, FORMAT_ELEMENT_COMMAND, FORMAT_TEXT_COMMAND, OUTDENT_CONTENT_COMMAND, INDENT_CONTENT_COMMAND, $createParagraphNode, $applyNodeReplacement, DecoratorNode, KEY_BACKSPACE_COMMAND, COMMAND_PRIORITY_LOW, KEY_DELETE_COMMAND, KEY_DOWN_COMMAND, $isNodeSelection, $getNodeByKey, $selectAll, SELECTION_CHANGE_COMMAND, KEY_TAB_COMMAND, $isElementNode, $isParagraphNode, $setSelection, FOCUS_COMMAND, $splitNode, $getNearestNodeFromDOMNode, CONTROLLED_TEXT_INSERTION_COMMAND, PASTE_COMMAND, KEY_ENTER_COMMAND, COMMAND_PRIORITY_HIGH, COMMAND_PRIORITY_CRITICAL, $insertNodes, $createLineBreakNode, $createTextNode, createCommand, COMMAND_PRIORITY_EDITOR } from "lexical";
7
7
  import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
8
8
  import { INSERT_ORDERED_LIST_COMMAND, INSERT_UNORDERED_LIST_COMMAND, ListNode, ListItemNode, $isListNode, $createListItemNode, $createListNode } from "@lexical/list";
9
9
  import { $setBlocksType } from "@lexical/selection";
@@ -1024,7 +1024,11 @@ const DocumentProvider = ({
1024
1024
  register: (pageId, editor) => {
1025
1025
  editorMapRef.current.set(pageId, editor);
1026
1026
  },
1027
- unregister: (pageId) => {
1027
+ unregister: (pageId, editor) => {
1028
+ const currentEditor = editorMapRef.current.get(pageId);
1029
+ if (editor && currentEditor && currentEditor !== editor) {
1030
+ return;
1031
+ }
1028
1032
  editorMapRef.current.delete(pageId);
1029
1033
  },
1030
1034
  get: (pageId) => editorMapRef.current.get(pageId),
@@ -2405,9 +2409,43 @@ function VariableChip({
2405
2409
  },
2406
2410
  COMMAND_PRIORITY_LOW
2407
2411
  );
2412
+ const moveCaretFromSelectedNode = (direction) => {
2413
+ editor.update(() => {
2414
+ const node = $getNodeByKey(nodeKey);
2415
+ if (!$isVariableNode(node)) {
2416
+ return;
2417
+ }
2418
+ if (direction === "backward") {
2419
+ node.selectPrevious();
2420
+ } else {
2421
+ node.selectNext();
2422
+ }
2423
+ });
2424
+ };
2425
+ const unregisterArrowNavigation = editor.registerCommand(
2426
+ KEY_DOWN_COMMAND,
2427
+ (event) => {
2428
+ if (!isSelected || event.metaKey || event.ctrlKey || event.altKey || event.shiftKey) {
2429
+ return false;
2430
+ }
2431
+ if (event.key === "ArrowLeft" || event.key === "ArrowUp") {
2432
+ event.preventDefault();
2433
+ moveCaretFromSelectedNode("backward");
2434
+ return true;
2435
+ }
2436
+ if (event.key === "ArrowRight" || event.key === "ArrowDown") {
2437
+ event.preventDefault();
2438
+ moveCaretFromSelectedNode("forward");
2439
+ return true;
2440
+ }
2441
+ return false;
2442
+ },
2443
+ COMMAND_PRIORITY_LOW
2444
+ );
2408
2445
  return () => {
2409
2446
  unregisterBackspace();
2410
2447
  unregisterDelete();
2448
+ unregisterArrowNavigation();
2411
2449
  };
2412
2450
  }, [editor, isSelected, nodeKey]);
2413
2451
  return /* @__PURE__ */ jsx(
@@ -2491,15 +2529,23 @@ function applyFontSizeToSelectedVariables(editor, size) {
2491
2529
  });
2492
2530
  }
2493
2531
  function readSelectedVariableFormatting(editor) {
2494
- const firstNode = getSelectedVariableNodes(editor)[0];
2495
- if (!firstNode) {
2496
- return {};
2497
- }
2498
- const style = firstNode.getStyle();
2499
- return {
2500
- fontFamily: extractFontFamilyFromStyle(style),
2501
- fontSize: extractFontSizePtFromStyle(style)
2502
- };
2532
+ let formatting = {};
2533
+ editor.getEditorState().read(() => {
2534
+ const selection = $getSelection();
2535
+ if (!$isNodeSelection(selection)) {
2536
+ return;
2537
+ }
2538
+ const firstNode = selection.getNodes().filter($isVariableNode)[0];
2539
+ if (!firstNode) {
2540
+ return;
2541
+ }
2542
+ const style = firstNode.getStyle();
2543
+ formatting = {
2544
+ fontFamily: extractFontFamilyFromStyle(style),
2545
+ fontSize: extractFontSizePtFromStyle(style)
2546
+ };
2547
+ });
2548
+ return formatting;
2503
2549
  }
2504
2550
  const BLOCK_TYPE_OPTIONS = [
2505
2551
  { value: "paragraph", shortLabel: "P" },
@@ -3783,14 +3829,18 @@ const ActiveEditorPlugin = ({
3783
3829
  ]);
3784
3830
  useEffect(() => {
3785
3831
  const caretPosition = { pageId, region };
3786
- const handleFocusIn = () => {
3832
+ const markEditorActive = () => {
3787
3833
  setActivePageId(pageId);
3788
3834
  setActiveEditor(editor, caretPosition);
3789
3835
  onFocus == null ? void 0 : onFocus(editor);
3790
3836
  };
3791
3837
  return editor.registerRootListener((rootElement, prevRootElement) => {
3792
- prevRootElement == null ? void 0 : prevRootElement.removeEventListener("focusin", handleFocusIn);
3793
- rootElement == null ? void 0 : rootElement.addEventListener("focusin", handleFocusIn);
3838
+ prevRootElement == null ? void 0 : prevRootElement.removeEventListener("focusin", markEditorActive);
3839
+ prevRootElement == null ? void 0 : prevRootElement.removeEventListener("mousedown", markEditorActive);
3840
+ prevRootElement == null ? void 0 : prevRootElement.removeEventListener("pointerdown", markEditorActive);
3841
+ rootElement == null ? void 0 : rootElement.addEventListener("focusin", markEditorActive);
3842
+ rootElement == null ? void 0 : rootElement.addEventListener("mousedown", markEditorActive);
3843
+ rootElement == null ? void 0 : rootElement.addEventListener("pointerdown", markEditorActive);
3794
3844
  });
3795
3845
  }, [editor, onFocus, pageId, region, setActiveEditor, setActivePageId]);
3796
3846
  useEffect(() => {
@@ -4460,7 +4510,7 @@ const EditorRegistryPlugin = ({ pageId }) => {
4460
4510
  editorRegistry.register(pageId, editor);
4461
4511
  debug("registry", `registered editor for page ${shortId(pageId)}`);
4462
4512
  return () => {
4463
- editorRegistry.unregister(pageId);
4513
+ editorRegistry.unregister(pageId, editor);
4464
4514
  debug("registry", `unregistered editor for page ${shortId(pageId)}`);
4465
4515
  };
4466
4516
  }, [editor, pageId, editorRegistry]);
@@ -4784,7 +4834,7 @@ const PageView = React.memo(({
4784
4834
  onMoveToPreviousPage,
4785
4835
  onMoveToNextPage
4786
4836
  }) => {
4787
- const { document: document2, dispatch, setActivePageId } = useDocument();
4837
+ const { document: document2, dispatch, editorRegistry, setActiveEditor, setActivePageId } = useDocument();
4788
4838
  const t = useTranslations();
4789
4839
  const page = document2.pages.find((p) => p.id === pageId);
4790
4840
  const showHeaderFooter = document2.headerFooterEnabled;
@@ -4814,7 +4864,11 @@ const PageView = React.memo(({
4814
4864
  );
4815
4865
  const handleFocus = useCallback(() => {
4816
4866
  setActivePageId(pageId);
4817
- }, [setActivePageId, pageId]);
4867
+ const editor = editorRegistry.get(pageId);
4868
+ if (editor) {
4869
+ setActiveEditor(editor, { pageId, region: "body" });
4870
+ }
4871
+ }, [editorRegistry, pageId, setActiveEditor, setActivePageId]);
4818
4872
  const handleOverflow = useCallback(
4819
4873
  (overflowContent, cause) => {
4820
4874
  onOverflow == null ? void 0 : onOverflow(overflowContent, cause);
@@ -5474,6 +5528,20 @@ function insertDocumentContent(editor, document2) {
5474
5528
  });
5475
5529
  return inserted;
5476
5530
  }
5531
+ function resolveDocumentInsertTarget({
5532
+ activeEditor,
5533
+ activeCaretPosition,
5534
+ activePageId,
5535
+ editorRegistry
5536
+ }) {
5537
+ if (activeEditor && (activeCaretPosition == null ? void 0 : activeCaretPosition.region) === "body") {
5538
+ return activeEditor;
5539
+ }
5540
+ if (activePageId) {
5541
+ return editorRegistry.get(activePageId) ?? null;
5542
+ }
5543
+ return editorRegistry.all()[0] ?? null;
5544
+ }
5477
5545
  function selectEntireDocument(rootElement, selectionBuffer) {
5478
5546
  if (!rootElement || !selectionBuffer) {
5479
5547
  return;
@@ -5679,8 +5747,10 @@ const EditorChrome = ({
5679
5747
  const EditorWithHandle = forwardRef(({ captureHistoryShortcutsOnWindow, onSave, className }, ref) => {
5680
5748
  const {
5681
5749
  document: doc,
5750
+ activePageId,
5682
5751
  activeEditor,
5683
5752
  activeCaretPosition,
5753
+ editorRegistry,
5684
5754
  historySidebarOpen,
5685
5755
  runHistoryAction,
5686
5756
  setHistorySidebarOpen
@@ -5689,7 +5759,6 @@ const EditorWithHandle = forwardRef(({ captureHistoryShortcutsOnWindow, onSave,
5689
5759
  const t = useTranslations();
5690
5760
  const documentRef = useRef(doc);
5691
5761
  const activeEditorRef = useRef(activeEditor);
5692
- const activeCaretRegionRef = useRef(activeCaretPosition == null ? void 0 : activeCaretPosition.region);
5693
5762
  const historySidebarOpenRef = useRef(historySidebarOpen);
5694
5763
  const runHistoryActionRef = useRef(runHistoryAction);
5695
5764
  const insertedDocumentContentLabelRef = useRef(t.history.actions.insertedDocumentContent);
@@ -5699,9 +5768,6 @@ const EditorWithHandle = forwardRef(({ captureHistoryShortcutsOnWindow, onSave,
5699
5768
  useEffect(() => {
5700
5769
  activeEditorRef.current = activeEditor;
5701
5770
  }, [activeEditor]);
5702
- useEffect(() => {
5703
- activeCaretRegionRef.current = activeCaretPosition == null ? void 0 : activeCaretPosition.region;
5704
- }, [activeCaretPosition == null ? void 0 : activeCaretPosition.region]);
5705
5771
  useEffect(() => {
5706
5772
  historySidebarOpenRef.current = historySidebarOpen;
5707
5773
  }, [historySidebarOpen]);
@@ -5724,8 +5790,13 @@ const EditorWithHandle = forwardRef(({ captureHistoryShortcutsOnWindow, onSave,
5724
5790
  setHistorySidebarOpen(!historySidebarOpenRef.current);
5725
5791
  };
5726
5792
  handle.insertDocumentContent = (documentToInsert) => {
5727
- const currentActiveEditor = activeEditorRef.current;
5728
- if (!currentActiveEditor || activeCaretRegionRef.current !== "body") {
5793
+ const targetEditor = resolveDocumentInsertTarget({
5794
+ activeEditor: activeEditorRef.current,
5795
+ activeCaretPosition,
5796
+ activePageId,
5797
+ editorRegistry
5798
+ });
5799
+ if (!targetEditor) {
5729
5800
  return false;
5730
5801
  }
5731
5802
  let inserted = false;
@@ -5736,7 +5807,7 @@ const EditorWithHandle = forwardRef(({ captureHistoryShortcutsOnWindow, onSave,
5736
5807
  region: "document"
5737
5808
  },
5738
5809
  () => {
5739
- inserted = insertDocumentContent(currentActiveEditor, documentToInsert);
5810
+ inserted = insertDocumentContent(targetEditor, documentToInsert);
5740
5811
  }
5741
5812
  );
5742
5813
  return inserted;