@sanity/code-input 3.0.1 → 4.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/LICENSE +1 -1
- package/README.md +140 -64
- package/lib/_chunks/CodeMirrorProxy-3836f097.js +619 -0
- package/lib/_chunks/CodeMirrorProxy-3836f097.js.map +1 -0
- package/lib/_chunks/CodeMirrorProxy-e83d4d37.js +611 -0
- package/lib/_chunks/CodeMirrorProxy-e83d4d37.js.map +1 -0
- package/lib/_chunks/index-17e68aff.js +563 -0
- package/lib/_chunks/index-17e68aff.js.map +1 -0
- package/lib/_chunks/index-9a4cb814.js +549 -0
- package/lib/_chunks/index-9a4cb814.js.map +1 -0
- package/lib/{src/index.d.ts → index.d.ts} +18 -10
- package/lib/index.esm.js +1 -1
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +10 -1
- package/lib/index.js.map +1 -1
- package/package.json +53 -27
- package/src/CodeInput.tsx +72 -272
- package/src/LanguageField.tsx +33 -0
- package/src/LanguageInput.tsx +32 -0
- package/src/PreviewCode.tsx +40 -68
- package/src/__workshop__/index.ts +22 -0
- package/src/__workshop__/lazy.tsx +54 -0
- package/src/__workshop__/preview.tsx +24 -0
- package/src/__workshop__/props.tsx +24 -0
- package/src/codemirror/CodeMirrorProxy.tsx +157 -0
- package/src/codemirror/CodeModeContext.tsx +4 -0
- package/src/codemirror/defaultCodeModes.ts +109 -0
- package/src/codemirror/extensions/highlightLineExtension.ts +164 -0
- package/src/codemirror/extensions/theme.ts +61 -0
- package/src/codemirror/extensions/useCodeMirrorTheme.ts +63 -0
- package/src/codemirror/extensions/useFontSize.ts +24 -0
- package/src/codemirror/useCodeMirror-client.test.tsx +49 -0
- package/src/{ace-editor/AceEditor-server.test.tsx → codemirror/useCodeMirror-server.test.tsx} +3 -4
- package/src/codemirror/useCodeMirror.tsx +12 -0
- package/src/codemirror/useLanguageMode.tsx +52 -0
- package/src/config.ts +1 -13
- package/src/getMedia.tsx +0 -2
- package/src/index.ts +4 -11
- package/src/plugin.tsx +39 -0
- package/src/schema.tsx +3 -7
- package/src/types.ts +19 -3
- package/src/ui/focusRingStyle.ts +27 -0
- package/src/useFieldMember.ts +16 -0
- package/lib/_chunks/editorSupport-895caf32.esm.js +0 -2
- package/lib/_chunks/editorSupport-895caf32.esm.js.map +0 -1
- package/lib/_chunks/editorSupport-bda3d360.js +0 -2
- package/lib/_chunks/editorSupport-bda3d360.js.map +0 -1
- package/src/ace-editor/AceEditor-client.test.tsx +0 -37
- package/src/ace-editor/AceEditorLazy.tsx +0 -19
- package/src/ace-editor/editorSupport.ts +0 -34
- package/src/ace-editor/groq.ts +0 -630
- package/src/createHighlightMarkers.ts +0 -24
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -7,117 +7,187 @@
|
|
|
7
7
|
|
|
8
8
|
Code input for [Sanity](https://sanity.io/).
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
A subset of languages and features are exposed by default. More can be added via the plugin options.
|
|
11
11
|
|
|
12
12
|

|
|
13
13
|
|
|
14
|
-
Click
|
|
14
|
+
Click the line numbers to toggle line highlighting.
|
|
15
15
|
|
|
16
16
|
## Installation
|
|
17
17
|
|
|
18
|
-
```
|
|
19
|
-
npm install
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
or
|
|
23
|
-
|
|
24
|
-
```
|
|
25
|
-
yarn add @sanity/code-input
|
|
18
|
+
```sh
|
|
19
|
+
npm install @sanity/code-input
|
|
26
20
|
```
|
|
27
21
|
|
|
28
22
|
## Usage
|
|
29
23
|
|
|
30
|
-
Add it as a plugin in sanity.config.ts (or .js):
|
|
24
|
+
Add it as a plugin in `sanity.config.ts` (or .js):
|
|
31
25
|
|
|
32
26
|
```js
|
|
33
|
-
import {
|
|
27
|
+
import {codeInput} from '@sanity/code-input'
|
|
34
28
|
|
|
35
29
|
export default defineConfig({
|
|
36
30
|
// ...
|
|
37
|
-
plugins: [
|
|
38
|
-
codeInput(),
|
|
39
|
-
]
|
|
31
|
+
plugins: [codeInput()],
|
|
40
32
|
})
|
|
41
33
|
```
|
|
42
34
|
|
|
43
35
|
Now you can use the `code` type in your schema types:
|
|
44
36
|
|
|
45
37
|
```js
|
|
46
|
-
|
|
47
|
-
|
|
38
|
+
import {defineType, defineField} from 'sanity'
|
|
39
|
+
|
|
40
|
+
defineType({
|
|
41
|
+
// [...]
|
|
48
42
|
fields: [
|
|
49
|
-
|
|
50
|
-
{
|
|
51
|
-
name: 'exampleUsage',
|
|
52
|
-
title: 'Example usage',
|
|
43
|
+
defineField({
|
|
53
44
|
type: 'code',
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
45
|
+
name: 'myCodeField',
|
|
46
|
+
title: 'My code field',
|
|
47
|
+
}),
|
|
48
|
+
],
|
|
49
|
+
})
|
|
57
50
|
```
|
|
58
51
|
|
|
59
52
|
## Options
|
|
60
53
|
|
|
61
|
-
- `language` - Default language for this code field
|
|
54
|
+
- `language` - Default language for this code field.
|
|
62
55
|
- `languageAlternatives` - Array of languages that should be available (se its format in the example below)
|
|
63
|
-
- `theme` - Name of the theme to use.
|
|
64
|
-
- Possible values include: `['github', 'monokai', 'terminal', 'tomorrow']`.
|
|
65
|
-
- For the full list and a live playground, refer to the [react-ace page](http://securingsincity.github.io/react-ace/).
|
|
66
|
-
- Default value: 'tomorrow'
|
|
67
|
-
- `darkTheme` - Name of the theme to use when Studio is using dark mode. See `theme` options for supported values.
|
|
68
|
-
- Default value: `'monokai'`
|
|
69
56
|
- `withFilename` - Boolean option to display input field for filename
|
|
70
57
|
|
|
71
58
|
```js
|
|
72
|
-
|
|
73
|
-
{
|
|
74
|
-
name: 'exampleUsage',
|
|
75
|
-
title: 'Code with all options',
|
|
59
|
+
//...fields,
|
|
60
|
+
defineField({
|
|
76
61
|
type: 'code',
|
|
62
|
+
name: 'myCodeField',
|
|
63
|
+
title: 'Code with all options',
|
|
77
64
|
options: {
|
|
78
|
-
|
|
79
|
-
darkTheme: 'terminal',
|
|
80
|
-
language: 'js',
|
|
65
|
+
language: 'javascript',
|
|
81
66
|
languageAlternatives: [
|
|
82
|
-
{title: 'Javascript', value: '
|
|
67
|
+
{title: 'Javascript', value: 'javascript'},
|
|
83
68
|
{title: 'HTML', value: 'html'},
|
|
84
69
|
{title: 'CSS', value: 'css'},
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
70
|
+
],
|
|
71
|
+
withFilename: true,
|
|
72
|
+
},
|
|
73
|
+
})
|
|
90
74
|
```
|
|
91
75
|
|
|
92
76
|

|
|
93
77
|
|
|
94
78
|
## Add support for more languages
|
|
95
79
|
|
|
96
|
-
Only a subset of languages are
|
|
80
|
+
Only a subset of languages are have syntax highlighting support by default (see full list [here](https://github.com/sanity-io/code-input/blob/main/src/codemirror/defaultCodeModes.ts)).
|
|
97
81
|
|
|
98
|
-
|
|
82
|
+
### Mode: Reuse an existing language
|
|
83
|
+
|
|
84
|
+
Some languages are similar enough, that reusing one of the default highlighters will be "good enough".
|
|
85
|
+
To reuse an existing language, specify mode for a value in `languageAlternatives`:
|
|
99
86
|
|
|
100
87
|
```js
|
|
101
|
-
|
|
102
|
-
|
|
88
|
+
//...fields,
|
|
89
|
+
defineField({
|
|
90
|
+
name: 'zhOnly',
|
|
91
|
+
type: 'code',
|
|
92
|
+
options: {
|
|
93
|
+
language: 'zh',
|
|
94
|
+
languageAlternatives: [
|
|
95
|
+
//Adds support for zh language, using sh syntax highlighting
|
|
96
|
+
{title: 'ZH', value: 'zh', mode: 'sh'},
|
|
97
|
+
],
|
|
98
|
+
},
|
|
99
|
+
})
|
|
100
|
+
```
|
|
103
101
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
102
|
+
### Add more languages
|
|
103
|
+
|
|
104
|
+
You can add support for additional languages, or override existing ones, by providing a `codeModes` array to the plugin.
|
|
105
|
+
`codeModes` should be an array where each value is an object with a name and a loader function.
|
|
106
|
+
The loader function should return a codemirror `Extension` or a `Promise` that resolves to `Extension`.
|
|
107
|
+
|
|
108
|
+
The loader function will be invoked when the language is selected.
|
|
109
|
+
|
|
110
|
+
For a full list of officialy code-mirror languages, see:
|
|
111
|
+
|
|
112
|
+
### Example: Add support for CodeMirror 6 language (Angular)
|
|
113
|
+
|
|
114
|
+
We can add support for a [CodeMirror 6 lang package](https://github.com/orgs/codemirror/repositories?language=&q=lang-&sort=&type=all):
|
|
115
|
+
|
|
116
|
+
```js
|
|
117
|
+
// sanity.config.js
|
|
118
|
+
|
|
119
|
+
// ... in the plugins array of defineConfig, where we add the codeInput plugin
|
|
120
|
+
codeInput({
|
|
121
|
+
codeModes: [
|
|
122
|
+
{
|
|
123
|
+
name: 'angular',
|
|
124
|
+
// dynamic import the angular package, and initialize the plugin after it is loaded
|
|
125
|
+
// This way, the language is only when it is selected
|
|
126
|
+
loader: () => import('@codemirror/lang-angular').then(({angular}) => angular()),
|
|
127
|
+
},
|
|
128
|
+
],
|
|
129
|
+
})
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
```js
|
|
133
|
+
// in a code field, you can now use rust as a language as a value, or mode
|
|
134
|
+
defineField({
|
|
135
|
+
name: 'exampleRust',
|
|
136
|
+
title: 'Example usage',
|
|
137
|
+
type: 'code',
|
|
138
|
+
options: {
|
|
139
|
+
languageAlternatives: [
|
|
140
|
+
{title: 'Javascript', value: 'javascript'},
|
|
141
|
+
{title: 'Angular', value: 'angular'},
|
|
142
|
+
{title: 'Angular-like', value: 'angular-like', mode: 'angular'}, // uses angular highlighter
|
|
143
|
+
],
|
|
144
|
+
},
|
|
145
|
+
})
|
|
119
146
|
```
|
|
120
147
|
|
|
148
|
+
For this to work, you will have to run `npm i @codemirror/lang-angular` as this package is not included by @sanity/code-input.
|
|
149
|
+
|
|
150
|
+
### Example: Add support for CodeMirror 5 legacy language (Rust)
|
|
151
|
+
|
|
152
|
+
We can add support for any [CodeMirror 5 legacy language](https://github.com/codemirror/legacy-modes/tree/main/mode) using
|
|
153
|
+
[CodeMirror 6 StreamLanguage](https://codemirror.net/docs/ref/#language.StreamLanguage).
|
|
154
|
+
|
|
155
|
+
```js
|
|
156
|
+
// sanity.config.js
|
|
157
|
+
import {StreamLanguage} from '@codemirror/language'
|
|
158
|
+
|
|
159
|
+
// ... in the plugins array of defineConfig, where we add the codeInput plugin
|
|
160
|
+
codeInput({
|
|
161
|
+
codeModes: [
|
|
162
|
+
{
|
|
163
|
+
name: 'rust',
|
|
164
|
+
// dynamic import so the language is only be loaded on demand
|
|
165
|
+
loader: () =>
|
|
166
|
+
import('@codemirror/legacy-modes/mode/rust').then(({rust}) => StreamLanguage.define(rust)),
|
|
167
|
+
},
|
|
168
|
+
],
|
|
169
|
+
})
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
```js
|
|
173
|
+
// in a code field, you can now use rust as a language as a value, or mode
|
|
174
|
+
defineField({
|
|
175
|
+
name: 'exampleRust',
|
|
176
|
+
title: 'Example usage',
|
|
177
|
+
type: 'code',
|
|
178
|
+
options: {
|
|
179
|
+
languageAlternatives: [
|
|
180
|
+
{title: 'Javascript', value: 'javascript'},
|
|
181
|
+
{title: 'Rust', value: 'rust'},
|
|
182
|
+
{title: 'Rust-like', value: 'rust-like', mode: 'rust'}, // uses rust highlighter
|
|
183
|
+
],
|
|
184
|
+
},
|
|
185
|
+
})
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
Note: `@sanity/code-input` already includes the `@codemirror/legacy-modes` and `@codemirror/language` dependencies,
|
|
189
|
+
so no need to install them explicitly.
|
|
190
|
+
|
|
121
191
|
## Data model
|
|
122
192
|
|
|
123
193
|
```js
|
|
@@ -126,7 +196,7 @@ import 'ace-builds/src-noconflict/mode-rust'
|
|
|
126
196
|
language: 'js',
|
|
127
197
|
highlightedLines: [1, 2],
|
|
128
198
|
code: 'const foo = "bar"\nconsole.log(foo.toUpperCase())\n// BAR',
|
|
129
|
-
filename: 'available when enabled'
|
|
199
|
+
filename: 'available when enabled'
|
|
130
200
|
}
|
|
131
201
|
```
|
|
132
202
|
|
|
@@ -176,6 +246,12 @@ with default configuration for build & watch scripts.
|
|
|
176
246
|
See [Testing a plugin in Sanity Studio](https://github.com/sanity-io/plugin-kit#testing-a-plugin-in-sanity-studio)
|
|
177
247
|
on how to run this plugin with hotreload in the studio.
|
|
178
248
|
|
|
249
|
+
### UI Workshop
|
|
250
|
+
Run
|
|
251
|
+
`workshop dev`
|
|
252
|
+
|
|
253
|
+
To test the CodeMirror lazy component.
|
|
254
|
+
|
|
179
255
|
### Release new version
|
|
180
256
|
|
|
181
257
|
Run ["CI & Release" workflow](https://github.com/sanity-io/code-input/actions/workflows/main.yml).
|