@yurikilian/lex4 1.5.8 → 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" },
@@ -3781,6 +3827,22 @@ const ActiveEditorPlugin = ({
3781
3827
  setActiveEditor,
3782
3828
  setActivePageId
3783
3829
  ]);
3830
+ useEffect(() => {
3831
+ const caretPosition = { pageId, region };
3832
+ const markEditorActive = () => {
3833
+ setActivePageId(pageId);
3834
+ setActiveEditor(editor, caretPosition);
3835
+ onFocus == null ? void 0 : onFocus(editor);
3836
+ };
3837
+ return editor.registerRootListener((rootElement, prevRootElement) => {
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);
3844
+ });
3845
+ }, [editor, onFocus, pageId, region, setActiveEditor, setActivePageId]);
3784
3846
  useEffect(() => {
3785
3847
  return editor.registerCommand(
3786
3848
  FOCUS_COMMAND,
@@ -4448,7 +4510,7 @@ const EditorRegistryPlugin = ({ pageId }) => {
4448
4510
  editorRegistry.register(pageId, editor);
4449
4511
  debug("registry", `registered editor for page ${shortId(pageId)}`);
4450
4512
  return () => {
4451
- editorRegistry.unregister(pageId);
4513
+ editorRegistry.unregister(pageId, editor);
4452
4514
  debug("registry", `unregistered editor for page ${shortId(pageId)}`);
4453
4515
  };
4454
4516
  }, [editor, pageId, editorRegistry]);
@@ -4772,7 +4834,7 @@ const PageView = React.memo(({
4772
4834
  onMoveToPreviousPage,
4773
4835
  onMoveToNextPage
4774
4836
  }) => {
4775
- const { document: document2, dispatch, setActivePageId } = useDocument();
4837
+ const { document: document2, dispatch, editorRegistry, setActiveEditor, setActivePageId } = useDocument();
4776
4838
  const t = useTranslations();
4777
4839
  const page = document2.pages.find((p) => p.id === pageId);
4778
4840
  const showHeaderFooter = document2.headerFooterEnabled;
@@ -4802,7 +4864,11 @@ const PageView = React.memo(({
4802
4864
  );
4803
4865
  const handleFocus = useCallback(() => {
4804
4866
  setActivePageId(pageId);
4805
- }, [setActivePageId, pageId]);
4867
+ const editor = editorRegistry.get(pageId);
4868
+ if (editor) {
4869
+ setActiveEditor(editor, { pageId, region: "body" });
4870
+ }
4871
+ }, [editorRegistry, pageId, setActiveEditor, setActivePageId]);
4806
4872
  const handleOverflow = useCallback(
4807
4873
  (overflowContent, cause) => {
4808
4874
  onOverflow == null ? void 0 : onOverflow(overflowContent, cause);
@@ -5462,6 +5528,20 @@ function insertDocumentContent(editor, document2) {
5462
5528
  });
5463
5529
  return inserted;
5464
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
+ }
5465
5545
  function selectEntireDocument(rootElement, selectionBuffer) {
5466
5546
  if (!rootElement || !selectionBuffer) {
5467
5547
  return;
@@ -5667,8 +5747,10 @@ const EditorChrome = ({
5667
5747
  const EditorWithHandle = forwardRef(({ captureHistoryShortcutsOnWindow, onSave, className }, ref) => {
5668
5748
  const {
5669
5749
  document: doc,
5750
+ activePageId,
5670
5751
  activeEditor,
5671
5752
  activeCaretPosition,
5753
+ editorRegistry,
5672
5754
  historySidebarOpen,
5673
5755
  runHistoryAction,
5674
5756
  setHistorySidebarOpen
@@ -5677,7 +5759,6 @@ const EditorWithHandle = forwardRef(({ captureHistoryShortcutsOnWindow, onSave,
5677
5759
  const t = useTranslations();
5678
5760
  const documentRef = useRef(doc);
5679
5761
  const activeEditorRef = useRef(activeEditor);
5680
- const activeCaretRegionRef = useRef(activeCaretPosition == null ? void 0 : activeCaretPosition.region);
5681
5762
  const historySidebarOpenRef = useRef(historySidebarOpen);
5682
5763
  const runHistoryActionRef = useRef(runHistoryAction);
5683
5764
  const insertedDocumentContentLabelRef = useRef(t.history.actions.insertedDocumentContent);
@@ -5687,9 +5768,6 @@ const EditorWithHandle = forwardRef(({ captureHistoryShortcutsOnWindow, onSave,
5687
5768
  useEffect(() => {
5688
5769
  activeEditorRef.current = activeEditor;
5689
5770
  }, [activeEditor]);
5690
- useEffect(() => {
5691
- activeCaretRegionRef.current = activeCaretPosition == null ? void 0 : activeCaretPosition.region;
5692
- }, [activeCaretPosition == null ? void 0 : activeCaretPosition.region]);
5693
5771
  useEffect(() => {
5694
5772
  historySidebarOpenRef.current = historySidebarOpen;
5695
5773
  }, [historySidebarOpen]);
@@ -5712,8 +5790,13 @@ const EditorWithHandle = forwardRef(({ captureHistoryShortcutsOnWindow, onSave,
5712
5790
  setHistorySidebarOpen(!historySidebarOpenRef.current);
5713
5791
  };
5714
5792
  handle.insertDocumentContent = (documentToInsert) => {
5715
- const currentActiveEditor = activeEditorRef.current;
5716
- if (!currentActiveEditor || activeCaretRegionRef.current !== "body") {
5793
+ const targetEditor = resolveDocumentInsertTarget({
5794
+ activeEditor: activeEditorRef.current,
5795
+ activeCaretPosition,
5796
+ activePageId,
5797
+ editorRegistry
5798
+ });
5799
+ if (!targetEditor) {
5717
5800
  return false;
5718
5801
  }
5719
5802
  let inserted = false;
@@ -5724,7 +5807,7 @@ const EditorWithHandle = forwardRef(({ captureHistoryShortcutsOnWindow, onSave,
5724
5807
  region: "document"
5725
5808
  },
5726
5809
  () => {
5727
- inserted = insertDocumentContent(currentActiveEditor, documentToInsert);
5810
+ inserted = insertDocumentContent(targetEditor, documentToInsert);
5728
5811
  }
5729
5812
  );
5730
5813
  return inserted;