yh-codemirror 1.0.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/CodeMirror.vue ADDED
@@ -0,0 +1,119 @@
1
+ <template>
2
+ <div class="yh-code-mirror-container">
3
+ <div class="code-slot">
4
+ <slot name="pre-code"></slot>
5
+ </div>
6
+ <div ref="codeMirrorEditorRef"></div>
7
+ <div class="code-slot">
8
+ <slot name="next-code"></slot>
9
+ </div>
10
+ </div>
11
+ </template>
12
+ <script setup lang="ts">
13
+ import { ref, onMounted } from "vue";
14
+ // @ts-ignore
15
+ import type { Extension } from "codemirror";
16
+ import { EditorView, basicSetup } from "codemirror";
17
+ import { ViewUpdate } from "@codemirror/view";
18
+ import { EditorState, EditorSelection } from "@codemirror/state";
19
+ import { javascriptLanguage } from "@codemirror/lang-javascript";
20
+ import { solarizedDark } from "cm6-theme-solarized-dark";
21
+ import { getCodeLanguagePlugin } from "./util";
22
+ import { watch } from "vue";
23
+
24
+ const codeMirrorEditorRef = ref();
25
+ let view: null | EditorView = null;
26
+ let state: null | EditorState = null;
27
+
28
+ const props = withDefaults(
29
+ defineProps<{
30
+ modelValue: string;
31
+ language?: string | string[];
32
+ autocomplete?: any;
33
+ }>(),
34
+ {
35
+ modelValue: "",
36
+ language: "javascript",
37
+ autocomplete: {},
38
+ }
39
+ );
40
+
41
+ watch(
42
+ () => props.modelValue,
43
+ (val) => {
44
+ if (state) {
45
+ let end = state.doc.length;
46
+ state.update({
47
+ changes: [
48
+ {
49
+ from: 0,
50
+ to: end,
51
+ insert: val,
52
+ },
53
+ ],
54
+ });
55
+ }
56
+ }
57
+ );
58
+
59
+ const emits = defineEmits(["update:modelvalue"]);
60
+
61
+ onMounted(() => {
62
+ const extensions: Extension[] = [
63
+ basicSetup,
64
+ solarizedDark,
65
+ EditorView.lineWrapping,
66
+ EditorView.updateListener.of((v: ViewUpdate) => {
67
+ emits("update:modelvalue", v.state.doc.toString());
68
+ }),
69
+ ];
70
+ const language = props.language;
71
+ if (language) {
72
+ if (Array.isArray(language)) {
73
+ language.forEach((l) => {
74
+ extensions.push(getCodeLanguagePlugin(l));
75
+ if (props.autocomplete[l] && l === "javascript") {
76
+ extensions.push(
77
+ javascriptLanguage.data.of({
78
+ autocomplete: props.autocomplete[l],
79
+ })
80
+ );
81
+ }
82
+ });
83
+ } else {
84
+ extensions.push(getCodeLanguagePlugin(language));
85
+ if (props.autocomplete[language] && language === "javascript") {
86
+ extensions.push(
87
+ javascriptLanguage.data.of({
88
+ autocomplete: props.autocomplete[language],
89
+ })
90
+ );
91
+ }
92
+ }
93
+ }
94
+
95
+ state = EditorState.create({
96
+ doc: props.modelValue,
97
+ extensions,
98
+ });
99
+ if (state) {
100
+ view = new EditorView({
101
+ state: state,
102
+ parent: codeMirrorEditorRef.value,
103
+ });
104
+ }
105
+ });
106
+ </script>
107
+ <style lang="scss">
108
+ .yh-code-mirror-container {
109
+ width: 100%;
110
+ .code-slot {
111
+ width: 100%;
112
+ line-height: 25px;
113
+ font-size: 14px;
114
+ padding-left: 29.77px;
115
+ color: #ffffff;
116
+ background-color: #173541;
117
+ }
118
+ }
119
+ </style>
package/index.ts ADDED
File without changes
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "yh-codemirror",
3
+ "version": "1.0.0",
4
+ "description": "wraper codemirror with vue3",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "pub": "npm publish --registry https://registry.npmjs.org/ --no-git-checks && npm publish --registry https://packages.aliyun.com/60765e0161a945067837bb5f/npm/npm-registry/ --no-git-checks"
8
+ },
9
+ "dependencies": {
10
+ "@codemirror/autocomplete": "^6.9.0",
11
+ "@codemirror/lang-css": "^6.2.1",
12
+ "@codemirror/lang-java": "^6.0.1",
13
+ "@codemirror/lang-javascript": "^6.1.8",
14
+ "@codemirror/lang-sql": "^6.5.3",
15
+ "@codemirror/lang-html": "^6.4.9",
16
+ "@codemirror/language": "^6.9.0",
17
+ "@codemirror/view": "^6.16.0",
18
+ "codemirror": "^6.0.1"
19
+ },
20
+ "peerDependencies": {
21
+ "vue-i18n": "9.8.0",
22
+ "vue": "3.2.47",
23
+ "element-plus": "2.3.8"
24
+ },
25
+ "author": "Liubin"
26
+ }
package/util.ts ADDED
@@ -0,0 +1,17 @@
1
+ import { javascript } from "@codemirror/lang-javascript";
2
+ import { java } from "@codemirror/lang-java";
3
+ import { css } from "@codemirror/lang-css";
4
+ import { sql } from "@codemirror/lang-sql";
5
+ import { html } from "@codemirror/lang-html";
6
+
7
+ const plugins = {
8
+ javascript: () => javascript(),
9
+ sql: () => sql(),
10
+ css: () => css(),
11
+ java: () => java(),
12
+ html: () => html(),
13
+ };
14
+
15
+ export const getCodeLanguagePlugin = function (language) {
16
+ return plugins[language]();
17
+ };