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