@yurikilian/lex4 1.6.0 → 1.7.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.
@@ -779,6 +779,115 @@ const TranslationsProvider = ({
779
779
  );
780
780
  return /* @__PURE__ */ jsxRuntime.jsx(TranslationsContext.Provider, { value: merged, children });
781
781
  };
782
+ const createStoreImpl = (createState) => {
783
+ let state;
784
+ const listeners = /* @__PURE__ */ new Set();
785
+ const setState = (partial, replace) => {
786
+ const nextState = typeof partial === "function" ? partial(state) : partial;
787
+ if (!Object.is(nextState, state)) {
788
+ const previousState = state;
789
+ state = (replace != null ? replace : typeof nextState !== "object" || nextState === null) ? nextState : Object.assign({}, state, nextState);
790
+ listeners.forEach((listener) => listener(state, previousState));
791
+ }
792
+ };
793
+ const getState = () => state;
794
+ const getInitialState = () => initialState;
795
+ const subscribe = (listener) => {
796
+ listeners.add(listener);
797
+ return () => listeners.delete(listener);
798
+ };
799
+ const api = { setState, getState, getInitialState, subscribe };
800
+ const initialState = state = createState(setState, getState, api);
801
+ return api;
802
+ };
803
+ const createStore = (createState) => createState ? createStoreImpl(createState) : createStoreImpl;
804
+ const identity = (arg) => arg;
805
+ function useStore(api, selector = identity) {
806
+ const slice = React.useSyncExternalStore(
807
+ api.subscribe,
808
+ React.useCallback(() => selector(api.getState()), [api, selector]),
809
+ React.useCallback(() => selector(api.getInitialState()), [api, selector])
810
+ );
811
+ React.useDebugValue(slice);
812
+ return slice;
813
+ }
814
+ const SUPPORTED_FONT_SIZES = [
815
+ 8,
816
+ 9,
817
+ 10,
818
+ 11,
819
+ 12,
820
+ 14,
821
+ 16,
822
+ 18,
823
+ 20,
824
+ 24,
825
+ 28,
826
+ 32,
827
+ 36,
828
+ 48,
829
+ 72
830
+ ];
831
+ const DEFAULT_FONT_SIZE = 12;
832
+ function applyFontSize(editor, size) {
833
+ editor.update(() => {
834
+ const selection2 = lexical.$getSelection();
835
+ if (!lexical.$isRangeSelection(selection2)) return;
836
+ const nodes = selection2.getNodes();
837
+ for (const node of nodes) {
838
+ if (lexical.$isTextNode(node)) {
839
+ const existing = node.getStyle();
840
+ const updated = mergeFontSize(existing, size);
841
+ node.setStyle(updated);
842
+ }
843
+ }
844
+ });
845
+ }
846
+ function mergeFontSize(existingStyle, size) {
847
+ const stripped = existingStyle.replace(/font-size:\s*[^;]+;?\s*/g, "").trim();
848
+ const sizeDecl = `font-size: ${size}pt`;
849
+ return stripped ? `${stripped}; ${sizeDecl}` : sizeDecl;
850
+ }
851
+ const DEFAULT_TOOLBAR_STYLE_SNAPSHOT = {
852
+ blockType: "paragraph",
853
+ fontFamily: "Calibri",
854
+ fontSize: DEFAULT_FONT_SIZE,
855
+ alignment: "left",
856
+ isBold: false,
857
+ isItalic: false,
858
+ isUnderline: false,
859
+ isStrikethrough: false,
860
+ hasSelectedVariable: false
861
+ };
862
+ function createToolbarStyleStore(initialSnapshot = DEFAULT_TOOLBAR_STYLE_SNAPSHOT) {
863
+ return createStore((set) => ({
864
+ ...initialSnapshot,
865
+ setSnapshot: (snapshot) => set(snapshot),
866
+ reset: () => set(DEFAULT_TOOLBAR_STYLE_SNAPSHOT)
867
+ }));
868
+ }
869
+ const ToolbarStyleStoreContext = React.createContext(null);
870
+ const ToolbarStyleStoreProvider = ({ children }) => {
871
+ const storeRef = React.useRef(null);
872
+ if (!storeRef.current) {
873
+ storeRef.current = createToolbarStyleStore();
874
+ }
875
+ return /* @__PURE__ */ jsxRuntime.jsx(ToolbarStyleStoreContext.Provider, { value: storeRef.current, children });
876
+ };
877
+ function useToolbarStyleStore(selector) {
878
+ const store = React.useContext(ToolbarStyleStoreContext);
879
+ if (!store) {
880
+ throw new Error("useToolbarStyleStore must be used within a ToolbarStyleStoreProvider");
881
+ }
882
+ return useStore(store, selector);
883
+ }
884
+ function useToolbarStyleStoreApi() {
885
+ const store = React.useContext(ToolbarStyleStoreContext);
886
+ if (!store) {
887
+ throw new Error("useToolbarStyleStoreApi must be used within a ToolbarStyleStoreProvider");
888
+ }
889
+ return store;
890
+ }
782
891
  const HISTORY_RESTORE_SUPPRESSION_MS = 100;
783
892
  const HISTORY_BATCH_FLUSH_MS = 16;
784
893
  function cloneDocumentSnapshot(document2) {
@@ -1335,7 +1444,7 @@ const DocumentProvider = ({
1335
1444
  undo,
1336
1445
  redo,
1337
1446
  editorRegistry
1338
- }, children });
1447
+ }, children: /* @__PURE__ */ jsxRuntime.jsx(ToolbarStyleStoreProvider, { children }) });
1339
1448
  };
1340
1449
  /**
1341
1450
  * @license lucide-react v1.8.0 - ISC
@@ -2083,43 +2192,6 @@ function applyFontFamily(editor, fontFamily) {
2083
2192
  }
2084
2193
  });
2085
2194
  }
2086
- const SUPPORTED_FONT_SIZES = [
2087
- 8,
2088
- 9,
2089
- 10,
2090
- 11,
2091
- 12,
2092
- 14,
2093
- 16,
2094
- 18,
2095
- 20,
2096
- 24,
2097
- 28,
2098
- 32,
2099
- 36,
2100
- 48,
2101
- 72
2102
- ];
2103
- const DEFAULT_FONT_SIZE = 12;
2104
- function applyFontSize(editor, size) {
2105
- editor.update(() => {
2106
- const selection2 = lexical.$getSelection();
2107
- if (!lexical.$isRangeSelection(selection2)) return;
2108
- const nodes = selection2.getNodes();
2109
- for (const node of nodes) {
2110
- if (lexical.$isTextNode(node)) {
2111
- const existing = node.getStyle();
2112
- const updated = mergeFontSize(existing, size);
2113
- node.setStyle(updated);
2114
- }
2115
- }
2116
- });
2117
- }
2118
- function mergeFontSize(existingStyle, size) {
2119
- const stripped = existingStyle.replace(/font-size:\s*[^;]+;?\s*/g, "").trim();
2120
- const sizeDecl = `font-size: ${size}pt`;
2121
- return stripped ? `${stripped}; ${sizeDecl}` : sizeDecl;
2122
- }
2123
2195
  function toggleFormat(editor, format) {
2124
2196
  editor.dispatchCommand(lexical.FORMAT_TEXT_COMMAND, format);
2125
2197
  }
@@ -2151,48 +2223,77 @@ function indentContent(editor) {
2151
2223
  function outdentContent(editor) {
2152
2224
  editor.dispatchCommand(lexical.OUTDENT_CONTENT_COMMAND, void 0);
2153
2225
  }
2154
- function setBlockType(editor, blockType) {
2155
- editor.update(() => {
2156
- const selection$1 = lexical.$getSelection();
2157
- if (!lexical.$isRangeSelection(selection$1)) {
2158
- return;
2159
- }
2160
- if (blockType === "paragraph") {
2161
- selection.$setBlocksType(selection$1, () => lexical.$createParagraphNode());
2162
- return;
2163
- }
2164
- selection.$setBlocksType(selection$1, () => richText.$createHeadingNode(blockType));
2165
- });
2226
+ const INLINE_BLOCK_STYLE_PROPERTY = "--lex4-block-type";
2227
+ const INLINE_BLOCK_STYLE_PRESETS = {
2228
+ paragraph: {
2229
+ [INLINE_BLOCK_STYLE_PROPERTY]: "paragraph",
2230
+ "font-size": "12pt",
2231
+ "font-weight": "400"
2232
+ },
2233
+ h1: {
2234
+ [INLINE_BLOCK_STYLE_PROPERTY]: "h1",
2235
+ "font-size": "22.5pt",
2236
+ "font-weight": "700"
2237
+ },
2238
+ h2: {
2239
+ [INLINE_BLOCK_STYLE_PROPERTY]: "h2",
2240
+ "font-size": "18pt",
2241
+ "font-weight": "700"
2242
+ },
2243
+ h3: {
2244
+ [INLINE_BLOCK_STYLE_PROPERTY]: "h3",
2245
+ "font-size": "15pt",
2246
+ "font-weight": "600"
2247
+ },
2248
+ h4: {
2249
+ [INLINE_BLOCK_STYLE_PROPERTY]: "h4",
2250
+ "font-size": "13.5pt",
2251
+ "font-weight": "600"
2252
+ },
2253
+ h5: {
2254
+ [INLINE_BLOCK_STYLE_PROPERTY]: "h5",
2255
+ "font-size": "12pt",
2256
+ "font-weight": "500"
2257
+ },
2258
+ h6: {
2259
+ [INLINE_BLOCK_STYLE_PROPERTY]: "h6",
2260
+ "font-size": "11.25pt",
2261
+ "font-weight": "500"
2262
+ }
2263
+ };
2264
+ function escapeStyleProperty(property) {
2265
+ const escapedProperty = property.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
2266
+ return escapedProperty;
2166
2267
  }
2167
- function getActiveBlockType(editor) {
2168
- let blockType = "paragraph";
2169
- editor.getEditorState().read(() => {
2170
- const selection2 = lexical.$getSelection();
2171
- if (!lexical.$isRangeSelection(selection2)) {
2172
- return;
2173
- }
2174
- const anchorNode = selection2.anchor.getNode();
2175
- const topLevelElement = anchorNode.getTopLevelElementOrThrow();
2176
- if (richText.$isHeadingNode(topLevelElement)) {
2177
- blockType = topLevelElement.getTag();
2178
- }
2179
- });
2180
- return blockType;
2268
+ function stripStyleDeclaration(existingStyle, property) {
2269
+ const escapedProperty = escapeStyleProperty(property);
2270
+ return existingStyle.replace(
2271
+ new RegExp(`${escapedProperty}:\\s*[^;]+;?\\s*`, "g"),
2272
+ ""
2273
+ ).trim();
2274
+ }
2275
+ function isSupportedInlineBlockType(value) {
2276
+ return value === "paragraph" || value === "h1" || value === "h2" || value === "h3" || value === "h4" || value === "h5" || value === "h6";
2181
2277
  }
2182
2278
  function extractStyleValue(style, property) {
2183
- const escapedProperty = property.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
2279
+ const escapedProperty = escapeStyleProperty(property);
2184
2280
  const match = style.match(new RegExp(`${escapedProperty}:\\s*([^;]+)`));
2185
2281
  return match ? match[1].trim().replace(/['"]/g, "") : void 0;
2186
2282
  }
2283
+ function removeStyleDeclaration(existingStyle, property) {
2284
+ return stripStyleDeclaration(existingStyle, property);
2285
+ }
2187
2286
  function mergeStyleDeclaration(existingStyle, property, value) {
2188
- const escapedProperty = property.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
2189
- const stripped = existingStyle.replace(
2190
- new RegExp(`${escapedProperty}:\\s*[^;]+;?\\s*`, "g"),
2191
- ""
2192
- ).trim();
2287
+ const stripped = stripStyleDeclaration(existingStyle, property);
2193
2288
  const declaration = `${property}: ${value}`;
2194
2289
  return stripped ? `${stripped}; ${declaration}` : declaration;
2195
2290
  }
2291
+ function mergeStyleDeclarations(existingStyle, declarations) {
2292
+ return Object.entries(declarations).reduce(
2293
+ (style, [property, value]) => mergeStyleDeclaration(style, property, value),
2294
+ existingStyle
2295
+ );
2296
+ }
2196
2297
  function extractFontFamilyFromStyle(style) {
2197
2298
  return extractStyleValue(style, "font-family");
2198
2299
  }
@@ -2210,6 +2311,21 @@ function mergeFontFamilyStyle(existingStyle, fontFamily) {
2210
2311
  function mergeFontSizeStyle(existingStyle, size) {
2211
2312
  return mergeStyleDeclaration(existingStyle, "font-size", `${size}pt`);
2212
2313
  }
2314
+ function extractInlineBlockTypeFromStyle(style) {
2315
+ const value = extractStyleValue(style, INLINE_BLOCK_STYLE_PROPERTY);
2316
+ return isSupportedInlineBlockType(value) ? value : void 0;
2317
+ }
2318
+ function createInlineBlockTypeStylePatch(blockType) {
2319
+ return INLINE_BLOCK_STYLE_PRESETS[blockType];
2320
+ }
2321
+ function mergeInlineBlockTypeStyle(existingStyle, blockType) {
2322
+ const baseStyle = [
2323
+ INLINE_BLOCK_STYLE_PROPERTY,
2324
+ "font-size",
2325
+ "font-weight"
2326
+ ].reduce((style, property) => removeStyleDeclaration(style, property), existingStyle);
2327
+ return mergeStyleDeclarations(baseStyle, createInlineBlockTypeStylePatch(blockType));
2328
+ }
2213
2329
  const EMPTY_CONTEXT = {
2214
2330
  definitions: [],
2215
2331
  refreshDefinitions: () => {
@@ -2351,9 +2467,11 @@ function VariableChip({
2351
2467
  const style = React.useMemo(() => {
2352
2468
  const fontFamily = extractFontFamilyFromStyle(styleValue);
2353
2469
  const fontSize = extractFontSizePtFromStyle(styleValue);
2470
+ const fontWeight = extractStyleValue(styleValue, "font-weight");
2354
2471
  return {
2355
2472
  ...fontFamily ? { fontFamily } : {},
2356
- ...fontSize ? { fontSize: `${fontSize}pt` } : {}
2473
+ ...fontSize ? { fontSize: `${fontSize}pt` } : {},
2474
+ ...fontWeight ? { fontWeight } : {}
2357
2475
  };
2358
2476
  }, [styleValue]);
2359
2477
  const className = [
@@ -2470,7 +2588,126 @@ function $createVariableNode(variableKey, format = 0, style = "") {
2470
2588
  function $isVariableNode(node) {
2471
2589
  return node instanceof VariableNode;
2472
2590
  }
2473
- const FORMAT_MASKS = {
2591
+ function getElementBlockType$1(element) {
2592
+ if (richText.$isHeadingNode(element)) {
2593
+ return element.getTag();
2594
+ }
2595
+ return "paragraph";
2596
+ }
2597
+ function getVariableTopLevelElement(variable) {
2598
+ const topLevelElement = variable.getTopLevelElementOrThrow();
2599
+ return lexical.$isElementNode(topLevelElement) ? topLevelElement : null;
2600
+ }
2601
+ function replaceTopLevelBlockType(element, blockType) {
2602
+ const currentType = getElementBlockType$1(element);
2603
+ if (currentType === blockType) {
2604
+ return;
2605
+ }
2606
+ const nextElement = blockType === "paragraph" ? lexical.$createParagraphNode() : richText.$createHeadingNode(blockType);
2607
+ nextElement.setFormat(element.getFormatType());
2608
+ nextElement.setIndent(element.getIndent());
2609
+ const children = element.getChildren();
2610
+ for (const child of children) {
2611
+ nextElement.append(child);
2612
+ }
2613
+ element.replace(nextElement);
2614
+ }
2615
+ function isPartialSingleBlockSelection(selection2) {
2616
+ if (selection2.isCollapsed()) {
2617
+ return false;
2618
+ }
2619
+ const anchorTopLevel = selection2.anchor.getNode().getTopLevelElementOrThrow();
2620
+ const focusTopLevel = selection2.focus.getNode().getTopLevelElementOrThrow();
2621
+ if (!anchorTopLevel.is(focusTopLevel)) {
2622
+ return false;
2623
+ }
2624
+ const selectedText = selection2.getTextContent().trim();
2625
+ const blockText = anchorTopLevel.getTextContent().trim();
2626
+ return selectedText.length > 0 && selectedText.length < blockText.length;
2627
+ }
2628
+ function applySemanticBlockType(selection$1, blockType) {
2629
+ if (blockType === "paragraph") {
2630
+ selection.$setBlocksType(selection$1, () => lexical.$createParagraphNode());
2631
+ return;
2632
+ }
2633
+ selection.$setBlocksType(selection$1, () => richText.$createHeadingNode(blockType));
2634
+ }
2635
+ function selectedVariablesOccupyEntireBlock(variables, topLevelElement) {
2636
+ const selectedKeys = new Set(variables.map((variable) => variable.getKey()));
2637
+ const meaningfulChildren = topLevelElement.getChildren().filter(
2638
+ (child) => !(lexical.$isTextNode(child) && child.getTextContent().trim() === "")
2639
+ );
2640
+ if (meaningfulChildren.length === 0) {
2641
+ return false;
2642
+ }
2643
+ return meaningfulChildren.every(
2644
+ (child) => $isVariableNode(child) && selectedKeys.has(child.getKey())
2645
+ );
2646
+ }
2647
+ function getStandaloneVariableChildren(topLevelElement) {
2648
+ const meaningfulChildren = topLevelElement.getChildren().filter(
2649
+ (child) => !(lexical.$isTextNode(child) && child.getTextContent().trim() === "")
2650
+ );
2651
+ if (meaningfulChildren.length === 0 || !meaningfulChildren.every($isVariableNode)) {
2652
+ return null;
2653
+ }
2654
+ return meaningfulChildren;
2655
+ }
2656
+ function setBlockType(editor, blockType) {
2657
+ editor.update(() => {
2658
+ const currentSelection = lexical.$getSelection();
2659
+ const selection$1 = lexical.$isNodeSelection(currentSelection) ? currentSelection : lexical.$createRangeSelectionFromDom(window.getSelection(), editor) ?? currentSelection;
2660
+ if (lexical.$isRangeSelection(selection$1)) {
2661
+ lexical.$setSelection(selection$1);
2662
+ }
2663
+ if (lexical.$isNodeSelection(selection$1)) {
2664
+ const variables = selection$1.getNodes().filter($isVariableNode);
2665
+ if (variables.length === 0) {
2666
+ return;
2667
+ }
2668
+ const firstTopLevelElement = getVariableTopLevelElement(variables[0]);
2669
+ if (!firstTopLevelElement) {
2670
+ return;
2671
+ }
2672
+ const sameTopLevelElement = variables.every(
2673
+ (variable) => {
2674
+ var _a;
2675
+ return ((_a = getVariableTopLevelElement(variable)) == null ? void 0 : _a.is(firstTopLevelElement)) ?? false;
2676
+ }
2677
+ );
2678
+ if (sameTopLevelElement && selectedVariablesOccupyEntireBlock(variables, firstTopLevelElement)) {
2679
+ for (const variable of variables) {
2680
+ variable.setStyle(mergeInlineBlockTypeStyle(variable.getStyle(), blockType));
2681
+ }
2682
+ replaceTopLevelBlockType(firstTopLevelElement, blockType);
2683
+ const nextSelection = lexical.$createNodeSelection();
2684
+ for (const variable of variables) {
2685
+ nextSelection.add(variable.getKey());
2686
+ }
2687
+ lexical.$setSelection(nextSelection);
2688
+ return;
2689
+ }
2690
+ for (const variable of variables) {
2691
+ variable.setStyle(mergeInlineBlockTypeStyle(variable.getStyle(), blockType));
2692
+ }
2693
+ return;
2694
+ }
2695
+ if (!lexical.$isRangeSelection(selection$1)) {
2696
+ return;
2697
+ }
2698
+ const anchorTopLevel = selection$1.anchor.getNode().getTopLevelElementOrThrow();
2699
+ const standaloneVariables = lexical.$isElementNode(anchorTopLevel) ? getStandaloneVariableChildren(anchorTopLevel) : null;
2700
+ if (isPartialSingleBlockSelection(selection$1)) {
2701
+ selection.$patchStyleText(selection$1, createInlineBlockTypeStylePatch(blockType));
2702
+ return;
2703
+ }
2704
+ for (const variable of standaloneVariables ?? []) {
2705
+ variable.setStyle(mergeInlineBlockTypeStyle(variable.getStyle(), blockType));
2706
+ }
2707
+ applySemanticBlockType(selection$1, blockType);
2708
+ });
2709
+ }
2710
+ const FORMAT_MASKS$1 = {
2474
2711
  bold: 1,
2475
2712
  italic: 2,
2476
2713
  strikethrough: 4,
@@ -2492,19 +2729,8 @@ function withSelectedVariableNodes(editor, updater) {
2492
2729
  });
2493
2730
  return updated;
2494
2731
  }
2495
- function getSelectedVariableNodes(editor) {
2496
- let nodes = [];
2497
- editor.getEditorState().read(() => {
2498
- const selection2 = lexical.$getSelection();
2499
- if (!lexical.$isNodeSelection(selection2)) {
2500
- return;
2501
- }
2502
- nodes = selection2.getNodes().filter($isVariableNode);
2503
- });
2504
- return nodes;
2505
- }
2506
2732
  function toggleSelectedVariableFormat(editor, format) {
2507
- const mask = FORMAT_MASKS[format];
2733
+ const mask = FORMAT_MASKS$1[format];
2508
2734
  if (!mask) {
2509
2735
  return false;
2510
2736
  }
@@ -2530,25 +2756,6 @@ function applyFontSizeToSelectedVariables(editor, size) {
2530
2756
  }
2531
2757
  });
2532
2758
  }
2533
- function readSelectedVariableFormatting(editor) {
2534
- let formatting = {};
2535
- editor.getEditorState().read(() => {
2536
- const selection2 = lexical.$getSelection();
2537
- if (!lexical.$isNodeSelection(selection2)) {
2538
- return;
2539
- }
2540
- const firstNode = selection2.getNodes().filter($isVariableNode)[0];
2541
- if (!firstNode) {
2542
- return;
2543
- }
2544
- const style = firstNode.getStyle();
2545
- formatting = {
2546
- fontFamily: extractFontFamilyFromStyle(style),
2547
- fontSize: extractFontSizePtFromStyle(style)
2548
- };
2549
- });
2550
- return formatting;
2551
- }
2552
2759
  const BLOCK_TYPE_OPTIONS = [
2553
2760
  { value: "paragraph", shortLabel: "P" },
2554
2761
  { value: "h1", shortLabel: "H1" },
@@ -2964,6 +3171,108 @@ const CanvasControls = () => {
2964
3171
  )
2965
3172
  ] });
2966
3173
  };
3174
+ const FORMAT_MASKS = {
3175
+ bold: 1,
3176
+ italic: 2,
3177
+ strikethrough: 4,
3178
+ underline: 8
3179
+ };
3180
+ function normalizeFontFamily(fontFamily) {
3181
+ if (fontFamily && SUPPORTED_FONTS.includes(fontFamily)) {
3182
+ return fontFamily;
3183
+ }
3184
+ return "Calibri";
3185
+ }
3186
+ function normalizeAlignment(alignment) {
3187
+ if (alignment === "center" || alignment === "right" || alignment === "justify") {
3188
+ return alignment;
3189
+ }
3190
+ return "left";
3191
+ }
3192
+ function getElementBlockType(node) {
3193
+ const topLevelElement = node.getTopLevelElementOrThrow();
3194
+ if (richText.$isHeadingNode(topLevelElement)) {
3195
+ return topLevelElement.getTag();
3196
+ }
3197
+ return "paragraph";
3198
+ }
3199
+ function getElementAlignment(node) {
3200
+ const topLevelElement = node.getTopLevelElementOrThrow();
3201
+ if (lexical.$isElementNode(topLevelElement)) {
3202
+ return normalizeAlignment(topLevelElement.getFormatType());
3203
+ }
3204
+ return "left";
3205
+ }
3206
+ function getInlineStyleTarget(nodes, anchorNode) {
3207
+ if (lexical.$isTextNode(anchorNode) || $isVariableNode(anchorNode)) {
3208
+ return anchorNode;
3209
+ }
3210
+ return nodes.find((node) => lexical.$isTextNode(node) || $isVariableNode(node)) ?? null;
3211
+ }
3212
+ function getInlineStyleFromNode(node) {
3213
+ if (lexical.$isTextNode(node) || $isVariableNode(node)) {
3214
+ return node.getStyle();
3215
+ }
3216
+ return "";
3217
+ }
3218
+ function hasInlineFormat(node, format) {
3219
+ if ($isVariableNode(node)) {
3220
+ return (node.getFormat() & FORMAT_MASKS[format]) !== 0;
3221
+ }
3222
+ if (lexical.$isTextNode(node) && "hasFormat" in node && typeof node.hasFormat === "function") {
3223
+ return node.hasFormat(format);
3224
+ }
3225
+ if (lexical.$isTextNode(node) && "getFormat" in node && typeof node.getFormat === "function") {
3226
+ return (node.getFormat() & FORMAT_MASKS[format]) !== 0;
3227
+ }
3228
+ return false;
3229
+ }
3230
+ function readToolbarStyleSnapshot(editor, editorState = editor.getEditorState()) {
3231
+ let snapshot = DEFAULT_TOOLBAR_STYLE_SNAPSHOT;
3232
+ editorState.read(() => {
3233
+ const currentSelection = lexical.$getSelection();
3234
+ const selection2 = lexical.$isNodeSelection(currentSelection) ? currentSelection : lexical.$createRangeSelectionFromDom(window.getSelection(), editor) ?? currentSelection;
3235
+ if (lexical.$isNodeSelection(selection2)) {
3236
+ const variableNodes = selection2.getNodes().filter($isVariableNode);
3237
+ if (variableNodes.length === 0) {
3238
+ return;
3239
+ }
3240
+ const firstVariableNode = variableNodes[0];
3241
+ const style2 = firstVariableNode.getStyle();
3242
+ snapshot = {
3243
+ blockType: extractInlineBlockTypeFromStyle(style2) ?? getElementBlockType(firstVariableNode),
3244
+ fontFamily: normalizeFontFamily(extractFontFamilyFromStyle(style2)),
3245
+ fontSize: extractFontSizePtFromStyle(style2) ?? DEFAULT_FONT_SIZE,
3246
+ alignment: getElementAlignment(firstVariableNode),
3247
+ isBold: variableNodes.every((node) => (node.getFormat() & FORMAT_MASKS.bold) !== 0),
3248
+ isItalic: variableNodes.every((node) => (node.getFormat() & FORMAT_MASKS.italic) !== 0),
3249
+ isUnderline: variableNodes.every((node) => (node.getFormat() & FORMAT_MASKS.underline) !== 0),
3250
+ isStrikethrough: variableNodes.every((node) => (node.getFormat() & FORMAT_MASKS.strikethrough) !== 0),
3251
+ hasSelectedVariable: true
3252
+ };
3253
+ return;
3254
+ }
3255
+ if (!lexical.$isRangeSelection(selection2)) {
3256
+ return;
3257
+ }
3258
+ const anchorNode = selection2.anchor.getNode();
3259
+ const inlineStyleTarget = getInlineStyleTarget(selection2.getNodes(), anchorNode);
3260
+ const style = selection2.style || getInlineStyleFromNode(inlineStyleTarget);
3261
+ const isCollapsed = selection2.isCollapsed();
3262
+ snapshot = {
3263
+ blockType: extractInlineBlockTypeFromStyle(style) ?? getElementBlockType(anchorNode),
3264
+ fontFamily: normalizeFontFamily(extractFontFamilyFromStyle(style)),
3265
+ fontSize: extractFontSizePtFromStyle(style) ?? DEFAULT_FONT_SIZE,
3266
+ alignment: getElementAlignment(anchorNode),
3267
+ isBold: selection2.hasFormat("bold") || isCollapsed && hasInlineFormat(inlineStyleTarget, "bold"),
3268
+ isItalic: selection2.hasFormat("italic") || isCollapsed && hasInlineFormat(inlineStyleTarget, "italic"),
3269
+ isUnderline: selection2.hasFormat("underline") || isCollapsed && hasInlineFormat(inlineStyleTarget, "underline"),
3270
+ isStrikethrough: selection2.hasFormat("strikethrough") || isCollapsed && hasInlineFormat(inlineStyleTarget, "strikethrough"),
3271
+ hasSelectedVariable: false
3272
+ };
3273
+ });
3274
+ return snapshot;
3275
+ }
2967
3276
  const Toolbar = () => {
2968
3277
  const {
2969
3278
  activeEditor,
@@ -2980,15 +3289,15 @@ const Toolbar = () => {
2980
3289
  const { toolbarItems, toolbarEndItems } = useExtensions();
2981
3290
  const toolbarConfig = useToolbarConfig();
2982
3291
  const t = useTranslations();
2983
- const [activeBlockType, setActiveBlockType] = React.useState("paragraph");
2984
- const [activeFontFamily, setActiveFontFamily] = React.useState("Calibri");
2985
- const [activeFontSize, setActiveFontSize] = React.useState(DEFAULT_FONT_SIZE);
2986
- const normalizeFontFamily = React.useCallback((fontFamily) => {
2987
- if (fontFamily && SUPPORTED_FONTS.includes(fontFamily)) {
2988
- return fontFamily;
2989
- }
2990
- return "Calibri";
2991
- }, []);
3292
+ const toolbarStyleStore = useToolbarStyleStoreApi();
3293
+ const activeBlockType = useToolbarStyleStore((state) => state.blockType);
3294
+ const activeFontFamily = useToolbarStyleStore((state) => state.fontFamily);
3295
+ const activeFontSize = useToolbarStyleStore((state) => state.fontSize);
3296
+ const activeAlignment = useToolbarStyleStore((state) => state.alignment);
3297
+ const isBoldActive = useToolbarStyleStore((state) => state.isBold);
3298
+ const isItalicActive = useToolbarStyleStore((state) => state.isItalic);
3299
+ const isUnderlineActive = useToolbarStyleStore((state) => state.isUnderline);
3300
+ const isStrikethroughActive = useToolbarStyleStore((state) => state.isStrikethrough);
2992
3301
  const withBodySelection = React.useCallback(
2993
3302
  (editor, action) => {
2994
3303
  editor.update(() => {
@@ -3026,38 +3335,11 @@ const Toolbar = () => {
3026
3335
  );
3027
3336
  React.useEffect(() => {
3028
3337
  if (!activeEditor) {
3029
- setActiveBlockType("paragraph");
3030
- setActiveFontFamily("Calibri");
3031
- setActiveFontSize(DEFAULT_FONT_SIZE);
3338
+ toolbarStyleStore.getState().reset();
3032
3339
  return;
3033
3340
  }
3034
- const updateSelectionState = () => {
3035
- const selectedVariables = getSelectedVariableNodes(activeEditor);
3036
- if (selectedVariables.length > 0) {
3037
- const formatting = readSelectedVariableFormatting(activeEditor);
3038
- setActiveBlockType("paragraph");
3039
- setActiveFontFamily(normalizeFontFamily(formatting.fontFamily));
3040
- setActiveFontSize(formatting.fontSize ?? DEFAULT_FONT_SIZE);
3041
- return;
3042
- }
3043
- setActiveBlockType(getActiveBlockType(activeEditor));
3044
- let nextFontFamily = "Calibri";
3045
- let nextFontSize = DEFAULT_FONT_SIZE;
3046
- activeEditor.getEditorState().read(() => {
3047
- const selection2 = lexical.$getSelection();
3048
- if (!lexical.$isRangeSelection(selection2)) {
3049
- return;
3050
- }
3051
- const textNode = selection2.getNodes().find(lexical.$isTextNode);
3052
- if (!textNode) {
3053
- return;
3054
- }
3055
- const style = textNode.getStyle();
3056
- nextFontFamily = normalizeFontFamily(extractFontFamilyFromStyle(style));
3057
- nextFontSize = extractFontSizePtFromStyle(style) ?? DEFAULT_FONT_SIZE;
3058
- });
3059
- setActiveFontFamily(nextFontFamily);
3060
- setActiveFontSize(nextFontSize);
3341
+ const updateSelectionState = (editorState = activeEditor.getEditorState()) => {
3342
+ toolbarStyleStore.getState().setSnapshot(readToolbarStyleSnapshot(activeEditor, editorState));
3061
3343
  };
3062
3344
  updateSelectionState();
3063
3345
  const unregisterSelectionChange = activeEditor.registerCommand(
@@ -3068,14 +3350,14 @@ const Toolbar = () => {
3068
3350
  },
3069
3351
  lexical.COMMAND_PRIORITY_LOW
3070
3352
  );
3071
- const unregisterUpdateListener = activeEditor.registerUpdateListener(() => {
3072
- updateSelectionState();
3353
+ const unregisterUpdateListener = activeEditor.registerUpdateListener(({ editorState }) => {
3354
+ updateSelectionState(editorState);
3073
3355
  });
3074
3356
  return () => {
3075
3357
  unregisterSelectionChange();
3076
3358
  unregisterUpdateListener();
3077
3359
  };
3078
- }, [activeEditor, normalizeFontFamily]);
3360
+ }, [activeEditor, toolbarStyleStore]);
3079
3361
  const handleBold = React.useCallback(() => {
3080
3362
  debug("toolbar", `bold (globalSelection=${globalSelectionActive}, editors=${editorRegistry.all().length}, hasEditor=${!!activeEditor})`);
3081
3363
  runToolbarAction(t.history.actions.boldApplied, () => {
@@ -3260,17 +3542,17 @@ const Toolbar = () => {
3260
3542
  ] }),
3261
3543
  /* @__PURE__ */ jsxRuntime.jsx(Divider, {}),
3262
3544
  /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "lex4-toolbar-group", "data-testid": "format-group", children: [
3263
- /* @__PURE__ */ jsxRuntime.jsx(ToolbarIconButton, { title: t.toolbar.bold, testId: "btn-bold", onClick: handleBold, children: /* @__PURE__ */ jsxRuntime.jsx(Bold, { size: 15 }) }),
3264
- /* @__PURE__ */ jsxRuntime.jsx(ToolbarIconButton, { title: t.toolbar.italic, testId: "btn-italic", onClick: handleItalic, children: /* @__PURE__ */ jsxRuntime.jsx(Italic, { size: 15 }) }),
3265
- /* @__PURE__ */ jsxRuntime.jsx(ToolbarIconButton, { title: t.toolbar.underline, testId: "btn-underline", onClick: handleUnderline, children: /* @__PURE__ */ jsxRuntime.jsx(Underline, { size: 15 }) }),
3266
- /* @__PURE__ */ jsxRuntime.jsx(ToolbarIconButton, { title: t.toolbar.strikethrough, testId: "btn-strike", onClick: handleStrikethrough, children: /* @__PURE__ */ jsxRuntime.jsx(Strikethrough, { size: 15 }) })
3545
+ /* @__PURE__ */ jsxRuntime.jsx(ToolbarIconButton, { title: t.toolbar.bold, testId: "btn-bold", active: isBoldActive, onClick: handleBold, children: /* @__PURE__ */ jsxRuntime.jsx(Bold, { size: 15 }) }),
3546
+ /* @__PURE__ */ jsxRuntime.jsx(ToolbarIconButton, { title: t.toolbar.italic, testId: "btn-italic", active: isItalicActive, onClick: handleItalic, children: /* @__PURE__ */ jsxRuntime.jsx(Italic, { size: 15 }) }),
3547
+ /* @__PURE__ */ jsxRuntime.jsx(ToolbarIconButton, { title: t.toolbar.underline, testId: "btn-underline", active: isUnderlineActive, onClick: handleUnderline, children: /* @__PURE__ */ jsxRuntime.jsx(Underline, { size: 15 }) }),
3548
+ /* @__PURE__ */ jsxRuntime.jsx(ToolbarIconButton, { title: t.toolbar.strikethrough, testId: "btn-strike", active: isStrikethroughActive, onClick: handleStrikethrough, children: /* @__PURE__ */ jsxRuntime.jsx(Strikethrough, { size: 15 }) })
3267
3549
  ] }),
3268
3550
  /* @__PURE__ */ jsxRuntime.jsx(Divider, {}),
3269
3551
  /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "lex4-toolbar-group", "data-testid": "align-group", children: [
3270
- /* @__PURE__ */ jsxRuntime.jsx(ToolbarIconButton, { title: t.toolbar.alignLeft, testId: "btn-align-left", onClick: handleAlignLeft, children: /* @__PURE__ */ jsxRuntime.jsx(TextAlignStart, { size: 15 }) }),
3271
- /* @__PURE__ */ jsxRuntime.jsx(ToolbarIconButton, { title: t.toolbar.alignCenter, testId: "btn-align-center", onClick: handleAlignCenter, children: /* @__PURE__ */ jsxRuntime.jsx(TextAlignCenter, { size: 15 }) }),
3272
- /* @__PURE__ */ jsxRuntime.jsx(ToolbarIconButton, { title: t.toolbar.alignRight, testId: "btn-align-right", onClick: handleAlignRight, children: /* @__PURE__ */ jsxRuntime.jsx(TextAlignEnd, { size: 15 }) }),
3273
- /* @__PURE__ */ jsxRuntime.jsx(ToolbarIconButton, { title: t.toolbar.justify, testId: "btn-align-justify", onClick: handleAlignJustify, children: /* @__PURE__ */ jsxRuntime.jsx(TextAlignJustify, { size: 15 }) })
3552
+ /* @__PURE__ */ jsxRuntime.jsx(ToolbarIconButton, { title: t.toolbar.alignLeft, testId: "btn-align-left", active: activeAlignment === "left", onClick: handleAlignLeft, children: /* @__PURE__ */ jsxRuntime.jsx(TextAlignStart, { size: 15 }) }),
3553
+ /* @__PURE__ */ jsxRuntime.jsx(ToolbarIconButton, { title: t.toolbar.alignCenter, testId: "btn-align-center", active: activeAlignment === "center", onClick: handleAlignCenter, children: /* @__PURE__ */ jsxRuntime.jsx(TextAlignCenter, { size: 15 }) }),
3554
+ /* @__PURE__ */ jsxRuntime.jsx(ToolbarIconButton, { title: t.toolbar.alignRight, testId: "btn-align-right", active: activeAlignment === "right", onClick: handleAlignRight, children: /* @__PURE__ */ jsxRuntime.jsx(TextAlignEnd, { size: 15 }) }),
3555
+ /* @__PURE__ */ jsxRuntime.jsx(ToolbarIconButton, { title: t.toolbar.justify, testId: "btn-align-justify", active: activeAlignment === "justify", onClick: handleAlignJustify, children: /* @__PURE__ */ jsxRuntime.jsx(TextAlignJustify, { size: 15 }) })
3274
3556
  ] }),
3275
3557
  /* @__PURE__ */ jsxRuntime.jsx(Divider, {}),
3276
3558
  /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "lex4-toolbar-group", "data-testid": "list-group", children: [
@@ -3321,6 +3603,7 @@ const ToolbarIconButton = ({
3321
3603
  type: "button",
3322
3604
  title,
3323
3605
  "aria-label": title,
3606
+ "aria-pressed": active,
3324
3607
  disabled,
3325
3608
  onMouseDown: (e) => e.preventDefault(),
3326
3609
  onClick,