@examind/block-editor 0.1.1 → 0.1.4

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.
package/dist/index.mjs CHANGED
@@ -15438,7 +15438,22 @@ function useTypeaheadTriggerMatch(trigger, {
15438
15438
  );
15439
15439
  }
15440
15440
 
15441
+ // src/utils/visitVariableNodes.ts
15442
+ import { $isElementNode as $isElementNode4 } from "lexical";
15443
+ function visitVariableNodes(rootNode, cb) {
15444
+ if ($isVariableNode(rootNode)) {
15445
+ cb(rootNode);
15446
+ }
15447
+ if ($isElementNode4(rootNode)) {
15448
+ const children = rootNode.getChildren();
15449
+ for (const childNode of children) {
15450
+ visitVariableNodes(childNode, cb);
15451
+ }
15452
+ }
15453
+ }
15454
+
15441
15455
  // src/plugins/VariablesPlugin/VariablesContext.tsx
15456
+ import { $getRoot as $getRoot22 } from "lexical";
15442
15457
  import React20, {
15443
15458
  useContext as useContext4,
15444
15459
  useEffect as useEffect75,
@@ -15447,10 +15462,32 @@ import React20, {
15447
15462
  import { jsx as jsx113 } from "react/jsx-runtime";
15448
15463
  var VariablesContext = React20.createContext(null);
15449
15464
  function VariablesProvider(props) {
15450
- const { children, variablesSettings, renameVariable } = props;
15465
+ const { getEditor, getAllNestedEditors } = useBlockEditor();
15466
+ const { children, variablesSettings } = props;
15451
15467
  const [variableList, setVariableList] = useState15(
15452
15468
  variablesSettings.list
15453
15469
  );
15470
+ const updateVariablesInEditor = (newName, oldName, editor) => {
15471
+ editor.update(() => {
15472
+ visitVariableNodes($getRoot22(), (variableNode) => {
15473
+ if (variableNode.__variable === oldName) {
15474
+ variableNode.setVariable(newName);
15475
+ }
15476
+ });
15477
+ });
15478
+ };
15479
+ const renameVariable = (newName, oldName) => {
15480
+ const allNestedEditors = getAllNestedEditors();
15481
+ for (const nestedEditor of allNestedEditors) {
15482
+ updateVariablesInEditor(newName, oldName, nestedEditor);
15483
+ }
15484
+ const editorRef = getEditor();
15485
+ if (editorRef) {
15486
+ updateVariablesInEditor(newName, oldName, editorRef);
15487
+ clearVariableSelection(editorRef, newName);
15488
+ setVariableSelection(editorRef, newName);
15489
+ }
15490
+ };
15454
15491
  useEffect75(() => {
15455
15492
  setVariableList(variablesSettings.list);
15456
15493
  }, [variablesSettings]);
@@ -15459,13 +15496,17 @@ function VariablesProvider(props) {
15459
15496
  const newVariableList = [...variableList, name];
15460
15497
  setVariableList(newVariableList);
15461
15498
  };
15499
+ const handleOnRenamed = (newName, oldName) => {
15500
+ renameVariable(newName, oldName);
15501
+ variablesSettings.onUpdated(newName, oldName);
15502
+ };
15462
15503
  return /* @__PURE__ */ jsx113(
15463
15504
  VariablesContext.Provider,
15464
15505
  {
15465
15506
  value: {
15466
15507
  variableList,
15467
15508
  onCreated: handleOnCreated,
15468
- onRenameVariable: renameVariable
15509
+ onRenamed: handleOnRenamed
15469
15510
  },
15470
15511
  children
15471
15512
  }
@@ -15801,7 +15842,7 @@ function TypeaheadVariablesPlugin() {
15801
15842
  COMMAND_PRIORITY_HIGH4
15802
15843
  )
15803
15844
  );
15804
- }, [editor]);
15845
+ }, [editor, variableList]);
15805
15846
  const menu = useMemo12(() => {
15806
15847
  return renderVariablesMenu(
15807
15848
  variableList || [],
@@ -15933,7 +15974,7 @@ import { $findMatchingParent as $findMatchingParent2, mergeRegister as mergeRegi
15933
15974
  import {
15934
15975
  $getSelection as $getSelection14,
15935
15976
  $isDecoratorNode as $isDecoratorNode2,
15936
- $isElementNode as $isElementNode4,
15977
+ $isElementNode as $isElementNode5,
15937
15978
  $isNodeSelection as $isNodeSelection5,
15938
15979
  $isParagraphNode as $isParagraphNode6,
15939
15980
  $isRangeSelection as $isRangeSelection9,
@@ -16014,7 +16055,7 @@ function TextToolbarPlugin() {
16014
16055
  for (const node of nodes) {
16015
16056
  const element = $findMatchingParent2(
16016
16057
  node,
16017
- (parentNode) => $isElementNode4(parentNode) && !parentNode.isInline()
16058
+ (parentNode) => $isElementNode5(parentNode) && !parentNode.isInline()
16018
16059
  );
16019
16060
  if (element !== null) {
16020
16061
  element.setFormat(format);
@@ -16341,7 +16382,7 @@ function TextToolbarPlugin() {
16341
16382
  "background-color",
16342
16383
  ""
16343
16384
  );
16344
- if ($isElementNode4(element)) {
16385
+ if ($isElementNode5(element)) {
16345
16386
  newToolbarState.textAlign = element.getFormatType();
16346
16387
  } else {
16347
16388
  newToolbarState.textAlign = "left";
@@ -17053,7 +17094,7 @@ import {
17053
17094
  $createNodeSelection as $createNodeSelection20,
17054
17095
  $createParagraphNode as $createParagraphNode23,
17055
17096
  $getNodeByKey as $getNodeByKey55,
17056
- $getRoot as $getRoot22,
17097
+ $getRoot as $getRoot23,
17057
17098
  $setSelection as $setSelection23,
17058
17099
  COMMAND_PRIORITY_EDITOR as COMMAND_PRIORITY_EDITOR16,
17059
17100
  createCommand as createCommand21,
@@ -17084,7 +17125,7 @@ function FinancialStatementQuestionPlugin() {
17084
17125
  if (targetNode) {
17085
17126
  const headerEditor = createEditor9();
17086
17127
  headerEditor.update(() => {
17087
- const root = $getRoot22();
17128
+ const root = $getRoot23();
17088
17129
  const paragraph = $createParagraphNode23();
17089
17130
  root.append(paragraph);
17090
17131
  });
@@ -17392,7 +17433,7 @@ import { mergeRegister as mergeRegister28 } from "@lexical/utils";
17392
17433
  import {
17393
17434
  $createParagraphNode as $createParagraphNode25,
17394
17435
  $getNodeByKey as $getNodeByKey57,
17395
- $getRoot as $getRoot23,
17436
+ $getRoot as $getRoot24,
17396
17437
  $getSelection as $getSelection15,
17397
17438
  $isParagraphNode as $isParagraphNode7,
17398
17439
  $isRangeSelection as $isRangeSelection10,
@@ -17702,7 +17743,7 @@ function TypeaheadMenuPlugin() {
17702
17743
  );
17703
17744
  });
17704
17745
  }
17705
- const rootNode = $getRoot23();
17746
+ const rootNode = $getRoot24();
17706
17747
  if (rootNode) {
17707
17748
  const childrenKeys = rootNode.getChildrenKeys();
17708
17749
  if (childrenKeys.length > 0) {
@@ -18033,6 +18074,15 @@ function BlockEditor(props) {
18033
18074
  getNestedEditor: (nestedEditorId) => {
18034
18075
  return nestedEditorsRef.current[nestedEditorId];
18035
18076
  },
18077
+ getAllNestedEditors: () => {
18078
+ const nestedEditors = [];
18079
+ for (const [_, value] of Object.entries(
18080
+ nestedEditorsRef.current
18081
+ )) {
18082
+ nestedEditors.push(value);
18083
+ }
18084
+ return nestedEditors;
18085
+ },
18036
18086
  unregisterNestedEditor: (nestedEditorId) => {
18037
18087
  delete nestedEditorsRef.current[nestedEditorId];
18038
18088
  },
@@ -18553,7 +18603,7 @@ function VariableSettings(props) {
18553
18603
  setEditingMode,
18554
18604
  getEditingModeDataCallback
18555
18605
  } = useBlockEditor();
18556
- const { variableList, onCreated } = useVariables();
18606
+ const { variableList, onRenamed } = useVariables();
18557
18607
  const [editor] = useLexicalComposerContext70();
18558
18608
  const activeEditorRef = useRef60();
18559
18609
  const [currentVariableNode, setCurrentVariableNode] = useState22();
@@ -18685,35 +18735,56 @@ function VariableSettings(props) {
18685
18735
  const foundVariable = variableList.find(
18686
18736
  (v) => v === variableNameInputRef.current?.getValue()
18687
18737
  );
18738
+ let prevVariableName = void 0;
18688
18739
  activeEditorRef.current?.update(
18689
18740
  () => {
18690
18741
  const foundVariableNode = $getNodeByKey58(nodeKey);
18691
18742
  if ($isVariableNode(foundVariableNode)) {
18743
+ prevVariableName = foundVariableNode.getVariable();
18692
18744
  foundVariableNode.setVariable(newVariableName);
18693
18745
  }
18694
18746
  },
18695
18747
  {
18696
18748
  onUpdate: () => {
18697
- if (!foundVariable) {
18698
- onCreated(newVariableName.toString());
18749
+ if (!foundVariable && prevVariableName) {
18750
+ onRenamed(newVariableName.toString(), prevVariableName);
18751
+ setTimeout(() => {
18752
+ activeEditorRef.current?.getEditorState().read(() => {
18753
+ setEditingMode({ mode: "edit" });
18754
+ const foundVariableNode = $getNodeByKey58(nodeKey);
18755
+ if ($isVariableNode(foundVariableNode)) {
18756
+ const rootEditor = getEditor();
18757
+ clearVariableSelection(
18758
+ rootEditor || editor,
18759
+ newVariableName
18760
+ );
18761
+ setVariableSelection(
18762
+ rootEditor || editor,
18763
+ newVariableName
18764
+ );
18765
+ setCurrentVariableNode(foundVariableNode);
18766
+ }
18767
+ });
18768
+ }, 300);
18769
+ } else {
18770
+ activeEditorRef.current?.getEditorState().read(() => {
18771
+ setEditingMode({ mode: "edit" });
18772
+ const foundVariableNode = $getNodeByKey58(nodeKey);
18773
+ if ($isVariableNode(foundVariableNode)) {
18774
+ const rootEditor = getEditor();
18775
+ const variableName = foundVariableNode.getVariable();
18776
+ clearVariableSelection(
18777
+ rootEditor || editor,
18778
+ variableName
18779
+ );
18780
+ setVariableSelection(
18781
+ rootEditor || editor,
18782
+ variableName
18783
+ );
18784
+ setCurrentVariableNode(foundVariableNode);
18785
+ }
18786
+ });
18699
18787
  }
18700
- activeEditorRef.current?.getEditorState().read(() => {
18701
- setEditingMode({ mode: "edit" });
18702
- const foundVariableNode = $getNodeByKey58(nodeKey);
18703
- if ($isVariableNode(foundVariableNode)) {
18704
- const rootEditor = getEditor();
18705
- const variableName = foundVariableNode.getVariable();
18706
- clearVariableSelection(
18707
- rootEditor || editor,
18708
- variableName
18709
- );
18710
- setVariableSelection(
18711
- rootEditor || editor,
18712
- variableName
18713
- );
18714
- setCurrentVariableNode(foundVariableNode);
18715
- }
18716
- });
18717
18788
  setSettingsEditingMode(false);
18718
18789
  }
18719
18790
  }
@@ -19133,88 +19204,29 @@ function VariableToolbarPlugin() {
19133
19204
  return /* @__PURE__ */ jsx131(PopupToolbar, { ref: popupToolbarRef, menu: menuItems });
19134
19205
  }
19135
19206
 
19136
- // src/utils/replaceVariablesInEditorState.ts
19137
- function replaceVariablesInEditorState(node, variablesMap) {
19138
- if (node.type === VariableNode.getType()) {
19139
- const foundPrevVariable = node.variableName ? variablesMap[node.variableName] : void 0;
19140
- if (foundPrevVariable) {
19141
- node.text = foundPrevVariable;
19142
- node.variableName = foundPrevVariable;
19143
- }
19144
- }
19145
- if (node.children && node.children.length > 0) {
19146
- node.children.forEach((child) => {
19147
- replaceVariablesInEditorState(child, variablesMap);
19148
- });
19149
- }
19150
- }
19151
-
19152
19207
  // src/modules/Variables.tsx
19153
- import {
19154
- forwardRef as forwardRef16,
19155
- useEffect as useEffect93,
19156
- useImperativeHandle as useImperativeHandle16
19157
- } from "react";
19208
+ import { useEffect as useEffect93 } from "react";
19158
19209
  import { jsx as jsx132, jsxs as jsxs49 } from "react/jsx-runtime";
19159
- var Variables = forwardRef16(
19160
- (props, ref) => {
19161
- const { variablesSettings } = props;
19162
- const {
19163
- getEditor,
19164
- onChange,
19165
- registerModule,
19166
- setEditingModeDataCallback
19167
- } = useBlockEditor();
19168
- const handleRenameVariable = (prevName, newName) => {
19169
- const editorRef = getEditor();
19170
- if (editorRef) {
19171
- const editorState = editorRef.getEditorState().toJSON();
19172
- if (editorState) {
19173
- replaceVariablesInEditorState(editorState.root, {
19174
- [prevName]: newName
19175
- });
19176
- const parsedEditorState = editorRef.parseEditorState(editorState);
19177
- if (parsedEditorState) {
19178
- editorRef.setEditorState(parsedEditorState, {
19179
- tag: "new_state"
19180
- });
19181
- if (onChange) {
19182
- onChange(editorState);
19183
- }
19184
- }
19185
- }
19186
- }
19187
- };
19188
- useImperativeHandle16(ref, () => {
19210
+ function Variables(props) {
19211
+ const { variablesSettings } = props;
19212
+ const { registerModule, setEditingModeDataCallback } = useBlockEditor();
19213
+ useEffect93(() => {
19214
+ registerModule("Variables");
19215
+ }, []);
19216
+ useEffect93(() => {
19217
+ setEditingModeDataCallback(() => {
19189
19218
  return {
19190
- renameVariable: handleRenameVariable
19219
+ variableValues: variablesSettings.getVariableValues()
19191
19220
  };
19192
19221
  });
19193
- useEffect93(() => {
19194
- registerModule("Variables");
19195
- }, []);
19196
- useEffect93(() => {
19197
- setEditingModeDataCallback(() => {
19198
- return {
19199
- variableValues: variablesSettings.getVariableValues()
19200
- };
19201
- });
19202
- }, [variablesSettings, setEditingModeDataCallback]);
19203
- return /* @__PURE__ */ jsxs49(
19204
- VariablesProvider,
19205
- {
19206
- variablesSettings,
19207
- renameVariable: handleRenameVariable,
19208
- children: [
19209
- /* @__PURE__ */ jsx132(TypeaheadVariablesPlugin, {}),
19210
- /* @__PURE__ */ jsx132(VariableSettingsPlugin, {}),
19211
- /* @__PURE__ */ jsx132(VariableToolbarPlugin, {}),
19212
- /* @__PURE__ */ jsx132(VariableToolbarAgentPlugin, {})
19213
- ]
19214
- }
19215
- );
19216
- }
19217
- );
19222
+ }, [variablesSettings, setEditingModeDataCallback]);
19223
+ return /* @__PURE__ */ jsxs49(VariablesProvider, { variablesSettings, children: [
19224
+ /* @__PURE__ */ jsx132(TypeaheadVariablesPlugin, {}),
19225
+ /* @__PURE__ */ jsx132(VariableSettingsPlugin, {}),
19226
+ /* @__PURE__ */ jsx132(VariableToolbarPlugin, {}),
19227
+ /* @__PURE__ */ jsx132(VariableToolbarAgentPlugin, {})
19228
+ ] });
19229
+ }
19218
19230
  export {
19219
19231
  BlockEditor,
19220
19232
  Callout,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@examind/block-editor",
3
- "version": "0.1.1",
3
+ "version": "0.1.4",
4
4
  "@comment version": [
5
5
  "Don't specify package version here. It will be injected by publish workflow."
6
6
  ],