@sanity/code-input 4.1.4 → 5.1.0
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/_chunks-cjs/CodeMirrorProxy.cjs +429 -0
- package/lib/_chunks-cjs/CodeMirrorProxy.cjs.map +1 -0
- package/lib/_chunks-cjs/index.cjs +5971 -0
- package/lib/_chunks-cjs/index.cjs.map +1 -0
- package/lib/_chunks-es/CodeMirrorProxy.js +417 -0
- package/lib/_chunks-es/CodeMirrorProxy.js.map +1 -0
- package/lib/_chunks-es/index.js +5970 -0
- package/lib/_chunks-es/index.js.map +1 -0
- package/lib/_legacy/CodeMirrorProxy.esm.js +417 -0
- package/lib/_legacy/CodeMirrorProxy.esm.js.map +1 -0
- package/lib/_legacy/index.esm.js +5970 -0
- package/lib/_legacy/index.esm.js.map +1 -0
- package/lib/index.cjs +3 -6
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +121 -0
- package/lib/index.d.ts +2 -2
- package/lib/index.esm.js +8 -0
- package/lib/index.esm.js.map +1 -0
- package/lib/index.js +7 -1
- package/lib/index.js.map +1 -1
- package/package.json +37 -42
- package/src/CodeInput.tsx +4 -3
- package/src/LanguageField.tsx +2 -1
- package/src/LanguageInput.tsx +3 -2
- package/src/PreviewCode.tsx +5 -4
- package/src/codemirror/CodeMirrorProxy.tsx +7 -6
- package/src/codemirror/CodeModeContext.tsx +2 -1
- package/src/codemirror/defaultCodeModes.ts +1 -1
- package/src/codemirror/extensions/backwardsCompatibleTone.ts +19 -0
- package/src/codemirror/extensions/highlightLineExtension.ts +12 -6
- package/src/codemirror/extensions/theme.ts +8 -4
- package/src/codemirror/extensions/useCodeMirrorTheme.ts +5 -4
- package/src/codemirror/extensions/useFontSize.ts +1 -1
- package/src/codemirror/useCodeMirror-client.test.tsx +10 -7
- package/src/codemirror/useCodeMirror-server.test.tsx +1 -0
- package/src/codemirror/useLanguageMode.tsx +11 -3
- package/src/index.ts +4 -7
- package/src/plugin.tsx +3 -2
- package/src/schema.tsx +3 -2
- package/lib/_chunks/CodeMirrorProxy-A8RWnbXx.js +0 -615
- package/lib/_chunks/CodeMirrorProxy-A8RWnbXx.js.map +0 -1
- package/lib/_chunks/CodeMirrorProxy-tci8hAnf.cjs +0 -623
- package/lib/_chunks/CodeMirrorProxy-tci8hAnf.cjs.map +0 -1
- package/lib/_chunks/index-0LQQ9tED.cjs +0 -4686
- package/lib/_chunks/index-0LQQ9tED.cjs.map +0 -1
- package/lib/_chunks/index-4cT0QYPN.js +0 -4672
- package/lib/_chunks/index-4cT0QYPN.js.map +0 -1
- package/lib/index.cjs.js +0 -7
- package/src/__workshop__/index.ts +0 -22
- package/src/__workshop__/lazy.tsx +0 -54
- package/src/__workshop__/preview.tsx +0 -24
- package/src/__workshop__/props.tsx +0 -24
package/lib/index.cjs
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
var index = require('./_chunks/index-0LQQ9tED.cjs');
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: !0 });
|
|
3
|
+
var index = require("./_chunks-cjs/index.cjs");
|
|
7
4
|
exports.PreviewCode = index.PreviewCode;
|
|
8
5
|
exports.codeInput = index.codeInput;
|
|
9
6
|
exports.codeSchema = index.codeSchema;
|
package/lib/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;"}
|
package/lib/index.d.cts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import type {Extension} from '@codemirror/state'
|
|
2
|
+
import {JSX as JSX_2} from 'react'
|
|
3
|
+
import {ObjectDefinition} from 'sanity'
|
|
4
|
+
import {ObjectInputProps} from 'sanity'
|
|
5
|
+
import {ObjectSchemaType} from 'sanity'
|
|
6
|
+
import {Plugin as Plugin_2} from 'sanity'
|
|
7
|
+
import {PreviewConfig} from 'sanity'
|
|
8
|
+
import {PreviewProps} from 'sanity'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export declare interface CodeDefinition
|
|
14
|
+
extends Omit<ObjectDefinition, 'type' | 'fields' | 'options'> {
|
|
15
|
+
type: typeof codeTypeName
|
|
16
|
+
options?: CodeOptions
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** @public */
|
|
20
|
+
export declare function CodeInput(props: CodeInputProps): JSX_2.Element
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
export declare const codeInput: Plugin_2<void | CodeInputConfig>
|
|
26
|
+
|
|
27
|
+
declare interface CodeInputConfig {
|
|
28
|
+
codeModes?: CodeMode[]
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export declare interface CodeInputLanguage {
|
|
32
|
+
title: string
|
|
33
|
+
value: string
|
|
34
|
+
mode?: string
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
export declare interface CodeInputProps extends ObjectInputProps<CodeInputValue, CodeSchemaType> {}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
export declare interface CodeInputValue {
|
|
46
|
+
_type?: 'code'
|
|
47
|
+
code?: string
|
|
48
|
+
filename?: string
|
|
49
|
+
language?: string
|
|
50
|
+
highlightedLines?: number[]
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
declare interface CodeMode {
|
|
54
|
+
name: string
|
|
55
|
+
loader: ModeLoader
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @public
|
|
60
|
+
*/
|
|
61
|
+
export declare interface CodeOptions {
|
|
62
|
+
theme?: string
|
|
63
|
+
darkTheme?: string
|
|
64
|
+
languageAlternatives?: CodeInputLanguage[]
|
|
65
|
+
language?: string
|
|
66
|
+
withFilename?: boolean
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @public
|
|
71
|
+
*/
|
|
72
|
+
export declare const codeSchema: {
|
|
73
|
+
type: 'object'
|
|
74
|
+
name: 'code'
|
|
75
|
+
} & Omit<ObjectDefinition, 'preview'> & {
|
|
76
|
+
preview?:
|
|
77
|
+
| PreviewConfig<
|
|
78
|
+
{
|
|
79
|
+
language: string
|
|
80
|
+
code: string
|
|
81
|
+
filename: string
|
|
82
|
+
highlightedLines: string
|
|
83
|
+
},
|
|
84
|
+
Record<'code' | 'language' | 'filename' | 'highlightedLines', any>
|
|
85
|
+
>
|
|
86
|
+
| undefined
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @public
|
|
91
|
+
*/
|
|
92
|
+
export declare interface CodeSchemaType extends Omit<ObjectSchemaType, 'options'> {
|
|
93
|
+
options?: CodeOptions
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @public
|
|
98
|
+
*/
|
|
99
|
+
export declare const codeTypeName: 'code'
|
|
100
|
+
|
|
101
|
+
declare type ModeLoader = () => Promise<Extension | undefined> | Extension | undefined
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @public
|
|
105
|
+
*/
|
|
106
|
+
export declare function PreviewCode(props: PreviewCodeProps): JSX_2.Element
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* @public
|
|
110
|
+
*/
|
|
111
|
+
export declare interface PreviewCodeProps extends PreviewProps {
|
|
112
|
+
selection?: CodeInputValue
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export {}
|
|
116
|
+
|
|
117
|
+
declare module '@sanity/types' {
|
|
118
|
+
interface IntrinsicDefinitions {
|
|
119
|
+
code: CodeDefinition
|
|
120
|
+
}
|
|
121
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {Extension} from '@codemirror/state'
|
|
2
|
-
import {JSX as JSX_2} from 'react
|
|
1
|
+
import type {Extension} from '@codemirror/state'
|
|
2
|
+
import {JSX as JSX_2} from 'react'
|
|
3
3
|
import {ObjectDefinition} from 'sanity'
|
|
4
4
|
import {ObjectInputProps} from 'sanity'
|
|
5
5
|
import {ObjectSchemaType} from 'sanity'
|
package/lib/index.esm.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
|
package/lib/index.js
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import { PreviewCode, codeInput, codeSchema, codeTypeName } from "./_chunks-es/index.js";
|
|
2
|
+
export {
|
|
3
|
+
PreviewCode,
|
|
4
|
+
codeInput,
|
|
5
|
+
codeSchema,
|
|
6
|
+
codeTypeName
|
|
7
|
+
};
|
|
2
8
|
//# 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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/code-input",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"description": "Sanity input component for code, powered by CodeMirror",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -25,20 +25,15 @@
|
|
|
25
25
|
"type": "module",
|
|
26
26
|
"exports": {
|
|
27
27
|
".": {
|
|
28
|
-
"types": "./lib/index.d.ts",
|
|
29
28
|
"source": "./src/index.ts",
|
|
30
|
-
"require": "./lib/index.cjs",
|
|
31
|
-
"node": {
|
|
32
|
-
"import": "./lib/index.cjs.js"
|
|
33
|
-
},
|
|
34
29
|
"import": "./lib/index.js",
|
|
30
|
+
"require": "./lib/index.cjs",
|
|
35
31
|
"default": "./lib/index.js"
|
|
36
32
|
},
|
|
37
33
|
"./package.json": "./package.json"
|
|
38
34
|
},
|
|
39
35
|
"main": "./lib/index.cjs",
|
|
40
|
-
"module": "./lib/index.js",
|
|
41
|
-
"source": "./src/index.ts",
|
|
36
|
+
"module": "./lib/index.esm.js",
|
|
42
37
|
"types": "./lib/index.d.ts",
|
|
43
38
|
"files": [
|
|
44
39
|
"lib",
|
|
@@ -46,54 +41,53 @@
|
|
|
46
41
|
"src",
|
|
47
42
|
"v2-incompatible.js"
|
|
48
43
|
],
|
|
44
|
+
"sideEffects": false,
|
|
45
|
+
"browserslist": "extends @sanity/browserslist-config",
|
|
49
46
|
"scripts": {
|
|
50
|
-
"
|
|
51
|
-
"build": "run-s clean && plugin-kit verify-package --silent && pkg-utils build --strict && pkg-utils --strict",
|
|
47
|
+
"build": "plugin-kit verify-package --silent && pkg-utils build --strict --check --clean",
|
|
52
48
|
"clean": "rimraf lib",
|
|
53
49
|
"compile": "tsc --noEmit",
|
|
54
|
-
"dev": "
|
|
50
|
+
"dev": "sanity dev",
|
|
55
51
|
"format": "prettier --write --cache --ignore-unknown .",
|
|
56
52
|
"link-watch": "plugin-kit link-watch",
|
|
57
53
|
"lint": "eslint .",
|
|
58
54
|
"prepare": "husky install",
|
|
59
|
-
"prepublishOnly": "run
|
|
55
|
+
"prepublishOnly": "npm run build",
|
|
60
56
|
"test": "jest",
|
|
61
57
|
"watch": "pkg-utils watch --strict"
|
|
62
58
|
},
|
|
63
59
|
"dependencies": {
|
|
64
|
-
"@codemirror/autocomplete": "^6.
|
|
65
|
-
"@codemirror/commands": "^6.
|
|
66
|
-
"@codemirror/lang-html": "^6.4.
|
|
60
|
+
"@codemirror/autocomplete": "^6.18.3",
|
|
61
|
+
"@codemirror/commands": "^6.7.1",
|
|
62
|
+
"@codemirror/lang-html": "^6.4.9",
|
|
67
63
|
"@codemirror/lang-java": "^6.0.1",
|
|
68
|
-
"@codemirror/lang-javascript": "^6.2.
|
|
64
|
+
"@codemirror/lang-javascript": "^6.2.2",
|
|
69
65
|
"@codemirror/lang-json": "^6.0.1",
|
|
70
|
-
"@codemirror/lang-markdown": "^6.
|
|
66
|
+
"@codemirror/lang-markdown": "^6.3.1",
|
|
71
67
|
"@codemirror/lang-php": "^6.0.1",
|
|
72
|
-
"@codemirror/lang-sql": "^6.
|
|
73
|
-
"@codemirror/language": "^6.
|
|
74
|
-
"@codemirror/legacy-modes": "^6.
|
|
75
|
-
"@codemirror/search": "^6.5.
|
|
76
|
-
"@codemirror/state": "^6.
|
|
77
|
-
"@codemirror/view": "^6.
|
|
78
|
-
"@juggle/resize-observer": "^3.
|
|
79
|
-
"@lezer/highlight": "^1.2.
|
|
68
|
+
"@codemirror/lang-sql": "^6.8.0",
|
|
69
|
+
"@codemirror/language": "^6.10.6",
|
|
70
|
+
"@codemirror/legacy-modes": "^6.4.2",
|
|
71
|
+
"@codemirror/search": "^6.5.8",
|
|
72
|
+
"@codemirror/state": "^6.5.0",
|
|
73
|
+
"@codemirror/view": "^6.35.3",
|
|
74
|
+
"@juggle/resize-observer": "^3.4.0",
|
|
75
|
+
"@lezer/highlight": "^1.2.1",
|
|
80
76
|
"@sanity/incompatible-plugin": "^1.0.4",
|
|
81
|
-
"@sanity/ui": "^
|
|
82
|
-
"@uiw/codemirror-themes": "^4.
|
|
83
|
-
"@uiw/react-codemirror": "^4.
|
|
77
|
+
"@sanity/ui": "^2.10.9",
|
|
78
|
+
"@uiw/codemirror-themes": "^4.23.6",
|
|
79
|
+
"@uiw/react-codemirror": "^4.23.6"
|
|
84
80
|
},
|
|
85
81
|
"devDependencies": {
|
|
86
82
|
"@babel/core": "^7.23.6",
|
|
87
83
|
"@babel/preset-env": "^7.23.6",
|
|
88
84
|
"@babel/preset-react": "^7.23.3",
|
|
89
|
-
"@commitlint/cli": "^
|
|
90
|
-
"@commitlint/config-conventional": "^
|
|
85
|
+
"@commitlint/cli": "^19.6.0",
|
|
86
|
+
"@commitlint/config-conventional": "^19.6.0",
|
|
91
87
|
"@sanity/icons": "^2.8.0",
|
|
92
|
-
"@sanity/pkg-utils": "^
|
|
93
|
-
"@sanity/plugin-kit": "^
|
|
94
|
-
"@sanity/semantic-release-preset": "^
|
|
95
|
-
"@sanity/ui-workshop": "^1.2.0",
|
|
96
|
-
"@sanity/ui-workshop-plugin-sanity": "^1.0.0",
|
|
88
|
+
"@sanity/pkg-utils": "^6.12.0",
|
|
89
|
+
"@sanity/plugin-kit": "^4.0.18",
|
|
90
|
+
"@sanity/semantic-release-preset": "^5.0.0",
|
|
97
91
|
"@testing-library/jest-dom": "^6.1.5",
|
|
98
92
|
"@testing-library/react": "^14.1.2",
|
|
99
93
|
"@types/jest": "^29.5.11",
|
|
@@ -118,27 +112,28 @@
|
|
|
118
112
|
"react-dom": "^18.2.0",
|
|
119
113
|
"react-is": "^18.2.0",
|
|
120
114
|
"rimraf": "^5.0.5",
|
|
121
|
-
"sanity": "^3.
|
|
122
|
-
"
|
|
123
|
-
"sanity-testing-library": "^1.0.0",
|
|
124
|
-
"semantic-release": "^22.0.0",
|
|
115
|
+
"sanity": "^3.67.1",
|
|
116
|
+
"semantic-release": "^24.2.0",
|
|
125
117
|
"styled-components": "^6.1.1",
|
|
126
118
|
"ts-jest": "^29.1.1",
|
|
127
119
|
"typescript": "^5.3.3"
|
|
128
120
|
},
|
|
129
121
|
"peerDependencies": {
|
|
130
|
-
"react": "^18",
|
|
131
|
-
"react-dom": "^18",
|
|
122
|
+
"react": "^18 || >=19.0.0-0",
|
|
123
|
+
"react-dom": "^18 || >=19.0.0-0",
|
|
132
124
|
"sanity": "^3",
|
|
133
125
|
"styled-components": "^5.2 || ^6"
|
|
134
126
|
},
|
|
135
127
|
"engines": {
|
|
136
|
-
"node": ">=
|
|
128
|
+
"node": ">=18"
|
|
137
129
|
},
|
|
138
130
|
"sanityExchangeUrl": "https://www.sanity.io/plugins/code-input",
|
|
139
131
|
"sanityPlugin": {
|
|
140
132
|
"verifyPackage": {
|
|
141
133
|
"babelConfig": false
|
|
142
134
|
}
|
|
135
|
+
},
|
|
136
|
+
"overrides": {
|
|
137
|
+
"conventional-changelog-conventionalcommits": ">= 8.0.0"
|
|
143
138
|
}
|
|
144
139
|
}
|
package/src/CodeInput.tsx
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
+
import {Box, Card, Stack, Text} from '@sanity/ui'
|
|
1
2
|
import {Suspense, useCallback} from 'react'
|
|
2
3
|
import {MemberField, ObjectInputProps, RenderInputCallback, set, setIfMissing, unset} from 'sanity'
|
|
3
|
-
import {Box, Card, Stack, Text} from '@sanity/ui'
|
|
4
4
|
import styled, {css} from 'styled-components'
|
|
5
|
-
|
|
5
|
+
|
|
6
6
|
import {useCodeMirror} from './codemirror/useCodeMirror'
|
|
7
7
|
import {useLanguageMode} from './codemirror/useLanguageMode'
|
|
8
8
|
import {PATH_CODE} from './config'
|
|
9
|
+
import {LanguageField} from './LanguageField'
|
|
9
10
|
import {CodeInputValue, CodeSchemaType} from './types'
|
|
10
|
-
import {useFieldMember} from './useFieldMember'
|
|
11
11
|
import {focusRingBorderStyle, focusRingStyle} from './ui/focusRingStyle'
|
|
12
|
+
import {useFieldMember} from './useFieldMember'
|
|
12
13
|
|
|
13
14
|
export type {CodeInputLanguage, CodeInputValue} from './types'
|
|
14
15
|
|
package/src/LanguageField.tsx
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import {useCallback} from 'react'
|
|
2
2
|
import {FieldMember, InputProps, MemberField, MemberFieldProps, StringInputProps} from 'sanity'
|
|
3
|
-
|
|
3
|
+
|
|
4
4
|
import {LanguageInput} from './LanguageInput'
|
|
5
|
+
import {CodeInputLanguage} from './types'
|
|
5
6
|
|
|
6
7
|
export function LanguageField(
|
|
7
8
|
props: MemberFieldProps & {member: FieldMember; language: string; languages: CodeInputLanguage[]},
|
package/src/LanguageInput.tsx
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {ChangeEvent, useCallback} from 'react'
|
|
2
|
-
import {StringInputProps, set, unset} from 'sanity'
|
|
3
1
|
import {Select} from '@sanity/ui'
|
|
2
|
+
import {ChangeEvent, useCallback} from 'react'
|
|
3
|
+
import {set, StringInputProps, unset} from 'sanity'
|
|
4
|
+
|
|
4
5
|
import {CodeInputLanguage} from './types'
|
|
5
6
|
|
|
6
7
|
export interface LanguageInputProps extends StringInputProps {
|
package/src/PreviewCode.tsx
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import {Box, Card, Flex, Label, Text} from '@sanity/ui'
|
|
1
2
|
import {Suspense} from 'react'
|
|
2
|
-
import styled from 'styled-components'
|
|
3
|
-
import {Label, Box, Card, Flex, Text} from '@sanity/ui'
|
|
4
|
-
import {CodeInputValue, CodeSchemaType} from './types'
|
|
5
3
|
import {PreviewProps} from 'sanity'
|
|
4
|
+
import styled from 'styled-components'
|
|
5
|
+
|
|
6
6
|
import {useCodeMirror} from './codemirror/useCodeMirror'
|
|
7
7
|
import {useLanguageMode} from './codemirror/useLanguageMode'
|
|
8
|
+
import {CodeInputValue, CodeSchemaType} from './types'
|
|
8
9
|
|
|
9
10
|
const PreviewContainer = styled(Box)`
|
|
10
11
|
position: relative;
|
|
@@ -20,7 +21,7 @@ export interface PreviewCodeProps extends PreviewProps {
|
|
|
20
21
|
/**
|
|
21
22
|
* @public
|
|
22
23
|
*/
|
|
23
|
-
export
|
|
24
|
+
export function PreviewCode(props: PreviewCodeProps) {
|
|
24
25
|
const {selection, schemaType: type} = props
|
|
25
26
|
const {languageMode} = useLanguageMode(type as CodeSchemaType, props.selection)
|
|
26
27
|
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import type {Extension} from '@codemirror/state'
|
|
2
|
+
import {EditorView} from '@codemirror/view'
|
|
3
|
+
import {useRootTheme} from '@sanity/ui'
|
|
4
|
+
import CodeMirror, {type ReactCodeMirrorProps, type ReactCodeMirrorRef} from '@uiw/react-codemirror'
|
|
1
5
|
import {forwardRef, useCallback, useContext, useEffect, useMemo, useState} from 'react'
|
|
2
|
-
|
|
3
|
-
import {useCodeMirrorTheme} from './extensions/useCodeMirrorTheme'
|
|
4
|
-
import {Extension} from '@codemirror/state'
|
|
6
|
+
|
|
5
7
|
import {CodeInputConfigContext} from './CodeModeContext'
|
|
6
8
|
import {defaultCodeModes} from './defaultCodeModes'
|
|
7
9
|
import {
|
|
@@ -9,10 +11,9 @@ import {
|
|
|
9
11
|
highlightState,
|
|
10
12
|
setHighlightedLines,
|
|
11
13
|
} from './extensions/highlightLineExtension'
|
|
12
|
-
import {EditorView} from '@codemirror/view'
|
|
13
|
-
import {useRootTheme} from '@sanity/ui'
|
|
14
|
-
import {useFontSizeExtension} from './extensions/useFontSize'
|
|
15
14
|
import {useThemeExtension} from './extensions/theme'
|
|
15
|
+
import {useCodeMirrorTheme} from './extensions/useCodeMirrorTheme'
|
|
16
|
+
import {useFontSizeExtension} from './extensions/useFontSize'
|
|
16
17
|
|
|
17
18
|
export interface CodeMirrorProps extends ReactCodeMirrorProps {
|
|
18
19
|
highlightLines?: number[]
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type {ThemeContextValue} from '@sanity/ui'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `@sanity/ui@v2.9` introduced two new tones; "neutral" and "suggest",
|
|
5
|
+
* which maps to "default" and "primary" respectively in the old theme.
|
|
6
|
+
* This function returns the "backwards compatible" tone value.
|
|
7
|
+
*
|
|
8
|
+
* @returns The tone value that is backwards compatible with the old theme.
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export function getBackwardsCompatibleTone(
|
|
12
|
+
themeCtx: ThemeContextValue,
|
|
13
|
+
): Exclude<ThemeContextValue['tone'], 'neutral' | 'suggest'> {
|
|
14
|
+
if (themeCtx.tone !== 'neutral' && themeCtx.tone !== 'suggest') {
|
|
15
|
+
return themeCtx.tone
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return themeCtx.tone === 'neutral' ? 'default' : 'primary'
|
|
19
|
+
}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
/* eslint-disable no-param-reassign */
|
|
2
2
|
|
|
3
|
-
import {Extension, StateEffect, StateField} from '@codemirror/state'
|
|
4
|
-
import {Decoration, EditorView, lineNumbers} from '@codemirror/view'
|
|
5
|
-
import {ThemeContextValue
|
|
3
|
+
import {type Extension, StateEffect, StateField} from '@codemirror/state'
|
|
4
|
+
import {Decoration, type DecorationSet, EditorView, lineNumbers} from '@codemirror/view'
|
|
5
|
+
import type {ThemeContextValue} from '@sanity/ui'
|
|
6
|
+
import {rgba} from '@sanity/ui/theme'
|
|
7
|
+
|
|
8
|
+
import {getBackwardsCompatibleTone} from './backwardsCompatibleTone'
|
|
6
9
|
|
|
7
10
|
const highlightLineClass = 'cm-highlight-line'
|
|
8
11
|
|
|
@@ -65,7 +68,7 @@ const lineHighlightMark = Decoration.line({
|
|
|
65
68
|
})
|
|
66
69
|
|
|
67
70
|
export const highlightState: {
|
|
68
|
-
[prop: string]: StateField<
|
|
71
|
+
[prop: string]: StateField<DecorationSet>
|
|
69
72
|
} = {
|
|
70
73
|
highlight: lineHighlightField,
|
|
71
74
|
}
|
|
@@ -78,8 +81,11 @@ export interface HighlightLineConfig {
|
|
|
78
81
|
|
|
79
82
|
function createCodeMirrorTheme(options: {themeCtx: ThemeContextValue}) {
|
|
80
83
|
const {themeCtx} = options
|
|
81
|
-
|
|
82
|
-
const
|
|
84
|
+
|
|
85
|
+
const fallbackTone = getBackwardsCompatibleTone(themeCtx)
|
|
86
|
+
|
|
87
|
+
const dark = {color: themeCtx.theme.color.dark[fallbackTone]}
|
|
88
|
+
const light = {color: themeCtx.theme.color.light[fallbackTone]}
|
|
83
89
|
|
|
84
90
|
return EditorView.baseTheme({
|
|
85
91
|
'.cm-lineNumbers': {
|
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
import {Extension} from '@codemirror/state'
|
|
1
|
+
import type {Extension} from '@codemirror/state'
|
|
2
2
|
import {EditorView} from '@codemirror/view'
|
|
3
|
-
import {
|
|
3
|
+
import {useRootTheme} from '@sanity/ui'
|
|
4
|
+
import {rgba} from '@sanity/ui/theme'
|
|
4
5
|
import {useMemo} from 'react'
|
|
5
6
|
|
|
7
|
+
import {getBackwardsCompatibleTone} from './backwardsCompatibleTone'
|
|
8
|
+
|
|
6
9
|
export function useThemeExtension(): Extension {
|
|
7
10
|
const themeCtx = useRootTheme()
|
|
8
11
|
|
|
9
12
|
return useMemo(() => {
|
|
10
|
-
const
|
|
11
|
-
const
|
|
13
|
+
const fallbackTone = getBackwardsCompatibleTone(themeCtx)
|
|
14
|
+
const dark = {color: themeCtx.theme.color.dark[fallbackTone]}
|
|
15
|
+
const light = {color: themeCtx.theme.color.light[fallbackTone]}
|
|
12
16
|
|
|
13
17
|
return EditorView.baseTheme({
|
|
14
18
|
'&.cm-editor': {
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {useMemo} from 'react'
|
|
3
|
-
import {createTheme} from '@uiw/codemirror-themes'
|
|
1
|
+
import type {Extension} from '@codemirror/state'
|
|
4
2
|
import {tags as t} from '@lezer/highlight'
|
|
5
|
-
import {
|
|
3
|
+
import {useTheme} from '@sanity/ui'
|
|
4
|
+
import {rgba} from '@sanity/ui/theme'
|
|
5
|
+
import {createTheme} from '@uiw/codemirror-themes'
|
|
6
|
+
import {useMemo} from 'react'
|
|
6
7
|
|
|
7
8
|
export function useCodeMirrorTheme(): Extension {
|
|
8
9
|
const theme = useTheme()
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
/** @jest-environment jsdom */
|
|
2
2
|
|
|
3
|
+
import {studioTheme, ThemeProvider} from '@sanity/ui'
|
|
4
|
+
import {act, render} from '@testing-library/react'
|
|
3
5
|
import {Suspense} from 'react'
|
|
4
6
|
|
|
5
|
-
import {act, render} from '@testing-library/react'
|
|
6
7
|
import {useCodeMirror} from './useCodeMirror'
|
|
7
|
-
import {studioTheme, ThemeProvider} from '@sanity/ui'
|
|
8
8
|
|
|
9
9
|
describe('useCodeMirror - client', () => {
|
|
10
|
+
let rafMock: jest.SpyInstance<number, [FrameRequestCallback]>
|
|
11
|
+
|
|
10
12
|
beforeEach(() => {
|
|
11
|
-
jest
|
|
13
|
+
rafMock = jest
|
|
12
14
|
.spyOn(window, 'requestAnimationFrame')
|
|
13
15
|
.mockImplementation((callback: FrameRequestCallback): number => {
|
|
14
16
|
try {
|
|
@@ -23,10 +25,10 @@ describe('useCodeMirror - client', () => {
|
|
|
23
25
|
})
|
|
24
26
|
|
|
25
27
|
afterEach(() => {
|
|
26
|
-
|
|
28
|
+
rafMock.mockRestore()
|
|
27
29
|
})
|
|
28
30
|
|
|
29
|
-
it('should render suspended
|
|
31
|
+
it('should render suspended codemirror editor', async () => {
|
|
30
32
|
const TestComponent = () => {
|
|
31
33
|
const CodeMirror = useCodeMirror()
|
|
32
34
|
return (
|
|
@@ -39,11 +41,12 @@ describe('useCodeMirror - client', () => {
|
|
|
39
41
|
</Suspense>
|
|
40
42
|
)
|
|
41
43
|
}
|
|
42
|
-
let container:
|
|
44
|
+
let container: HTMLElement | undefined
|
|
43
45
|
await act(async () => {
|
|
44
46
|
const result = render(<TestComponent />)
|
|
45
47
|
container = result.container
|
|
46
48
|
})
|
|
47
|
-
expect(container
|
|
49
|
+
expect(container).toBeTruthy()
|
|
50
|
+
expect(container!.querySelector('.cm-theme')).toBeTruthy()
|
|
48
51
|
})
|
|
49
52
|
})
|
|
@@ -1,10 +1,18 @@
|
|
|
1
|
-
import {LANGUAGE_ALIASES, SUPPORTED_LANGUAGES} from '../config'
|
|
2
|
-
import {CodeInputLanguage, CodeInputValue, CodeSchemaType} from '../types'
|
|
3
1
|
import {useMemo} from 'react'
|
|
4
2
|
|
|
3
|
+
import {LANGUAGE_ALIASES, SUPPORTED_LANGUAGES} from '../config'
|
|
4
|
+
import type {CodeInputLanguage, CodeInputValue, CodeSchemaType} from '../types'
|
|
5
|
+
|
|
5
6
|
export const defaultLanguageMode = 'text'
|
|
6
7
|
|
|
7
|
-
export function useLanguageMode(
|
|
8
|
+
export function useLanguageMode(
|
|
9
|
+
schemaType: CodeSchemaType,
|
|
10
|
+
value?: CodeInputValue,
|
|
11
|
+
): {
|
|
12
|
+
language: string
|
|
13
|
+
languageMode: string
|
|
14
|
+
languages: CodeInputLanguage[]
|
|
15
|
+
} {
|
|
8
16
|
const languages = useLanguageAlternatives(schemaType)
|
|
9
17
|
const fixedLanguage = schemaType.options?.language
|
|
10
18
|
const language = value?.language ?? fixedLanguage ?? defaultLanguageMode
|
package/src/index.ts
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import {codeSchema, codeTypeName
|
|
2
|
-
|
|
3
|
-
export {
|
|
4
|
-
|
|
5
|
-
export type {CodeInputLanguage, CodeInputValue, CodeSchemaType, CodeOptions} from './types'
|
|
6
|
-
|
|
7
|
-
export {PreviewCode, type PreviewCodeProps}
|
|
1
|
+
import {CodeDefinition, codeSchema, codeTypeName} from './schema'
|
|
2
|
+
export {type CodeInput, type CodeInputProps} from './CodeInput'
|
|
3
|
+
export {PreviewCode, type PreviewCodeProps} from './PreviewCode'
|
|
4
|
+
export type {CodeInputLanguage, CodeInputValue, CodeOptions, CodeSchemaType} from './types'
|
|
8
5
|
export {codeSchema, codeTypeName}
|
|
9
6
|
export type {CodeDefinition}
|
|
10
7
|
|
package/src/plugin.tsx
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import {definePlugin} from 'sanity'
|
|
2
|
-
|
|
3
|
-
import {CodeMode} from './codemirror/defaultCodeModes'
|
|
2
|
+
|
|
4
3
|
import {CodeInputConfigContext} from './codemirror/CodeModeContext'
|
|
4
|
+
import {CodeMode} from './codemirror/defaultCodeModes'
|
|
5
|
+
import {codeSchema} from './schema'
|
|
5
6
|
|
|
6
7
|
export interface CodeInputConfig {
|
|
7
8
|
codeModes?: CodeMode[]
|
package/src/schema.tsx
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import {CodeBlockIcon} from '@sanity/icons'
|
|
2
|
+
import {defineType, ObjectDefinition} from 'sanity'
|
|
3
|
+
|
|
2
4
|
import {CodeInput} from './CodeInput'
|
|
3
|
-
import PreviewCode from './PreviewCode'
|
|
4
5
|
import {getMedia} from './getMedia'
|
|
5
|
-
import {
|
|
6
|
+
import {PreviewCode} from './PreviewCode'
|
|
6
7
|
import {CodeOptions} from './types'
|
|
7
8
|
|
|
8
9
|
/**
|