@wildix/uikit-handlebars 0.0.12 → 0.0.14
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/components/input.d.ts +10 -6
- package/dist/components/input.d.ts.map +1 -1
- package/dist/components/input.js +25 -11
- package/dist/components/input.js.map +1 -1
- package/dist/components/lexical/extensions/HandlebarsDefaultsExtension.d.ts.map +1 -1
- package/dist/components/lexical/extensions/HandlebarsDefaultsExtension.js +21 -2
- package/dist/components/lexical/extensions/HandlebarsDefaultsExtension.js.map +1 -1
- package/dist/components/lexical/extensions/HandlebarsSuggestionsExtension.d.ts +2 -0
- package/dist/components/lexical/extensions/HandlebarsSuggestionsExtension.d.ts.map +1 -1
- package/dist/components/lexical/extensions/HandlebarsSuggestionsExtension.js +94 -42
- package/dist/components/lexical/extensions/HandlebarsSuggestionsExtension.js.map +1 -1
- package/dist/components/lexical/extensions/HandlebarsVariableHighlightExtension.d.ts.map +1 -1
- package/dist/components/lexical/extensions/HandlebarsVariableHighlightExtension.js +49 -43
- package/dist/components/lexical/extensions/HandlebarsVariableHighlightExtension.js.map +1 -1
- package/dist/components/lexical/handlebarsStore.d.ts +6 -3
- package/dist/components/lexical/handlebarsStore.d.ts.map +1 -1
- package/dist/components/lexical/handlebarsStore.js +6 -0
- package/dist/components/lexical/handlebarsStore.js.map +1 -1
- package/dist/components/lexical/handlebarsUtils.d.ts +10 -0
- package/dist/components/lexical/handlebarsUtils.d.ts.map +1 -1
- package/dist/components/lexical/handlebarsUtils.js +36 -0
- package/dist/components/lexical/handlebarsUtils.js.map +1 -1
- package/dist/components/lexical/helpers/lineText.d.ts +4 -0
- package/dist/components/lexical/helpers/lineText.d.ts.map +1 -0
- package/dist/components/lexical/helpers/lineText.js +13 -0
- package/dist/components/lexical/helpers/lineText.js.map +1 -0
- package/dist/components/lexical/nodes/HandlebarsVariableNode.d.ts +7 -3
- package/dist/components/lexical/nodes/HandlebarsVariableNode.d.ts.map +1 -1
- package/dist/components/lexical/nodes/HandlebarsVariableNode.js +30 -11
- package/dist/components/lexical/nodes/HandlebarsVariableNode.js.map +1 -1
- package/dist/components/lexical/plugins/ControlledValuePlugin.d.ts +3 -2
- package/dist/components/lexical/plugins/ControlledValuePlugin.d.ts.map +1 -1
- package/dist/components/lexical/plugins/ControlledValuePlugin.js +17 -6
- package/dist/components/lexical/plugins/ControlledValuePlugin.js.map +1 -1
- package/dist/components/textarea.d.ts +14 -6
- package/dist/components/textarea.d.ts.map +1 -1
- package/dist/components/textarea.js +29 -11
- package/dist/components/textarea.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/analyzeTemplate.d.ts +44 -0
- package/dist/lib/analyzeTemplate.d.ts.map +1 -0
- package/dist/lib/analyzeTemplate.js +138 -0
- package/dist/lib/analyzeTemplate.js.map +1 -0
- package/package.json +13 -10
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
+
import type { TemplateHelper } from '../lib/analyzeTemplate';
|
|
3
|
+
import type { UnknownTone } from './lexical/nodes/HandlebarsVariableNode';
|
|
2
4
|
type HandlebarsOptions = {
|
|
3
5
|
variables?: string[];
|
|
4
|
-
helpers?: Record<string,
|
|
5
|
-
description?: string;
|
|
6
|
-
}>;
|
|
6
|
+
helpers?: Record<string, TemplateHelper>;
|
|
7
7
|
};
|
|
8
8
|
type SuggestionsOptions = {
|
|
9
9
|
showSuggestions?: boolean;
|
|
@@ -26,9 +26,7 @@ export type InputHandlebarsProps = {
|
|
|
26
26
|
handlebars?: HandlebarsOptions;
|
|
27
27
|
suggestions?: SuggestionsOptions;
|
|
28
28
|
variables?: string[];
|
|
29
|
-
helpers?: Record<string,
|
|
30
|
-
description?: string;
|
|
31
|
-
}>;
|
|
29
|
+
helpers?: Record<string, TemplateHelper>;
|
|
32
30
|
onCreateVariable?: (name: string) => void;
|
|
33
31
|
unstyled?: boolean;
|
|
34
32
|
'aria-invalid'?: React.AriaAttributes['aria-invalid'];
|
|
@@ -37,6 +35,12 @@ export type InputHandlebarsProps = {
|
|
|
37
35
|
'aria-describedby'?: React.AriaAttributes['aria-describedby'];
|
|
38
36
|
hideHelperSuggestions?: boolean;
|
|
39
37
|
hideCreateVariableButton?: boolean;
|
|
38
|
+
/** How an undeclared name is marked: red for `error`, dashed amber for `warning`. */
|
|
39
|
+
unknownTone?: UnknownTone;
|
|
40
|
+
/** A change that makes the text longer than this is refused, not cut: a cut can leave a broken `{{`. */
|
|
41
|
+
maxLength?: number;
|
|
42
|
+
/** Set on the editable element, and used as the prefix of the suggestion menu test ids. */
|
|
43
|
+
'data-testid'?: string;
|
|
40
44
|
};
|
|
41
45
|
export declare function InputHandlebars(props: InputHandlebarsProps): React.JSX.Element;
|
|
42
46
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"input.d.ts","sourceRoot":"","sources":["../../src/components/input.tsx"],"names":[],"mappings":"AAEA,OAAO,KAA2E,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"input.d.ts","sourceRoot":"","sources":["../../src/components/input.tsx"],"names":[],"mappings":"AAEA,OAAO,KAA2E,MAAM,OAAO,CAAC;AAOhG,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,wBAAwB,CAAC;AAM3D,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,wCAAwC,CAAC;AAyCxE,KAAK,iBAAiB,GAAG;IACvB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;CAC1C,CAAC;AAEF,KAAK,kBAAkB,GAAG;IACxB,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC9B,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,IAAI,CAAC,EAAE,IAAI,G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AW,CAAC;IAC1B,wGAAwG;IACxG,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,2FAA2F;IAC3F,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAkGF,wBAAgB,eAAe,CAAC,KAAK,EAAE,oBAAoB,qBA6K1D"}
|
package/dist/components/input.js
CHANGED
|
@@ -8,8 +8,9 @@ import { HandlebarsDefaultsExtension } from './lexical/extensions/HandlebarsDefa
|
|
|
8
8
|
import { HandlebarsSuggestionsExtension } from './lexical/extensions/HandlebarsSuggestionsExtension';
|
|
9
9
|
import { HandlebarsVariableHighlightExtension } from './lexical/extensions/HandlebarsVariableHighlightExtension';
|
|
10
10
|
import { createHandlebarsStore } from './lexical/handlebarsStore';
|
|
11
|
+
import setEditorText from './lexical/helpers/setEditorText';
|
|
11
12
|
import { AutoFocusPlugin } from './lexical/plugins/AutoFocusPlugin';
|
|
12
|
-
import { ControlledValuePlugin } from './lexical/plugins/ControlledValuePlugin';
|
|
13
|
+
import { CONTROLLED_VALUE_TAG, ControlledValuePlugin } from './lexical/plugins/ControlledValuePlugin';
|
|
13
14
|
import { DisabledValuePlugin } from './lexical/plugins/DisabledValuePlugin';
|
|
14
15
|
import { CodeExtension } from '@lexical/code';
|
|
15
16
|
import { HistoryExtension } from '@lexical/history';
|
|
@@ -65,12 +66,12 @@ const EditablePropsContext = createContext({
|
|
|
65
66
|
size: 'default',
|
|
66
67
|
});
|
|
67
68
|
function StableContentEditable() {
|
|
68
|
-
const { size, disabled, placeholder, onFocus, onBlur, ariaInvalid, ariaLabel, ariaLabelledBy, ariaDescribedBy, id, editableRef, } = useContext(EditablePropsContext);
|
|
69
|
+
const { size, disabled, placeholder, onFocus, onBlur, ariaInvalid, ariaLabel, ariaLabelledBy, ariaDescribedBy, id, editableRef, testId, minHeight, } = useContext(EditablePropsContext);
|
|
69
70
|
const renderPlaceholder = useCallback(() => (placeholder ? _jsx(InputPlaceholder, { size: size, text: placeholder }) : null), [placeholder, size]);
|
|
70
|
-
return (_jsx(ContentEditable, { "data-slot": "input", className: inputEditableVariants({ size, disabled }), role: "textbox", placeholder: renderPlaceholder, spellCheck: false, "aria-disabled": disabled || undefined, "aria-placeholder": placeholder || '', "aria-invalid": ariaInvalid, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, "aria-describedby": ariaDescribedBy, id: id, ref: editableRef, disabled: disabled, onFocus: onFocus, onBlur: onBlur }));
|
|
71
|
+
return (_jsx(ContentEditable, { "data-slot": "input", className: inputEditableVariants({ size, disabled }), role: "textbox", placeholder: renderPlaceholder, spellCheck: false, "aria-disabled": disabled || undefined, "aria-placeholder": placeholder || '', "aria-invalid": ariaInvalid, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, "aria-describedby": ariaDescribedBy, id: id, "data-testid": testId, style: minHeight ? { minHeight } : undefined, ref: editableRef, disabled: disabled, onFocus: onFocus, onBlur: onBlur }));
|
|
71
72
|
}
|
|
72
73
|
export function InputHandlebars(props) {
|
|
73
|
-
const { className, size = 'default', id, ref, value, placeholder, disabled, autoFocus, onChange, onFocus, onBlur, handlebars, suggestions, variables: variablesProp, helpers: helpersProp, onCreateVariable, unstyled = false, hideHelperSuggestions = false, hideCreateVariableButton = false, } = props;
|
|
74
|
+
const { className, size = 'default', id, ref, value, placeholder, disabled, autoFocus, onChange, onFocus, onBlur, handlebars, suggestions, variables: variablesProp, helpers: helpersProp, onCreateVariable, unstyled = false, hideHelperSuggestions = false, hideCreateVariableButton = false, unknownTone = 'error', maxLength, } = props;
|
|
74
75
|
const variables = handlebars?.variables ?? variablesProp ?? [];
|
|
75
76
|
const helpers = handlebars?.helpers ?? helpersProp ?? {};
|
|
76
77
|
const hideHelperSuggestionsValue = suggestions?.showSuggestions === false || hideHelperSuggestions;
|
|
@@ -82,10 +83,11 @@ export function InputHandlebars(props) {
|
|
|
82
83
|
const editorValueRef = useRef(valueSingleLine);
|
|
83
84
|
const controlled = Boolean(onChange);
|
|
84
85
|
// Create a single store instance per component and update it when props change
|
|
85
|
-
const handlebarsStore = useMemo(() => createHandlebarsStore({ variables: variables ?? [], helpers: helpers ?? {} }), []);
|
|
86
|
+
const handlebarsStore = useMemo(() => createHandlebarsStore({ variables: variables ?? [], helpers: helpers ?? {}, unknownTone }), []);
|
|
87
|
+
const storeKey = JSON.stringify([variables, helpers, unknownTone]);
|
|
86
88
|
useEffect(() => {
|
|
87
|
-
handlebarsStore.setAll({ variables: variables ?? [], helpers: helpers ?? {} });
|
|
88
|
-
}, [handlebarsStore,
|
|
89
|
+
handlebarsStore.setAll({ variables: variables ?? [], helpers: helpers ?? {}, unknownTone });
|
|
90
|
+
}, [handlebarsStore, storeKey]);
|
|
89
91
|
const rootExtension = useMemo(() => defineExtension({
|
|
90
92
|
name: '[uikit-handlebars-root]',
|
|
91
93
|
dependencies: [
|
|
@@ -98,17 +100,27 @@ export function InputHandlebars(props) {
|
|
|
98
100
|
onCreateVariable,
|
|
99
101
|
hideHelperSuggestions: hideHelperSuggestionsValue,
|
|
100
102
|
hideCreateVariableButton: hideCreateVariableButtonValue,
|
|
103
|
+
testId: props['data-testid'] ?? id,
|
|
101
104
|
}),
|
|
102
105
|
HandlebarsVariableHighlightExtension({ store: handlebarsStore }),
|
|
103
106
|
],
|
|
104
107
|
// Let DisabledValuePlugin control editor editable state to avoid
|
|
105
108
|
// recreating the extension/editor when the disabled prop changes.
|
|
106
109
|
editable: true,
|
|
107
|
-
}), [handlebarsStore, hideHelperSuggestionsValue, hideCreateVariableButtonValue]);
|
|
108
|
-
const handleChange = useCallback((editorState, editor) => {
|
|
110
|
+
}), [handlebarsStore, hideHelperSuggestionsValue, hideCreateVariableButtonValue, props['data-testid'], id]);
|
|
111
|
+
const handleChange = useCallback((editorState, editor, tags) => {
|
|
109
112
|
if (controlled) {
|
|
110
113
|
editor.read(() => {
|
|
111
114
|
const text = $getRoot().getTextContent();
|
|
115
|
+
if (tags.has(CONTROLLED_VALUE_TAG)) {
|
|
116
|
+
editorValueRef.current = text;
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (maxLength !== undefined && text.length > maxLength) {
|
|
120
|
+
const restored = editorValueRef.current ?? '';
|
|
121
|
+
editor.update(() => setEditorText(restored), { tag: 'history-merge' });
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
112
124
|
editorValueRef.current = text;
|
|
113
125
|
if (valueRef.current !== text) {
|
|
114
126
|
// Create a synthetic event compatible with React.ChangeEvent<HTMLInputElement>
|
|
@@ -125,7 +137,7 @@ export function InputHandlebars(props) {
|
|
|
125
137
|
}
|
|
126
138
|
});
|
|
127
139
|
}
|
|
128
|
-
}, [onChange]);
|
|
140
|
+
}, [onChange, maxLength, controlled]);
|
|
129
141
|
const editablePropsValue = useMemo(() => ({
|
|
130
142
|
size,
|
|
131
143
|
disabled,
|
|
@@ -138,6 +150,7 @@ export function InputHandlebars(props) {
|
|
|
138
150
|
ariaDescribedBy: props['aria-describedby'],
|
|
139
151
|
id,
|
|
140
152
|
editableRef: ref,
|
|
153
|
+
testId: props['data-testid'],
|
|
141
154
|
}), [
|
|
142
155
|
size,
|
|
143
156
|
disabled,
|
|
@@ -150,12 +163,13 @@ export function InputHandlebars(props) {
|
|
|
150
163
|
props['aria-describedby'],
|
|
151
164
|
id,
|
|
152
165
|
ref,
|
|
166
|
+
props['data-testid'],
|
|
153
167
|
]);
|
|
154
168
|
const contentEditable = useMemo(() => (
|
|
155
169
|
// Keep ContentEditable instance stable to prevent Lexical from remounting the editor
|
|
156
170
|
// when visual or aria props change. We pass a stable wrapper component which consumes
|
|
157
171
|
// latest props from context so DOM attributes still update without recreating the CE node.
|
|
158
172
|
_jsx(StableContentEditable, {})), []);
|
|
159
|
-
return (_jsx("span", { "data-slot": "input-control", "data-size": size,
|
|
173
|
+
return (_jsx("span", { "data-slot": "input-control", "data-size": size, className: cn(inputControlVariants({ unstyled }), className), children: _jsx(EditablePropsContext.Provider, { value: editablePropsValue, children: _jsxs(LexicalExtensionComposer, { extension: rootExtension, contentEditable: contentEditable, children: [_jsx(ControlledValuePlugin, { initialValue: initialValueRef.current, currentValue: valueRef.current, controlled: controlled }), _jsx(DisabledValuePlugin, { disabled: disabled }), _jsx(OnChangePlugin, { onChange: handleChange }), autoFocus && _jsx(AutoFocusPlugin, {})] }) }) }));
|
|
160
174
|
}
|
|
161
175
|
//# sourceMappingURL=input.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"input.js","sourceRoot":"","sources":["../../src/components/input.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,OAAc,EAAC,aAAa,EAAE,WAAW,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAC,MAAM,OAAO,CAAC;AAEhG,OAAO,EAAC,EAAE,EAAC,MAAM,eAAe,CAAC;AAEjC,OAAO,EAAC,GAAG,EAAC,MAAM,0BAA0B,CAAC;AAC7C,OAAO,EAAC,QAAQ,EAAE,eAAe,EAAuC,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"input.js","sourceRoot":"","sources":["../../src/components/input.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,OAAc,EAAC,aAAa,EAAE,WAAW,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAC,MAAM,OAAO,CAAC;AAEhG,OAAO,EAAC,EAAE,EAAC,MAAM,eAAe,CAAC;AAEjC,OAAO,EAAC,GAAG,EAAC,MAAM,0BAA0B,CAAC;AAC7C,OAAO,EAAC,QAAQ,EAAE,eAAe,EAAuC,MAAM,SAAS,CAAC;AAGxF,OAAO,EAAC,2BAA2B,EAAC,MAAM,kDAAkD,CAAC;AAC7F,OAAO,EAAC,8BAA8B,EAAC,MAAM,qDAAqD,CAAC;AACnG,OAAO,EAAC,oCAAoC,EAAC,MAAM,2DAA2D,CAAC;AAC/G,OAAO,EAAC,qBAAqB,EAAC,MAAM,2BAA2B,CAAC;AAChE,OAAO,aAAa,MAAM,iCAAiC,CAAC;AAE5D,OA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KAAC,eAAe,KAAG,IACR,GACG,GAC3B,CACR,CAAC;AACJ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HandlebarsDefaultsExtension.d.ts","sourceRoot":"","sources":["../../../../src/components/lexical/extensions/HandlebarsDefaultsExtension.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"HandlebarsDefaultsExtension.d.ts","sourceRoot":"","sources":["../../../../src/components/lexical/extensions/HandlebarsDefaultsExtension.tsx"],"names":[],"mappings":"AAAA,OAAO,EAQL,aAAa,EACd,MAAM,SAAS,CAAC;AAmCjB,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,aAAa,GAAG,MAAM,IAAI,CA2CrE;AAED,eAAO,MAAM,2BAA2B,6HAItC,CAAC"}
|
|
@@ -1,9 +1,28 @@
|
|
|
1
|
-
import { $createParagraphNode, $getRoot, $getSelection, defineExtension } from 'lexical';
|
|
1
|
+
import { $createParagraphNode, $getRoot, $getSelection, $isRangeSelection, COMMAND_PRIORITY_LOW, defineExtension, KEY_BACKSPACE_COMMAND, } from 'lexical';
|
|
2
|
+
import { isPlainHandlebarsVariable } from '../handlebarsUtils';
|
|
3
|
+
import lineText from '../helpers/lineText';
|
|
2
4
|
import { $createCodeHighlightNode, $createCodeNode } from '@lexical/code';
|
|
3
5
|
import { PlainTextPlugin } from '@lexical/react/LexicalPlainTextPlugin';
|
|
4
6
|
import { mergeRegister } from '@lexical/utils';
|
|
7
|
+
/** Backspace right after `{{name}}` removes the whole reference, so it never leaves a broken `{{name}`. */
|
|
8
|
+
function registerWholeVariableBackspace(editor) {
|
|
9
|
+
return editor.registerCommand(KEY_BACKSPACE_COMMAND, (event) => {
|
|
10
|
+
const selection = $getSelection();
|
|
11
|
+
if (!$isRangeSelection(selection) || !selection.isCollapsed())
|
|
12
|
+
return false;
|
|
13
|
+
const before = lineText(selection.anchor.getNode(), selection.anchor.offset, 'before');
|
|
14
|
+
const match = /\{\{\{?([^{}]*)\}\}\}?$/.exec(before);
|
|
15
|
+
if (!match || !isPlainHandlebarsVariable(match[1] ?? ''))
|
|
16
|
+
return false;
|
|
17
|
+
event?.preventDefault();
|
|
18
|
+
for (let index = 0; index < match[0].length; index++) {
|
|
19
|
+
selection.deleteCharacter(true);
|
|
20
|
+
}
|
|
21
|
+
return true;
|
|
22
|
+
}, COMMAND_PRIORITY_LOW);
|
|
23
|
+
}
|
|
5
24
|
export function registerDefaultNode(editor) {
|
|
6
|
-
const removeListener = mergeRegister(editor.registerUpdateListener((payload) => {
|
|
25
|
+
const removeListener = mergeRegister(registerWholeVariableBackspace(editor), editor.registerUpdateListener((payload) => {
|
|
7
26
|
payload.editorState.read(() => {
|
|
8
27
|
const root = $getRoot();
|
|
9
28
|
const children = root.getChildren();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HandlebarsDefaultsExtension.js","sourceRoot":"","sources":["../../../../src/components/lexical/extensions/HandlebarsDefaultsExtension.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"HandlebarsDefaultsExtension.js","sourceRoot":"","sources":["../../../../src/components/lexical/extensions/HandlebarsDefaultsExtension.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,QAAQ,EACR,aAAa,EACb,iBAAiB,EACjB,oBAAoB,EACpB,eAAe,EACf,qBAAqB,GAEtB,MAAM,SAAS,CAAC;AAEjB,OAAO,EAAC,yBAAyB,EAAC,MAAM,oBAAoB,CAAC;AAC7D,OAAO,QAAQ,MAAM,qBAAqB,CAAC;AAE3C,OAAO,EAAC,wBAAwB,EAAE,eAAe,EAAC,MAAM,eAAe,CAAC;AACxE,OAAO,EAAC,eAAe,EAAC,MAAM,uCAAuC,CAAC;AACtE,OAAO,EAAC,aAAa,EAAC,MAAM,gBAAgB,CAAC;AAE7C,2GAA2G;AAC3G,SAAS,8BAA8B,CAAC,MAAqB;IAC3D,OAAO,MAAM,CAAC,eAAe,CAC3B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
6
|
hideHelperSuggestions?: boolean;
|
|
7
7
|
hideCreateVariableButton?: boolean;
|
|
8
|
+
/** Prefix of the `data-testid` of the suggestion menu and its options. */
|
|
9
|
+
testId?: string;
|
|
8
10
|
}
|
|
9
11
|
export declare function HandlebarsSuggestionsPlugin(props: HandlebarsSuggestionsProps): React.JSX.Element | null;
|
|
10
12
|
export declare const HandlebarsSuggestionsExtension: (props: HandlebarsSuggestionsProps) => import("lexical").LexicalExtension<import("lexical").ExtensionConfigBase, "@lexical/handlebars-suggestions", unknown, unknown>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HandlebarsSuggestionsExtension.d.ts","sourceRoot":"","sources":["../../../../src/components/lexical/extensions/HandlebarsSuggestionsExtension.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"HandlebarsSuggestionsExtension.d.ts","sourceRoot":"","sources":["../../../../src/components/lexical/extensions/HandlebarsSuggestionsExtension.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAkC/B,OAAO,EAAC,eAAe,EAAC,MAAM,oBAAoB,CAAC;AAsCnD,UAAU,0BAA0B;IAClC,KAAK,EAAE,eAAe,CAAC;IACvB,gBAAgB,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IAC1C,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,wBAAwB,CAAC,EAAE,OAAO,CAAC;IACnC,0EAA0E;IAC1E,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAyLD,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,0BAA0B,4BAkgB5E;AAED,eAAO,MAAM,8BAA8B,GAAI,OAAO,0BAA0B,mIAQ5E,CAAC"}
|
|
@@ -2,7 +2,9 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
2
2
|
import * as React from 'react';
|
|
3
3
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
4
4
|
import { Badge, Button, cn, PreviewCard, PreviewCardPopup, PreviewCardTrigger, Tooltip, TooltipPopup, TooltipTrigger, } from '@wildix/uikit';
|
|
5
|
-
import { $getNearestNodeFromDOMNode, $getNodeByKey, $getSelection, $isRangeSelection, $isTextNode, COMMAND_PRIORITY_NORMAL, configExtension, defineExtension, KEY_ARROW_DOWN_COMMAND, KEY_ARROW_UP_COMMAND, KEY_ENTER_COMMAND, KEY_ESCAPE_COMMAND, KEY_TAB_COMMAND, } from 'lexical';
|
|
5
|
+
import { $getNearestNodeFromDOMNode, $getNodeByKey, $getSelection, $isRangeSelection, $isTextNode, BLUR_COMMAND, COMMAND_PRIORITY_LOW, COMMAND_PRIORITY_NORMAL, configExtension, defineExtension, KEY_ARROW_DOWN_COMMAND, KEY_ARROW_UP_COMMAND, KEY_ENTER_COMMAND, KEY_ESCAPE_COMMAND, KEY_TAB_COMMAND, } from 'lexical';
|
|
6
|
+
import { openBlocks, suggestionContext } from '../handlebarsUtils';
|
|
7
|
+
import lineText from '../helpers/lineText';
|
|
6
8
|
import { $createHandlebarsHelperNode, $isHandlebarsVariableNode } from '../nodes/HandlebarsVariableNode';
|
|
7
9
|
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
|
|
8
10
|
import { ReactExtension } from '@lexical/react/ReactExtension';
|
|
@@ -11,6 +13,9 @@ const INITIAL_STATE = {
|
|
|
11
13
|
mode: 'CLOSED',
|
|
12
14
|
position: null,
|
|
13
15
|
};
|
|
16
|
+
const BLOCK_HELPERS = ['if', 'unless', 'each', 'with'];
|
|
17
|
+
/** What follows the argument of a new block: its end, with an `else` branch for a condition. */
|
|
18
|
+
const blockBody = (name) => `}}${name === 'if' || name === 'unless' ? '{{else}}' : ''}{{/${name}}}`;
|
|
14
19
|
// -----------------------------------------------------------------------------
|
|
15
20
|
// Helper: Position Calculation
|
|
16
21
|
// -----------------------------------------------------------------------------
|
|
@@ -24,7 +29,7 @@ function getPositionFromRect(rect) {
|
|
|
24
29
|
left: rect.left + window.scrollX,
|
|
25
30
|
};
|
|
26
31
|
}
|
|
27
|
-
function AutocompletePreview({
|
|
32
|
+
function AutocompletePreview({ options, selectedIndex, onSelect, testId }) {
|
|
28
33
|
const containerRef = React.useRef(null);
|
|
29
34
|
// Ensure the selected item is visible when navigating with keyboard
|
|
30
35
|
React.useEffect(() => {
|
|
@@ -50,11 +55,11 @@ function AutocompletePreview({ variables, selectedIndex, onSelect }) {
|
|
|
50
55
|
}
|
|
51
56
|
}
|
|
52
57
|
}, [selectedIndex]);
|
|
53
|
-
if (
|
|
58
|
+
if (options.length === 0)
|
|
54
59
|
return null;
|
|
55
|
-
return (_jsx("div", { ref: containerRef, className: "flex max-h-60 w-full flex-col overflow-y-auto", children:
|
|
60
|
+
return (_jsx("div", { ref: containerRef, className: "flex max-h-60 w-full flex-col overflow-y-auto", "data-testid": testId ? `${testId}-variable-menu` : undefined, children: options.map((option, index) => (_jsxs("button", { type: "button", "data-index": index, "data-testid": testId ? `${testId}-${option.kind}-option-${option.value}` : undefined, onClick: () => onSelect(option), className: cn('relative flex cursor-default select-none items-center rounded-sm px-2 py-1.5 text-sm outline-none transition-colors', index === selectedIndex
|
|
56
61
|
? 'bg-accent text-accent-foreground'
|
|
57
|
-
: 'hover:bg-accent hover:text-accent-foreground'), children:
|
|
62
|
+
: 'hover:bg-accent hover:text-accent-foreground'), children: [_jsx("span", { className: "flex-1 text-left", children: option.value }), option.kind !== 'variable' && _jsx("span", { className: "ml-3 text-xs text-muted-foreground", children: option.kind })] }, `${option.kind}-${option.value}`))) }));
|
|
58
63
|
}
|
|
59
64
|
function NodeInfoPreview({ variableName, helperName, isValid, isDefined, helpers, onApplyHelper, onCreateVariable, hideHelperSuggestions, hideCreateVariableButton, }) {
|
|
60
65
|
return (_jsxs("div", { className: "flex flex-col overflow-hidden", children: [_jsxs("div", { className: "p-2", children: [_jsxs("div", { className: "flex items-center justify-between gap-2", children: [_jsx("span", { className: "text-sm font-medium", children: variableName }), helperName && _jsxs("span", { className: "text-xs text-muted-foreground", children: ["Helper: ", helperName] })] }), isDefined === false && (_jsx("div", { className: "mt-1 text-[11px] text-destructive-foreground/90", children: "Variable is not defined." })), isDefined === false && variableName && onCreateVariable && !hideCreateVariableButton && (_jsx("div", { className: "mt-2", children: _jsx(Button, { size: "xs", onClick: () => onCreateVariable(variableName), children: "Create variable" }) })), helperName && !helpers[helperName] && (_jsxs("div", { className: "mt-1 text-[11px] text-destructive-foreground/90", children: ["Helper \"", helperName, "\" is not defined."] })), helperName && helpers[helperName] && isValid === false && (_jsx("div", { className: "mt-1 text-[11px] text-destructive-foreground/90", children: "Invalid helper usage." }))] }), !hideHelperSuggestions && (_jsx("div", { className: "flex flex-1 flex-col overflow-hidden", children: _jsx("div", { className: "flex flex-wrap gap-1.5 p-2 overflow-y-auto", children: Object.entries(helpers).length === 0 ? (_jsx("div", { className: "p-2 text-center text-xs text-muted-foreground w-full", children: "No helpers available" })) : (Object.entries(helpers).map(([name, { description }]) => {
|
|
@@ -68,7 +73,7 @@ function NodeInfoPreview({ variableName, helperName, isValid, isDefined, helpers
|
|
|
68
73
|
export function HandlebarsSuggestionsPlugin(props) {
|
|
69
74
|
const [editor] = useLexicalComposerContext();
|
|
70
75
|
// Derive variables/helpers from store if provided to avoid prop identity changes forcing re-creation
|
|
71
|
-
const { store, onCreateVariable, hideHelperSuggestions, hideCreateVariableButton } = props;
|
|
76
|
+
const { store, onCreateVariable, hideHelperSuggestions, hideCreateVariableButton, testId } = props;
|
|
72
77
|
const [varsState, setVarsState] = useState(() => store?.getVariables() ?? []);
|
|
73
78
|
const [helpersState, setHelpersState] = useState(() => store?.getHelpers() ?? {});
|
|
74
79
|
// Subscribe to store updates to keep a stable extension instance while data updates
|
|
@@ -87,6 +92,8 @@ export function HandlebarsSuggestionsPlugin(props) {
|
|
|
87
92
|
// References for hovering logic
|
|
88
93
|
useRef(null);
|
|
89
94
|
const hoveringPopupRef = useRef(false);
|
|
95
|
+
/** The line before the caret when Escape closed the menu: the menu stays closed until it changes. */
|
|
96
|
+
const dismissedRef = useRef(null);
|
|
90
97
|
const popupRef = useRef(null);
|
|
91
98
|
const pendingInfoOpenRef = useRef(false);
|
|
92
99
|
// Filter variables for autocomplete
|
|
@@ -95,8 +102,23 @@ export function HandlebarsSuggestionsPlugin(props) {
|
|
|
95
102
|
return [];
|
|
96
103
|
}
|
|
97
104
|
const lowerQuery = state.query.toLowerCase();
|
|
98
|
-
|
|
99
|
-
|
|
105
|
+
const matches = (value) => value.toLowerCase().includes(lowerQuery);
|
|
106
|
+
if (state.kind === 'block') {
|
|
107
|
+
return BLOCK_HELPERS.filter(matches).map((value) => ({ value, kind: 'block' }));
|
|
108
|
+
}
|
|
109
|
+
const innermost = state.blocks?.at(-1);
|
|
110
|
+
if (state.kind === 'close') {
|
|
111
|
+
return innermost !== undefined && matches(innermost) ? [{ value: innermost, kind: 'close' }] : [];
|
|
112
|
+
}
|
|
113
|
+
const keywords = state.kind === 'plain' && innermost !== undefined && matches('else') ? [{ value: 'else', kind: 'keyword' }] : [];
|
|
114
|
+
const variables = varsState.filter(matches).map((value) => ({ value, kind: 'variable' }));
|
|
115
|
+
const helpers = state.kind === 'plain' && !state.triple && !hideHelperSuggestions
|
|
116
|
+
? Object.keys(helpersState)
|
|
117
|
+
.filter(matches)
|
|
118
|
+
.map((value) => ({ value, kind: 'helper' }))
|
|
119
|
+
: [];
|
|
120
|
+
return [...keywords, ...variables, ...helpers];
|
|
121
|
+
}, [varsState, helpersState, hideHelperSuggestions, state.mode, state.query, state.kind, state.triple, state.blocks]);
|
|
100
122
|
// Reset selection index when list changes
|
|
101
123
|
useEffect(() => {
|
|
102
124
|
setSelectedIndex(0);
|
|
@@ -107,8 +129,13 @@ export function HandlebarsSuggestionsPlugin(props) {
|
|
|
107
129
|
useEffect(() => editor.registerUpdateListener(({ editorState }) => {
|
|
108
130
|
editorState.read(() => {
|
|
109
131
|
const selection = $getSelection();
|
|
132
|
+
// A caret in an open `{{…` is typing, even inside a highlighted node: suggestions win over the info card.
|
|
133
|
+
const typing = $isRangeSelection(selection) &&
|
|
134
|
+
selection.isCollapsed() &&
|
|
135
|
+
!pendingInfoOpenRef.current &&
|
|
136
|
+
suggestionContext(lineText(selection.anchor.getNode(), selection.anchor.offset, 'before')) !== undefined;
|
|
110
137
|
// 1a. Handle Selection for Info (Prioritize actual Nodes)
|
|
111
|
-
if ($isRangeSelection(selection) && selection.isCollapsed()) {
|
|
138
|
+
if (!typing && $isRangeSelection(selection) && selection.isCollapsed()) {
|
|
112
139
|
const node = selection.anchor.getNode();
|
|
113
140
|
if ($isHandlebarsVariableNode(node)) {
|
|
114
141
|
const dom = editor.getElementByKey(node.getKey());
|
|
@@ -118,7 +145,7 @@ export function HandlebarsSuggestionsPlugin(props) {
|
|
|
118
145
|
mode: 'INFO',
|
|
119
146
|
position: getPositionFromRect(rect),
|
|
120
147
|
nodeKey: node.getKey(),
|
|
121
|
-
variableName: node.__isHelper ? node.__variableName : node.getTextContent(),
|
|
148
|
+
variableName: node.__isHelper ? node.__variableName : node.getTextContent().trim(),
|
|
122
149
|
helperName: node.__isHelper ? node.__helperName : undefined,
|
|
123
150
|
isDefined: node.__isHelper ? node.__isVariableDefined : node.__isDefined,
|
|
124
151
|
isValid: node.__isHelper ? node.__isValid : undefined,
|
|
@@ -132,23 +159,22 @@ export function HandlebarsSuggestionsPlugin(props) {
|
|
|
132
159
|
const { anchor } = selection;
|
|
133
160
|
const node = anchor.getNode();
|
|
134
161
|
if ($isTextNode(node)) {
|
|
135
|
-
const text = node.getTextContent();
|
|
136
162
|
const { offset } = anchor;
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
163
|
+
const before = lineText(node, offset, 'before');
|
|
164
|
+
const match = before === dismissedRef.current ? undefined : suggestionContext(before);
|
|
165
|
+
if (before !== dismissedRef.current) {
|
|
166
|
+
dismissedRef.current = null;
|
|
167
|
+
}
|
|
141
168
|
if (match) {
|
|
142
169
|
// Check if next sibling is already a handlebars node to avoid double suggestions
|
|
143
170
|
// This prevents suggesting a new variable when one is already immediately following the cursor
|
|
144
171
|
const nextSibling = node.getNextSibling();
|
|
145
|
-
if (offset ===
|
|
172
|
+
if (offset === node.getTextContentSize() && $isHandlebarsVariableNode(nextSibling)) {
|
|
146
173
|
setState(INITIAL_STATE);
|
|
147
174
|
return;
|
|
148
175
|
}
|
|
149
|
-
const
|
|
150
|
-
const
|
|
151
|
-
const triple = braces === '{{{';
|
|
176
|
+
const { query, triple, kind } = match;
|
|
177
|
+
const blocks = openBlocks(lineText(node, offset, 'before', true));
|
|
152
178
|
// Calculate position for the popover
|
|
153
179
|
const nativeSelection = window.getSelection();
|
|
154
180
|
if (nativeSelection && nativeSelection.rangeCount > 0) {
|
|
@@ -160,6 +186,8 @@ export function HandlebarsSuggestionsPlugin(props) {
|
|
|
160
186
|
position: getPositionFromRect(rect),
|
|
161
187
|
query,
|
|
162
188
|
triple,
|
|
189
|
+
kind,
|
|
190
|
+
blocks,
|
|
163
191
|
});
|
|
164
192
|
return;
|
|
165
193
|
}
|
|
@@ -180,33 +208,42 @@ export function HandlebarsSuggestionsPlugin(props) {
|
|
|
180
208
|
// ---------------------------------------------------------------------------
|
|
181
209
|
// 2. Command Listener: Keyboard Navigation & Action
|
|
182
210
|
// ---------------------------------------------------------------------------
|
|
183
|
-
const selectOption = useCallback((
|
|
211
|
+
const selectOption = useCallback((option) => {
|
|
184
212
|
editor.update(() => {
|
|
185
213
|
const selection = $getSelection();
|
|
186
|
-
if (
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
214
|
+
if (!$isRangeSelection(selection))
|
|
215
|
+
return;
|
|
216
|
+
const node = selection.anchor.getNode();
|
|
217
|
+
const match = $isTextNode(node)
|
|
218
|
+
? suggestionContext(lineText(node, selection.anchor.offset, 'before'))
|
|
219
|
+
: undefined;
|
|
220
|
+
if (!match || !$isTextNode(node))
|
|
221
|
+
return;
|
|
222
|
+
const closed = lineText(node, selection.anchor.offset, 'after').trimStart().startsWith('}');
|
|
223
|
+
const closing = closed ? '' : match.triple ? '}}}' : '}}';
|
|
224
|
+
const closes = option.kind === 'keyword' || option.kind === 'close';
|
|
225
|
+
const text = option.kind === 'variable'
|
|
226
|
+
? `${option.value}${match.kind === 'plain' ? closing : ''}`
|
|
227
|
+
: closes
|
|
228
|
+
? `${option.value}${closing}`
|
|
229
|
+
: `${option.value} `;
|
|
230
|
+
for (let index = 0; index < match.query.length; index++) {
|
|
231
|
+
selection.deleteCharacter(true);
|
|
232
|
+
}
|
|
233
|
+
// A block is inserted whole, with the caret on its argument: `{{#if ▮}}{{else}}{{/if}}`.
|
|
234
|
+
const body = option.kind === 'block' && !closed ? blockBody(option.value) : '';
|
|
235
|
+
selection.insertText(text + body);
|
|
236
|
+
const after = $getSelection();
|
|
237
|
+
if (body.length > 0 && $isRangeSelection(after)) {
|
|
238
|
+
const inserted = after.anchor.getNode();
|
|
239
|
+
const offset = after.anchor.offset - body.length;
|
|
240
|
+
if ($isTextNode(inserted) && offset >= 0) {
|
|
241
|
+
after.setTextNodeRange(inserted, offset, inserted, offset);
|
|
205
242
|
}
|
|
206
243
|
}
|
|
207
244
|
});
|
|
208
245
|
setState(INITIAL_STATE);
|
|
209
|
-
}, [editor
|
|
246
|
+
}, [editor]);
|
|
210
247
|
const applyHelper = useCallback((newHelperName) => {
|
|
211
248
|
editor.update(() => {
|
|
212
249
|
const { nodeKey } = state;
|
|
@@ -266,6 +303,14 @@ export function HandlebarsSuggestionsPlugin(props) {
|
|
|
266
303
|
}
|
|
267
304
|
if (command === KEY_ESCAPE_COMMAND) {
|
|
268
305
|
keyEvent.preventDefault();
|
|
306
|
+
// The menu owns this Escape: a dialog or a sheet around the field must not close on it.
|
|
307
|
+
keyEvent.stopPropagation();
|
|
308
|
+
editor.getEditorState().read(() => {
|
|
309
|
+
const selection = $getSelection();
|
|
310
|
+
dismissedRef.current = $isRangeSelection(selection)
|
|
311
|
+
? lineText(selection.anchor.getNode(), selection.anchor.offset, 'before')
|
|
312
|
+
: null;
|
|
313
|
+
});
|
|
269
314
|
setState(INITIAL_STATE);
|
|
270
315
|
return true;
|
|
271
316
|
}
|
|
@@ -303,7 +348,7 @@ export function HandlebarsSuggestionsPlugin(props) {
|
|
|
303
348
|
mode: 'INFO',
|
|
304
349
|
position,
|
|
305
350
|
nodeKey,
|
|
306
|
-
variableName: node.__isHelper ? node.__variableName : node.getTextContent(),
|
|
351
|
+
variableName: node.__isHelper ? node.__variableName : node.getTextContent().trim(),
|
|
307
352
|
helperName: node.__isHelper ? node.__helperName : undefined,
|
|
308
353
|
isDefined: node.__isHelper ? node.__isVariableDefined : node.__isDefined,
|
|
309
354
|
isValid: node.__isHelper ? node.__isValid : undefined,
|
|
@@ -323,6 +368,13 @@ export function HandlebarsSuggestionsPlugin(props) {
|
|
|
323
368
|
document.removeEventListener('click', handleClick);
|
|
324
369
|
};
|
|
325
370
|
}, [editor, state.mode, state.nodeKey, hideHelperSuggestions]);
|
|
371
|
+
useEffect(() => editor.registerCommand(BLUR_COMMAND, (event) => {
|
|
372
|
+
const next = event.relatedTarget;
|
|
373
|
+
if (!(next instanceof Node && popupRef.current?.contains(next))) {
|
|
374
|
+
setState(INITIAL_STATE);
|
|
375
|
+
}
|
|
376
|
+
return false;
|
|
377
|
+
}, COMMAND_PRIORITY_LOW), [editor]);
|
|
326
378
|
// ---------------------------------------------------------------------------
|
|
327
379
|
// 4. Scroll Listener: Close popover on scroll
|
|
328
380
|
// ---------------------------------------------------------------------------
|
|
@@ -375,7 +427,7 @@ export function HandlebarsSuggestionsPlugin(props) {
|
|
|
375
427
|
height: 1,
|
|
376
428
|
pointerEvents: 'none',
|
|
377
429
|
opacity: 0,
|
|
378
|
-
} }), _jsx(PreviewCardPopup, { sideOffset: state.mode === 'INFO' ? 8 : 6, align: "start", className: "p-1", children: _jsxs("div", { ref: popupRef, className: "flex flex-col w-full rounded-md bg-popover text-popover-foreground", children: [state.mode === 'AUTOCOMPLETE' && (_jsx(AutocompletePreview, {
|
|
430
|
+
} }), _jsx(PreviewCardPopup, { sideOffset: state.mode === 'INFO' ? 8 : 6, align: "start", className: "p-1", children: _jsxs("div", { ref: popupRef, className: "flex flex-col w-full rounded-md bg-popover text-popover-foreground", children: [state.mode === 'AUTOCOMPLETE' && (_jsx(AutocompletePreview, { options: filteredVariables, selectedIndex: selectedIndex, onSelect: selectOption, testId: testId })), state.mode === 'INFO' && (_jsx(NodeInfoPreview, { variableName: state.variableName, helperName: state.helperName, isValid: state.isValid, isDefined: state.isDefined, helpers: helpersState, onApplyHelper: applyHelper, onCreateVariable: handleCreateVariable, hideHelperSuggestions: hideHelperSuggestions, hideCreateVariableButton: hideCreateVariableButton }))] }) })] }));
|
|
379
431
|
}
|
|
380
432
|
export const HandlebarsSuggestionsExtension = (props) => defineExtension({
|
|
381
433
|
name: '@lexical/handlebars-suggestions',
|