yh-codemirror 1.0.2 → 1.0.4

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 CHANGED
@@ -39,19 +39,19 @@
39
39
  </div>
40
40
  </template>
41
41
  <script setup lang="ts">
42
- import { ref, onMounted, toRaw } from "vue";
42
+ import {ref, onMounted, toRaw} from 'vue';
43
43
  // @ts-ignore
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";
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 { js_beautify } from "js-beautify";
54
- import { ElMessage } from "element-plus";
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: "javascript",
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 = "flex";
93
- style.flexDirection = "column";
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 = "hidden";
102
+ style.overflow = 'auto';
103
103
  }
104
104
  return style;
105
105
  });
@@ -122,14 +122,14 @@ watch(
122
122
  }
123
123
  );
124
124
 
125
- const emits = defineEmits(["update:modelValue"]);
125
+ const emits = defineEmits(['update:modelValue']);
126
126
 
127
127
  // 代码编辑器操作按钮
128
128
  const onEditorBtnClick = (type) => {
129
129
  if (view) {
130
130
  const state = toRaw(view.state);
131
131
  const pos = state.doc.toString().length;
132
- if (type == "beautify") {
132
+ if (type == 'beautify') {
133
133
  const beautifyCode = js_beautify(props.modelValue, codeBeautifyConfig);
134
134
  view.dispatch({
135
135
  changes: [
@@ -140,15 +140,15 @@ const onEditorBtnClick = (type) => {
140
140
  },
141
141
  ],
142
142
  });
143
- } else if (type == "checkAll") {
143
+ } else if (type == 'checkAll') {
144
144
  const tr = state.update({
145
145
  // selection: EditorSelection.cursor(pos), // 设置光标
146
146
  selection: EditorSelection.range(0, pos),
147
147
  scrollIntoView: true,
148
148
  });
149
149
  view.dispatch(tr);
150
- } else if (type == "copy") {
151
- let selectedText = "";
150
+ } else if (type == 'copy') {
151
+ let selectedText = '';
152
152
  if (state.selection.ranges.some((range) => !range.empty)) {
153
153
  const selection = state.selection.ranges[0];
154
154
  selectedText = state.sliceDoc(selection.from, selection.to);
@@ -157,16 +157,16 @@ const onEditorBtnClick = (type) => {
157
157
  }
158
158
  navigator.clipboard.writeText(selectedText);
159
159
  ElMessage({
160
- message: "复制成功!",
161
- type: "success",
160
+ message: '复制成功!',
161
+ type: 'success',
162
162
  });
163
- } else if (type == "clear") {
163
+ } else if (type == 'clear') {
164
164
  view.dispatch({
165
165
  changes: [
166
166
  {
167
167
  from: 0,
168
168
  to: pos,
169
- insert: "",
169
+ insert: '',
170
170
  },
171
171
  ],
172
172
  });
@@ -182,7 +182,7 @@ onMounted(() => {
182
182
  EditorView.lineWrapping,
183
183
  EditorView.updateListener.of((v: ViewUpdate) => {
184
184
  if (v.docChanged) {
185
- emits("update:modelValue", v.state.doc.toString());
185
+ emits('update:modelValue', v.state.doc.toString());
186
186
  }
187
187
  }),
188
188
  ];
@@ -191,7 +191,7 @@ onMounted(() => {
191
191
  if (Array.isArray(language)) {
192
192
  language.forEach((l) => {
193
193
  extensions.push(getCodeLanguagePlugin(l));
194
- if (props.autocomplete[l] && l === "javascript") {
194
+ if (props.autocomplete[l] && l === 'javascript') {
195
195
  extensions.push(
196
196
  javascriptLanguage.data.of({
197
197
  autocomplete: props.autocomplete[l],
@@ -201,7 +201,7 @@ onMounted(() => {
201
201
  });
202
202
  } else {
203
203
  extensions.push(getCodeLanguagePlugin(language));
204
- if (props.autocomplete[language] && language === "javascript") {
204
+ if (props.autocomplete[language] && language === 'javascript') {
205
205
  extensions.push(
206
206
  javascriptLanguage.data.of({
207
207
  autocomplete: props.autocomplete[language],
@@ -210,9 +210,12 @@ onMounted(() => {
210
210
  }
211
211
  }
212
212
  }
213
-
213
+ let doc = props.modelValue;
214
+ if (typeof doc !== 'string') {
215
+ doc = '';
216
+ }
214
217
  state = EditorState.create({
215
- doc: props.modelValue,
218
+ doc,
216
219
  extensions,
217
220
  });
218
221
  if (state) {
@@ -227,20 +230,21 @@ onMounted(() => {
227
230
  .yh-code-mirror-container {
228
231
  width: 100%;
229
232
  position: relative;
233
+ background-color: #173541;
234
+ min-height: 70px;
230
235
  .code-slot {
231
236
  width: 100%;
232
- font-family: "Fira Code", "Roboto Mono", Consolas, "Microsoft Yahei";
237
+ font-family: 'Fira Code', 'Roboto Mono', Consolas, 'Microsoft Yahei';
233
238
  line-height: 25px;
234
239
  font-size: 16px;
235
240
  padding-left: 29.77px;
236
241
  color: #ffffff;
237
- background-color: #173541;
238
242
  }
239
243
  .cm-editor {
240
244
  .cm-line {
241
245
  font-size: 16px;
242
246
  line-height: 25px;
243
- font-family: "Fira Code", "Roboto Mono", Consolas, "Microsoft Yahei";
247
+ font-family: 'Fira Code', 'Roboto Mono', Consolas, 'Microsoft Yahei';
244
248
  }
245
249
  }
246
250
  .codemirror-editor {
package/index.ts CHANGED
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yh-codemirror",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "wraper codemirror with vue3",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/util.ts CHANGED
File without changes