yh-codemirror 1.0.3 → 1.0.5
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/CodeMirror.vue +47 -37
- package/index.ts +0 -0
- package/package.json +1 -1
- package/util.ts +0 -0
package/CodeMirror.vue
CHANGED
|
@@ -39,19 +39,19 @@
|
|
|
39
39
|
</div>
|
|
40
40
|
</template>
|
|
41
41
|
<script setup lang="ts">
|
|
42
|
-
import {
|
|
42
|
+
import {ref, onMounted, toRaw} from 'vue';
|
|
43
43
|
// @ts-ignore
|
|
44
|
-
import type {
|
|
45
|
-
import {
|
|
46
|
-
import {
|
|
47
|
-
import {
|
|
48
|
-
import {
|
|
49
|
-
import {
|
|
50
|
-
import {
|
|
51
|
-
import {
|
|
44
|
+
import type {Extension} from 'codemirror';
|
|
45
|
+
import {EditorView, basicSetup} from 'codemirror';
|
|
46
|
+
import {ViewUpdate} from '@codemirror/view';
|
|
47
|
+
import {EditorState, EditorSelection} from '@codemirror/state';
|
|
48
|
+
import {javascriptLanguage} from '@codemirror/lang-javascript';
|
|
49
|
+
import {solarizedDark} from 'cm6-theme-solarized-dark';
|
|
50
|
+
import {getCodeLanguagePlugin} from './util';
|
|
51
|
+
import {watch, computed} from 'vue';
|
|
52
52
|
// @ts-ignore
|
|
53
|
-
import {
|
|
54
|
-
import {
|
|
53
|
+
import {js_beautify} from 'js-beautify';
|
|
54
|
+
import {ElMessage} from 'element-plus';
|
|
55
55
|
|
|
56
56
|
const YhCodemirrorContainer = ref();
|
|
57
57
|
function fullscreen() {
|
|
@@ -79,8 +79,8 @@ const props = withDefaults(
|
|
|
79
79
|
height?: number;
|
|
80
80
|
}>(),
|
|
81
81
|
{
|
|
82
|
-
modelValue:
|
|
83
|
-
language:
|
|
82
|
+
modelValue: '',
|
|
83
|
+
language: 'javascript',
|
|
84
84
|
autocomplete: {},
|
|
85
85
|
}
|
|
86
86
|
);
|
|
@@ -89,8 +89,8 @@ const codeMirrorStyle = computed(() => {
|
|
|
89
89
|
let style: any = {};
|
|
90
90
|
if (props.height !== undefined && !isNaN(props.height)) {
|
|
91
91
|
style.height = `${props.height}px`;
|
|
92
|
-
style.display =
|
|
93
|
-
style.flexDirection =
|
|
92
|
+
style.display = 'flex';
|
|
93
|
+
style.flexDirection = 'column';
|
|
94
94
|
}
|
|
95
95
|
return style;
|
|
96
96
|
});
|
|
@@ -99,7 +99,7 @@ const codeMirrorEditorStyle = computed(() => {
|
|
|
99
99
|
let style: any = {};
|
|
100
100
|
if (props.height !== undefined && !isNaN(props.height)) {
|
|
101
101
|
style.flex = `1`;
|
|
102
|
-
style.overflow =
|
|
102
|
+
style.overflow = 'auto';
|
|
103
103
|
}
|
|
104
104
|
return style;
|
|
105
105
|
});
|
|
@@ -107,7 +107,7 @@ const codeMirrorEditorStyle = computed(() => {
|
|
|
107
107
|
watch(
|
|
108
108
|
() => props.modelValue,
|
|
109
109
|
(val) => {
|
|
110
|
-
if (view && state && val != view.state.doc.toString()) {
|
|
110
|
+
if (view && state && typeof val === 'string' && val != view.state.doc.toString()) {
|
|
111
111
|
let end = view.state.doc.toString().length;
|
|
112
112
|
view.dispatch({
|
|
113
113
|
changes: [
|
|
@@ -119,17 +119,21 @@ watch(
|
|
|
119
119
|
],
|
|
120
120
|
});
|
|
121
121
|
}
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
deep: true,
|
|
125
|
+
immediate: true,
|
|
122
126
|
}
|
|
123
127
|
);
|
|
124
128
|
|
|
125
|
-
const emits = defineEmits([
|
|
129
|
+
const emits = defineEmits(['update:modelValue']);
|
|
126
130
|
|
|
127
131
|
// 代码编辑器操作按钮
|
|
128
132
|
const onEditorBtnClick = (type) => {
|
|
129
133
|
if (view) {
|
|
130
134
|
const state = toRaw(view.state);
|
|
131
135
|
const pos = state.doc.toString().length;
|
|
132
|
-
if (type ==
|
|
136
|
+
if (type == 'beautify') {
|
|
133
137
|
const beautifyCode = js_beautify(props.modelValue, codeBeautifyConfig);
|
|
134
138
|
view.dispatch({
|
|
135
139
|
changes: [
|
|
@@ -140,15 +144,15 @@ const onEditorBtnClick = (type) => {
|
|
|
140
144
|
},
|
|
141
145
|
],
|
|
142
146
|
});
|
|
143
|
-
} else if (type ==
|
|
147
|
+
} else if (type == 'checkAll') {
|
|
144
148
|
const tr = state.update({
|
|
145
149
|
// selection: EditorSelection.cursor(pos), // 设置光标
|
|
146
150
|
selection: EditorSelection.range(0, pos),
|
|
147
151
|
scrollIntoView: true,
|
|
148
152
|
});
|
|
149
153
|
view.dispatch(tr);
|
|
150
|
-
} else if (type ==
|
|
151
|
-
let selectedText =
|
|
154
|
+
} else if (type == 'copy') {
|
|
155
|
+
let selectedText = '';
|
|
152
156
|
if (state.selection.ranges.some((range) => !range.empty)) {
|
|
153
157
|
const selection = state.selection.ranges[0];
|
|
154
158
|
selectedText = state.sliceDoc(selection.from, selection.to);
|
|
@@ -157,16 +161,16 @@ const onEditorBtnClick = (type) => {
|
|
|
157
161
|
}
|
|
158
162
|
navigator.clipboard.writeText(selectedText);
|
|
159
163
|
ElMessage({
|
|
160
|
-
message:
|
|
161
|
-
type:
|
|
164
|
+
message: '复制成功!',
|
|
165
|
+
type: 'success',
|
|
162
166
|
});
|
|
163
|
-
} else if (type ==
|
|
167
|
+
} else if (type == 'clear') {
|
|
164
168
|
view.dispatch({
|
|
165
169
|
changes: [
|
|
166
170
|
{
|
|
167
171
|
from: 0,
|
|
168
172
|
to: pos,
|
|
169
|
-
insert:
|
|
173
|
+
insert: '',
|
|
170
174
|
},
|
|
171
175
|
],
|
|
172
176
|
});
|
|
@@ -182,7 +186,7 @@ onMounted(() => {
|
|
|
182
186
|
EditorView.lineWrapping,
|
|
183
187
|
EditorView.updateListener.of((v: ViewUpdate) => {
|
|
184
188
|
if (v.docChanged) {
|
|
185
|
-
emits(
|
|
189
|
+
emits('update:modelValue', v.state.doc.toString());
|
|
186
190
|
}
|
|
187
191
|
}),
|
|
188
192
|
];
|
|
@@ -191,7 +195,7 @@ onMounted(() => {
|
|
|
191
195
|
if (Array.isArray(language)) {
|
|
192
196
|
language.forEach((l) => {
|
|
193
197
|
extensions.push(getCodeLanguagePlugin(l));
|
|
194
|
-
if (props.autocomplete[l] && l ===
|
|
198
|
+
if (props.autocomplete[l] && l === 'javascript') {
|
|
195
199
|
extensions.push(
|
|
196
200
|
javascriptLanguage.data.of({
|
|
197
201
|
autocomplete: props.autocomplete[l],
|
|
@@ -201,7 +205,7 @@ onMounted(() => {
|
|
|
201
205
|
});
|
|
202
206
|
} else {
|
|
203
207
|
extensions.push(getCodeLanguagePlugin(language));
|
|
204
|
-
if (props.autocomplete[language] && language ===
|
|
208
|
+
if (props.autocomplete[language] && language === 'javascript') {
|
|
205
209
|
extensions.push(
|
|
206
210
|
javascriptLanguage.data.of({
|
|
207
211
|
autocomplete: props.autocomplete[language],
|
|
@@ -210,11 +214,16 @@ onMounted(() => {
|
|
|
210
214
|
}
|
|
211
215
|
}
|
|
212
216
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
doc
|
|
216
|
-
|
|
217
|
-
|
|
217
|
+
if (!state) {
|
|
218
|
+
let doc = props.modelValue;
|
|
219
|
+
if (typeof doc !== 'string') {
|
|
220
|
+
doc = '';
|
|
221
|
+
}
|
|
222
|
+
state = EditorState.create({
|
|
223
|
+
doc,
|
|
224
|
+
extensions,
|
|
225
|
+
});
|
|
226
|
+
}
|
|
218
227
|
if (state) {
|
|
219
228
|
view = new EditorView({
|
|
220
229
|
state: state,
|
|
@@ -227,20 +236,21 @@ onMounted(() => {
|
|
|
227
236
|
.yh-code-mirror-container {
|
|
228
237
|
width: 100%;
|
|
229
238
|
position: relative;
|
|
239
|
+
background-color: #173541;
|
|
240
|
+
min-height: 70px;
|
|
230
241
|
.code-slot {
|
|
231
242
|
width: 100%;
|
|
232
|
-
font-family:
|
|
243
|
+
font-family: 'Fira Code', 'Roboto Mono', Consolas, 'Microsoft Yahei';
|
|
233
244
|
line-height: 25px;
|
|
234
245
|
font-size: 16px;
|
|
235
246
|
padding-left: 29.77px;
|
|
236
247
|
color: #ffffff;
|
|
237
|
-
background-color: #173541;
|
|
238
248
|
}
|
|
239
249
|
.cm-editor {
|
|
240
250
|
.cm-line {
|
|
241
251
|
font-size: 16px;
|
|
242
252
|
line-height: 25px;
|
|
243
|
-
font-family:
|
|
253
|
+
font-family: 'Fira Code', 'Roboto Mono', Consolas, 'Microsoft Yahei';
|
|
244
254
|
}
|
|
245
255
|
}
|
|
246
256
|
.codemirror-editor {
|
package/index.ts
CHANGED
|
File without changes
|
package/package.json
CHANGED
package/util.ts
CHANGED
|
File without changes
|