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

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