@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.
@@ -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
+ }
@@ -1,3 +1,6 @@
1
+ import ReactAce from 'react-ace'
2
+ export default ReactAce
3
+
1
4
  // NOTE: MAKE SURE THESE ALIGN WITH SUPPORTED_LANGUAGES in ./config
2
5
  import './groq'
3
6
 
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
- const Preview = (props: PreviewCodeProps) => {
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
+ })