react-dsl-editor 0.2.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Krzysztof Rzymkowski
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # React DSL Editor
2
+
3
+ Lightweight React textarea for Domain Specific Language (DSL) editing.
4
+
5
+ ## Usage
6
+ ```bash
7
+ pnpm install @rzymek/react-dsl-editor
8
+ ```
9
+
10
+ ```typescript jsx
11
+ // Define your grammar using the provided parser combinator library:
12
+ const identifier = pattern(`[a-zA-Z_][a-zA-Z0-9_]*`, 'identifier');
13
+ const grammar = sequence('declaration',
14
+ term('var'),
15
+ ws,
16
+ identifier,
17
+ term('='),
18
+ rational,
19
+ );
20
+
21
+ // Optionally provide autocomplete suggestions for specific types of tokens:
22
+ function suggest(type: NodeTypes<typeof grammar>) {
23
+ if(type === 'identifier') {
24
+ return ['x', 'y', 'z'];
25
+ }
26
+ }
27
+
28
+ function Usage() {
29
+ const [code, setCode] = useState(`
30
+ var x = 1.3
31
+ `.trim());
32
+ return <DslEditor grammar={grammar}
33
+ code={code}
34
+ onChange={setCode}
35
+ suggestions={suggest} />;
36
+ }
37
+
38
+ ```
@@ -0,0 +1,86 @@
1
+ import { CSSProperties } from 'react';
2
+ import { JSX } from 'react/jsx-runtime';
3
+ import { TextareaHTMLAttributes } from 'react';
4
+
5
+ export declare function alternative<T extends string>(type?: T, ...seq: Parse<T>[]): Parse<T>;
6
+
7
+ export declare function appendOffset<T>(error: ParserError<T>, offset: number): ParserError<T>;
8
+
9
+ export declare function appendOffsets<T>(errors: ParserError<T>[], offsets: number): ParserError<T>[];
10
+
11
+ export declare interface ASTNode<T extends string> {
12
+ type: T;
13
+ text: string;
14
+ children?: ASTNode<T>[];
15
+ errors: ParserError<T>[];
16
+ offset: number;
17
+ }
18
+
19
+ export declare function DslEditor<T extends string>({ code, onChange, onParsed, grammar, wrap, suggestions: clientSuggestions, className, styles, ...textareaProps }: {
20
+ code: string;
21
+ onChange: (text: string) => void;
22
+ onParsed?: (ast: ParserResult<T>) => void;
23
+ grammar: Parse<T>;
24
+ wrap?: boolean;
25
+ className?: string;
26
+ styles?: Partial<Record<T | 'error', CSSProperties>>;
27
+ suggestions?: (type: T | 'error') => string[] | undefined;
28
+ } & Omit<TextareaHTMLAttributes<HTMLTextAreaElement>, 'wrap' | 'onChange'>): JSX.Element;
29
+
30
+ export declare function isParserError<T extends string>(result: ParserResult<T>): boolean;
31
+
32
+ export declare function isParserSuccess<T extends string>(result: ParserResult<T>): boolean;
33
+
34
+ export declare type NodeTypes<T> = _NodeTypes<T> | 'error';
35
+
36
+ declare type _NodeTypes<T> = T extends Parse<infer U> ? U : never;
37
+
38
+ export declare function optional<T extends string>(subparser: Parse<T>): Parse<T>;
39
+
40
+ export declare const optionalWhitespace: Parse<"optionalWhitespace">;
41
+
42
+ export declare type Parse<T extends string> = (text: string) => ParserResult<T>;
43
+
44
+ export declare class Parser<T extends string> {
45
+ private readonly parser;
46
+ constructor(parser: Parse<T>);
47
+ parse(input: string): ASTNode<T>;
48
+ }
49
+
50
+ export declare interface ParserError<T> {
51
+ offset: number;
52
+ expected: string | RegExp;
53
+ got: string;
54
+ type: T;
55
+ }
56
+
57
+ export declare interface ParserResult<T extends string> {
58
+ type: T;
59
+ text: string;
60
+ children?: ParserResult<T>[];
61
+ errors: ParserError<T>[];
62
+ }
63
+
64
+ export declare class ParsingError extends Error {
65
+ constructor(msg: string);
66
+ }
67
+
68
+ export declare function pattern(regex: string): Parse<'pattern'>;
69
+
70
+ export declare function pattern<T extends string>(regex: string, type: T): Parse<T>;
71
+
72
+ export declare const rational: Parse<"rational">;
73
+
74
+ export declare function repeat<T extends string>(type: T | undefined, parser: Parse<T>, min?: number, max?: number): Parse<T>;
75
+
76
+ export declare function seq<T extends string>(type?: T, ...p: Parse<T>[]): Parse<T | 'optionalWhitespace'>;
77
+
78
+ export declare function sequence<T extends string>(type?: T, ...seq: Parse<T>[]): Parse<T>;
79
+
80
+ export declare function term<T extends string>(str: T): Parse<T>;
81
+
82
+ export declare function term<T extends string>(str: string, type: T): Parse<T>;
83
+
84
+ export declare const ws: Parse<"space" | "ws">;
85
+
86
+ export { }
@@ -0,0 +1,565 @@
1
+ import { forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from "react";
2
+ import { jsx, jsxs } from "react/jsx-runtime";
3
+ const textStyle = {
4
+ position: "absolute",
5
+ inset: 0,
6
+ overflow: "auto",
7
+ padding: 3,
8
+ fontSize: "1em",
9
+ lineHeight: "1.3em",
10
+ fontFamily: "monospace"
11
+ }, ReadOnlyTextarea = forwardRef(function({ wrap: o, children: _, style: v = {} }) {
12
+ return /* @__PURE__ */ jsx("pre", {
13
+ style: {
14
+ ...textStyle,
15
+ pointerEvents: "none",
16
+ margin: 0,
17
+ overflow: "auto",
18
+ whiteSpace: o ? "pre-wrap" : "pre",
19
+ ...v
20
+ },
21
+ children: _
22
+ });
23
+ });
24
+ function SyntaxHighlighter({ syntax: o = [], ref: _, wrap: v, styles: y }) {
25
+ return /* @__PURE__ */ jsx(ReadOnlyTextarea, {
26
+ ref: _,
27
+ wrap: v,
28
+ children: o.map((o, _) => /* @__PURE__ */ jsx("span", {
29
+ className: [o.name].join(" "),
30
+ style: y?.[o.name],
31
+ children: o.text
32
+ }, `token_${_}`))
33
+ });
34
+ }
35
+ function e$3(o, _, v) {
36
+ let y = (v) => o(v, ..._);
37
+ return v === void 0 ? y : Object.assign(y, {
38
+ lazy: v,
39
+ lazyArgs: _
40
+ });
41
+ }
42
+ function t$2(o, _, v) {
43
+ let y = o.length - _.length;
44
+ if (y === 0) return o(..._);
45
+ if (y === 1) return e$3(o, _, v);
46
+ throw Error("Wrong number of arguments");
47
+ }
48
+ var t$5 = {
49
+ done: !1,
50
+ hasNext: !1
51
+ };
52
+ function t$6(o, ..._) {
53
+ let v = o, y = _.map((o) => "lazy" in o ? r$2(o) : void 0), b = 0;
54
+ for (; b < _.length;) {
55
+ if (y[b] === void 0 || !i$1(v)) {
56
+ let o = _[b];
57
+ v = o(v), b += 1;
58
+ continue;
59
+ }
60
+ let o = [];
61
+ for (let v = b; v < _.length; v++) {
62
+ let _ = y[v];
63
+ if (_ === void 0 || (o.push(_), _.isSingle)) break;
64
+ }
65
+ let x = [];
66
+ for (let _ of v) if (n$3(_, x, o)) break;
67
+ let { isSingle: S } = o.at(-1);
68
+ v = S ? x[0] : x, b += o.length;
69
+ }
70
+ return v;
71
+ }
72
+ function n$3(o, _, v) {
73
+ if (v.length === 0) return _.push(o), !1;
74
+ let y = o, b = t$5, x = !1;
75
+ for (let [o, S] of v.entries()) {
76
+ let { index: C, items: w } = S;
77
+ if (w.push(y), b = S(y, C, w), S.index += 1, b.hasNext) {
78
+ if (b.hasMany ?? !1) {
79
+ for (let y of b.next) if (n$3(y, _, v.slice(o + 1))) return !0;
80
+ return x;
81
+ }
82
+ y = b.next;
83
+ }
84
+ if (!b.hasNext) break;
85
+ b.done && (x = !0);
86
+ }
87
+ return b.hasNext && _.push(y), x;
88
+ }
89
+ function r$2(o) {
90
+ let { lazy: _, lazyArgs: v } = o, y = _(...v);
91
+ return Object.assign(y, {
92
+ isSingle: _.single ?? !1,
93
+ index: 0,
94
+ items: []
95
+ });
96
+ }
97
+ function i$1(o) {
98
+ return typeof o == "string" || typeof o == "object" && !!o && Symbol.iterator in o;
99
+ }
100
+ var e$1 = {
101
+ asc: (o, _) => o > _,
102
+ desc: (o, _) => o < _
103
+ };
104
+ function t(o, _) {
105
+ let [v, ...y] = _;
106
+ if (!i(v)) {
107
+ let _ = r$1(...y);
108
+ return o(v, _);
109
+ }
110
+ let b = r$1(v, ...y);
111
+ return (_) => o(_, b);
112
+ }
113
+ function r$1(o, _, ...v) {
114
+ let y = typeof o == "function" ? o : o[0], b = typeof o == "function" ? "asc" : o[1], { [b]: x } = e$1, S = _ === void 0 ? void 0 : r$1(_, ...v);
115
+ return (o, _) => {
116
+ let v = y(o), b = y(_);
117
+ return x(v, b) ? 1 : x(b, v) ? -1 : S?.(o, _) ?? 0;
118
+ };
119
+ }
120
+ function i(o) {
121
+ if (a(o)) return !0;
122
+ if (typeof o != "object" || !Array.isArray(o)) return !1;
123
+ let [_, v, ...y] = o;
124
+ return a(_) && typeof v == "string" && v in e$1 && y.length === 0;
125
+ }
126
+ var a = (o) => typeof o == "function" && o.length === 1;
127
+ function e(o) {
128
+ return o === "" || o === void 0 ? !0 : Array.isArray(o) ? o.length === 0 : Object.keys(o).length === 0;
129
+ }
130
+ function t$4(...o) {
131
+ return t$2(n$2, o);
132
+ }
133
+ var n$2 = (o) => o.at(-1);
134
+ function t$3(...o) {
135
+ return t$2(n$1, o, r);
136
+ }
137
+ var n$1 = (o, _) => o.map(_), r = (o) => (_, v, y) => ({
138
+ done: !1,
139
+ hasNext: !0,
140
+ next: o(_, v, y)
141
+ });
142
+ function t$1(...o) {
143
+ return t(n, o);
144
+ }
145
+ var n = (o, _) => [...o].sort(_);
146
+ function isParserError(o) {
147
+ return !e(o.errors);
148
+ }
149
+ function isParserSuccess(o) {
150
+ return !e(o.text);
151
+ }
152
+ function alternative(o = "alternative", ..._) {
153
+ return (v) => {
154
+ let y = [];
155
+ for (let o of _) {
156
+ let _ = o(v);
157
+ if (isParserSuccess(_)) return _;
158
+ y.push(..._.errors);
159
+ }
160
+ return {
161
+ type: o,
162
+ text: "",
163
+ errors: y
164
+ };
165
+ };
166
+ }
167
+ function appendOffset(o, _) {
168
+ return {
169
+ ...o,
170
+ offset: o.offset + _
171
+ };
172
+ }
173
+ function appendOffsets(o, _) {
174
+ return o.map((o) => appendOffset(o, _));
175
+ }
176
+ function optional(o) {
177
+ return (_) => {
178
+ let v = o(_);
179
+ return isParserError(v) ? {
180
+ type: v.type,
181
+ text: "",
182
+ errors: []
183
+ } : v;
184
+ };
185
+ }
186
+ function pattern(o, _ = "pattern") {
187
+ return (v) => {
188
+ let y = (/* @__PURE__ */ RegExp(`^${o}`)).exec(v);
189
+ return y ? {
190
+ type: _,
191
+ text: y[0],
192
+ errors: []
193
+ } : {
194
+ type: _,
195
+ text: "",
196
+ errors: [{
197
+ expected: new RegExp(o),
198
+ got: v,
199
+ offset: 0,
200
+ type: _
201
+ }]
202
+ };
203
+ };
204
+ }
205
+ const optionalWhitespace = pattern("\\s*", "optionalWhitespace");
206
+ function filter(o, _) {
207
+ if (!isParserSuccess(_)) return _;
208
+ let { children: v } = _;
209
+ return v ? {
210
+ ..._,
211
+ children: v.filter(o).map((_) => filter(o, _))
212
+ } : _;
213
+ }
214
+ function trimEmptyNode(o) {
215
+ return filter((o) => isParserError(o) || o.text !== "" || !!o.children, o);
216
+ }
217
+ function withOffset(o, _ = 0) {
218
+ let v = _;
219
+ return {
220
+ ...o,
221
+ children: o.children?.map((o, _, y) => {
222
+ let { text: b } = y[_ - 1] ?? { text: "" };
223
+ return v += b.length, withOffset(o, v);
224
+ }),
225
+ offset: _
226
+ };
227
+ }
228
+ var Parser = class {
229
+ parser;
230
+ constructor(o) {
231
+ this.parser = o;
232
+ }
233
+ parse(o) {
234
+ let _ = withOffset(this.parser(o));
235
+ if (_.text !== o) {
236
+ let v = _.text.length;
237
+ _.errors.push({
238
+ offset: v,
239
+ got: o.substring(v),
240
+ expected: "",
241
+ type: "error"
242
+ });
243
+ }
244
+ return [trimEmptyNode].reduce((o, _) => _(o), _);
245
+ }
246
+ }, ParsingError = class extends Error {
247
+ constructor(o) {
248
+ super(o);
249
+ }
250
+ };
251
+ const rational = pattern("\\d+\\.?\\d*", "rational");
252
+ function repeat(o = "repeat", _, v = 1, y = Infinity) {
253
+ return (b) => {
254
+ let x = [], S = 0, C = [];
255
+ for (let o = 0; o < y; o++) {
256
+ let y = _(b.substring(S));
257
+ if (S += y.text.length, (isParserError(y) || C.length > 0) && o >= v) break;
258
+ y.errors.forEach((o) => C.push(appendOffset(o, S))), x.push(y);
259
+ }
260
+ return {
261
+ type: o,
262
+ text: x.map((o) => o.text).join(""),
263
+ errors: C,
264
+ children: x
265
+ };
266
+ };
267
+ }
268
+ function sequence(o = "sequence", ..._) {
269
+ return (v) => {
270
+ let y = [], b = 0, x = [];
271
+ for (let o of _) {
272
+ let _ = o(v.substring(b));
273
+ _.errors = appendOffsets(_.errors, b), b += _.text.length, x.push(..._.errors), y.push(_);
274
+ }
275
+ return {
276
+ type: o,
277
+ text: v.substring(0, b),
278
+ errors: x,
279
+ children: y
280
+ };
281
+ };
282
+ }
283
+ function seq(o = "seq", ..._) {
284
+ return sequence(o, optionalWhitespace, ..._.flatMap((o) => [o, optionalWhitespace]));
285
+ }
286
+ function term(o, _) {
287
+ let v = _ ?? o;
288
+ return (_) => _.startsWith(o) ? {
289
+ type: v,
290
+ text: o,
291
+ errors: []
292
+ } : {
293
+ type: v,
294
+ text: "",
295
+ errors: [{
296
+ expected: o,
297
+ got: _,
298
+ offset: 0,
299
+ type: v
300
+ }]
301
+ };
302
+ }
303
+ const ws = repeat("ws", term(" ", "space"));
304
+ function getSuggestions(o, _, v) {
305
+ return console.assert(!0, o, _, v), {
306
+ suggestions: [],
307
+ prefix: ""
308
+ };
309
+ }
310
+ function syntaxForParserResult(o, _, v = []) {
311
+ if (o.children) for (let y of o.children) syntaxForParserResult(y, _, v), _ += isParserSuccess(y) ? y.text.length : 0;
312
+ else isParserSuccess(o) && v.push({
313
+ name: o.type,
314
+ text: o.text,
315
+ startOffset: _
316
+ });
317
+ return v;
318
+ }
319
+ function removeOverlap(o) {
320
+ return o.flatMap((o, _, v) => {
321
+ let y = v[_ - 1] ?? {
322
+ text: "",
323
+ startOffset: 0,
324
+ name: "error"
325
+ }, b = y.startOffset + y.text.length;
326
+ return o.startOffset < b ? [] : [o];
327
+ });
328
+ }
329
+ function textSyntax(o, _) {
330
+ let v = syntaxForParserResult(o, 0), y = t$6(v, t$3((o) => o.startOffset + o.text.length), t$1((o) => o), t$4(), (o) => o ?? 0);
331
+ if (y < _.length) {
332
+ let o = y;
333
+ v.push({
334
+ name: "error",
335
+ text: _.substring(o),
336
+ startOffset: o
337
+ });
338
+ }
339
+ return removeOverlap(v);
340
+ }
341
+ const CursorPosition = forwardRef(function(o, _) {
342
+ let v = useRef(null), b = useRef(null);
343
+ return useImperativeHandle(_, () => ({ getCursorPosition() {
344
+ if (!v.current || !b.current) return {
345
+ top: 0,
346
+ left: 0
347
+ };
348
+ let { offsetTop: o, offsetLeft: _ } = v.current, { scrollTop: y, scrollLeft: x } = b.current, S = window.getComputedStyle(b.current), C = parseInt(S.lineHeight);
349
+ return {
350
+ top: o - y + C,
351
+ left: _ - x
352
+ };
353
+ } }), []), /* @__PURE__ */ jsxs(ReadOnlyTextarea, {
354
+ ref: b,
355
+ wrap: o.wrap,
356
+ style: { visibility: "hidden" },
357
+ children: [o.text, /* @__PURE__ */ jsx("span", {
358
+ ref: v,
359
+ children: "\xA0"
360
+ })]
361
+ });
362
+ });
363
+ function SuggestionsView({ suggestions: o, onSelect: _ }) {
364
+ return /* @__PURE__ */ jsx("div", {
365
+ style: {
366
+ display: "flex",
367
+ gap: 4,
368
+ padding: "4px 0",
369
+ overflowY: "auto"
370
+ },
371
+ children: o.map((o, v) => /* @__PURE__ */ jsxs("button", {
372
+ onClick: () => _(o),
373
+ children: [
374
+ "\xA0",
375
+ o,
376
+ "\xA0"
377
+ ]
378
+ }, v))
379
+ });
380
+ }
381
+ function shortcutName(o) {
382
+ let _ = "";
383
+ o.ctrlKey && (_ = "Ctrl");
384
+ let { key: v } = o;
385
+ return v === " " && (v = "Space"), `${_}${v}`;
386
+ }
387
+ function useSyncScroll(o) {
388
+ return useCallback((_) => {
389
+ let v = _.currentTarget;
390
+ o.current && v && (o.current.scrollTop = v.scrollTop, o.current.scrollLeft = v.scrollLeft);
391
+ }, [o]);
392
+ }
393
+ function SuggestionsMenu({ suggestions: o, onSelect: _, style: v, selectedIndex: y, onHover: b }) {
394
+ return /* @__PURE__ */ jsx("div", {
395
+ style: {
396
+ position: "absolute",
397
+ ...v,
398
+ background: "#252526",
399
+ border: "1px solid #454545",
400
+ boxShadow: "0 2px 8px rgba(0,0,0,0.3)",
401
+ color: "#cccccc",
402
+ fontFamily: "monospace",
403
+ fontSize: "0.9em",
404
+ minWidth: 200,
405
+ zIndex: 100
406
+ },
407
+ children: o.map((o, v) => /* @__PURE__ */ jsx("div", {
408
+ onClick: () => _(o),
409
+ onMouseOver: () => b(v),
410
+ style: {
411
+ cursor: "pointer",
412
+ padding: "4px 8px",
413
+ backgroundColor: y === v ? "#094771" : "transparent"
414
+ },
415
+ children: o
416
+ }, v))
417
+ });
418
+ }
419
+ function DslEditor({ code: o, onChange: y, onParsed: T, grammar: E, wrap: D = !1, suggestions: O, className: k = DslEditor.name, styles: A,...j }) {
420
+ let [M, N] = useState({
421
+ suggestions: [],
422
+ prefix: ""
423
+ }), [P, F] = useState([]), [I, L] = useState({
424
+ top: 0,
425
+ left: 0,
426
+ visible: !1
427
+ }), [R, z] = useState(0), [B, V] = useState(""), H = useRef(null), U = useRef(null), W = useRef(null), G = useRef(null), K = useCallback((o) => {
428
+ let _ = U.current?.selectionStart ?? 0, v = getSuggestions(o, _, O);
429
+ N(v);
430
+ }, [O]), q = useCallback(() => {
431
+ K(P), V(U.current?.value?.substring(0, U.current?.selectionStart ?? 0) ?? "");
432
+ }, [P, K]);
433
+ useEffect(() => {
434
+ H.current = new Parser(E);
435
+ }, [E]), useEffect(() => {
436
+ if (!H.current) return;
437
+ let _ = H.current.parse(o);
438
+ T?.(_);
439
+ let v = textSyntax(_, o);
440
+ K(v), F(v), V(o.substring(0, U.current?.selectionStart ?? 0));
441
+ }, [
442
+ o,
443
+ T,
444
+ K
445
+ ]);
446
+ let J = useCallback(() => W.current?.getCursorPosition?.() ?? {
447
+ top: 0,
448
+ left: 0
449
+ }, []), Y = useCallback((_) => {
450
+ if (!U.current) return;
451
+ let { selectionStart: v, selectionEnd: b } = U.current, { prefix: x } = M, S = o.substring(0, v - x.length) + _ + o.substring(b);
452
+ y(S), setTimeout(() => {
453
+ U.current && (U.current.focus(), U.current.selectionStart = U.current.selectionEnd = v - x.length + _.length);
454
+ }, 0), L((o) => ({
455
+ ...o,
456
+ visible: !1
457
+ }));
458
+ }, [
459
+ o,
460
+ y,
461
+ M
462
+ ]), X = useMemo(() => ({
463
+ ArrowDown() {
464
+ z((o) => (o + 1) % M.suggestions.length);
465
+ },
466
+ ArrowUp() {
467
+ z((o) => (o - 1 + M.suggestions.length) % M.suggestions.length);
468
+ },
469
+ Enter() {
470
+ M.suggestions[R] && Y(M.suggestions[R]);
471
+ },
472
+ Escape() {
473
+ L((o) => ({
474
+ ...o,
475
+ visible: !1
476
+ }));
477
+ }
478
+ }), [
479
+ Y,
480
+ R,
481
+ M
482
+ ]), Z = useMemo(() => ({ CtrlSpace() {
483
+ let { top: o, left: _ } = J();
484
+ z(0), L({
485
+ visible: !0,
486
+ top: o,
487
+ left: _
488
+ });
489
+ } }), [J]), Q = useCallback((o) => {
490
+ let _ = (I.visible ? X : Z)[shortcutName(o)];
491
+ _ && (o.preventDefault(), _());
492
+ }, [
493
+ I.visible,
494
+ X,
495
+ Z
496
+ ]), $ = useCallback((o) => {
497
+ y(o.target.value), L((o) => ({
498
+ ...o,
499
+ visible: !1
500
+ })), V(o.target.value.substring(0, o.target.selectionStart));
501
+ }, [y]);
502
+ return /* @__PURE__ */ jsxs("div", {
503
+ style: {
504
+ display: "grid",
505
+ gridTemplateRows: "1fr auto",
506
+ flex: 1,
507
+ width: "100%",
508
+ height: "100%"
509
+ },
510
+ className: k,
511
+ children: [/* @__PURE__ */ jsxs("div", {
512
+ style: {
513
+ position: "relative",
514
+ border: "1px solid black",
515
+ overflow: "hidden"
516
+ },
517
+ children: [
518
+ /* @__PURE__ */ jsx("textarea", {
519
+ ref: U,
520
+ spellcheck: !1,
521
+ wrap: D ? "soft" : "off",
522
+ style: {
523
+ ...textStyle,
524
+ color: "transparent",
525
+ background: "transparent",
526
+ caretColor: "black",
527
+ border: "none",
528
+ resize: "none"
529
+ },
530
+ value: o,
531
+ onSelect: q,
532
+ onChange: $,
533
+ onScroll: useSyncScroll(G),
534
+ onKeyDown: Q,
535
+ ...j
536
+ }),
537
+ /* @__PURE__ */ jsx(SyntaxHighlighter, {
538
+ syntax: P,
539
+ ref: G,
540
+ wrap: D,
541
+ styles: A
542
+ }),
543
+ /* @__PURE__ */ jsx(CursorPosition, {
544
+ ref: W,
545
+ text: B,
546
+ wrap: D
547
+ }),
548
+ I.visible && M.suggestions.length > 0 && /* @__PURE__ */ jsx(SuggestionsMenu, {
549
+ suggestions: M.suggestions,
550
+ onSelect: Y,
551
+ style: {
552
+ top: I.top,
553
+ left: I.left
554
+ },
555
+ selectedIndex: R,
556
+ onHover: z
557
+ })
558
+ ]
559
+ }), /* @__PURE__ */ jsx(SuggestionsView, {
560
+ suggestions: M.suggestions,
561
+ onSelect: Y
562
+ })]
563
+ });
564
+ }
565
+ export { DslEditor, Parser, ParsingError, alternative, appendOffset, appendOffsets, isParserError, isParserSuccess, optional, optionalWhitespace, pattern, rational, repeat, seq, sequence, term, ws };
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "react-dsl-editor",
3
+ "private": false,
4
+ "license": "MIT",
5
+ "version": "0.2.0",
6
+ "publishConfig": {
7
+ "access": "public",
8
+ "provenance": true
9
+ },
10
+ "type": "module",
11
+ "module": "./dist/react-dsl-editor.js",
12
+ "types": "./dist/index.d.ts",
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/react-dsl-editor.js"
20
+ }
21
+ },
22
+ "dependencies": {
23
+ "remeda": "^2.32.0"
24
+ },
25
+ "peerDependencies": {
26
+ "react": "^19",
27
+ "react-dom": "^19"
28
+ },
29
+ "devDependencies": {
30
+ "@eslint/js": "^9.39.2",
31
+ "@types/node": "^25.0.3",
32
+ "@types/react": "^19.2.7",
33
+ "@types/react-dom": "^19.2.3",
34
+ "@vitejs/plugin-react": "^5.1.2",
35
+ "eslint": "^9.39.2",
36
+ "eslint-plugin-react-hooks": "^7.0.1",
37
+ "eslint-plugin-react-refresh": "^0.4.26",
38
+ "globals": "^16.5.0",
39
+ "npm-run-all": "^4.1.5",
40
+ "react": "^19.2.3",
41
+ "react-dom": "^19.2.3",
42
+ "rimraf": "^6.1.2",
43
+ "typescript": "~5.9.3",
44
+ "typescript-eslint": "^8.50.0",
45
+ "vite": "npm:rolldown-vite@7.1.14",
46
+ "vite-plugin-dts": "^4.5.4",
47
+ "vitest": "^4.0.16"
48
+ },
49
+ "scripts": {
50
+ "dev": "vite",
51
+ "build": "rimraf dist && vite build",
52
+ "lint": "eslint",
53
+ "test": "vitest"
54
+ }
55
+ }