@scalar/use-codemirror 0.2.0 → 0.3.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/package.json +7 -1
- package/src/index.ts +1 -0
- package/src/useCodeMirror.test.ts +7 -0
- package/src/useCodeMirror.ts +162 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scalar/use-codemirror",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"author": "Scalar",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"engines": {
|
|
@@ -8,9 +8,15 @@
|
|
|
8
8
|
},
|
|
9
9
|
"type": "module",
|
|
10
10
|
"main": "dist/index.js",
|
|
11
|
+
"module": "dist/index.js",
|
|
11
12
|
"files": [
|
|
13
|
+
"src",
|
|
12
14
|
"dist"
|
|
13
15
|
],
|
|
16
|
+
"exports": {
|
|
17
|
+
"development": "./src/index.ts",
|
|
18
|
+
"import": "./dist/index.js"
|
|
19
|
+
},
|
|
14
20
|
"dependencies": {
|
|
15
21
|
"@codemirror/lang-javascript": "6.1.9",
|
|
16
22
|
"@codemirror/lang-json": "6.0.1",
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useCodeMirror'
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { type Extension, StateEffect } from '@codemirror/state'
|
|
2
|
+
import { type EditorViewConfig } from '@codemirror/view'
|
|
3
|
+
import { duotoneDark, duotoneLight } from '@uiw/codemirror-theme-duotone'
|
|
4
|
+
import { EditorView } from 'codemirror'
|
|
5
|
+
import { type Ref, ref, watch } from 'vue'
|
|
6
|
+
|
|
7
|
+
/** TODO: This is a static value, make it work with a dynamic parameter. */
|
|
8
|
+
const isDark = ref(false)
|
|
9
|
+
|
|
10
|
+
type UseCodeMirrorParameters = {
|
|
11
|
+
/**
|
|
12
|
+
* Some additional CodeMirror extensions.
|
|
13
|
+
*/
|
|
14
|
+
extensions: Extension[]
|
|
15
|
+
/**
|
|
16
|
+
* Prefill the content. Will be ignored when a provider is given.
|
|
17
|
+
*/
|
|
18
|
+
content?: string
|
|
19
|
+
/**
|
|
20
|
+
* Inverse the dark mode.
|
|
21
|
+
*/
|
|
22
|
+
forceDarkMode?: boolean
|
|
23
|
+
/**
|
|
24
|
+
* Whether to load a theme.
|
|
25
|
+
*/
|
|
26
|
+
withoutTheme?: boolean
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const useCodeMirror = (
|
|
30
|
+
parameters: UseCodeMirrorParameters,
|
|
31
|
+
): {
|
|
32
|
+
value: Ref<string>
|
|
33
|
+
codeMirrorRef: Ref<HTMLDivElement | null>
|
|
34
|
+
codeMirror: Ref<EditorView | null>
|
|
35
|
+
setCodeMirrorContent: (content: string) => void
|
|
36
|
+
reconfigureCodeMirror: (newExtensions: Extension[]) => void
|
|
37
|
+
restartCodeMirror: (newExtensions: Extension[]) => void
|
|
38
|
+
} => {
|
|
39
|
+
const { extensions, content, forceDarkMode, withoutTheme } = parameters
|
|
40
|
+
const value = ref(content ?? '')
|
|
41
|
+
const codeMirrorRef = ref<HTMLDivElement | null>(null)
|
|
42
|
+
const codeMirror = ref<EditorView | null>(null)
|
|
43
|
+
|
|
44
|
+
// Unmounts CodeMirror if it’s mounted already, and mounts CodeMirror, if the given ref exists.
|
|
45
|
+
watch(codeMirrorRef, () => {
|
|
46
|
+
destroyCodeMirror()
|
|
47
|
+
mountCodeMirror(extensions)
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
// Initializes CodeMirror.
|
|
51
|
+
const mountCodeMirror = (withCustomExtensions: Extension[]) => {
|
|
52
|
+
if (codeMirrorRef.value) {
|
|
53
|
+
const configuration: EditorViewConfig = {
|
|
54
|
+
parent: codeMirrorRef.value,
|
|
55
|
+
extensions: addDefaultExtensions(withCustomExtensions),
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Only set the content, when not in collaborative editing mode
|
|
59
|
+
if (content) {
|
|
60
|
+
configuration.doc = content
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
codeMirror.value = new EditorView(configuration)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Extend the given extension list with a dark/light theme.
|
|
68
|
+
const addDefaultExtensions = (newExtensions: Extension[] = []) => {
|
|
69
|
+
const theme = withoutTheme
|
|
70
|
+
? null
|
|
71
|
+
: forceDarkMode
|
|
72
|
+
? duotoneLight
|
|
73
|
+
: isDark.value
|
|
74
|
+
? duotoneDark
|
|
75
|
+
: duotoneLight
|
|
76
|
+
// Themes
|
|
77
|
+
const defaultExtensions: Extension[] = [
|
|
78
|
+
EditorView.theme({}, { dark: forceDarkMode ? false : isDark.value }),
|
|
79
|
+
theme,
|
|
80
|
+
].filter((extension) => extension !== null) as Extension[]
|
|
81
|
+
|
|
82
|
+
return [...defaultExtensions, newExtensions]
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
watch(isDark, () => {
|
|
86
|
+
const { extensions: configuredExtensions } = parameters
|
|
87
|
+
|
|
88
|
+
if (!forceDarkMode) {
|
|
89
|
+
reconfigureCodeMirror(configuredExtensions)
|
|
90
|
+
}
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
// Removes CodeMirror.
|
|
94
|
+
const destroyCodeMirror = () => {
|
|
95
|
+
codeMirror.value?.destroy()
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const setCodeMirrorContent = (newValue: string) => {
|
|
99
|
+
if (!codeMirror.value) {
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
value.value = newValue
|
|
104
|
+
|
|
105
|
+
codeMirror.value.dispatch({
|
|
106
|
+
changes: {
|
|
107
|
+
from: 0,
|
|
108
|
+
to: codeMirror.value.state.doc.length,
|
|
109
|
+
insert: newValue,
|
|
110
|
+
},
|
|
111
|
+
selection: {
|
|
112
|
+
anchor: Math.min(
|
|
113
|
+
codeMirror.value.state.selection.main.anchor,
|
|
114
|
+
newValue.length,
|
|
115
|
+
),
|
|
116
|
+
},
|
|
117
|
+
})
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const reconfigureCodeMirror = (newExtensions: Extension[]) => {
|
|
121
|
+
if (!codeMirror.value) {
|
|
122
|
+
return
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
codeMirror.value.dispatch({
|
|
126
|
+
effects: StateEffect.reconfigure.of(addDefaultExtensions(newExtensions)),
|
|
127
|
+
})
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const restartCodeMirror = (newExtensions: Extension[]) => {
|
|
131
|
+
destroyCodeMirror()
|
|
132
|
+
mountCodeMirror(newExtensions)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
/**
|
|
137
|
+
* The current value
|
|
138
|
+
*/
|
|
139
|
+
value,
|
|
140
|
+
/**
|
|
141
|
+
* An empty reference used to mount CodeMirror when bound to the DOM.
|
|
142
|
+
*/
|
|
143
|
+
codeMirrorRef,
|
|
144
|
+
/**
|
|
145
|
+
* The CodeMirror instance.
|
|
146
|
+
*/
|
|
147
|
+
// @ts-ignore
|
|
148
|
+
codeMirror,
|
|
149
|
+
/**
|
|
150
|
+
* Replaces the current content with the given value.
|
|
151
|
+
*/
|
|
152
|
+
setCodeMirrorContent,
|
|
153
|
+
/**
|
|
154
|
+
* Reconfigure the used extensions.
|
|
155
|
+
*/
|
|
156
|
+
reconfigureCodeMirror,
|
|
157
|
+
/**
|
|
158
|
+
* Restarts CodeMirror (destroy + mount)
|
|
159
|
+
*/
|
|
160
|
+
restartCodeMirror,
|
|
161
|
+
}
|
|
162
|
+
}
|