@scalar/use-codemirror 0.1.4 → 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Scalar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/package.json CHANGED
@@ -1,16 +1,22 @@
1
1
  {
2
2
  "name": "@scalar/use-codemirror",
3
- "version": "0.1.4",
3
+ "version": "0.3.0",
4
4
  "author": "Scalar",
5
- "license": "UNLICENSED",
5
+ "license": "MIT",
6
6
  "engines": {
7
7
  "node": ">=18"
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,7 @@
1
+ import { describe, expect, it } from 'vitest'
2
+
3
+ describe('example', () => {
4
+ it('executes tests', () => {
5
+ expect(true).toBe(true)
6
+ })
7
+ })
@@ -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
+ }