|
1 | 1 | <template>
|
2 |
| - <div> |
3 |
| - darcula theme: |
4 |
| - <Codemirror v-model:value="code" :options="cmOptions" border :height="200" /> |
5 |
| - </div> |
| 2 | + <Codemirror |
| 3 | + v-model:value="code" |
| 4 | + :options="cmOptions" |
| 5 | + :height="300" |
| 6 | + width="90%" |
| 7 | + class="cm-component" |
| 8 | + :border="true" |
| 9 | + @ready="onReady" |
| 10 | + /> |
6 | 11 | </template>
|
7 |
| -<script lang="ts"> |
8 |
| -import { ref } from "vue"; |
9 |
| -import type { EditorConfiguration } from "codemirror"; |
| 12 | +<script lang="ts" setup> |
| 13 | +import { ref, reactive } from "vue"; |
| 14 | +import { Editor, EditorConfiguration } from "codemirror"; |
10 | 15 | import Codemirror from "codemirror-editor-vue3";
|
11 |
| -
|
12 |
| -// language |
| 16 | +import jsonlint from "jsonlint-mod-fixed"; |
| 17 | +// language json or js |
13 | 18 | import "codemirror/mode/javascript/javascript.js";
|
14 | 19 | import "codemirror/addon/lint/lint.css";
|
15 |
| -// import 'codemirror/addon/fold/foldgutter.css' |
16 |
| -// import 'codemirror/addon/fold/foldcode.js' |
17 |
| -// import 'codemirror/addon/fold/foldgutter.js' |
18 |
| -// import 'codemirror/addon/fold/brace-fold.js' |
| 20 | +import "codemirror/addon/lint/lint.js"; |
| 21 | +import "codemirror/addon/lint/json-lint"; |
19 | 22 |
|
20 |
| -export default defineComponent({ |
21 |
| - components: { |
22 |
| - Codemirror, |
23 |
| - }, |
24 |
| - setup() { |
25 |
| - const code = ref(`{ |
26 |
| - "name": "codemirror-editor-vue3", |
27 |
| - "description": "CodeMirror component for Vue3", |
28 |
| - "version": "2.4.1", |
29 |
| - "license": "MIT", |
30 |
| - "files": [ |
31 |
| - "dist" |
32 |
| - ], |
33 |
| - "type": "module", |
34 |
| - "types": "./dist/packages/index.d.ts", |
35 |
| - "main": "./dist/codemirror-editor-vue3.umd.cjs", |
36 |
| - "module": "./dist/codemirror-editor-vue3.js", |
37 |
| - "exports": { |
38 |
| - ".": { |
39 |
| - "import": "./dist/codemirror-editor-vue3.js", |
40 |
| - "require": "./dist/codemirror-editor-vue3.umd.cjs" |
| 23 | +window.jsonlint = jsonlint; |
| 24 | +
|
| 25 | +const code = ref(`{ |
| 26 | + "compilerOptions": { |
| 27 | + "baseUrl": ".", |
| 28 | + "module": "ESNext", |
| 29 | + "target": "esnext", |
| 30 | + "lib": ["DOM", "ESNext"], |
| 31 | + "types": ["vite/client"], |
| 32 | + "jsx": "preserve", |
| 33 | + "allowJs": true, |
| 34 | + "strict": true, |
| 35 | + "esModuleInterop": true, |
| 36 | + "incremental": false, |
| 37 | + "skipLibCheck": true, |
| 38 | + "moduleResolution": "node", |
| 39 | + "resolveJsonModule": true, |
| 40 | + "noUnusedLocals": true, |
| 41 | + "strictNullChecks": true, |
| 42 | + "declaration": true, |
| 43 | + "forceConsistentCasingInFileNames": true, |
| 44 | + "paths": { |
| 45 | + "@/*": ["packages/*"], |
| 46 | + "./*": ["./*"], |
| 47 | + "codemirror-editor-vue3": ["packages/index.ts"] |
41 | 48 | }
|
42 | 49 | },
|
43 |
| - "private": false, |
44 |
| - "author": { |
45 |
| - "name": "RennCheung", |
46 |
| - "email": "906155099@qq.com" |
47 |
| - }, |
48 |
| - "homepage": "https://rennzhang.github.io/codemirror-editor-vue3/", |
49 |
| - "unpkg": "dist/codemirror-editor-vue3.js", |
50 |
| - "jsnext:main": "dist/codemirror-editor-vue3.js", |
51 |
| - "jspm": { |
52 |
| - "main": "dist/codemirror-editor-vue3.js", |
53 |
| - "registry": "npm", |
54 |
| - "format": "esm" |
55 |
| - }, |
56 |
| - "repository": { |
57 |
| - "type": "git", |
58 |
| - "url": "https://github.com/rennzhang/codemirror-editor-vue3/" |
59 |
| - }, |
60 |
| - "keywords": [ |
61 |
| - "codemirror-editor-vue3", |
62 |
| - "vue3 codemirror", |
63 |
| - "codemirror", |
64 |
| - "vue3", |
65 |
| - "log mode", |
66 |
| - "log" |
67 |
| - ], |
68 |
| - "scripts": { |
69 |
| - "dev": "vite", |
70 |
| - "build": "vite build", |
71 |
| - "docs-dev": "vitepress dev docs --open", |
72 |
| - "docs-build": "vitepress build docs", |
73 |
| - "docs-serve": "vitepress serve docs", |
74 |
| - "deploy-docs": "bash scripts/deploy-docs.sh", |
75 |
| - "release": "tsx scripts/release.ts", |
76 |
| - "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s", |
77 |
| - "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix", |
78 |
| - "tsc": "tsc", |
79 |
| - "prepublishOnly": "npm run build" |
80 |
| - }, |
81 |
| - "dependencies": { |
82 |
| - "codemirror": "^5.65.16", |
83 |
| - "diff-match-patch": "^1.0.5" |
84 |
| - }, |
85 |
| - "peerDependencies": { |
86 |
| - "codemirror": "^5.65.16", |
87 |
| - "diff-match-patch": "^1.0.5", |
88 |
| - "vue": "^3.x" |
89 |
| - }, |
90 |
| - "devDependencies": { |
91 |
| - "@commitlint/cli": "^16.1.0", |
92 |
| - "@commitlint/config-conventional": "^16.0.0", |
93 |
| - "@types/codemirror": "^5.60.15", |
94 |
| - "@types/diff-match-patch": "^1.0.32", |
95 |
| - "@types/node": "^16.11.13", |
96 |
| - "@typescript-eslint/eslint-plugin": "^5.54.0", |
97 |
| - "@typescript-eslint/parser": "^5.54.0", |
98 |
| - "@vitejs/plugin-vue": "^4.0.0", |
99 |
| - "@vitejs/plugin-vue-jsx": "^3.0.0", |
100 |
| - "@vue/cli-plugin-typescript": "~4.5.15", |
101 |
| - "chalk": "^4.1.2", |
102 |
| - "conventional-changelog-cli": "^2.0.31", |
103 |
| - "dedent": "^0.7.0", |
104 |
| - "diff-match-patch": "^1.0.5", |
105 |
| - "eslint": "^8.35.0", |
106 |
| - "eslint-config-airbnb-base": "^15.0.0", |
107 |
| - "eslint-config-prettier": "^8.6.0", |
108 |
| - "eslint-plugin-import": "^2.27.5", |
109 |
| - "eslint-plugin-prettier": "^4.2.1", |
110 |
| - "eslint-plugin-vue": "^9.9.0", |
111 |
| - "execa": "^7.0.0", |
112 |
| - "less": "^4.1.3", |
113 |
| - "lint-staged": "^10.2.10", |
114 |
| - "minimist": "^1.2.6", |
115 |
| - "picocolors": "^1.0.0", |
116 |
| - "prettier": "^2.8.4", |
117 |
| - "prompts": "^2.4.2", |
118 |
| - "rollup-plugin-vue": "^6.0.0", |
119 |
| - "semver": "^7.3.5", |
120 |
| - "tsx": "^3.12.3", |
121 |
| - "enquirer": "^2.3.6", |
122 |
| - "typescript": "^4.9.5", |
123 |
| - "unplugin-auto-import": "^0.15.0", |
124 |
| - "vite": "^4.1.4", |
125 |
| - "vite-plugin-checker": "^0.5.6", |
126 |
| - "vite-plugin-dts": "^2.0.2", |
127 |
| - "vite-plugin-pages": "^0.28.0", |
128 |
| - "vite-plugin-windicss": "^1.8.10", |
129 |
| - "vitepress": "1.0.0-alpha.49", |
130 |
| - "vue": "^3.2.47", |
131 |
| - "vue-router": "^4.1.6", |
132 |
| - "vue-tsc": "^1.2.0", |
133 |
| - "windicss": "^3.5.6", |
134 |
| - "yorkie-pnpm": "^2.0.1" |
135 |
| - } |
| 50 | + "include": [ |
| 51 | + "packages/**/*", |
| 52 | + "playground/**/*", |
| 53 | + "docs/**/*", |
| 54 | + "docs/demo/**/*", |
| 55 | + "script", |
| 56 | + "types", |
| 57 | + "*.ts", |
| 58 | + "*.tsx", |
| 59 | + "*.vue", |
| 60 | + "*.d.ts", |
| 61 | + "*.md" |
| 62 | + ] |
136 | 63 | }
|
137 | 64 | `);
|
138 | 65 |
|
139 |
| - const cmOptions: EditorConfiguration = { |
140 |
| - // json编辑器选项 |
141 |
| - mode: "application/json", |
142 |
| - indentWithTabs: true, |
143 |
| - smartIndent: true, |
144 |
| - lineNumbers: true, |
145 |
| - autofocus: false, |
146 |
| - foldGutter: false, |
147 |
| - gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"], |
148 |
| - }; |
| 66 | +const cmOptions: EditorConfiguration = reactive({ |
| 67 | + mode: "application/json", |
| 68 | + lineNumbers: true, |
| 69 | + lineWiseCopyCut: true, |
| 70 | + gutters: ["CodeMirror-lint-markers"], |
| 71 | + lint: true, |
| 72 | +}); |
| 73 | +
|
| 74 | +const cminstance = ref<Editor | null>(null); |
| 75 | +const onReady = (cm: Editor) => { |
| 76 | + cminstance.value = cm; |
| 77 | + console.log(cm.getValue()); |
| 78 | +}; |
149 | 79 |
|
150 |
| - return { |
151 |
| - code, |
152 |
| - cmOptions, |
153 |
| - }; |
| 80 | +defineExpose({ |
| 81 | + setTheme: (theme: string) => { |
| 82 | + console.log(" theme", theme); |
| 83 | + cminstance.value?.setOption("theme", theme); |
154 | 84 | },
|
155 | 85 | });
|
156 | 86 | </script>
|
| 87 | +<style lang="less" scoped> |
| 88 | +.cm-component { |
| 89 | + font-family: monospace; |
| 90 | +} |
| 91 | +</style> |
0 commit comments