@sanity/code-input 0.147.10-document-actions.37 → 1.0.0-v3-studio.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.
- package/LICENSE +1 -1
- package/README.md +126 -11
- package/lib/cjs/index.js +1417 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/esm/index.js +1412 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/types/index.d.ts +52 -0
- package/lib/types/index.d.ts.map +1 -0
- package/package.json +80 -11
- package/sanity.json +2 -22
- package/src/CodeInput.tsx +354 -0
- package/src/PreviewCode.tsx +88 -0
- package/src/ace-editor/AceEditor-client.test.tsx +24 -0
- package/src/ace-editor/AceEditor-server.test.tsx +18 -0
- package/src/ace-editor/AceEditorLazy.tsx +19 -0
- package/src/ace-editor/editorSupport.ts +34 -0
- package/src/ace-editor/groq.ts +630 -0
- package/src/config.ts +46 -0
- package/src/createHighlightMarkers.ts +24 -0
- package/src/getMedia.tsx +95 -0
- package/src/index.ts +12 -0
- package/src/schema.tsx +65 -0
- package/src/types.ts +26 -0
- package/v2-incompatible.js +11 -0
- package/lib/CodeInput.css +0 -7
- package/lib/CodeInput.js +0 -342
- package/lib/HighlightMarkers.css +0 -9
- package/lib/PreviewCode.css +0 -25
- package/lib/PreviewCode.js +0 -145
- package/lib/config.js +0 -69
- package/lib/createHighlightMarkers.js +0 -22
- package/lib/deprecatedSchema.js +0 -26
- package/lib/groq.js +0 -401
- package/lib/jsonLogo.js +0 -62
- package/lib/schema.js +0 -150
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,18 +1,49 @@
|
|
|
1
1
|
# @sanity/code-input
|
|
2
2
|
|
|
3
|
+
> **NOTE**
|
|
4
|
+
>
|
|
5
|
+
> This is the **Sanity Studio v3 version** of @sanity/code-input.
|
|
6
|
+
>
|
|
7
|
+
> For the v2 version, please refer to the [v2-branch](https://github.com/sanity-io/sanity/tree/next/packages/%40sanity/code-input).
|
|
8
|
+
|
|
9
|
+
## What is it?
|
|
10
|
+
|
|
3
11
|
Code input for [Sanity](https://sanity.io/).
|
|
4
12
|
|
|
5
13
|
Currently only a subset of languages and features are exposed, over time we will implement a richer set of options.
|
|
6
14
|
|
|
15
|
+

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

|
|
96
|
+
|
|
97
|
+
## Add support for more languages
|
|
98
|
+
|
|
99
|
+
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.
|
|
100
|
+
|
|
101
|
+
Example: Add support for the Rust Programming Language
|
|
102
|
+
|
|
103
|
+
```js
|
|
104
|
+
// import rust support for ace, see https://github.com/securingsincity/react-ace for more details
|
|
105
|
+
import 'ace-builds/src-noconflict/mode-rust'
|
|
106
|
+
|
|
107
|
+
{
|
|
108
|
+
name: 'exampleUsage',
|
|
109
|
+
title: 'Example usage',
|
|
110
|
+
type: 'code',
|
|
111
|
+
options: {
|
|
112
|
+
languageAlternatives: [
|
|
113
|
+
{title: 'Javascript', value: 'js'},
|
|
114
|
+
{
|
|
115
|
+
title: 'Rust',
|
|
116
|
+
value: 'rust',
|
|
117
|
+
mode:'rust' // <- specify the mode to use here. Make sure this mode is also imported from ace-builds (see above)
|
|
118
|
+
},
|
|
119
|
+
]
|
|
48
120
|
}
|
|
49
121
|
}
|
|
50
122
|
```
|
|
@@ -56,10 +128,53 @@ Note that the above only works if you import and use the `all:part:@sanity/base/
|
|
|
56
128
|
_type: 'code',
|
|
57
129
|
language: 'js',
|
|
58
130
|
highlightedLines: [1, 2],
|
|
59
|
-
code: 'const foo = "bar"\nconsole.log(foo.toUpperCase())\n// BAR'
|
|
131
|
+
code: 'const foo = "bar"\nconsole.log(foo.toUpperCase())\n// BAR',
|
|
132
|
+
filename: 'available when enabled'
|
|
60
133
|
}
|
|
61
134
|
```
|
|
62
135
|
|
|
136
|
+
## Example usage in frontend (React)
|
|
137
|
+
|
|
138
|
+
You can use any syntax highlighter you want - but not all of them might support highlighted lines or the syntax you've defined.
|
|
139
|
+
|
|
140
|
+
As outlined above, the actual code is stored in a `code` property, so if your schema has a field called `codeExample` of type `code`, the property you'd want to pass to the highlighter would be `codeExample.code`.
|
|
141
|
+
|
|
142
|
+
Here's an example using [react-refractor](https://github.com/rexxars/react-refractor):
|
|
143
|
+
|
|
144
|
+
```jsx
|
|
145
|
+
import React from 'react'
|
|
146
|
+
import Refractor from 'react-refractor'
|
|
147
|
+
import js from 'refractor/lang/javascript'
|
|
148
|
+
|
|
149
|
+
Refractor.registerLanguage(js)
|
|
150
|
+
|
|
151
|
+
export function Code(props) {
|
|
152
|
+
return (
|
|
153
|
+
<Refractor
|
|
154
|
+
// In this example, `props` is the value of a `code` field
|
|
155
|
+
language={props.language}
|
|
156
|
+
value={props.code}
|
|
157
|
+
markers={props.highlightedLines}
|
|
158
|
+
/>
|
|
159
|
+
)
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Other syntax highlighters include:
|
|
164
|
+
|
|
165
|
+
- [react-lowlight](https://github.com/rexxars/react-lowlight)
|
|
166
|
+
- [react-syntax-highlighter](https://github.com/react-syntax-highlighter/react-syntax-highlighter)
|
|
167
|
+
- [highlight.js](https://github.com/highlightjs/highlight.js)
|
|
168
|
+
- [prism](https://github.com/PrismJS/prism)
|
|
169
|
+
|
|
63
170
|
## License
|
|
64
171
|
|
|
65
172
|
MIT-licensed. See LICENSE.
|
|
173
|
+
|
|
174
|
+
## Develop & test
|
|
175
|
+
|
|
176
|
+
This plugin uses [@sanity/plugin-kit](https://github.com/sanity-io/plugin-kit)
|
|
177
|
+
with default configuration for build & watch scripts.
|
|
178
|
+
|
|
179
|
+
See [Testing a plugin in Sanity Studio](https://github.com/sanity-io/plugin-kit#testing-a-plugin-in-sanity-studio)
|
|
180
|
+
on how to run this plugin with hotreload in the studio.
|