@sanity/code-input 4.1.1 → 4.1.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/lib/index.cjs ADDED
@@ -0,0 +1,11 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true
5
+ });
6
+ var index = require('./_chunks/index-cttyJsiZ.cjs');
7
+ exports.PreviewCode = index.PreviewCode;
8
+ exports.codeInput = index.codeInput;
9
+ exports.codeSchema = index.codeSchema;
10
+ exports.codeTypeName = index.codeTypeName;
11
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1,7 @@
1
+ import cjs from './index.cjs';
2
+
3
+ export const PreviewCode = cjs.PreviewCode;
4
+ export const codeInput = cjs.codeInput;
5
+ export const codeSchema = cjs.codeSchema;
6
+ export const codeTypeName = cjs.codeTypeName;
7
+
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;"}
package/lib/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
- /// <reference types="react" />
2
-
3
1
  import {Extension} from '@codemirror/state'
2
+ import {JSX as JSX_2} from 'react/jsx-runtime'
4
3
  import {ObjectDefinition} from 'sanity'
5
4
  import {ObjectInputProps} from 'sanity'
6
5
  import {ObjectSchemaType} from 'sanity'
@@ -18,7 +17,7 @@ export declare interface CodeDefinition
18
17
  }
19
18
 
20
19
  /** @public */
21
- export declare function CodeInput(props: CodeInputProps): JSX.Element
20
+ export declare function CodeInput(props: CodeInputProps): JSX_2.Element
22
21
 
23
22
  /**
24
23
  * @public
@@ -104,7 +103,7 @@ declare type ModeLoader = () => Promise<Extension | undefined> | Extension | und
104
103
  /**
105
104
  * @public
106
105
  */
107
- export declare function PreviewCode(props: PreviewCodeProps): JSX.Element
106
+ export declare function PreviewCode(props: PreviewCodeProps): JSX_2.Element
108
107
 
109
108
  /**
110
109
  * @public
package/lib/index.js CHANGED
@@ -1,11 +1,2 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', {
4
- value: true
5
- });
6
- var index = require('./_chunks/index-160c0f7b.js');
7
- exports.PreviewCode = index.PreviewCode;
8
- exports.codeInput = index.codeInput;
9
- exports.codeSchema = index.codeSchema;
10
- exports.codeTypeName = index.codeTypeName;
1
+ export { PreviewCode, codeInput, codeSchema, codeTypeName } from './_chunks/index-KYa8fGra.js';
11
2
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sanity/code-input",
3
- "version": "4.1.1",
4
- "description": "Ace editor for editing code",
3
+ "version": "4.1.3",
4
+ "description": "Sanity input component for code, powered by CodeMirror",
5
5
  "keywords": [
6
6
  "sanity",
7
7
  "cms",
@@ -22,18 +22,22 @@
22
22
  },
23
23
  "license": "MIT",
24
24
  "author": "Sanity.io <hello@sanity.io>",
25
+ "type": "module",
25
26
  "exports": {
26
27
  ".": {
27
28
  "types": "./lib/index.d.ts",
28
29
  "source": "./src/index.ts",
29
- "import": "./lib/index.esm.js",
30
- "require": "./lib/index.js",
31
- "default": "./lib/index.esm.js"
30
+ "require": "./lib/index.cjs",
31
+ "node": {
32
+ "import": "./lib/index.cjs.js"
33
+ },
34
+ "import": "./lib/index.js",
35
+ "default": "./lib/index.js"
32
36
  },
33
37
  "./package.json": "./package.json"
34
38
  },
35
- "main": "./lib/index.js",
36
- "module": "./lib/index.esm.js",
39
+ "main": "./lib/index.cjs",
40
+ "module": "./lib/index.js",
37
41
  "source": "./src/index.ts",
38
42
  "types": "./lib/index.d.ts",
39
43
  "files": [
@@ -57,76 +61,76 @@
57
61
  "watch": "pkg-utils watch --strict"
58
62
  },
59
63
  "dependencies": {
60
- "@codemirror/autocomplete": "^6.1.0",
61
- "@codemirror/commands": "^6.0.1",
62
- "@codemirror/lang-html": "^6.4.0",
64
+ "@codemirror/autocomplete": "^6.11.1",
65
+ "@codemirror/commands": "^6.3.2",
66
+ "@codemirror/lang-html": "^6.4.7",
63
67
  "@codemirror/lang-java": "^6.0.1",
64
- "@codemirror/lang-javascript": "^6.1.2",
68
+ "@codemirror/lang-javascript": "^6.2.1",
65
69
  "@codemirror/lang-json": "^6.0.1",
66
- "@codemirror/lang-markdown": "^6.0.5",
70
+ "@codemirror/lang-markdown": "^6.2.3",
67
71
  "@codemirror/lang-php": "^6.0.1",
68
- "@codemirror/lang-sql": "^6.3.3",
69
- "@codemirror/language": "^6.2.1",
70
- "@codemirror/legacy-modes": "^6.3.1",
71
- "@codemirror/search": "^6.0.1",
72
- "@codemirror/state": "^6.1.0",
73
- "@codemirror/view": "^6.1.1",
72
+ "@codemirror/lang-sql": "^6.5.4",
73
+ "@codemirror/language": "^6.9.3",
74
+ "@codemirror/legacy-modes": "^6.3.3",
75
+ "@codemirror/search": "^6.5.5",
76
+ "@codemirror/state": "^6.3.3",
77
+ "@codemirror/view": "^6.22.3",
74
78
  "@juggle/resize-observer": "^3.3.1",
75
- "@lezer/highlight": "^1.0.0",
76
- "@sanity/icons": "^2.0.0",
79
+ "@lezer/highlight": "^1.2.0",
77
80
  "@sanity/incompatible-plugin": "^1.0.4",
78
81
  "@sanity/ui": "^1.0.0",
79
- "@uiw/codemirror-themes": "^4.19.4",
80
- "@uiw/react-codemirror": "^4.19.4"
82
+ "@uiw/codemirror-themes": "^4.21.21",
83
+ "@uiw/react-codemirror": "^4.21.21"
81
84
  },
82
85
  "devDependencies": {
83
- "@babel/core": "^7.20.12",
84
- "@babel/preset-env": "^7.18.10",
85
- "@babel/preset-react": "^7.18.6",
86
- "@commitlint/cli": "^17.2.0",
87
- "@commitlint/config-conventional": "^17.2.0",
88
- "@sanity/icons": "^2.2.2",
89
- "@sanity/pkg-utils": "^2.1.1",
90
- "@sanity/plugin-kit": "^3.1.2",
91
- "@sanity/semantic-release-preset": "^2.0.2",
86
+ "@babel/core": "^7.23.6",
87
+ "@babel/preset-env": "^7.23.6",
88
+ "@babel/preset-react": "^7.23.3",
89
+ "@commitlint/cli": "^18.4.3",
90
+ "@commitlint/config-conventional": "^18.4.3",
91
+ "@sanity/icons": "^2.8.0",
92
+ "@sanity/pkg-utils": "^3.3.5",
93
+ "@sanity/plugin-kit": "^3.1.10",
94
+ "@sanity/semantic-release-preset": "^4.1.6",
92
95
  "@sanity/ui-workshop": "^1.2.0",
93
96
  "@sanity/ui-workshop-plugin-sanity": "^1.0.0",
94
- "@testing-library/jest-dom": "^5.16.5",
95
- "@testing-library/react": "^13.4.0",
96
- "@types/jest": "^29.1.2",
97
- "@types/react": "^18.0.26",
98
- "@types/styled-components": "^5.1.25",
99
- "@typescript-eslint/eslint-plugin": "^5.48.0",
100
- "@typescript-eslint/parser": "^5.48.0",
101
- "eslint": "^8.31.0",
102
- "eslint-config-prettier": "^8.6.0",
103
- "eslint-config-sanity": "^6.0.0",
104
- "eslint-plugin-prettier": "^4.2.1",
105
- "eslint-plugin-react": "^7.31.11",
97
+ "@testing-library/jest-dom": "^6.1.5",
98
+ "@testing-library/react": "^14.1.2",
99
+ "@types/jest": "^29.5.11",
100
+ "@types/react": "^18.2.45",
101
+ "@types/styled-components": "^5.1.34",
102
+ "@typescript-eslint/eslint-plugin": "^6.14.0",
103
+ "@typescript-eslint/parser": "^6.14.0",
104
+ "eslint": "^8.55.0",
105
+ "eslint-config-prettier": "^9.1.0",
106
+ "eslint-config-sanity": "^7.0.1",
107
+ "eslint-plugin-prettier": "^5.0.1",
108
+ "eslint-plugin-react": "^7.33.2",
106
109
  "eslint-plugin-react-hooks": "^4.6.0",
107
110
  "husky": "^8.0.1",
108
- "jest": "^28.1.3",
109
- "jest-environment-jsdom": "^28.1.3",
110
- "lint-staged": "^13.0.3",
111
+ "jest": "^29.7.0",
112
+ "jest-environment-jsdom": "^29.7.0",
113
+ "lint-staged": "^15.2.0",
111
114
  "npm-run-all": "^4.1.5",
112
- "prettier": "^2.8.1",
113
- "prettier-plugin-packagejson": "^2.3.0",
115
+ "prettier": "^3.1.1",
116
+ "prettier-plugin-packagejson": "^2.4.7",
114
117
  "react": "^18.2.0",
115
118
  "react-dom": "^18.2.0",
116
119
  "react-is": "^18.2.0",
117
- "rimraf": "^3.0.2",
118
- "sanity": "^3.2.3",
120
+ "rimraf": "^5.0.5",
121
+ "sanity": "^3.21.3",
119
122
  "sanity-extra": "^1.0.0",
120
123
  "sanity-testing-library": "^1.0.0",
121
- "styled-components": "^5.3.6",
122
- "ts-jest": "^28.0.7",
123
- "typescript": "^4.9.4"
124
+ "semantic-release": "^22.0.0",
125
+ "styled-components": "^6.1.1",
126
+ "ts-jest": "^29.1.1",
127
+ "typescript": "^5.3.3"
124
128
  },
125
129
  "peerDependencies": {
126
130
  "react": "^18",
127
131
  "react-dom": "^18",
128
132
  "sanity": "^3",
129
- "styled-components": "^5.2"
133
+ "styled-components": "^5.2 || ^6"
130
134
  },
131
135
  "engines": {
132
136
  "node": ">=14"
package/src/CodeInput.tsx CHANGED
@@ -77,7 +77,7 @@ export function CodeInput(props: CodeInputProps) {
77
77
 
78
78
  const onHighlightChange = useCallback(
79
79
  (lines: number[]) => onChange(set(lines, ['highlightedLines'])),
80
- [onChange]
80
+ [onChange],
81
81
  )
82
82
 
83
83
  const handleCodeChange = useCallback(
@@ -90,7 +90,7 @@ export function CodeInput(props: CodeInputProps) {
90
90
  code ? set(code, path) : unset(path),
91
91
  ])
92
92
  },
93
- [onChange, type]
93
+ [onChange, type],
94
94
  )
95
95
  const {languages, language, languageMode} = useLanguageMode(props.schemaType, props.value)
96
96
 
@@ -132,7 +132,7 @@ export function CodeInput(props: CodeInputProps) {
132
132
  elementProps.onBlur,
133
133
  readOnly,
134
134
  value,
135
- ]
135
+ ],
136
136
  )
137
137
 
138
138
  return (
@@ -4,7 +4,7 @@ import {CodeInputLanguage} from './types'
4
4
  import {LanguageInput} from './LanguageInput'
5
5
 
6
6
  export function LanguageField(
7
- props: MemberFieldProps & {member: FieldMember; language: string; languages: CodeInputLanguage[]}
7
+ props: MemberFieldProps & {member: FieldMember; language: string; languages: CodeInputLanguage[]},
8
8
  ) {
9
9
  const {member, languages, language, renderItem, renderField, renderPreview} = props
10
10
 
@@ -18,7 +18,7 @@ export function LanguageField(
18
18
  />
19
19
  )
20
20
  },
21
- [languages, language]
21
+ [languages, language],
22
22
  )
23
23
 
24
24
  return (
@@ -17,7 +17,7 @@ export function LanguageInput(props: LanguageInputProps) {
17
17
  const newValue = e.currentTarget.value
18
18
  onChange(newValue ? set(newValue) : unset())
19
19
  },
20
- [onChange]
20
+ [onChange],
21
21
  )
22
22
 
23
23
  return (
@@ -25,98 +25,97 @@ export interface CodeMirrorProps extends ReactCodeMirrorProps {
25
25
  *
26
26
  * It is also responsible for integrating any CodeMirror extensions.
27
27
  */
28
- const CodeMirrorProxy = forwardRef<ReactCodeMirrorRef, CodeMirrorProps>(function CodeMirrorProxy(
29
- props,
30
- ref
31
- ) {
32
- const {
33
- basicSetup: basicSetupProp,
34
- highlightLines,
35
- languageMode,
36
- onHighlightChange,
37
- readOnly,
38
- value,
39
- ...codeMirrorProps
40
- } = props
41
-
42
- const themeCtx = useRootTheme()
43
- const codeMirrorTheme = useCodeMirrorTheme()
44
- const [editorView, setEditorView] = useState<EditorView | undefined>(undefined)
45
-
46
- // Resolve extensions
47
- const themeExtension = useThemeExtension()
48
- const fontSizeExtension = useFontSizeExtension({fontSize: 1})
49
- const languageExtension = useLanguageExtension(languageMode)
50
- const highlightLineExtension = useMemo(
51
- () =>
52
- highlightLine({
53
- onHighlightChange,
54
- readOnly,
55
- theme: themeCtx,
56
- }),
57
- [onHighlightChange, readOnly, themeCtx]
58
- )
59
-
60
- const extensions = useMemo(() => {
61
- const baseExtensions = [
62
- themeExtension,
63
- fontSizeExtension,
64
- highlightLineExtension,
65
- EditorView.lineWrapping,
66
- ]
67
- if (languageExtension) {
68
- return [...baseExtensions, languageExtension]
69
- }
70
- return baseExtensions
71
- }, [fontSizeExtension, highlightLineExtension, languageExtension, themeExtension])
72
-
73
- useEffect(() => {
74
- if (editorView) {
75
- setHighlightedLines(editorView, highlightLines ?? [])
76
- }
77
- }, [editorView, highlightLines, value])
78
-
79
- const initialState = useMemo(() => {
80
- return {
81
- json: {
82
- doc: value ?? '',
83
- selection: {
84
- main: 0,
85
- ranges: [{anchor: 0, head: 0}],
28
+ const CodeMirrorProxy = forwardRef<ReactCodeMirrorRef, CodeMirrorProps>(
29
+ function CodeMirrorProxy(props, ref) {
30
+ const {
31
+ basicSetup: basicSetupProp,
32
+ highlightLines,
33
+ languageMode,
34
+ onHighlightChange,
35
+ readOnly,
36
+ value,
37
+ ...codeMirrorProps
38
+ } = props
39
+
40
+ const themeCtx = useRootTheme()
41
+ const codeMirrorTheme = useCodeMirrorTheme()
42
+ const [editorView, setEditorView] = useState<EditorView | undefined>(undefined)
43
+
44
+ // Resolve extensions
45
+ const themeExtension = useThemeExtension()
46
+ const fontSizeExtension = useFontSizeExtension({fontSize: 1})
47
+ const languageExtension = useLanguageExtension(languageMode)
48
+ const highlightLineExtension = useMemo(
49
+ () =>
50
+ highlightLine({
51
+ onHighlightChange,
52
+ readOnly,
53
+ theme: themeCtx,
54
+ }),
55
+ [onHighlightChange, readOnly, themeCtx],
56
+ )
57
+
58
+ const extensions = useMemo(() => {
59
+ const baseExtensions = [
60
+ themeExtension,
61
+ fontSizeExtension,
62
+ highlightLineExtension,
63
+ EditorView.lineWrapping,
64
+ ]
65
+ if (languageExtension) {
66
+ return [...baseExtensions, languageExtension]
67
+ }
68
+ return baseExtensions
69
+ }, [fontSizeExtension, highlightLineExtension, languageExtension, themeExtension])
70
+
71
+ useEffect(() => {
72
+ if (editorView) {
73
+ setHighlightedLines(editorView, highlightLines ?? [])
74
+ }
75
+ }, [editorView, highlightLines, value])
76
+
77
+ const initialState = useMemo(() => {
78
+ return {
79
+ json: {
80
+ doc: value ?? '',
81
+ selection: {
82
+ main: 0,
83
+ ranges: [{anchor: 0, head: 0}],
84
+ },
85
+ highlight: highlightLines ?? [],
86
86
  },
87
- highlight: highlightLines ?? [],
88
- },
89
- fields: highlightState,
90
- }
91
- // only need to calculate this on initial render
92
- // eslint-disable-next-line react-hooks/exhaustive-deps
93
- }, [])
94
-
95
- const handleCreateEditor = useCallback((view: EditorView) => {
96
- setEditorView(view)
97
- }, [])
98
-
99
- const basicSetup = useMemo(
100
- () =>
101
- basicSetupProp ?? {
102
- highlightActiveLine: false,
103
- },
104
- [basicSetupProp]
105
- )
106
-
107
- return (
108
- <CodeMirror
109
- {...codeMirrorProps}
110
- value={value}
111
- ref={ref}
112
- extensions={extensions}
113
- theme={codeMirrorTheme}
114
- onCreateEditor={handleCreateEditor}
115
- initialState={initialState}
116
- basicSetup={basicSetup}
117
- />
118
- )
119
- })
87
+ fields: highlightState,
88
+ }
89
+ // only need to calculate this on initial render
90
+ // eslint-disable-next-line react-hooks/exhaustive-deps
91
+ }, [])
92
+
93
+ const handleCreateEditor = useCallback((view: EditorView) => {
94
+ setEditorView(view)
95
+ }, [])
96
+
97
+ const basicSetup = useMemo(
98
+ () =>
99
+ basicSetupProp ?? {
100
+ highlightActiveLine: false,
101
+ },
102
+ [basicSetupProp],
103
+ )
104
+
105
+ return (
106
+ <CodeMirror
107
+ {...codeMirrorProps}
108
+ value={value}
109
+ ref={ref}
110
+ extensions={extensions}
111
+ theme={codeMirrorTheme}
112
+ onCreateEditor={handleCreateEditor}
113
+ initialState={initialState}
114
+ basicSetup={basicSetup}
115
+ />
116
+ )
117
+ },
118
+ )
120
119
 
121
120
  function useLanguageExtension(mode?: string) {
122
121
  const codeConfig = useContext(CodeInputConfigContext)
@@ -130,7 +129,7 @@ function useLanguageExtension(mode?: string) {
130
129
  const codeMode = modes.find((m) => m.name === mode)
131
130
  if (!codeMode?.loader) {
132
131
  console.warn(
133
- `Found no codeMode for language mode ${mode}, syntax highlighting will be disabled.`
132
+ `Found no codeMode for language mode ${mode}, syntax highlighting will be disabled.`,
134
133
  )
135
134
  }
136
135
  let active = true
@@ -27,14 +27,14 @@ export const defaultCodeModes: CodeMode[] = [
27
27
  name: 'typescript',
28
28
  loader: () =>
29
29
  import('@codemirror/lang-javascript').then(({javascript}) =>
30
- javascript({jsx: false, typescript: true})
30
+ javascript({jsx: false, typescript: true}),
31
31
  ),
32
32
  },
33
33
  {
34
34
  name: 'tsx',
35
35
  loader: () =>
36
36
  import('@codemirror/lang-javascript').then(({javascript}) =>
37
- javascript({jsx: true, typescript: true})
37
+ javascript({jsx: true, typescript: true}),
38
38
  ),
39
39
  },
40
40
  {name: 'php', loader: () => import('@codemirror/lang-php').then(({php}) => php())},
@@ -54,7 +54,7 @@ export const defaultCodeModes: CodeMode[] = [
54
54
  name: 'csharp',
55
55
  loader: () =>
56
56
  import('@codemirror/legacy-modes/mode/clike').then(({csharp}) =>
57
- StreamLanguage.define(csharp)
57
+ StreamLanguage.define(csharp),
58
58
  ),
59
59
  },
60
60
  {
@@ -86,7 +86,7 @@ export const defaultCodeModes: CodeMode[] = [
86
86
  name: 'python',
87
87
  loader: () =>
88
88
  import('@codemirror/legacy-modes/mode/python').then(({python}) =>
89
- StreamLanguage.define(python)
89
+ StreamLanguage.define(python),
90
90
  ),
91
91
  },
92
92
  {
@@ -29,7 +29,7 @@ function useLanguageAlternatives(type: CodeSchemaType) {
29
29
 
30
30
  if (!Array.isArray(languageAlternatives)) {
31
31
  throw new Error(
32
- `'options.languageAlternatives' should be an array, got ${typeof languageAlternatives}`
32
+ `'options.languageAlternatives' should be an array, got ${typeof languageAlternatives}`,
33
33
  )
34
34
  }
35
35
 
@@ -41,7 +41,7 @@ function useLanguageAlternatives(type: CodeSchemaType) {
41
41
  `'options.languageAlternatives' lists a language with value "%s", which is an alias of "%s" - please replace the value to read "%s"`,
42
42
  val,
43
43
  alias,
44
- alias
44
+ alias,
45
45
  )
46
46
 
47
47
  return acc.concat({title, value: alias, mode: mode})
@@ -4,13 +4,13 @@ import {FieldMember, ObjectMember} from 'sanity'
4
4
  /** @internal */
5
5
  export function useFieldMember(
6
6
  members: ObjectMember[],
7
- fieldName: string
7
+ fieldName: string,
8
8
  ): FieldMember | undefined {
9
9
  return useMemo(
10
10
  () =>
11
11
  members.find(
12
- (member): member is FieldMember => member.kind === 'field' && member.name === fieldName
12
+ (member): member is FieldMember => member.kind === 'field' && member.name === fieldName,
13
13
  ),
14
- [members, fieldName]
14
+ [members, fieldName],
15
15
  )
16
16
  }