@sanity/code-input 3.0.0-v3-studio.0 → 3.0.0-v3-studio.3
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/README.md +8 -15
- package/lib/cjs/index.js +145 -79
- package/lib/cjs/index.js.map +1 -1
- package/lib/esm/index.js +145 -76
- package/lib/esm/index.js.map +1 -1
- package/lib/types/index.d.ts +6 -2
- package/lib/types/index.d.ts.map +1 -1
- package/package.json +24 -9
- package/src/CodeInput.tsx +50 -32
- package/src/PreviewCode.tsx +1 -1
- 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/{editorSupport.ts → ace-editor/editorSupport.ts} +3 -0
- package/src/{groq.ts → ace-editor/groq.ts} +0 -0
- package/src/schema.tsx +5 -9
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import {renderToString} from 'react-dom/server'
|
|
3
|
+
import {useAceEditor} from './AceEditorLazy'
|
|
4
|
+
|
|
5
|
+
describe('AceEditor - server', () => {
|
|
6
|
+
it('should render null to string (and not throw and Error)', () => {
|
|
7
|
+
const TestComponent = () => {
|
|
8
|
+
const Editor = useAceEditor()
|
|
9
|
+
if (!Editor) {
|
|
10
|
+
return null
|
|
11
|
+
}
|
|
12
|
+
throw new Error('editor should always be null in envs without window')
|
|
13
|
+
}
|
|
14
|
+
const serverString = renderToString(<TestComponent />)
|
|
15
|
+
|
|
16
|
+
expect(serverString).toEqual('')
|
|
17
|
+
})
|
|
18
|
+
})
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React, {useEffect, useState} from 'react'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* AceEditor loads window global directly when imported, which crashes in node envs.
|
|
5
|
+
* This works around the issue by only importing ace-dependencies when window is defined.
|
|
6
|
+
*
|
|
7
|
+
* We only set the ace lazy component after mounting, to allow us to render null on the server,
|
|
8
|
+
* and use suspense on the client. This will make hydration work correctly.
|
|
9
|
+
*/
|
|
10
|
+
export const AceEditorLazy = React.lazy(() => import('./editorSupport'))
|
|
11
|
+
|
|
12
|
+
export function useAceEditor() {
|
|
13
|
+
const [mounted, setMounted] = useState(false)
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
setMounted(true)
|
|
16
|
+
}, [])
|
|
17
|
+
|
|
18
|
+
return mounted ? AceEditorLazy : null
|
|
19
|
+
}
|
|
File without changes
|
package/src/schema.tsx
CHANGED
|
@@ -1,22 +1,19 @@
|
|
|
1
|
-
import React from 'react'
|
|
2
1
|
import {CodeBlockIcon} from '@sanity/icons'
|
|
3
2
|
import {CodeInput} from './CodeInput'
|
|
4
3
|
import PreviewCode, {PreviewCodeProps} from './PreviewCode'
|
|
5
4
|
import {getMedia} from './getMedia'
|
|
5
|
+
import {defineType} from 'sanity'
|
|
6
6
|
|
|
7
7
|
export type {CodeInputProps, CodeSchemaType} from './CodeInput'
|
|
8
8
|
|
|
9
9
|
export type {CodeInputLanguage, CodeInputValue} from './types'
|
|
10
|
+
export type {PreviewCode, PreviewCodeProps, CodeInput}
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
return <PreviewCode {...props} />
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export default {
|
|
12
|
+
export default defineType({
|
|
16
13
|
name: 'code',
|
|
17
14
|
type: 'object',
|
|
18
15
|
title: 'Code',
|
|
19
|
-
components: {input: CodeInput},
|
|
16
|
+
components: {input: CodeInput, preview: PreviewCode},
|
|
20
17
|
icon: CodeBlockIcon,
|
|
21
18
|
fields: [
|
|
22
19
|
{
|
|
@@ -62,8 +59,7 @@ export default {
|
|
|
62
59
|
return {
|
|
63
60
|
title: value.filename || (value.language || 'unknown').toUpperCase(),
|
|
64
61
|
media: getMedia(value?.language),
|
|
65
|
-
extendedPreview: <Preview value={value} />,
|
|
66
62
|
}
|
|
67
63
|
},
|
|
68
64
|
},
|
|
69
|
-
}
|
|
65
|
+
})
|