@sanity/code-input 3.0.0-v3-studio.8 → 3.0.0-v3-studio.10

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/package.json CHANGED
@@ -1,17 +1,20 @@
1
1
  {
2
2
  "name": "@sanity/code-input",
3
- "version": "3.0.0-v3-studio.8",
3
+ "version": "3.0.0-v3-studio.10",
4
4
  "description": "Ace editor for editing code",
5
5
  "source": "./src/index.ts",
6
6
  "exports": {
7
7
  ".": {
8
- "require": "./lib/cjs/index.js",
9
- "default": "./lib/esm/index.js"
8
+ "types": "./lib/src/index.d.ts",
9
+ "source": "./src/index.ts",
10
+ "import": "./lib/index.esm.js",
11
+ "require": "./lib/index.js",
12
+ "default": "./lib/index.esm.js"
10
13
  }
11
14
  },
12
- "main": "./lib/cjs/index.js",
13
- "module": "./lib/esm/index.js",
14
- "types": "./lib/types/index.d.ts",
15
+ "main": "./lib/index.js",
16
+ "module": "./lib/index.esm.js",
17
+ "types": "./lib/src/index.d.ts",
15
18
  "files": [
16
19
  "src",
17
20
  "lib",
@@ -22,10 +25,10 @@
22
25
  "license": "MIT",
23
26
  "scripts": {
24
27
  "clean": "rimraf lib",
25
- "prebuild": "npm run clean && plugin-kit verify-package --silent",
26
- "build": "parcel build --no-cache",
28
+ "prebuild": "npm run clean && plugin-kit verify-package --silent && pkg-utils",
29
+ "build": "pkg-utils build",
30
+ "watch": "pkg-utils watch",
27
31
  "link-watch": "plugin-kit link-watch",
28
- "watch": "parcel watch",
29
32
  "prepublishOnly": "npm run build",
30
33
  "compile": "tsc --noEmit",
31
34
  "lint": "eslint .",
@@ -45,7 +48,7 @@
45
48
  ],
46
49
  "dependencies": {
47
50
  "@sanity/icons": "^1.2.8",
48
- "@sanity/incompatible-plugin": "^1.0.1",
51
+ "@sanity/incompatible-plugin": "^1.0.4",
49
52
  "@sanity/ui": "^0.38.2",
50
53
  "ace-builds": "^1.9.5",
51
54
  "react-ace": "^10.1.0"
@@ -55,13 +58,14 @@
55
58
  "@babel/preset-react": "^7.18.6",
56
59
  "@commitlint/cli": "^17.1.2",
57
60
  "@commitlint/config-conventional": "^17.1.0",
58
- "@parcel/packager-ts": "^2.6.0",
59
- "@parcel/transformer-typescript-types": "^2.6.0",
61
+ "@parcel/packager-ts": "2.6.2",
62
+ "@parcel/transformer-typescript-types": "2.6.2",
60
63
  "@sanity/plugin-kit": "^1.1.0-ecosystem-preset.5",
64
+ "@sanity/pkg-utils": "^1.11.0",
61
65
  "@sanity/semantic-release-preset": "^2.0.1",
62
66
  "@testing-library/jest-dom": "^5.16.5",
63
- "@testing-library/react": "^12.1.3",
64
- "@types/jest": "^28.1.6",
67
+ "@testing-library/react": "^13.4.0",
68
+ "@types/jest": "^29.1.2",
65
69
  "@types/styled-components": "^5.1.25",
66
70
  "@typescript-eslint/eslint-plugin": "^5.12.0",
67
71
  "@typescript-eslint/parser": "^5.12.0",
@@ -75,19 +79,19 @@
75
79
  "jest": "^28.1.3",
76
80
  "jest-environment-jsdom": "^28.1.3",
77
81
  "lint-staged": "^13.0.3",
78
- "parcel": "^2.6.0",
82
+ "parcel": "2.6.2",
79
83
  "prettier": "^2.6.2",
80
- "react": "^17.0.0",
81
- "react-dom": "^17.0.0",
84
+ "react": "^18.0.0",
85
+ "react-dom": "^18.0.0",
82
86
  "rimraf": "^3.0.2",
83
- "sanity": "3.0.0-dev-preview.17",
87
+ "sanity": "3.0.0-dev-preview.21",
84
88
  "styled-components": "^5.2.0",
85
89
  "ts-jest": "^28.0.7",
86
90
  "typescript": "4.7.4"
87
91
  },
88
92
  "peerDependencies": {
89
- "react": "^17.0.0 || ^18.0.0",
90
- "react-dom": "^17.0.0 || ^18.0.0",
93
+ "react": "^18.0.0",
94
+ "react-dom": "^18.0.0",
91
95
  "sanity": "dev-preview",
92
96
  "styled-components": "^5.2.0"
93
97
  },
@@ -105,7 +109,8 @@
105
109
  },
106
110
  "sanityPlugin": {
107
111
  "verifyPackage": {
108
- "babelConfig": false
112
+ "babelConfig": false,
113
+ "scripts": false
109
114
  }
110
115
  }
111
116
  }
package/src/CodeInput.tsx CHANGED
@@ -1,7 +1,17 @@
1
1
  /* eslint-disable react/jsx-handler-names */
2
2
  import React, {Suspense, useCallback, useEffect, useImperativeHandle, useMemo, useRef} from 'react'
3
- import {FieldMember, MemberField, ObjectInputProps, set, setIfMissing, unset} from 'sanity/form'
4
- import {InputProps, ObjectSchemaType, StringInputProps, useColorScheme} from 'sanity'
3
+ import {
4
+ InputProps,
5
+ ObjectSchemaType,
6
+ StringInputProps,
7
+ useColorScheme,
8
+ FieldMember,
9
+ MemberField,
10
+ ObjectInputProps,
11
+ set,
12
+ setIfMissing,
13
+ unset,
14
+ } from 'sanity'
5
15
  import {Card, Select, Stack, ThemeColorSchemeKey} from '@sanity/ui'
6
16
  import styled from 'styled-components'
7
17
  import createHighlightMarkers, {highlightMarkersCSS} from './createHighlightMarkers'
@@ -44,16 +54,27 @@ const EditorContainer = styled(Card)`
44
54
  }
45
55
  `
46
56
 
57
+ /**
58
+ * @public
59
+ */
60
+ export interface CodeOptions {
61
+ theme?: string
62
+ darkTheme?: string
63
+ languageAlternatives?: CodeInputLanguage[]
64
+ language?: string
65
+ withFilename?: boolean
66
+ }
67
+
68
+ /**
69
+ * @public
70
+ */
47
71
  export type CodeSchemaType = Omit<ObjectSchemaType, 'options'> & {
48
- options?: {
49
- theme?: string
50
- darkTheme?: string
51
- languageAlternatives: CodeInputLanguage[]
52
- language: string
53
- withFilename?: boolean
54
- }
72
+ options?: CodeOptions
55
73
  }
56
74
 
75
+ /**
76
+ * @public
77
+ */
57
78
  export type CodeInputProps = ObjectInputProps<CodeInputValue, CodeSchemaType> & {
58
79
  /** @internal */
59
80
  colorScheme?: ThemeColorSchemeKey
@@ -247,7 +268,7 @@ export function CodeInput(props: CodeInputProps) {
247
268
  const mode = configured?.mode || (supported ? language : 'text')
248
269
 
249
270
  const renderLanguageInput = useCallback(
250
- (inputProps: InputProps) => {
271
+ (inputProps: Omit<InputProps, 'renderDefault'>) => {
251
272
  return (
252
273
  <Select
253
274
  {...(inputProps as StringInputProps)}
@@ -3,8 +3,9 @@ import styled from 'styled-components'
3
3
  import {Box} from '@sanity/ui'
4
4
  import {ACE_EDITOR_PROPS, ACE_SET_OPTIONS} from './config'
5
5
  import createHighlightMarkers from './createHighlightMarkers'
6
- import {CodeInputType, CodeInputValue} from './types'
6
+ import {CodeInputValue} from './types'
7
7
  import {useAceEditor} from './ace-editor/AceEditorLazy'
8
+ import {CodeSchemaType} from './CodeInput'
8
9
 
9
10
  const PreviewContainer = styled(Box)`
10
11
  position: relative;
@@ -25,11 +26,17 @@ const PreviewInner = styled(Box)`
25
26
  }
26
27
  `
27
28
 
29
+ /**
30
+ * @public
31
+ */
28
32
  export interface PreviewCodeProps {
29
- type?: CodeInputType
30
- value?: CodeInputValue
33
+ type?: CodeSchemaType
34
+ value?: {selection?: CodeInputValue}
31
35
  }
32
36
 
37
+ /**
38
+ * @public
39
+ */
33
40
  export default function PreviewCode(props: PreviewCodeProps) {
34
41
  const aceEditorRef = useRef<any>()
35
42
 
@@ -52,14 +59,15 @@ export default function PreviewCode(props: PreviewCodeProps) {
52
59
  const {value, type} = props
53
60
  const fixedLanguage = type?.options?.language
54
61
 
55
- const mode = value?.language || fixedLanguage || 'text'
56
- const AceEditor = useAceEditor()
62
+ const selection = value?.selection
63
+ const mode = selection?.language || fixedLanguage || 'text'
57
64
 
65
+ const AceEditor = useAceEditor()
58
66
  return (
59
67
  <PreviewContainer>
60
68
  <PreviewInner padding={4}>
61
69
  {AceEditor && (
62
- <Suspense fallback={<div>Loading code editor...</div>}>
70
+ <Suspense fallback={<div>Loading code preview...</div>}>
63
71
  <AceEditor
64
72
  ref={aceEditorRef}
65
73
  focus={false}
@@ -73,10 +81,10 @@ export default function PreviewCode(props: PreviewCodeProps) {
73
81
  showPrintMargin={false}
74
82
  highlightActiveLine={false}
75
83
  cursorStart={-1}
76
- value={(value && value.code) || ''}
84
+ value={selection?.code || ''}
77
85
  markers={
78
- value && value.highlightedLines
79
- ? createHighlightMarkers(value.highlightedLines)
86
+ selection?.highlightedLines
87
+ ? createHighlightMarkers(selection.highlightedLines)
80
88
  : undefined
81
89
  }
82
90
  tabSize={2}
@@ -5,6 +5,19 @@ import {queryByText, render, waitForElementToBeRemoved} from '@testing-library/r
5
5
  import {useAceEditor} from './AceEditorLazy'
6
6
 
7
7
  describe('AceEditor - client', () => {
8
+ beforeEach(() => {
9
+ jest
10
+ .spyOn(window, 'requestAnimationFrame')
11
+ .mockImplementation((callback: FrameRequestCallback): number => {
12
+ callback(0)
13
+ return 0
14
+ })
15
+ })
16
+
17
+ afterEach(() => {
18
+ ;(window.requestAnimationFrame as any).mockRestore()
19
+ })
20
+
8
21
  it('should render suspended ace editor', async () => {
9
22
  const fallbackString = 'loading'
10
23
 
@@ -12,7 +12,7 @@ export const AceEditorLazy = React.lazy(() => import('./editorSupport'))
12
12
  export function useAceEditor() {
13
13
  const [mounted, setMounted] = useState(false)
14
14
  useEffect(() => {
15
- setMounted(true)
15
+ requestAnimationFrame(() => setMounted(true))
16
16
  }, [])
17
17
 
18
18
  return mounted ? AceEditorLazy : null
package/src/index.ts CHANGED
@@ -1,12 +1,18 @@
1
1
  import {createPlugin} from 'sanity'
2
- import schema from './schema'
3
- import PreviewCode from './PreviewCode'
4
2
 
5
- export type {CodeInputProps, CodeSchemaType} from './CodeInput'
3
+ import {codeSchema, codeTypeName, CodeDefinition} from './schema'
4
+ import PreviewCode, {PreviewCodeProps} from './PreviewCode'
5
+ export type {CodeInputProps, CodeSchemaType, CodeOptions} from './CodeInput'
6
+
6
7
  export type {CodeInputLanguage, CodeInputValue} from './types'
7
- export {PreviewCode}
8
8
 
9
+ export {PreviewCode, type PreviewCodeProps}
10
+ export {codeSchema, codeTypeName}
11
+ export type {CodeDefinition}
12
+ /**
13
+ * @public
14
+ */
9
15
  export const codeInput = createPlugin({
10
16
  name: '@sanity/code-input',
11
- schema: {types: [schema]},
17
+ schema: {types: [codeSchema]},
12
18
  })
package/src/schema.tsx CHANGED
@@ -1,15 +1,38 @@
1
1
  import {CodeBlockIcon} from '@sanity/icons'
2
- import {CodeInput} from './CodeInput'
2
+ import {CodeInput, CodeOptions} from './CodeInput'
3
3
  import PreviewCode, {PreviewCodeProps} from './PreviewCode'
4
4
  import {getMedia} from './getMedia'
5
- import {defineType} from 'sanity'
5
+ import {defineType, ObjectDefinition} from 'sanity'
6
6
 
7
7
  export type {CodeInputProps, CodeSchemaType} from './CodeInput'
8
8
 
9
9
  export type {CodeInputLanguage, CodeInputValue} from './types'
10
10
  export type {PreviewCode, PreviewCodeProps, CodeInput}
11
11
 
12
- export default defineType({
12
+ /**
13
+ * @public
14
+ */
15
+ export const codeTypeName = 'code' as const
16
+
17
+ /**
18
+ * @public
19
+ */
20
+ export interface CodeDefinition extends Omit<ObjectDefinition, 'type' | 'fields' | 'options'> {
21
+ type: typeof codeTypeName
22
+ options?: CodeOptions
23
+ }
24
+
25
+ declare module '@sanity/types' {
26
+ // makes type: 'code' narrow correctly when using defineType/defineField/defineArrayMember
27
+ export interface IntrinsicDefinitions {
28
+ code: CodeDefinition
29
+ }
30
+ }
31
+
32
+ /**
33
+ * @public
34
+ */
35
+ export const codeSchema = defineType({
13
36
  name: 'code',
14
37
  type: 'object',
15
38
  title: 'Code',
@@ -59,6 +82,7 @@ export default defineType({
59
82
  return {
60
83
  title: value.filename || (value.language || 'unknown').toUpperCase(),
61
84
  media: getMedia(value?.language),
85
+ selection: value,
62
86
  }
63
87
  },
64
88
  },
package/src/types.ts CHANGED
@@ -1,22 +1,15 @@
1
+ /**
2
+ * @public
3
+ */
1
4
  export interface CodeInputLanguage {
2
5
  title: string
3
6
  value: string
4
7
  mode?: string
5
8
  }
6
9
 
7
- export interface CodeInputType {
8
- name?: string
9
- title?: string
10
- description?: string
11
- fields: {name: string; title?: string; placeholder?: string}[]
12
- options?: {
13
- theme?: string
14
- languageAlternatives: CodeInputLanguage[]
15
- language: string
16
- withFilename?: boolean
17
- }
18
- }
19
-
10
+ /**
11
+ * @public
12
+ */
20
13
  export interface CodeInputValue {
21
14
  _type?: 'code'
22
15
  code?: string