@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.
Files changed (52) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +140 -64
  3. package/lib/_chunks/CodeMirrorProxy-3836f097.js +619 -0
  4. package/lib/_chunks/CodeMirrorProxy-3836f097.js.map +1 -0
  5. package/lib/_chunks/CodeMirrorProxy-e83d4d37.js +611 -0
  6. package/lib/_chunks/CodeMirrorProxy-e83d4d37.js.map +1 -0
  7. package/lib/_chunks/index-17e68aff.js +563 -0
  8. package/lib/_chunks/index-17e68aff.js.map +1 -0
  9. package/lib/_chunks/index-9a4cb814.js +549 -0
  10. package/lib/_chunks/index-9a4cb814.js.map +1 -0
  11. package/lib/{src/index.d.ts → index.d.ts} +18 -10
  12. package/lib/index.esm.js +1 -1
  13. package/lib/index.esm.js.map +1 -1
  14. package/lib/index.js +10 -1
  15. package/lib/index.js.map +1 -1
  16. package/package.json +53 -27
  17. package/src/CodeInput.tsx +72 -272
  18. package/src/LanguageField.tsx +33 -0
  19. package/src/LanguageInput.tsx +32 -0
  20. package/src/PreviewCode.tsx +40 -68
  21. package/src/__workshop__/index.ts +22 -0
  22. package/src/__workshop__/lazy.tsx +54 -0
  23. package/src/__workshop__/preview.tsx +24 -0
  24. package/src/__workshop__/props.tsx +24 -0
  25. package/src/codemirror/CodeMirrorProxy.tsx +157 -0
  26. package/src/codemirror/CodeModeContext.tsx +4 -0
  27. package/src/codemirror/defaultCodeModes.ts +109 -0
  28. package/src/codemirror/extensions/highlightLineExtension.ts +164 -0
  29. package/src/codemirror/extensions/theme.ts +61 -0
  30. package/src/codemirror/extensions/useCodeMirrorTheme.ts +63 -0
  31. package/src/codemirror/extensions/useFontSize.ts +24 -0
  32. package/src/codemirror/useCodeMirror-client.test.tsx +49 -0
  33. package/src/{ace-editor/AceEditor-server.test.tsx → codemirror/useCodeMirror-server.test.tsx} +3 -4
  34. package/src/codemirror/useCodeMirror.tsx +12 -0
  35. package/src/codemirror/useLanguageMode.tsx +52 -0
  36. package/src/config.ts +1 -13
  37. package/src/getMedia.tsx +0 -2
  38. package/src/index.ts +4 -11
  39. package/src/plugin.tsx +39 -0
  40. package/src/schema.tsx +3 -7
  41. package/src/types.ts +19 -3
  42. package/src/ui/focusRingStyle.ts +27 -0
  43. package/src/useFieldMember.ts +16 -0
  44. package/lib/_chunks/editorSupport-895caf32.esm.js +0 -2
  45. package/lib/_chunks/editorSupport-895caf32.esm.js.map +0 -1
  46. package/lib/_chunks/editorSupport-bda3d360.js +0 -2
  47. package/lib/_chunks/editorSupport-bda3d360.js.map +0 -1
  48. package/src/ace-editor/AceEditor-client.test.tsx +0 -37
  49. package/src/ace-editor/AceEditorLazy.tsx +0 -19
  50. package/src/ace-editor/editorSupport.ts +0 -34
  51. package/src/ace-editor/groq.ts +0 -630
  52. package/src/createHighlightMarkers.ts +0 -24
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2022 Sanity.io
3
+ Copyright (c) 2023 Sanity.io
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -7,117 +7,187 @@
7
7
 
8
8
  Code input for [Sanity](https://sanity.io/).
9
9
 
10
- Currently only a subset of languages and features are exposed, over time we will implement a richer set of options.
10
+ A subset of languages and features are exposed by default. More can be added via the plugin options.
11
11
 
12
12
  ![Code input](assets/basic-input.png)
13
13
 
14
- Click lines to highlight them.
14
+ Click the line numbers to toggle line highlighting.
15
15
 
16
16
  ## Installation
17
17
 
18
- ```
19
- npm install --save @sanity/code-input
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 { codeInput } from "@sanity/code-input";
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
- // ...fields...
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
- theme: 'github',
79
- darkTheme: 'terminal',
80
- language: 'js',
65
+ language: 'javascript',
81
66
  languageAlternatives: [
82
- {title: 'Javascript', value: 'js'},
67
+ {title: 'Javascript', value: 'javascript'},
83
68
  {title: 'HTML', value: 'html'},
84
69
  {title: 'CSS', value: 'css'},
85
- {title: 'Rust', value: 'rust', mode:'rust'},
86
- {title: 'SASS', value: 'sass'},
87
- ]
88
- }
89
- }
70
+ ],
71
+ withFilename: true,
72
+ },
73
+ })
90
74
  ```
91
75
 
92
76
  ![Code input with all options in dark mode](assets/all-options.png)
93
77
 
94
78
  ## Add support for more languages
95
79
 
96
- Only a subset of languages are supported by default (see full list [here](https://github.com/sanity-io/sanity/blob/current/packages/@sanity/code-input/src/config.ts#L4)). You can add support for other languages by importing the ace mode yourself, and specifying `mode` for the `languageAlternatives` schema config.
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
- Example: Add support for the Rust Programming Language
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
- // import rust support for ace, see https://github.com/securingsincity/react-ace for more details
102
- import 'ace-builds/src-noconflict/mode-rust'
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
- name: 'exampleUsage',
106
- title: 'Example usage',
107
- type: 'code',
108
- options: {
109
- languageAlternatives: [
110
- {title: 'Javascript', value: 'js'},
111
- {
112
- title: 'Rust',
113
- value: 'rust',
114
- mode:'rust' // <- specify the mode to use here. Make sure this mode is also imported from ace-builds (see above)
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).