-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
1,159 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
### ✨ Features | ||
|
||
- 新增图形编辑器示例 | ||
- 新增代码编辑器(包含 Json 编辑器) | ||
|
||
### ⚡ Performance Improvements | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { App } from 'vue'; | ||
import codeEditor from './src/CodeEditor.vue'; | ||
|
||
export const CodeEditor = Object.assign(codeEditor, { | ||
install(app: App) { | ||
app.component(codeEditor.name, codeEditor); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<template> | ||
<div class="h-full"> | ||
<CodeMirrorEditor :value="getValue" @change="handleValueChange" :mode="mode" /> | ||
</div> | ||
</template> | ||
<script lang="ts"> | ||
import { defineComponent, computed } from 'vue'; | ||
import CodeMirrorEditor from './codemirror/CodeMirror.vue'; | ||
import { isString } from '/@/utils/is'; | ||
const MODE = { | ||
JSON: 'application/json', | ||
html: 'htmlmixed', | ||
js: 'javascript', | ||
}; | ||
export default defineComponent({ | ||
name: 'CodeEditor', | ||
components: { CodeMirrorEditor }, | ||
props: { | ||
value: { | ||
type: [Object, String], | ||
}, | ||
mode: { | ||
type: String, | ||
default: MODE.JSON, | ||
}, | ||
}, | ||
emits: ['change'], | ||
setup(props, { emit }) { | ||
const getValue = computed(() => { | ||
const { value, mode } = props; | ||
if (mode === MODE.JSON) { | ||
return isString(value) | ||
? JSON.stringify(JSON.parse(value), null, 2) | ||
: JSON.stringify(value, null, 2); | ||
} | ||
return value; | ||
}); | ||
function handleValueChange(v) { | ||
emit('change', v); | ||
} | ||
return { | ||
handleValueChange, | ||
getValue, | ||
}; | ||
}, | ||
}); | ||
</script> |
125 changes: 125 additions & 0 deletions
125
src/components/CodeEditor/src/codemirror/CodeMirror.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<template> | ||
<div class="relative h-100 !h-full w-full overflow-hidden" ref="el"> </div> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { | ||
ref, | ||
onMounted, | ||
onUnmounted, | ||
watchEffect, | ||
watch, | ||
defineComponent, | ||
unref, | ||
nextTick, | ||
} from 'vue'; | ||
import { useDebounceFn } from '@vueuse/core'; | ||
import { useAppStore } from '/@/store/modules/app'; | ||
import CodeMirror from 'codemirror'; | ||
import './codemirror.css'; | ||
import 'codemirror/theme/idea.css'; | ||
import 'codemirror/theme/material-palenight.css'; | ||
// modes | ||
import 'codemirror/mode/javascript/javascript'; | ||
import 'codemirror/mode/css/css'; | ||
import 'codemirror/mode/htmlmixed/htmlmixed'; | ||
export default defineComponent({ | ||
props: { | ||
mode: { | ||
type: String, | ||
default: 'application/json', | ||
}, | ||
value: { | ||
type: String, | ||
default: '', | ||
}, | ||
readonly: { | ||
type: Boolean, | ||
default: false, | ||
}, | ||
}, | ||
emits: ['change'], | ||
setup(props, { emit }) { | ||
const el = ref(); | ||
let editor: Nullable<CodeMirror.Editor>; | ||
const debounceRefresh = useDebounceFn(refresh, 100); | ||
const appStore = useAppStore(); | ||
watch( | ||
() => props.value, | ||
async (v) => { | ||
await nextTick(); | ||
const oldValue = editor?.getValue(); | ||
v && v !== oldValue && editor?.setValue(v); | ||
}, | ||
{ flush: 'post' } | ||
); | ||
watchEffect(() => { | ||
editor?.setOption('mode', props.mode); | ||
}); | ||
watch( | ||
() => appStore.getDarkMode, | ||
async () => { | ||
setTheme(); | ||
}, | ||
{ | ||
immediate: true, | ||
} | ||
); | ||
function setTheme() { | ||
unref(editor)?.setOption( | ||
'theme', | ||
appStore.getDarkMode === 'light' ? 'idea' : 'material-palenight' | ||
); | ||
} | ||
function refresh() { | ||
editor?.refresh(); | ||
} | ||
async function init() { | ||
const addonOptions = { | ||
autoCloseBrackets: true, | ||
autoCloseTags: true, | ||
foldGutter: true, | ||
gutters: ['CodeMirror-linenumbers'], | ||
}; | ||
editor = CodeMirror(el.value!, { | ||
value: '', | ||
mode: props.mode, | ||
readOnly: props.readonly, | ||
tabSize: 2, | ||
theme: 'material-palenight', | ||
lineWrapping: true, | ||
lineNumbers: true, | ||
...addonOptions, | ||
}); | ||
editor?.setValue(props.value); | ||
setTheme(); | ||
editor?.on('change', () => { | ||
emit('change', editor?.getValue()); | ||
}); | ||
} | ||
onMounted(async () => { | ||
await nextTick(); | ||
init(); | ||
window.addEventListener('resize', debounceRefresh); | ||
setTimeout(refresh, 50); | ||
}); | ||
onUnmounted(() => { | ||
window.removeEventListener('resize', debounceRefresh); | ||
editor = null; | ||
}); | ||
return { el }; | ||
}, | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import CodeMirror from 'codemirror'; | ||
import './codemirror.css'; | ||
import 'codemirror/theme/idea.css'; | ||
import 'codemirror/theme/material-palenight.css'; | ||
// import 'codemirror/addon/lint/lint.css'; | ||
|
||
// modes | ||
import 'codemirror/mode/javascript/javascript'; | ||
import 'codemirror/mode/css/css'; | ||
import 'codemirror/mode/htmlmixed/htmlmixed'; | ||
// addons | ||
// import 'codemirror/addon/edit/closebrackets'; | ||
// import 'codemirror/addon/edit/closetag'; | ||
// import 'codemirror/addon/comment/comment'; | ||
// import 'codemirror/addon/fold/foldcode'; | ||
// import 'codemirror/addon/fold/foldgutter'; | ||
// import 'codemirror/addon/fold/brace-fold'; | ||
// import 'codemirror/addon/fold/indent-fold'; | ||
// import 'codemirror/addon/lint/json-lint'; | ||
// import 'codemirror/addon/fold/comment-fold'; | ||
export { CodeMirror }; |
Oops, something went wrong.