@sanity/code-input 2.29.5-purple-unicorn.856 → 2.29.5-purple-unicorn-remix.873

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