@sanity/code-input 6.0.1 → 6.0.2

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/src/CodeInput.tsx CHANGED
@@ -1,13 +1,20 @@
1
1
  import {Box, Card, Stack, Text} from '@sanity/ui'
2
2
  import {Suspense, useCallback} from 'react'
3
- import {MemberField, ObjectInputProps, RenderInputCallback, set, setIfMissing, unset} from 'sanity'
4
- import styled, {css} from 'styled-components'
5
-
6
- import {useCodeMirror} from './codemirror/useCodeMirror'
3
+ import {
4
+ MemberField,
5
+ type ObjectInputProps,
6
+ type RenderInputCallback,
7
+ set,
8
+ setIfMissing,
9
+ unset,
10
+ } from 'sanity'
11
+ import {css, styled} from 'styled-components'
12
+
13
+ import {CodeMirrorProxy, useMounted} from './codemirror/useCodeMirror'
7
14
  import {useLanguageMode} from './codemirror/useLanguageMode'
8
15
  import {PATH_CODE} from './config'
9
16
  import {LanguageField} from './LanguageField'
10
- import {CodeInputValue, CodeSchemaType} from './types'
17
+ import type {CodeInputValue, CodeSchemaType} from './types'
11
18
  import {focusRingBorderStyle, focusRingStyle} from './ui/focusRingStyle'
12
19
  import {useFieldMember} from './useFieldMember'
13
20
 
@@ -53,7 +60,7 @@ const EditorContainer = styled(Card)(({theme}) => {
53
60
  })
54
61
 
55
62
  /** @public */
56
- export function CodeInput(props: CodeInputProps) {
63
+ export function CodeInput(props: CodeInputProps): React.JSX.Element {
57
64
  const {
58
65
  members,
59
66
  elementProps,
@@ -95,13 +102,13 @@ export function CodeInput(props: CodeInputProps) {
95
102
  )
96
103
  const {languages, language, languageMode} = useLanguageMode(props.schemaType, props.value)
97
104
 
98
- const CodeMirror = useCodeMirror()
105
+ const mounted = useMounted()
99
106
 
100
107
  const renderCodeInput: RenderInputCallback = useCallback(
101
108
  (inputProps) => {
102
109
  return (
103
110
  <EditorContainer border overflow="hidden" radius={1} sizing="border" readOnly={readOnly}>
104
- {CodeMirror && (
111
+ {mounted && (
105
112
  <Suspense
106
113
  fallback={
107
114
  <Box padding={3}>
@@ -109,7 +116,7 @@ export function CodeInput(props: CodeInputProps) {
109
116
  </Box>
110
117
  }
111
118
  >
112
- <CodeMirror
119
+ <CodeMirrorProxy
113
120
  languageMode={languageMode}
114
121
  onChange={handleCodeChange}
115
122
  value={inputProps.value as string}
@@ -125,14 +132,14 @@ export function CodeInput(props: CodeInputProps) {
125
132
  )
126
133
  },
127
134
  [
128
- CodeMirror,
135
+ readOnly,
136
+ mounted,
137
+ languageMode,
129
138
  handleCodeChange,
130
- handleCodeFocus,
139
+ value?.highlightedLines,
131
140
  onHighlightChange,
132
- languageMode,
141
+ handleCodeFocus,
133
142
  elementProps.onBlur,
134
- readOnly,
135
- value,
136
143
  ],
137
144
  )
138
145
 
@@ -1,6 +1,6 @@
1
1
  import {useCallback} from 'react'
2
2
  import {
3
- FieldMember,
3
+ type FieldMember,
4
4
  type InputProps,
5
5
  MemberField,
6
6
  type MemberFieldProps,
@@ -2,7 +2,7 @@ import {Select} from '@sanity/ui'
2
2
  import {type ChangeEvent, useCallback} from 'react'
3
3
  import {set, type StringInputProps, unset} from 'sanity'
4
4
 
5
- import {CodeInputLanguage} from './types'
5
+ import type {CodeInputLanguage} from './types'
6
6
 
7
7
  export interface LanguageInputProps {
8
8
  language: string
@@ -1,11 +1,11 @@
1
1
  import {Box, Card, Flex, Label, Text} from '@sanity/ui'
2
2
  import {Suspense} from 'react'
3
- import {PreviewProps} from 'sanity'
4
- import styled from 'styled-components'
3
+ import type {PreviewProps} from 'sanity'
4
+ import {styled} from 'styled-components'
5
5
 
6
- import {useCodeMirror} from './codemirror/useCodeMirror'
6
+ import {CodeMirrorProxy, useMounted} from './codemirror/useCodeMirror'
7
7
  import {useLanguageMode} from './codemirror/useLanguageMode'
8
- import {CodeInputValue, CodeSchemaType} from './types'
8
+ import type {CodeInputValue, CodeSchemaType} from './types'
9
9
 
10
10
  const PreviewContainer = styled(Box)`
11
11
  position: relative;
@@ -25,7 +25,7 @@ export function PreviewCode(props: PreviewCodeProps) {
25
25
  const {selection, schemaType: type} = props
26
26
  const {languageMode} = useLanguageMode(type as CodeSchemaType, props.selection)
27
27
 
28
- const CodeMirror = useCodeMirror()
28
+ const mounted = useMounted()
29
29
  return (
30
30
  <PreviewContainer>
31
31
  <Card padding={4}>
@@ -47,9 +47,9 @@ export function PreviewCode(props: PreviewCodeProps) {
47
47
  </Flex>
48
48
  </Card>
49
49
  ) : null}
50
- {CodeMirror && (
50
+ {mounted && (
51
51
  <Suspense fallback={<Card padding={2}>Loading code preview...</Card>}>
52
- <CodeMirror
52
+ <CodeMirrorProxy
53
53
  readOnly
54
54
  editable={false}
55
55
  value={selection?.code || ''}
@@ -129,6 +129,7 @@ function useLanguageExtension(mode?: string) {
129
129
 
130
130
  const codeMode = modes.find((m) => m.name === mode)
131
131
  if (!codeMode?.loader) {
132
+ // eslint-disable-next-line no-console
132
133
  console.warn(
133
134
  `Found no codeMode for language mode ${mode}, syntax highlighting will be disabled.`,
134
135
  )
@@ -141,6 +142,7 @@ function useLanguageExtension(mode?: string) {
141
142
  }
142
143
  })
143
144
  .catch((e) => {
145
+ // eslint-disable-next-line no-console
144
146
  console.error(`Failed to load language mode ${mode}`, e)
145
147
  if (active) {
146
148
  setLanguageExtension(undefined)
@@ -1,5 +1,3 @@
1
- /* eslint-disable no-param-reassign */
2
-
3
1
  import {type Extension, StateEffect, StateField} from '@codemirror/state'
4
2
  import {Decoration, type DecorationSet, EditorView, lineNumbers} from '@codemirror/view'
5
3
  import type {ThemeContextValue} from '@sanity/ui'
@@ -56,6 +54,7 @@ export const lineHighlightField = StateField.define({
56
54
  add: highlights,
57
55
  })
58
56
  } catch (e) {
57
+ // eslint-disable-next-line no-console
59
58
  console.error(e)
60
59
  return Decoration.none
61
60
  }
@@ -130,7 +129,7 @@ export const highlightLine = (config: HighlightLineConfig): Extension => {
130
129
  let isHighlighted = false
131
130
  editorView.state
132
131
  .field(lineHighlightField)
133
- .between(line.from, line.to, (from, to, value) => {
132
+ .between(line.from, line.to, (_from, _to, value) => {
134
133
  if (value) {
135
134
  isHighlighted = true
136
135
  return false // stop iteration
@@ -163,7 +162,7 @@ export function setHighlightedLines(view: EditorView, highlightLines: number[]):
163
162
  const doc = view.state.doc
164
163
  const lines = doc.lines
165
164
  //1-based line numbers
166
- const allLineNumbers = Array.from({length: lines}, (x, i) => i + 1)
165
+ const allLineNumbers = Array.from({length: lines}, (_x, i) => i + 1)
167
166
  view.dispatch({
168
167
  effects: allLineNumbers.map((lineNumber) => {
169
168
  const line = doc.line(lineNumber)
@@ -9,7 +9,7 @@ export function useFontSizeExtension(props: {fontSize: number}): Extension {
9
9
 
10
10
  return useMemo(() => {
11
11
  const {code: codeFont} = theme.sanity.fonts
12
- const {fontSize, lineHeight} = codeFont.sizes[fontSizeProp] || codeFont.sizes[2]
12
+ const {fontSize, lineHeight} = codeFont.sizes[fontSizeProp] || codeFont.sizes[2]!
13
13
 
14
14
  return EditorView.baseTheme({
15
15
  '&': {
@@ -4,7 +4,7 @@ import {studioTheme, ThemeProvider} from '@sanity/ui'
4
4
  import {act, render} from '@testing-library/react'
5
5
  import {Suspense} from 'react'
6
6
 
7
- import {useCodeMirror} from './useCodeMirror'
7
+ import {CodeMirrorProxy, useMounted} from './useCodeMirror'
8
8
 
9
9
  describe('useCodeMirror - client', () => {
10
10
  let rafMock: jest.SpyInstance<number, [FrameRequestCallback]>
@@ -14,9 +14,8 @@ describe('useCodeMirror - client', () => {
14
14
  .spyOn(window, 'requestAnimationFrame')
15
15
  .mockImplementation((callback: FrameRequestCallback): number => {
16
16
  try {
17
- // eslint-disable-next-line callback-return
18
17
  callback(0)
19
- } catch (e) {
18
+ } catch {
20
19
  // CodeMirror does some mesurement shenanigance that json dont support
21
20
  // we just let it crash silently
22
21
  }
@@ -30,12 +29,12 @@ describe('useCodeMirror - client', () => {
30
29
 
31
30
  it('should render suspended codemirror editor', async () => {
32
31
  const TestComponent = () => {
33
- const CodeMirror = useCodeMirror()
32
+ const mounted = useMounted()
34
33
  return (
35
34
  <Suspense fallback={'loading'}>
36
- {CodeMirror && (
35
+ {mounted && (
37
36
  <ThemeProvider theme={studioTheme}>
38
- <CodeMirror languageMode={'tsx'} />
37
+ <CodeMirrorProxy languageMode={'tsx'} />
39
38
  </ThemeProvider>
40
39
  )}
41
40
  </Suspense>
@@ -1,12 +1,12 @@
1
1
  import {renderToString} from 'react-dom/server'
2
2
 
3
- import {useCodeMirror} from './useCodeMirror'
3
+ import {useMounted} from './useCodeMirror'
4
4
 
5
5
  describe('useCodeMirror - server', () => {
6
6
  it('should render null to string (and not throw and Error)', () => {
7
7
  const TestComponent = () => {
8
- const Editor = useCodeMirror()
9
- if (!Editor) {
8
+ const mounted = useMounted()
9
+ if (!mounted) {
10
10
  return null
11
11
  }
12
12
  throw new Error('editor should always be null in envs without window')
@@ -1,12 +1,11 @@
1
- import {lazy, useEffect, useState} from 'react'
1
+ import {lazy, startTransition, useEffect, useState} from 'react'
2
2
 
3
3
  export const CodeMirrorProxy = lazy(() => import('./CodeMirrorProxy'))
4
4
 
5
- export function useCodeMirror() {
5
+ export function useMounted() {
6
6
  const [mounted, setMounted] = useState(false)
7
7
  useEffect(() => {
8
- requestAnimationFrame(() => setMounted(true))
8
+ requestAnimationFrame(() => startTransition(() => setMounted(true)))
9
9
  }, [])
10
-
11
- return mounted ? CodeMirrorProxy : null
10
+ return mounted
12
11
  }
package/src/config.ts CHANGED
@@ -1,4 +1,4 @@
1
- import {CodeInputLanguage} from './types'
1
+ import type {CodeInputLanguage} from './types'
2
2
 
3
3
  // NOTE: MAKE SURE THESE ALIGN WITH CODE MODES IN ./codemirror/defaultCodeModes.ts
4
4
  export const SUPPORTED_LANGUAGES: CodeInputLanguage[] = [
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import {CodeDefinition, codeSchema, codeTypeName} from './schema'
1
+ import {type CodeDefinition, codeSchema, codeTypeName} from './schema'
2
2
  export {type CodeInput, type CodeInputProps} from './CodeInput'
3
3
  export {PreviewCode, type PreviewCodeProps} from './PreviewCode'
4
4
  export type {CodeInputLanguage, CodeInputValue, CodeOptions, CodeSchemaType} from './types'
package/src/plugin.tsx CHANGED
@@ -1,7 +1,7 @@
1
1
  import {definePlugin} from 'sanity'
2
2
 
3
3
  import {CodeInputConfigContext} from './codemirror/CodeModeContext'
4
- import {CodeMode} from './codemirror/defaultCodeModes'
4
+ import type {CodeMode} from './codemirror/defaultCodeModes'
5
5
  import {codeSchema} from './schema'
6
6
 
7
7
  export interface CodeInputConfig {
@@ -1,6 +1,5 @@
1
1
  import {type Theme} from '@sanity/ui/theme'
2
2
 
3
3
  declare module 'styled-components' {
4
- // eslint-disable-next-line no-unused-vars
5
4
  interface DefaultTheme extends Theme {}
6
5
  }
package/src/schema.tsx CHANGED
@@ -1,10 +1,10 @@
1
1
  import {CodeBlockIcon} from '@sanity/icons'
2
- import {defineType, ObjectDefinition} from 'sanity'
2
+ import {defineType, type ObjectDefinition} from 'sanity'
3
3
 
4
4
  import {CodeInput} from './CodeInput'
5
5
  import {getMedia} from './getMedia'
6
6
  import {PreviewCode} from './PreviewCode'
7
- import {CodeOptions} from './types'
7
+ import type {CodeOptions} from './types'
8
8
 
9
9
  /**
10
10
  * @public
@@ -19,7 +19,7 @@ export interface CodeDefinition extends Omit<ObjectDefinition, 'type' | 'fields'
19
19
  options?: CodeOptions
20
20
  }
21
21
 
22
- declare module '@sanity/types' {
22
+ declare module 'sanity' {
23
23
  // makes type: 'code' narrow correctly when using defineType/defineField/defineArrayMember
24
24
  export interface IntrinsicDefinitions {
25
25
  code: CodeDefinition
package/src/types.ts CHANGED
@@ -1,4 +1,4 @@
1
- import {ObjectSchemaType} from 'sanity'
1
+ import type {ObjectSchemaType} from 'sanity'
2
2
 
3
3
  export interface CodeInputLanguage {
4
4
  title: string
@@ -1,5 +1,5 @@
1
1
  import {useMemo} from 'react'
2
- import {FieldMember, ObjectMember} from 'sanity'
2
+ import type {FieldMember, ObjectMember} from 'sanity'
3
3
 
4
4
  /** @internal */
5
5
  export function useFieldMember(