@sanity/code-input 2.29.5-purple-unicorn-remix.876 → 2.29.5-purple-unicorn-remix.958

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.
@@ -0,0 +1,849 @@
1
+ import React, { useRef, useMemo, useImperativeHandle, useCallback, useEffect } from 'react';
2
+ import { set, setIfMissing, unset, MemberField } from '@sanity/base/form';
3
+ import { Card, Select, Stack } from '@sanity/ui';
4
+ import AceEditor from 'react-ace';
5
+ import styled, { css } from 'styled-components';
6
+ import 'ace-builds/src-noconflict/mode-batchfile';
7
+ import 'ace-builds/src-noconflict/mode-csharp';
8
+ import 'ace-builds/src-noconflict/mode-css';
9
+ import 'ace-builds/src-noconflict/mode-golang';
10
+ import 'ace-builds/src-noconflict/mode-html';
11
+ import 'ace-builds/src-noconflict/mode-java';
12
+ import 'ace-builds/src-noconflict/mode-javascript';
13
+ import 'ace-builds/src-noconflict/mode-json';
14
+ import 'ace-builds/src-noconflict/mode-jsx';
15
+ import 'ace-builds/src-noconflict/mode-markdown';
16
+ import 'ace-builds/src-noconflict/mode-mysql';
17
+ import 'ace-builds/src-noconflict/mode-php';
18
+ import 'ace-builds/src-noconflict/mode-python';
19
+ import 'ace-builds/src-noconflict/mode-ruby';
20
+ import 'ace-builds/src-noconflict/mode-sass';
21
+ import 'ace-builds/src-noconflict/mode-scss';
22
+ import 'ace-builds/src-noconflict/mode-sh';
23
+ import 'ace-builds/src-noconflict/mode-text';
24
+ import 'ace-builds/src-noconflict/mode-tsx';
25
+ import 'ace-builds/src-noconflict/mode-typescript';
26
+ import 'ace-builds/src-noconflict/mode-xml';
27
+ import 'ace-builds/src-noconflict/mode-yaml';
28
+ import 'ace-builds/src-noconflict/theme-github';
29
+ import 'ace-builds/src-noconflict/theme-monokai';
30
+ import 'ace-builds/src-noconflict/theme-terminal';
31
+ import 'ace-builds/src-noconflict/theme-tomorrow';
32
+
33
+ const highlightMarkersCSS = css `
34
+ .ace_editor_markers_highlight {
35
+ position: absolute;
36
+ background-color: ${({ theme }) => theme.sanity.color.solid.primary.enabled.bg};
37
+ opacity: 0.2;
38
+ width: 100% !important;
39
+ border-radius: 0 !important;
40
+ }
41
+ `;
42
+ function createHighlightMarkers(rows) {
43
+ return rows.map((row) => ({
44
+ startRow: Number(row) - 1,
45
+ startCol: 0,
46
+ endRow: Number(row) - 1,
47
+ endCol: +Infinity,
48
+ className: 'ace_editor_markers_highlight',
49
+ type: 'screenLine',
50
+ inFront: true,
51
+ }));
52
+ }
53
+
54
+ /* eslint-disable no-undef */
55
+ // Grammar from https://github.com/sanity-io/vscode-sanity
56
+ const rules = {
57
+ start: [
58
+ {
59
+ include: '#query',
60
+ },
61
+ {
62
+ include: '#value',
63
+ },
64
+ {
65
+ include: '#pair',
66
+ },
67
+ ],
68
+ '#query': [
69
+ {
70
+ include: '#nullary-access-operator',
71
+ },
72
+ {
73
+ include: '#arraylike',
74
+ },
75
+ {
76
+ include: '#pipe',
77
+ },
78
+ {
79
+ include: '#sort-order',
80
+ },
81
+ {
82
+ include: '#filter',
83
+ },
84
+ ],
85
+ '#variable': [
86
+ {
87
+ token: 'variable.other.groq',
88
+ regex: /\$[_A-Za-z][_0-9A-Za-z]*/,
89
+ },
90
+ ],
91
+ '#keyword': [
92
+ {
93
+ token: 'keyword.other.groq',
94
+ regex: /\b(?:asc|desc|in|match)\b/,
95
+ },
96
+ ],
97
+ '#comparison': [
98
+ {
99
+ token: 'keyword.operator.comparison.groq',
100
+ // eslint-disable-next-line no-div-regex
101
+ regex: /==|!=|>=|<=|<!=>|<|>/,
102
+ },
103
+ ],
104
+ '#operator': [
105
+ {
106
+ token: 'keyword.operator.arithmetic.groq',
107
+ regex: /\+|-|\*{1,2}|\/|%/,
108
+ },
109
+ ],
110
+ '#pipe': [
111
+ {
112
+ token: 'keyword.operator.pipe.groq',
113
+ regex: /\|/,
114
+ },
115
+ ],
116
+ '#logical': [
117
+ {
118
+ token: 'keyword.operator.logical.groq',
119
+ regex: /!|&&|\|\|/,
120
+ },
121
+ ],
122
+ '#reference': [
123
+ {
124
+ token: 'keyword.operator.reference.groq',
125
+ regex: /->/,
126
+ },
127
+ ],
128
+ '#pair': [
129
+ {
130
+ include: '#identifier',
131
+ },
132
+ {
133
+ include: '#value',
134
+ },
135
+ {
136
+ include: '#filter',
137
+ },
138
+ {
139
+ token: 'keyword.operator.pair.groq',
140
+ regex: /[=]>/,
141
+ },
142
+ ],
143
+ '#arraylike': [
144
+ {
145
+ token: 'punctuation.definition.bracket.begin.groq',
146
+ regex: /\[/,
147
+ push: [
148
+ {
149
+ token: ['text', 'keyword.operator.descendant.groq'],
150
+ regex: /(\])((?:\s*\.)?)/,
151
+ next: 'pop',
152
+ },
153
+ {
154
+ include: '#range',
155
+ },
156
+ {
157
+ include: '#filter',
158
+ },
159
+ {
160
+ include: '#array-values',
161
+ },
162
+ ],
163
+ },
164
+ ],
165
+ '#array': [
166
+ {
167
+ token: 'punctuation.definition.bracket.begin.groq',
168
+ regex: /\[/,
169
+ push: [
170
+ {
171
+ token: 'punctuation.definition.bracket.end.groq',
172
+ regex: /\]/,
173
+ next: 'pop',
174
+ },
175
+ {
176
+ include: '#array-values',
177
+ },
178
+ {
179
+ defaultToken: 'meta.structure.array.groq',
180
+ },
181
+ ],
182
+ },
183
+ ],
184
+ '#range': [
185
+ {
186
+ token: [
187
+ 'meta.structure.range.groq',
188
+ 'constant.numeric.groq',
189
+ 'meta.structure.range.groq',
190
+ 'keyword.operator.range.groq',
191
+ 'meta.structure.range.groq',
192
+ 'constant.numeric.groq',
193
+ 'meta.structure.range.groq',
194
+ ],
195
+ regex: /(\s*)(\d+)(\s*)(\.{2,3})(\s*)(\d+)(\s*)/,
196
+ },
197
+ ],
198
+ '#spread': [
199
+ {
200
+ token: 'punctuation.definition.spread.begin.groq',
201
+ regex: /\.\.\./,
202
+ push: [
203
+ {
204
+ include: '#array',
205
+ },
206
+ {
207
+ include: '#function-call',
208
+ },
209
+ {
210
+ include: '#projection',
211
+ },
212
+ {
213
+ token: 'punctuation.definition.spread.end.groq',
214
+ regex: /(?=.)/,
215
+ next: 'pop',
216
+ },
217
+ {
218
+ defaultToken: 'meta.structure.spread.groq',
219
+ },
220
+ ],
221
+ },
222
+ ],
223
+ '#array-values': [
224
+ {
225
+ include: '#value',
226
+ },
227
+ {
228
+ include: '#spread',
229
+ },
230
+ {
231
+ token: 'punctuation.separator.array.groq',
232
+ regex: /,/,
233
+ },
234
+ {
235
+ token: 'invalid.illegal.expected-array-separator.groq',
236
+ regex: /[^\s\]]/,
237
+ },
238
+ ],
239
+ '#filter': [
240
+ {
241
+ include: '#function-call',
242
+ },
243
+ {
244
+ include: '#keyword',
245
+ },
246
+ {
247
+ include: '#constant',
248
+ },
249
+ {
250
+ include: '#identifier',
251
+ },
252
+ {
253
+ include: '#value',
254
+ },
255
+ {
256
+ include: '#comparison',
257
+ },
258
+ {
259
+ include: '#operator',
260
+ },
261
+ {
262
+ include: '#logical',
263
+ },
264
+ ],
265
+ '#comments': [
266
+ {
267
+ token: ['punctuation.definition.comment.groq', 'comment.line.double-slash.js'],
268
+ regex: /(\/\/)(.*$)/,
269
+ },
270
+ ],
271
+ '#nullary-access-operator': [
272
+ {
273
+ token: 'constant.language.groq',
274
+ regex: /[*@^]/,
275
+ },
276
+ ],
277
+ '#constant': [
278
+ {
279
+ token: 'constant.language.groq',
280
+ regex: /\b(?:true|false|null)\b/,
281
+ },
282
+ ],
283
+ '#number': [
284
+ {
285
+ token: 'constant.numeric.groq',
286
+ regex: /-?(?:0|[1-9]\d*)(?:(?:\.\d+)?(?:[eE][+-]?\d+)?)?/,
287
+ },
288
+ ],
289
+ '#named-projection': [
290
+ {
291
+ include: '#identifier',
292
+ },
293
+ {
294
+ include: '#objectkey',
295
+ },
296
+ {
297
+ include: '#projection',
298
+ },
299
+ ],
300
+ '#projection': [
301
+ {
302
+ token: 'punctuation.definition.projection.begin.groq',
303
+ regex: /\{/,
304
+ push: [
305
+ {
306
+ token: 'punctuation.definition.projection.end.groq',
307
+ regex: /\}/,
308
+ next: 'pop',
309
+ },
310
+ {
311
+ include: '#identifier',
312
+ },
313
+ {
314
+ include: '#objectkey',
315
+ },
316
+ {
317
+ include: '#named-projection',
318
+ },
319
+ {
320
+ include: '#comments',
321
+ },
322
+ {
323
+ include: '#spread',
324
+ },
325
+ {
326
+ include: '#pair',
327
+ },
328
+ {
329
+ token: 'punctuation.separator.projection.key-value.groq',
330
+ regex: /:/,
331
+ push: [
332
+ {
333
+ token: 'punctuation.separator.projection.pair.groq',
334
+ regex: /,|(?=\})/,
335
+ next: 'pop',
336
+ },
337
+ {
338
+ include: '#nullary-access-operator',
339
+ },
340
+ {
341
+ include: '#arraylike',
342
+ },
343
+ {
344
+ include: '#value',
345
+ },
346
+ {
347
+ include: '#spread',
348
+ },
349
+ {
350
+ include: '#identifier',
351
+ },
352
+ {
353
+ include: '#operator',
354
+ },
355
+ {
356
+ include: '#comparison',
357
+ },
358
+ {
359
+ include: '#pair',
360
+ },
361
+ {
362
+ token: 'invalid.illegal.expected-projection-separator.groq',
363
+ regex: /[^\s,]/,
364
+ },
365
+ {
366
+ defaultToken: 'meta.structure.projection.value.groq',
367
+ },
368
+ ],
369
+ },
370
+ {
371
+ token: 'invalid.illegal.expected-projection-separator.groq',
372
+ regex: /[^\s},]/,
373
+ },
374
+ {
375
+ defaultToken: 'meta.structure.projection.groq',
376
+ },
377
+ ],
378
+ },
379
+ ],
380
+ '#string': [
381
+ {
382
+ include: '#single-string',
383
+ },
384
+ {
385
+ include: '#double-string',
386
+ },
387
+ ],
388
+ '#double-string': [
389
+ {
390
+ token: 'punctuation.definition.string.begin.groq',
391
+ regex: /"/,
392
+ push: [
393
+ {
394
+ token: 'punctuation.definition.string.end.groq',
395
+ regex: /"/,
396
+ next: 'pop',
397
+ },
398
+ {
399
+ include: '#stringcontent',
400
+ },
401
+ {
402
+ defaultToken: 'string.quoted.double.groq',
403
+ },
404
+ ],
405
+ },
406
+ ],
407
+ '#single-string': [
408
+ {
409
+ token: 'punctuation.definition.string.single.begin.groq',
410
+ regex: /'/,
411
+ push: [
412
+ {
413
+ token: 'punctuation.definition.string.single.end.groq',
414
+ regex: /'/,
415
+ next: 'pop',
416
+ },
417
+ {
418
+ include: '#stringcontent',
419
+ },
420
+ {
421
+ defaultToken: 'string.quoted.single.groq',
422
+ },
423
+ ],
424
+ },
425
+ ],
426
+ '#objectkey': [
427
+ {
428
+ include: '#string',
429
+ },
430
+ ],
431
+ '#stringcontent': [
432
+ {
433
+ token: 'constant.character.escape.groq',
434
+ regex: /\\(?:["\\/bfnrt]|u[0-9a-fA-F]{4})/,
435
+ },
436
+ {
437
+ token: 'invalid.illegal.unrecognized-string-escape.groq',
438
+ regex: /\\./,
439
+ },
440
+ ],
441
+ '#sort-pair': [
442
+ {
443
+ token: ['variable.other.readwrite.groq', 'text', 'keyword.other.groq'],
444
+ regex: /([_A-Za-z][_0-9A-Za-z]*)(?:(\s*)(asc|desc))?/,
445
+ },
446
+ {
447
+ token: ['constant.language.groq', 'punctuation.definition.bracket.begin.groq'],
448
+ regex: /(@)(\[)/,
449
+ push: [
450
+ {
451
+ token: ['punctuation.definition.bracket.begin.groq', 'text', 'keyword.other.groq'],
452
+ regex: /(\])(?:(\s*)(asc|desc))?/,
453
+ next: 'pop',
454
+ },
455
+ {
456
+ include: '#string',
457
+ },
458
+ ],
459
+ },
460
+ ],
461
+ '#sort-order': [
462
+ {
463
+ token: 'support.function.sortorder.begin.groq',
464
+ regex: /\border\s*\(/,
465
+ push: [
466
+ {
467
+ token: 'support.function.sortorder.end.groq',
468
+ regex: /\)/,
469
+ next: 'pop',
470
+ },
471
+ {
472
+ include: '#sort-pair',
473
+ },
474
+ {
475
+ token: 'punctuation.separator.array.groq',
476
+ regex: /,/,
477
+ },
478
+ {
479
+ token: 'invalid.illegal.expected-sort-separator.groq',
480
+ regex: /[^\s\]]/,
481
+ },
482
+ {
483
+ defaultToken: 'support.function.sortorder.groq',
484
+ },
485
+ ],
486
+ },
487
+ ],
488
+ '#function-call': [
489
+ {
490
+ include: '#function-var-arg',
491
+ },
492
+ {
493
+ include: '#function-single-arg',
494
+ },
495
+ {
496
+ include: '#function-round',
497
+ },
498
+ ],
499
+ '#function-var-arg': [
500
+ {
501
+ token: 'support.function.vararg.begin.groq',
502
+ regex: /\b(?:coalesce|select)\s*\(/,
503
+ push: [
504
+ {
505
+ token: 'support.function.vararg.end.groq',
506
+ regex: /\)/,
507
+ next: 'pop',
508
+ },
509
+ {
510
+ include: '#value',
511
+ },
512
+ {
513
+ include: '#identifier',
514
+ },
515
+ {
516
+ include: '#filter',
517
+ },
518
+ {
519
+ include: '#pair',
520
+ },
521
+ {
522
+ token: 'punctuation.separator.array.groq',
523
+ regex: /,/,
524
+ },
525
+ {
526
+ defaultToken: 'support.function.vararg.groq',
527
+ },
528
+ ],
529
+ },
530
+ ],
531
+ '#function-single-arg': [
532
+ {
533
+ token: 'support.function.singlearg.begin.groq',
534
+ regex: /\b(?:count|defined|length|path|references)\s*\(/,
535
+ push: [
536
+ {
537
+ token: 'support.function.singlearg.end.groq',
538
+ regex: /\)/,
539
+ next: 'pop',
540
+ },
541
+ {
542
+ include: '#query',
543
+ },
544
+ {
545
+ include: '#identifier',
546
+ },
547
+ {
548
+ include: '#value',
549
+ },
550
+ {
551
+ include: '#pair',
552
+ },
553
+ {
554
+ defaultToken: 'support.function.singlearg.groq',
555
+ },
556
+ ],
557
+ },
558
+ ],
559
+ '#identifier': [
560
+ {
561
+ token: [
562
+ 'variable.other.readwrite.groq',
563
+ 'text',
564
+ 'punctuation.definition.block.js',
565
+ 'text',
566
+ 'keyword.operator.reference.groq',
567
+ ],
568
+ regex: /([_A-Za-z][_0-9A-Za-z]*)(\s*)((?:\[\s*\])?)(\s*)(->)/,
569
+ },
570
+ {
571
+ token: [
572
+ 'variable.other.readwrite.groq',
573
+ 'constant.language.groq',
574
+ 'text',
575
+ 'punctuation.definition.block.js',
576
+ 'text',
577
+ 'keyword.operator.descendant.groq',
578
+ ],
579
+ regex: /(?:([_A-Za-z][_0-9A-Za-z]*)|([@^]))(\s*)((?:\[\s*\])?)(\s*)(\.)/,
580
+ },
581
+ {
582
+ token: 'variable.other.readwrite.groq',
583
+ regex: /[_A-Za-z][_0-9A-Za-z]*/,
584
+ },
585
+ ],
586
+ '#value': [
587
+ {
588
+ include: '#constant',
589
+ },
590
+ {
591
+ include: '#number',
592
+ },
593
+ {
594
+ include: '#string',
595
+ },
596
+ {
597
+ include: '#array',
598
+ },
599
+ {
600
+ include: '#variable',
601
+ },
602
+ {
603
+ include: '#projection',
604
+ },
605
+ {
606
+ include: '#comments',
607
+ },
608
+ {
609
+ include: '#function-call',
610
+ },
611
+ ],
612
+ };
613
+ ace.define('ace/mode/groq_highlight_rules', ['require', 'exports', 'module', 'ace/lib/oop', 'ace/mode/text_highlight_rules'], (acequire, exports, _module) => {
614
+ const oop = acequire('../lib/oop');
615
+ const TextHighlightRules = acequire('./text_highlight_rules').TextHighlightRules;
616
+ const GroqHighlightRules = function () {
617
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
618
+ // @ts-ignore
619
+ this.$rules = rules;
620
+ // @ts-ignore
621
+ this.normalizeRules();
622
+ /* eslint-enable @typescript-eslint/ban-ts-comment */
623
+ };
624
+ oop.inherits(GroqHighlightRules, TextHighlightRules);
625
+ exports.GroqHighlightRules = GroqHighlightRules;
626
+ });
627
+ ace.define('ace/mode/groq', [
628
+ 'require',
629
+ 'exports',
630
+ 'module',
631
+ 'ace/lib/oop',
632
+ 'ace/mode/text',
633
+ 'ace/tokenizer',
634
+ 'ace/mode/groq_highlight_rules',
635
+ 'ace/mode/folding/cstyle',
636
+ ], (acequire, exports, _module) => {
637
+ const oop = acequire('../lib/oop');
638
+ const TextMode = acequire('./text').Mode;
639
+ const Tokenizer = acequire('../tokenizer').Tokenizer;
640
+ const GroqHighlightRules = acequire('./groq_highlight_rules').GroqHighlightRules;
641
+ const FoldMode = acequire('./folding/cstyle').FoldMode;
642
+ const Mode = function () {
643
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
644
+ const highlighter = new GroqHighlightRules();
645
+ // @ts-ignore
646
+ this.foldingRules = new FoldMode();
647
+ // @ts-ignore
648
+ this.$tokenizer = new Tokenizer(highlighter.getRules());
649
+ // @ts-ignore
650
+ this.$keywordList = highlighter.$keywordList;
651
+ /* eslint-enable @typescript-eslint/ban-ts-comment */
652
+ };
653
+ oop.inherits(Mode, TextMode);
654
+ (function () {
655
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
656
+ // @ts-ignore
657
+ this.lineCommentStart = "'";
658
+ /* eslint-enable @typescript-eslint/ban-ts-comment */
659
+ }.call(Mode.prototype));
660
+ exports.Mode = Mode;
661
+ });
662
+
663
+ // NOTE: MAKE SURE THESE ALIGN WITH IMPORTS IN ./editorSupport
664
+ const SUPPORTED_LANGUAGES = [
665
+ { title: 'Batch file', value: 'batchfile' },
666
+ { title: 'C#', value: 'csharp' },
667
+ { title: 'CSS', value: 'css' },
668
+ { title: 'Go', value: 'golang' },
669
+ { title: 'GROQ', value: 'groq' },
670
+ { title: 'HTML', value: 'html' },
671
+ { title: 'Java', value: 'java' },
672
+ { title: 'JavaScript', value: 'javascript' },
673
+ { title: 'JSON', value: 'json' },
674
+ { title: 'JSX', value: 'jsx' },
675
+ { title: 'Markdown', value: 'markdown' },
676
+ { title: 'MySQL', value: 'mysql' },
677
+ { title: 'PHP', value: 'php' },
678
+ { title: 'Plain text', value: 'text' },
679
+ { title: 'Python', value: 'python' },
680
+ { title: 'Ruby', value: 'ruby' },
681
+ { title: 'SASS', value: 'sass' },
682
+ { title: 'SCSS', value: 'scss' },
683
+ { title: 'sh', value: 'sh' },
684
+ { title: 'TSX', value: 'tsx' },
685
+ { title: 'TypeScript', value: 'typescript' },
686
+ { title: 'XML', value: 'xml' },
687
+ { title: 'YAML', value: 'yaml' },
688
+ ];
689
+ const LANGUAGE_ALIASES = { js: 'javascript' };
690
+ const SUPPORTED_THEMES = ['github', 'monokai', 'terminal', 'tomorrow'];
691
+ const DEFAULT_THEME = 'tomorrow';
692
+ const ACE_SET_OPTIONS = {
693
+ useSoftTabs: true,
694
+ navigateWithinSoftTabs: true /* note only supported by ace v1.2.7 or higher */,
695
+ };
696
+ const ACE_EDITOR_PROPS = { $blockScrolling: true };
697
+ const PATH_CODE = ['code'];
698
+
699
+ /* eslint-disable react/jsx-handler-names */
700
+ const EditorContainer = styled(Card) `
701
+ position: relative;
702
+ box-sizing: border-box;
703
+ overflow: hidden;
704
+ z-index: 0;
705
+
706
+ .ace_editor {
707
+ font-family: ${({ theme }) => theme.sanity.fonts.code.family};
708
+ font-size: ${({ theme }) => theme.sanity.fonts.code.sizes[1]};
709
+ line-height: inherit;
710
+ }
711
+
712
+ ${highlightMarkersCSS}
713
+
714
+ &:not([disabled]):not([readonly]) {
715
+ &:focus,
716
+ &:focus-within {
717
+ box-shadow: 0 0 0 2px ${({ theme }) => theme.sanity.color.base.focusRing};
718
+ background-color: ${({ theme }) => theme.sanity.color.base.bg};
719
+ border-color: ${({ theme }) => theme.sanity.color.base.focusRing};
720
+ }
721
+ }
722
+ `;
723
+ // Returns a string with the mode name if supported (because aliases), otherwise false
724
+ function isSupportedLanguage(mode) {
725
+ const alias = LANGUAGE_ALIASES[mode];
726
+ if (alias) {
727
+ return alias;
728
+ }
729
+ const isSupported = SUPPORTED_LANGUAGES.find((lang) => lang.value === mode);
730
+ if (isSupported) {
731
+ return mode;
732
+ }
733
+ return false;
734
+ }
735
+ function CodeInput(props) {
736
+ const { focusRef, members, onBlur, onChange, onFocusPath, readOnly, renderField, renderInput, renderItem, renderPreview, schemaType: type, value, } = props;
737
+ const aceEditorRef = useRef();
738
+ const fieldMembers = useMemo(
739
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
740
+ // @ts-ignore
741
+ () => members.filter((member) => member.kind === 'field'), [members]);
742
+ const languageFieldMember = fieldMembers.find((member) => member.name === 'language');
743
+ const filenameMember = fieldMembers.find((member) => member.name === 'filename');
744
+ const codeFieldMember = fieldMembers.find((member) => member.name === 'code');
745
+ useImperativeHandle(focusRef, () => ({
746
+ focus: () => {
747
+ aceEditorRef?.current?.editor?.focus();
748
+ },
749
+ }));
750
+ const handleCodeFocus = useCallback(() => {
751
+ onFocusPath(PATH_CODE);
752
+ }, [onFocusPath]);
753
+ const getTheme = useCallback(() => {
754
+ const preferredTheme = type.options?.theme;
755
+ return preferredTheme && SUPPORTED_THEMES.find((theme) => theme === preferredTheme)
756
+ ? preferredTheme
757
+ : DEFAULT_THEME;
758
+ }, [type]);
759
+ const handleToggleSelectLine = useCallback((lineNumber) => {
760
+ const editorSession = aceEditorRef.current?.editor?.getSession();
761
+ const backgroundMarkers = editorSession?.getMarkers(true);
762
+ const currentHighlightedLines = Object.keys(backgroundMarkers)
763
+ .filter((key) => backgroundMarkers[key].type === 'screenLine')
764
+ .map((key) => backgroundMarkers[key].range.start.row);
765
+ const currentIndex = currentHighlightedLines.indexOf(lineNumber);
766
+ if (currentIndex > -1) {
767
+ // toggle remove
768
+ currentHighlightedLines.splice(currentIndex, 1);
769
+ }
770
+ else {
771
+ // toggle add
772
+ currentHighlightedLines.push(lineNumber);
773
+ currentHighlightedLines.sort();
774
+ }
775
+ onChange(set(currentHighlightedLines.map((line) =>
776
+ // ace starts at line (row) 0, but we store it starting at line 1
777
+ line + 1), ['highlightedLines']));
778
+ }, [aceEditorRef, onChange]);
779
+ const handleGutterMouseDown = useCallback((event) => {
780
+ const target = event.domEvent.target;
781
+ if (target.classList.contains('ace_gutter-cell')) {
782
+ const row = event.getDocumentPosition().row;
783
+ handleToggleSelectLine(row);
784
+ }
785
+ }, [handleToggleSelectLine]);
786
+ useEffect(() => {
787
+ const editor = aceEditorRef?.current?.editor;
788
+ return () => {
789
+ editor?.session?.removeListener('guttermousedown', handleGutterMouseDown);
790
+ };
791
+ }, [aceEditorRef, handleGutterMouseDown]);
792
+ const handleEditorLoad = useCallback((editor) => {
793
+ editor?.on('guttermousedown', handleGutterMouseDown);
794
+ }, [handleGutterMouseDown]);
795
+ const getLanguageAlternatives = useCallback(() => {
796
+ const languageAlternatives = type.options?.languageAlternatives;
797
+ if (!languageAlternatives) {
798
+ return SUPPORTED_LANGUAGES;
799
+ }
800
+ if (!Array.isArray(languageAlternatives)) {
801
+ throw new Error(`'options.languageAlternatives' should be an array, got ${typeof languageAlternatives}`);
802
+ }
803
+ return languageAlternatives.reduce((acc, { title, value: val, mode }) => {
804
+ const alias = LANGUAGE_ALIASES[val];
805
+ if (alias) {
806
+ // eslint-disable-next-line no-console
807
+ console.warn(`'options.languageAlternatives' lists a language with value "%s", which is an alias of "%s" - please replace the value to read "%s"`, val, alias, alias);
808
+ return acc.concat({ title, value: alias, mode: mode });
809
+ }
810
+ if (!mode && !SUPPORTED_LANGUAGES.find((lang) => lang.value === val)) {
811
+ // eslint-disable-next-line no-console
812
+ console.warn(`'options.languageAlternatives' lists a language which is not supported: "%s", syntax highlighting will be disabled.`, val);
813
+ }
814
+ return acc.concat({ title, value: val, mode });
815
+ }, []);
816
+ }, [type]);
817
+ const handleCodeChange = useCallback((code) => {
818
+ const path = PATH_CODE;
819
+ const fixedLanguage = type.options?.language;
820
+ onChange([
821
+ setIfMissing({ _type: type.name, language: fixedLanguage }),
822
+ code ? set(code, path) : unset(path),
823
+ ]);
824
+ }, [onChange, type]);
825
+ const languages = getLanguageAlternatives().slice();
826
+ const fixedLanguage = type.options?.language;
827
+ const language = value?.language || fixedLanguage;
828
+ // the language config from the schema
829
+ const configured = languages.find((entry) => entry.value === language);
830
+ // is the language officially supported (e.g. we import the mode by default)
831
+ const supported = language && isSupportedLanguage(language);
832
+ const mode = configured?.mode || (supported ? language : 'text');
833
+ const renderLanguageInput = useCallback((inputProps) => {
834
+ return (React.createElement(Select, { ...inputProps }, languages.map((lang) => (React.createElement("option", { key: lang.value, value: lang.value }, lang.title)))));
835
+ }, [languages]);
836
+ const renderCodeInput = useCallback((inputProps) => {
837
+ return (React.createElement(EditorContainer, { radius: 1, shadow: 1, readOnly: readOnly },
838
+ React.createElement(AceEditor, { ref: aceEditorRef, mode: mode, theme: getTheme(), width: "100%", onChange: handleCodeChange, name: inputProps.id, value: inputProps.value, markers: value && value.highlightedLines
839
+ ? createHighlightMarkers(value.highlightedLines)
840
+ : undefined, onLoad: handleEditorLoad, readOnly: readOnly, tabSize: 2, wrapEnabled: true, setOptions: ACE_SET_OPTIONS, editorProps: ACE_EDITOR_PROPS, onFocus: handleCodeFocus, onBlur: onBlur })));
841
+ }, [getTheme, handleCodeChange, handleCodeFocus, handleEditorLoad, mode, onBlur, readOnly, value]);
842
+ return (React.createElement(Stack, { space: 4 },
843
+ languageFieldMember && (React.createElement(MemberField, { member: languageFieldMember, renderItem: renderItem, renderField: renderField, renderInput: renderLanguageInput, renderPreview: renderPreview })),
844
+ type.options?.withFilename && filenameMember && (React.createElement(MemberField, { member: filenameMember, renderItem: renderItem, renderField: renderField, renderInput: renderInput, renderPreview: renderPreview })),
845
+ codeFieldMember && (React.createElement(MemberField, { member: codeFieldMember, renderInput: renderCodeInput, renderItem: renderItem, renderField: renderField, renderPreview: renderPreview }))));
846
+ }
847
+
848
+ export { ACE_SET_OPTIONS as A, CodeInput as C, ACE_EDITOR_PROPS as a, createHighlightMarkers as c };
849
+ //# sourceMappingURL=_CodeInput-af3045a8.js.map