@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.
package/dist/lex4-editor.js
CHANGED
|
@@ -4352,6 +4352,36 @@ function astExtension() {
|
|
|
4352
4352
|
}
|
|
4353
4353
|
};
|
|
4354
4354
|
}
|
|
4355
|
+
const EMPTY_CONTEXT = {
|
|
4356
|
+
definitions: [],
|
|
4357
|
+
refreshDefinitions: () => {
|
|
4358
|
+
},
|
|
4359
|
+
getDefinition: () => void 0
|
|
4360
|
+
};
|
|
4361
|
+
const VariableContext = createContext(EMPTY_CONTEXT);
|
|
4362
|
+
const VariableProvider = ({
|
|
4363
|
+
initialDefinitions = [],
|
|
4364
|
+
children
|
|
4365
|
+
}) => {
|
|
4366
|
+
const [definitions, setDefinitions] = useState(initialDefinitions);
|
|
4367
|
+
const refresh = useCallback((newDefinitions) => {
|
|
4368
|
+
setDefinitions(newDefinitions);
|
|
4369
|
+
}, []);
|
|
4370
|
+
const getDefinition = useCallback(
|
|
4371
|
+
(key) => {
|
|
4372
|
+
return definitions.find((d) => d.key === key);
|
|
4373
|
+
},
|
|
4374
|
+
[definitions]
|
|
4375
|
+
);
|
|
4376
|
+
const value = useMemo(
|
|
4377
|
+
() => ({ definitions, refreshDefinitions: refresh, getDefinition }),
|
|
4378
|
+
[definitions, refresh, getDefinition]
|
|
4379
|
+
);
|
|
4380
|
+
return /* @__PURE__ */ jsx(VariableContext.Provider, { value, children });
|
|
4381
|
+
};
|
|
4382
|
+
function useVariables() {
|
|
4383
|
+
return useContext(VariableContext);
|
|
4384
|
+
}
|
|
4355
4385
|
class VariableNode extends DecoratorNode {
|
|
4356
4386
|
constructor(variableKey, key) {
|
|
4357
4387
|
super(key);
|
|
@@ -4411,17 +4441,23 @@ class VariableNode extends DecoratorNode {
|
|
|
4411
4441
|
}
|
|
4412
4442
|
// -- Rendering --
|
|
4413
4443
|
decorate() {
|
|
4414
|
-
return /* @__PURE__ */ jsx(
|
|
4415
|
-
"span",
|
|
4416
|
-
{
|
|
4417
|
-
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",
|
|
4418
|
-
"data-testid": `variable-chip-${this.__variableKey}`,
|
|
4419
|
-
title: this.__variableKey,
|
|
4420
|
-
children: `{{${this.__variableKey}}}`
|
|
4421
|
-
}
|
|
4422
|
-
);
|
|
4444
|
+
return /* @__PURE__ */ jsx(VariableChip, { variableKey: this.__variableKey });
|
|
4423
4445
|
}
|
|
4424
4446
|
}
|
|
4447
|
+
function VariableChip({ variableKey }) {
|
|
4448
|
+
var _a;
|
|
4449
|
+
const { getDefinition } = useVariables();
|
|
4450
|
+
const label = ((_a = getDefinition(variableKey)) == null ? void 0 : _a.label) ?? variableKey;
|
|
4451
|
+
return /* @__PURE__ */ jsx(
|
|
4452
|
+
"span",
|
|
4453
|
+
{
|
|
4454
|
+
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",
|
|
4455
|
+
"data-testid": `variable-chip-${variableKey}`,
|
|
4456
|
+
title: variableKey,
|
|
4457
|
+
children: label
|
|
4458
|
+
}
|
|
4459
|
+
);
|
|
4460
|
+
}
|
|
4425
4461
|
function $createVariableNode(variableKey) {
|
|
4426
4462
|
return $applyNodeReplacement(new VariableNode(variableKey));
|
|
4427
4463
|
}
|
|
@@ -4448,36 +4484,6 @@ const VariablePlugin = () => {
|
|
|
4448
4484
|
}, [editor]);
|
|
4449
4485
|
return null;
|
|
4450
4486
|
};
|
|
4451
|
-
const EMPTY_CONTEXT = {
|
|
4452
|
-
definitions: [],
|
|
4453
|
-
refreshDefinitions: () => {
|
|
4454
|
-
},
|
|
4455
|
-
getDefinition: () => void 0
|
|
4456
|
-
};
|
|
4457
|
-
const VariableContext = createContext(EMPTY_CONTEXT);
|
|
4458
|
-
const VariableProvider = ({
|
|
4459
|
-
initialDefinitions = [],
|
|
4460
|
-
children
|
|
4461
|
-
}) => {
|
|
4462
|
-
const [definitions, setDefinitions] = useState(initialDefinitions);
|
|
4463
|
-
const refresh = useCallback((newDefinitions) => {
|
|
4464
|
-
setDefinitions(newDefinitions);
|
|
4465
|
-
}, []);
|
|
4466
|
-
const getDefinition = useCallback(
|
|
4467
|
-
(key) => {
|
|
4468
|
-
return definitions.find((d) => d.key === key);
|
|
4469
|
-
},
|
|
4470
|
-
[definitions]
|
|
4471
|
-
);
|
|
4472
|
-
const value = useMemo(
|
|
4473
|
-
() => ({ definitions, refreshDefinitions: refresh, getDefinition }),
|
|
4474
|
-
[definitions, refresh, getDefinition]
|
|
4475
|
-
);
|
|
4476
|
-
return /* @__PURE__ */ jsx(VariableContext.Provider, { value, children });
|
|
4477
|
-
};
|
|
4478
|
-
function useVariables() {
|
|
4479
|
-
return useContext(VariableContext);
|
|
4480
|
-
}
|
|
4481
4487
|
const VariablePicker = ({ onInsert, disabled = false }) => {
|
|
4482
4488
|
const { definitions } = useVariables();
|
|
4483
4489
|
const [open, setOpen] = useState(false);
|