@sanity/code-input 5.0.0 → 5.1.1

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.
Files changed (52) hide show
  1. package/lib/_chunks-cjs/CodeMirrorProxy.cjs +429 -0
  2. package/lib/_chunks-cjs/CodeMirrorProxy.cjs.map +1 -0
  3. package/lib/_chunks-cjs/index.cjs +463 -0
  4. package/lib/_chunks-cjs/index.cjs.map +1 -0
  5. package/lib/_chunks-es/CodeMirrorProxy.js +417 -0
  6. package/lib/_chunks-es/CodeMirrorProxy.js.map +1 -0
  7. package/lib/_chunks-es/index.js +463 -0
  8. package/lib/_chunks-es/index.js.map +1 -0
  9. package/lib/_legacy/CodeMirrorProxy.esm.js +417 -0
  10. package/lib/_legacy/CodeMirrorProxy.esm.js.map +1 -0
  11. package/lib/_legacy/index.esm.js +463 -0
  12. package/lib/_legacy/index.esm.js.map +1 -0
  13. package/lib/index.cjs +3 -6
  14. package/lib/index.cjs.map +1 -1
  15. package/lib/index.d.cts +121 -0
  16. package/lib/index.d.ts +2 -2
  17. package/lib/index.esm.js +8 -0
  18. package/lib/index.esm.js.map +1 -0
  19. package/lib/index.js +7 -1
  20. package/lib/index.js.map +1 -1
  21. package/package.json +38 -43
  22. package/src/CodeInput.tsx +4 -3
  23. package/src/LanguageField.tsx +2 -1
  24. package/src/LanguageInput.tsx +3 -2
  25. package/src/PreviewCode.tsx +5 -4
  26. package/src/codemirror/CodeMirrorProxy.tsx +7 -6
  27. package/src/codemirror/CodeModeContext.tsx +2 -1
  28. package/src/codemirror/defaultCodeModes.ts +1 -1
  29. package/src/codemirror/extensions/backwardsCompatibleTone.ts +19 -0
  30. package/src/codemirror/extensions/highlightLineExtension.ts +12 -6
  31. package/src/codemirror/extensions/theme.ts +8 -4
  32. package/src/codemirror/extensions/useCodeMirrorTheme.ts +5 -4
  33. package/src/codemirror/extensions/useFontSize.ts +1 -1
  34. package/src/codemirror/useCodeMirror-client.test.tsx +10 -7
  35. package/src/codemirror/useCodeMirror-server.test.tsx +1 -0
  36. package/src/codemirror/useLanguageMode.tsx +11 -3
  37. package/src/index.ts +4 -7
  38. package/src/plugin.tsx +3 -2
  39. package/src/schema.tsx +3 -2
  40. package/lib/_chunks/CodeMirrorProxy-A8RWnbXx.js +0 -615
  41. package/lib/_chunks/CodeMirrorProxy-A8RWnbXx.js.map +0 -1
  42. package/lib/_chunks/CodeMirrorProxy-tci8hAnf.cjs +0 -623
  43. package/lib/_chunks/CodeMirrorProxy-tci8hAnf.cjs.map +0 -1
  44. package/lib/_chunks/index-0LQQ9tED.cjs +0 -4686
  45. package/lib/_chunks/index-0LQQ9tED.cjs.map +0 -1
  46. package/lib/_chunks/index-4cT0QYPN.js +0 -4672
  47. package/lib/_chunks/index-4cT0QYPN.js.map +0 -1
  48. package/lib/index.cjs.js +0 -7
  49. package/src/__workshop__/index.ts +0 -22
  50. package/src/__workshop__/lazy.tsx +0 -54
  51. package/src/__workshop__/preview.tsx +0 -24
  52. package/src/__workshop__/props.tsx +0 -24
@@ -1,615 +0,0 @@
1
- import { jsx } from 'react/jsx-runtime';
2
- import { useMemo, forwardRef, useState, useEffect, useCallback, useContext } from 'react';
3
- import CodeMirror from '@uiw/react-codemirror';
4
- import { useTheme, rgba, rem, useRootTheme } from '@sanity/ui';
5
- import { createTheme } from '@uiw/codemirror-themes';
6
- import { tags } from '@lezer/highlight';
7
- import { CodeInputConfigContext } from './index-4cT0QYPN.js';
8
- import { StreamLanguage } from '@codemirror/language';
9
- import { StateEffect, StateField } from '@codemirror/state';
10
- import { Decoration, EditorView, lineNumbers } from '@codemirror/view';
11
- function useCodeMirrorTheme() {
12
- const theme = useTheme();
13
- return useMemo(() => {
14
- const {
15
- code: codeFont
16
- } = theme.sanity.fonts;
17
- const {
18
- base,
19
- card,
20
- dark,
21
- syntax
22
- } = theme.sanity.color;
23
- return createTheme({
24
- theme: dark ? "dark" : "light",
25
- settings: {
26
- background: card.enabled.bg,
27
- foreground: card.enabled.code.fg,
28
- lineHighlight: card.enabled.bg,
29
- fontFamily: codeFont.family,
30
- caret: base.focusRing,
31
- selection: rgba(base.focusRing, 0.2),
32
- selectionMatch: rgba(base.focusRing, 0.4),
33
- gutterBackground: card.disabled.bg,
34
- gutterForeground: card.disabled.code.fg,
35
- gutterActiveForeground: card.enabled.fg
36
- },
37
- styles: [{
38
- tag: [tags.heading, tags.heading2, tags.heading3, tags.heading4, tags.heading5, tags.heading6],
39
- color: card.enabled.fg
40
- }, {
41
- tag: tags.angleBracket,
42
- color: card.enabled.code.fg
43
- }, {
44
- tag: tags.atom,
45
- color: syntax.keyword
46
- }, {
47
- tag: tags.attributeName,
48
- color: syntax.attrName
49
- }, {
50
- tag: tags.bool,
51
- color: syntax.boolean
52
- }, {
53
- tag: tags.bracket,
54
- color: card.enabled.code.fg
55
- }, {
56
- tag: tags.className,
57
- color: syntax.className
58
- }, {
59
- tag: tags.comment,
60
- color: syntax.comment
61
- }, {
62
- tag: tags.definition(tags.typeName),
63
- color: syntax.function
64
- }, {
65
- tag: [tags.definition(tags.variableName), tags.function(tags.variableName), tags.className, tags.attributeName],
66
- color: syntax.function
67
- }, {
68
- tag: [tags.function(tags.propertyName), tags.propertyName],
69
- color: syntax.function
70
- }, {
71
- tag: tags.keyword,
72
- color: syntax.keyword
73
- }, {
74
- tag: tags.null,
75
- color: syntax.number
76
- }, {
77
- tag: tags.number,
78
- color: syntax.number
79
- }, {
80
- tag: tags.meta,
81
- color: card.enabled.code.fg
82
- }, {
83
- tag: tags.operator,
84
- color: syntax.operator
85
- }, {
86
- tag: tags.propertyName,
87
- color: syntax.property
88
- }, {
89
- tag: [tags.string, tags.special(tags.brace)],
90
- color: syntax.string
91
- }, {
92
- tag: tags.tagName,
93
- color: syntax.className
94
- }, {
95
- tag: tags.typeName,
96
- color: syntax.keyword
97
- }]
98
- });
99
- }, [theme]);
100
- }
101
- const defaultCodeModes = [{
102
- name: "groq",
103
- loader: () => import('@codemirror/lang-javascript').then(_ref => {
104
- let {
105
- javascriptLanguage
106
- } = _ref;
107
- return javascriptLanguage;
108
- })
109
- }, {
110
- name: "javascript",
111
- loader: () => import('@codemirror/lang-javascript').then(_ref2 => {
112
- let {
113
- javascript
114
- } = _ref2;
115
- return javascript({
116
- jsx: false
117
- });
118
- })
119
- }, {
120
- name: "jsx",
121
- loader: () => import('@codemirror/lang-javascript').then(_ref3 => {
122
- let {
123
- javascript
124
- } = _ref3;
125
- return javascript({
126
- jsx: true
127
- });
128
- })
129
- }, {
130
- name: "typescript",
131
- loader: () => import('@codemirror/lang-javascript').then(_ref4 => {
132
- let {
133
- javascript
134
- } = _ref4;
135
- return javascript({
136
- jsx: false,
137
- typescript: true
138
- });
139
- })
140
- }, {
141
- name: "tsx",
142
- loader: () => import('@codemirror/lang-javascript').then(_ref5 => {
143
- let {
144
- javascript
145
- } = _ref5;
146
- return javascript({
147
- jsx: true,
148
- typescript: true
149
- });
150
- })
151
- }, {
152
- name: "php",
153
- loader: () => import('@codemirror/lang-php').then(_ref6 => {
154
- let {
155
- php
156
- } = _ref6;
157
- return php();
158
- })
159
- }, {
160
- name: "sql",
161
- loader: () => import('@codemirror/lang-sql').then(_ref7 => {
162
- let {
163
- sql
164
- } = _ref7;
165
- return sql();
166
- })
167
- }, {
168
- name: "mysql",
169
- loader: () => import('@codemirror/lang-sql').then(_ref8 => {
170
- let {
171
- sql,
172
- MySQL
173
- } = _ref8;
174
- return sql({
175
- dialect: MySQL
176
- });
177
- })
178
- }, {
179
- name: "json",
180
- loader: () => import('@codemirror/lang-json').then(_ref9 => {
181
- let {
182
- json
183
- } = _ref9;
184
- return json();
185
- })
186
- }, {
187
- name: "markdown",
188
- loader: () => import('@codemirror/lang-markdown').then(_ref10 => {
189
- let {
190
- markdown
191
- } = _ref10;
192
- return markdown();
193
- })
194
- }, {
195
- name: "java",
196
- loader: () => import('@codemirror/lang-java').then(_ref11 => {
197
- let {
198
- java
199
- } = _ref11;
200
- return java();
201
- })
202
- }, {
203
- name: "html",
204
- loader: () => import('@codemirror/lang-html').then(_ref12 => {
205
- let {
206
- html
207
- } = _ref12;
208
- return html();
209
- })
210
- }, {
211
- name: "csharp",
212
- loader: () => import('@codemirror/legacy-modes/mode/clike').then(_ref13 => {
213
- let {
214
- csharp
215
- } = _ref13;
216
- return StreamLanguage.define(csharp);
217
- })
218
- }, {
219
- name: "sh",
220
- loader: () => import('@codemirror/legacy-modes/mode/shell').then(_ref14 => {
221
- let {
222
- shell
223
- } = _ref14;
224
- return StreamLanguage.define(shell);
225
- })
226
- }, {
227
- name: "css",
228
- loader: () => import('@codemirror/legacy-modes/mode/css').then(_ref15 => {
229
- let {
230
- css
231
- } = _ref15;
232
- return StreamLanguage.define(css);
233
- })
234
- }, {
235
- name: "scss",
236
- loader: () => import('@codemirror/legacy-modes/mode/css').then(_ref16 => {
237
- let {
238
- css
239
- } = _ref16;
240
- return StreamLanguage.define(css);
241
- })
242
- }, {
243
- name: "sass",
244
- loader: () => import('@codemirror/legacy-modes/mode/sass').then(_ref17 => {
245
- let {
246
- sass
247
- } = _ref17;
248
- return StreamLanguage.define(sass);
249
- })
250
- }, {
251
- name: "ruby",
252
- loader: () => import('@codemirror/legacy-modes/mode/ruby').then(_ref18 => {
253
- let {
254
- ruby
255
- } = _ref18;
256
- return StreamLanguage.define(ruby);
257
- })
258
- }, {
259
- name: "python",
260
- loader: () => import('@codemirror/legacy-modes/mode/python').then(_ref19 => {
261
- let {
262
- python
263
- } = _ref19;
264
- return StreamLanguage.define(python);
265
- })
266
- }, {
267
- name: "xml",
268
- loader: () => import('@codemirror/legacy-modes/mode/xml').then(_ref20 => {
269
- let {
270
- xml
271
- } = _ref20;
272
- return StreamLanguage.define(xml);
273
- })
274
- }, {
275
- name: "yaml",
276
- loader: () => import('@codemirror/legacy-modes/mode/yaml').then(_ref21 => {
277
- let {
278
- yaml
279
- } = _ref21;
280
- return StreamLanguage.define(yaml);
281
- })
282
- }, {
283
- name: "golang",
284
- loader: () => import('@codemirror/legacy-modes/mode/go').then(_ref22 => {
285
- let {
286
- go
287
- } = _ref22;
288
- return StreamLanguage.define(go);
289
- })
290
- }, {
291
- name: "text",
292
- loader: () => void 0
293
- }, {
294
- name: "batch",
295
- loader: () => void 0
296
- }];
297
- const highlightLineClass = "cm-highlight-line";
298
- const addLineHighlight = StateEffect.define();
299
- const removeLineHighlight = StateEffect.define();
300
- const lineHighlightField = StateField.define({
301
- create() {
302
- return Decoration.none;
303
- },
304
- update(lines, tr) {
305
- lines = lines.map(tr.changes);
306
- for (const e of tr.effects) {
307
- if (e.is(addLineHighlight)) {
308
- lines = lines.update({
309
- add: [lineHighlightMark.range(e.value)]
310
- });
311
- }
312
- if (e.is(removeLineHighlight)) {
313
- lines = lines.update({
314
- filter: from => {
315
- return from !== e.value;
316
- }
317
- });
318
- }
319
- }
320
- return lines;
321
- },
322
- toJSON(value, state) {
323
- const highlightLines = [];
324
- const iter = value.iter();
325
- while (iter.value) {
326
- const lineNumber = state.doc.lineAt(iter.from).number;
327
- if (!highlightLines.includes(lineNumber)) {
328
- highlightLines.push(lineNumber);
329
- }
330
- iter.next();
331
- }
332
- return highlightLines;
333
- },
334
- fromJSON(value, state) {
335
- const lines = state.doc.lines;
336
- const highlights = value.filter(line => line <= lines).map(line => lineHighlightMark.range(state.doc.line(line).from));
337
- highlights.sort((a, b) => a.from - b.from);
338
- try {
339
- return Decoration.none.update({
340
- add: highlights
341
- });
342
- } catch (e) {
343
- console.error(e);
344
- return Decoration.none;
345
- }
346
- },
347
- provide: f => EditorView.decorations.from(f)
348
- });
349
- const lineHighlightMark = Decoration.line({
350
- class: highlightLineClass
351
- });
352
- const highlightState = {
353
- highlight: lineHighlightField
354
- };
355
- function createCodeMirrorTheme(options) {
356
- const {
357
- themeCtx
358
- } = options;
359
- const dark = {
360
- color: themeCtx.theme.color.dark[themeCtx.tone]
361
- };
362
- const light = {
363
- color: themeCtx.theme.color.light[themeCtx.tone]
364
- };
365
- return EditorView.baseTheme({
366
- ".cm-lineNumbers": {
367
- cursor: "default"
368
- },
369
- ".cm-line.cm-line": {
370
- position: "relative"
371
- },
372
- // need set background with pseudoelement so it does not render over selection color
373
- [".".concat(highlightLineClass, "::before")]: {
374
- position: "absolute",
375
- top: 0,
376
- bottom: 0,
377
- left: 0,
378
- right: 0,
379
- zIndex: -3,
380
- content: "''",
381
- boxSizing: "border-box"
382
- },
383
- ["&dark .".concat(highlightLineClass, "::before")]: {
384
- background: rgba(dark.color.muted.caution.pressed.bg, 0.5)
385
- },
386
- ["&light .".concat(highlightLineClass, "::before")]: {
387
- background: rgba(light.color.muted.caution.pressed.bg, 0.75)
388
- }
389
- });
390
- }
391
- const highlightLine = config => {
392
- const highlightTheme = createCodeMirrorTheme({
393
- themeCtx: config.theme
394
- });
395
- return [lineHighlightField, config.readOnly ? [] : lineNumbers({
396
- domEventHandlers: {
397
- mousedown: (editorView, lineInfo) => {
398
- const line = editorView.state.doc.lineAt(lineInfo.from);
399
- let isHighlighted = false;
400
- editorView.state.field(lineHighlightField).between(line.from, line.to, (from, to, value) => {
401
- if (value) {
402
- isHighlighted = true;
403
- return false;
404
- }
405
- return void 0;
406
- });
407
- if (isHighlighted) {
408
- editorView.dispatch({
409
- effects: removeLineHighlight.of(line.from)
410
- });
411
- } else {
412
- editorView.dispatch({
413
- effects: addLineHighlight.of(line.from)
414
- });
415
- }
416
- if (config == null ? void 0 : config.onHighlightChange) {
417
- config.onHighlightChange(editorView.state.toJSON(highlightState).highlight);
418
- }
419
- return true;
420
- }
421
- }
422
- }), highlightTheme];
423
- };
424
- function setHighlightedLines(view, highlightLines) {
425
- const doc = view.state.doc;
426
- const lines = doc.lines;
427
- const allLineNumbers = Array.from({
428
- length: lines
429
- }, (x, i) => i + 1);
430
- view.dispatch({
431
- effects: allLineNumbers.map(lineNumber => {
432
- const line = doc.line(lineNumber);
433
- if (highlightLines == null ? void 0 : highlightLines.includes(lineNumber)) {
434
- return addLineHighlight.of(line.from);
435
- }
436
- return removeLineHighlight.of(line.from);
437
- })
438
- });
439
- }
440
- function useFontSizeExtension(props) {
441
- const {
442
- fontSize: fontSizeProp
443
- } = props;
444
- const theme = useTheme();
445
- return useMemo(() => {
446
- const {
447
- code: codeFont
448
- } = theme.sanity.fonts;
449
- const {
450
- fontSize,
451
- lineHeight
452
- } = codeFont.sizes[fontSizeProp] || codeFont.sizes[2];
453
- return EditorView.baseTheme({
454
- "&": {
455
- fontSize: rem(fontSize)
456
- },
457
- "& .cm-scroller": {
458
- lineHeight: "".concat(lineHeight / fontSize, " !important")
459
- }
460
- });
461
- }, [fontSizeProp, theme]);
462
- }
463
- function useThemeExtension() {
464
- const themeCtx = useRootTheme();
465
- return useMemo(() => {
466
- const dark = {
467
- color: themeCtx.theme.color.dark[themeCtx.tone]
468
- };
469
- const light = {
470
- color: themeCtx.theme.color.light[themeCtx.tone]
471
- };
472
- return EditorView.baseTheme({
473
- "&.cm-editor": {
474
- height: "100%"
475
- },
476
- "&.cm-editor.cm-focused": {
477
- outline: "none"
478
- },
479
- // Matching brackets
480
- "&.cm-editor.cm-focused .cm-matchingBracket": {
481
- backgroundColor: "transparent"
482
- },
483
- "&.cm-editor.cm-focused .cm-nonmatchingBracket": {
484
- backgroundColor: "transparent"
485
- },
486
- "&dark.cm-editor.cm-focused .cm-matchingBracket": {
487
- outline: "1px solid ".concat(dark.color.base.border)
488
- },
489
- "&dark.cm-editor.cm-focused .cm-nonmatchingBracket": {
490
- outline: "1px solid ".concat(dark.color.base.border)
491
- },
492
- "&light.cm-editor.cm-focused .cm-matchingBracket": {
493
- outline: "1px solid ".concat(light.color.base.border)
494
- },
495
- "&light.cm-editor.cm-focused .cm-nonmatchingBracket": {
496
- outline: "1px solid ".concat(light.color.base.border)
497
- },
498
- // Size and padding of gutter
499
- "& .cm-lineNumbers .cm-gutterElement": {
500
- minWidth: "32px !important",
501
- padding: "0 8px !important"
502
- },
503
- "& .cm-gutter.cm-foldGutter": {
504
- width: "0px !important"
505
- },
506
- // Color of gutter
507
- "&dark .cm-gutters": {
508
- color: "".concat(rgba(dark.color.card.enabled.code.fg, 0.5), " !important"),
509
- borderRight: "1px solid ".concat(rgba(dark.color.base.border, 0.5))
510
- },
511
- "&light .cm-gutters": {
512
- color: "".concat(rgba(light.color.card.enabled.code.fg, 0.5), " !important"),
513
- borderRight: "1px solid ".concat(rgba(light.color.base.border, 0.5))
514
- }
515
- });
516
- }, [themeCtx]);
517
- }
518
- const CodeMirrorProxy = forwardRef(function CodeMirrorProxy2(props, ref) {
519
- const {
520
- basicSetup: basicSetupProp,
521
- highlightLines,
522
- languageMode,
523
- onHighlightChange,
524
- readOnly,
525
- value,
526
- ...codeMirrorProps
527
- } = props;
528
- const themeCtx = useRootTheme();
529
- const codeMirrorTheme = useCodeMirrorTheme();
530
- const [editorView, setEditorView] = useState(void 0);
531
- const themeExtension = useThemeExtension();
532
- const fontSizeExtension = useFontSizeExtension({
533
- fontSize: 1
534
- });
535
- const languageExtension = useLanguageExtension(languageMode);
536
- const highlightLineExtension = useMemo(() => highlightLine({
537
- onHighlightChange,
538
- readOnly,
539
- theme: themeCtx
540
- }), [onHighlightChange, readOnly, themeCtx]);
541
- const extensions = useMemo(() => {
542
- const baseExtensions = [themeExtension, fontSizeExtension, highlightLineExtension, EditorView.lineWrapping];
543
- if (languageExtension) {
544
- return [...baseExtensions, languageExtension];
545
- }
546
- return baseExtensions;
547
- }, [fontSizeExtension, highlightLineExtension, languageExtension, themeExtension]);
548
- useEffect(() => {
549
- if (editorView) {
550
- setHighlightedLines(editorView, highlightLines != null ? highlightLines : []);
551
- }
552
- }, [editorView, highlightLines, value]);
553
- const initialState = useMemo(() => {
554
- return {
555
- json: {
556
- doc: value != null ? value : "",
557
- selection: {
558
- main: 0,
559
- ranges: [{
560
- anchor: 0,
561
- head: 0
562
- }]
563
- },
564
- highlight: highlightLines != null ? highlightLines : []
565
- },
566
- fields: highlightState
567
- };
568
- }, []);
569
- const handleCreateEditor = useCallback(view => {
570
- setEditorView(view);
571
- }, []);
572
- const basicSetup = useMemo(() => basicSetupProp != null ? basicSetupProp : {
573
- highlightActiveLine: false
574
- }, [basicSetupProp]);
575
- return /* @__PURE__ */jsx(CodeMirror, {
576
- ...codeMirrorProps,
577
- value,
578
- ref,
579
- extensions,
580
- theme: codeMirrorTheme,
581
- onCreateEditor: handleCreateEditor,
582
- initialState,
583
- basicSetup
584
- });
585
- });
586
- function useLanguageExtension(mode) {
587
- const codeConfig = useContext(CodeInputConfigContext);
588
- const [languageExtension, setLanguageExtension] = useState();
589
- useEffect(() => {
590
- var _a;
591
- const customModes = (_a = codeConfig == null ? void 0 : codeConfig.codeModes) != null ? _a : [];
592
- const modes = [...customModes, ...defaultCodeModes];
593
- const codeMode = modes.find(m => m.name === mode);
594
- if (!(codeMode == null ? void 0 : codeMode.loader)) {
595
- console.warn("Found no codeMode for language mode ".concat(mode, ", syntax highlighting will be disabled."));
596
- }
597
- let active = true;
598
- Promise.resolve(codeMode == null ? void 0 : codeMode.loader()).then(extension => {
599
- if (active) {
600
- setLanguageExtension(extension);
601
- }
602
- }).catch(e => {
603
- console.error("Failed to load language mode ".concat(mode), e);
604
- if (active) {
605
- setLanguageExtension(void 0);
606
- }
607
- });
608
- return () => {
609
- active = false;
610
- };
611
- }, [mode, codeConfig]);
612
- return languageExtension;
613
- }
614
- export { CodeMirrorProxy as default };
615
- //# sourceMappingURL=CodeMirrorProxy-A8RWnbXx.js.map