@yurikilian/lex4 0.2.0 → 0.2.1

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.
@@ -4354,6 +4354,36 @@ function astExtension() {
4354
4354
  }
4355
4355
  };
4356
4356
  }
4357
+ const EMPTY_CONTEXT = {
4358
+ definitions: [],
4359
+ refreshDefinitions: () => {
4360
+ },
4361
+ getDefinition: () => void 0
4362
+ };
4363
+ const VariableContext = React.createContext(EMPTY_CONTEXT);
4364
+ const VariableProvider = ({
4365
+ initialDefinitions = [],
4366
+ children
4367
+ }) => {
4368
+ const [definitions, setDefinitions] = React.useState(initialDefinitions);
4369
+ const refresh = React.useCallback((newDefinitions) => {
4370
+ setDefinitions(newDefinitions);
4371
+ }, []);
4372
+ const getDefinition = React.useCallback(
4373
+ (key) => {
4374
+ return definitions.find((d) => d.key === key);
4375
+ },
4376
+ [definitions]
4377
+ );
4378
+ const value = React.useMemo(
4379
+ () => ({ definitions, refreshDefinitions: refresh, getDefinition }),
4380
+ [definitions, refresh, getDefinition]
4381
+ );
4382
+ return /* @__PURE__ */ jsxRuntime.jsx(VariableContext.Provider, { value, children });
4383
+ };
4384
+ function useVariables() {
4385
+ return React.useContext(VariableContext);
4386
+ }
4357
4387
  class VariableNode extends lexical.DecoratorNode {
4358
4388
  constructor(variableKey, key) {
4359
4389
  super(key);
@@ -4413,17 +4443,23 @@ class VariableNode extends lexical.DecoratorNode {
4413
4443
  }
4414
4444
  // -- Rendering --
4415
4445
  decorate() {
4416
- return /* @__PURE__ */ jsxRuntime.jsx(
4417
- "span",
4418
- {
4419
- className: "lex4-variable-chip inline-flex items-center rounded bg-white px-1.5 py-0.5\n text-xs font-medium text-blue-700 border border-blue-300 select-none\n cursor-default whitespace-nowrap mx-0.5",
4420
- "data-testid": `variable-chip-${this.__variableKey}`,
4421
- title: this.__variableKey,
4422
- children: `{{${this.__variableKey}}}`
4423
- }
4424
- );
4446
+ return /* @__PURE__ */ jsxRuntime.jsx(VariableChip, { variableKey: this.__variableKey });
4425
4447
  }
4426
4448
  }
4449
+ function VariableChip({ variableKey }) {
4450
+ var _a;
4451
+ const { getDefinition } = useVariables();
4452
+ const label = ((_a = getDefinition(variableKey)) == null ? void 0 : _a.label) ?? variableKey;
4453
+ return /* @__PURE__ */ jsxRuntime.jsx(
4454
+ "span",
4455
+ {
4456
+ className: "lex4-variable-chip inline-flex items-center rounded-full border border-blue-300\n bg-white px-2 py-0.5 text-[11px] font-medium text-blue-700 select-none\n cursor-default whitespace-nowrap mx-0.5",
4457
+ "data-testid": `variable-chip-${variableKey}`,
4458
+ title: variableKey,
4459
+ children: label
4460
+ }
4461
+ );
4462
+ }
4427
4463
  function $createVariableNode(variableKey) {
4428
4464
  return lexical.$applyNodeReplacement(new VariableNode(variableKey));
4429
4465
  }
@@ -4450,36 +4486,6 @@ const VariablePlugin = () => {
4450
4486
  }, [editor]);
4451
4487
  return null;
4452
4488
  };
4453
- const EMPTY_CONTEXT = {
4454
- definitions: [],
4455
- refreshDefinitions: () => {
4456
- },
4457
- getDefinition: () => void 0
4458
- };
4459
- const VariableContext = React.createContext(EMPTY_CONTEXT);
4460
- const VariableProvider = ({
4461
- initialDefinitions = [],
4462
- children
4463
- }) => {
4464
- const [definitions, setDefinitions] = React.useState(initialDefinitions);
4465
- const refresh = React.useCallback((newDefinitions) => {
4466
- setDefinitions(newDefinitions);
4467
- }, []);
4468
- const getDefinition = React.useCallback(
4469
- (key) => {
4470
- return definitions.find((d) => d.key === key);
4471
- },
4472
- [definitions]
4473
- );
4474
- const value = React.useMemo(
4475
- () => ({ definitions, refreshDefinitions: refresh, getDefinition }),
4476
- [definitions, refresh, getDefinition]
4477
- );
4478
- return /* @__PURE__ */ jsxRuntime.jsx(VariableContext.Provider, { value, children });
4479
- };
4480
- function useVariables() {
4481
- return React.useContext(VariableContext);
4482
- }
4483
4489
  const VariablePicker = ({ onInsert, disabled = false }) => {
4484
4490
  const { definitions } = useVariables();
4485
4491
  const [open, setOpen] = React.useState(false);