-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(editor): add monaco editor component
- Loading branch information
Showing
9 changed files
with
235 additions
and
3 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
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,71 @@ | ||
import { onMounted, Ref, ToRefs, ref, onBeforeUnmount, watch } from 'vue' | ||
import * as Monaco from 'monaco-editor/esm/vs/editor/editor.api' | ||
import { CustomMonacoOption } from '../types/editor' | ||
|
||
const getMonacoOptions = (options: ToRefs<CustomMonacoOption>): Monaco.editor.IStandaloneEditorConstructionOptions => { | ||
const defaultOptions: Monaco.editor.IStandaloneEditorConstructionOptions = { | ||
value: options.value.value, | ||
language: options.lang.value, | ||
readOnly: options.disabled.value, | ||
wordWrap: options.wordWrap.value, | ||
lineHeight: options.lineHeight.value, | ||
fontSize: options.fontSize.value, | ||
scrollBeyondLastLine: false, | ||
lineNumbers: options.lineNumbers.value, | ||
renderLineHighlight: options.renderLineHighlight.value, | ||
matchBrackets: 'near', | ||
folding: false, | ||
lightbulb: { | ||
enabled: false, | ||
}, | ||
minimap: { | ||
enabled: false, | ||
}, | ||
scrollbar: { | ||
horizontal: options.scrollbarStatus.value, | ||
vertical: options.scrollbarStatus.value, | ||
useShadows: true, | ||
alwaysConsumeMouseWheel: false, | ||
}, | ||
smoothScrolling: true, | ||
// theme: editorTheme || getTheme(), | ||
} | ||
return defaultOptions | ||
} | ||
|
||
export default function useEditor(id: Ref<string>, refOptions: ToRefs<CustomMonacoOption>) { | ||
const elementId = `monaco-${id.value}` | ||
let editor: Monaco.editor.IStandaloneCodeEditor | null = null | ||
let subscriber: Monaco.IDisposable | null = null | ||
const value: Ref<string | undefined> = ref('') | ||
const initEditor = () => { | ||
const editorElement = document.getElementById(elementId) | ||
if (editorElement) { | ||
const defaultOptions = getMonacoOptions(refOptions) | ||
editor = Monaco.editor.create(editorElement, defaultOptions) | ||
subscriber = editor.onDidChangeModelContent(() => { | ||
value.value = editor?.getValue() | ||
}) | ||
} | ||
} | ||
const resetEditor = () => { | ||
subscriber?.dispose() | ||
editor?.getModel()?.dispose() | ||
editor?.dispose() | ||
editor = null | ||
} | ||
window.onresize = () => { | ||
editor?.layout() | ||
} | ||
watch(refOptions.lang, () => { | ||
resetEditor() | ||
initEditor() | ||
}) | ||
onMounted(initEditor) | ||
onBeforeUnmount(() => { | ||
resetEditor() | ||
}) | ||
return { | ||
value, | ||
} | ||
} |
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 { App } from 'vue' | ||
import Editor from './src/editor.vue' | ||
|
||
Editor.install = (Vue: App): void => { | ||
Vue.component(Editor.name, Editor) | ||
} | ||
|
||
export default Editor |
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,86 @@ | ||
<template> | ||
<div class="emqx-editor" :style="{ height: editorHeight }"> | ||
<div :id="`monaco-${id}`" class="emqx-editor-view"></div> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { computed, defineComponent, PropType, ToRefs, toRefs, watch } from 'vue' | ||
import useEditor from '../composables/useEditor' | ||
import { Lang, Switch, renderLineHighlight, ScrollBarStatus, CustomMonacoOption } from '../types/editor' | ||
export default defineComponent({ | ||
name: 'EmqxEditor', | ||
props: { | ||
id: { | ||
required: true, | ||
type: String, | ||
}, | ||
height: { | ||
type: Number, | ||
default: 300, | ||
}, | ||
modelValue: { | ||
required: true, | ||
type: String, | ||
}, | ||
lang: { | ||
required: true, | ||
type: String as PropType<Lang>, | ||
}, | ||
fontSize: { | ||
type: Number, | ||
default: 14, | ||
}, | ||
lineNumbers: { | ||
type: String as PropType<Switch>, | ||
default: 'on', | ||
}, | ||
renderLineHighlight: { | ||
type: String as PropType<renderLineHighlight>, | ||
default: 'line', | ||
}, | ||
scrollbarStatus: { | ||
type: String as PropType<ScrollBarStatus>, | ||
default: 'visible', | ||
}, | ||
disabled: { | ||
type: Boolean, | ||
default: false, | ||
}, | ||
wordWrap: { | ||
type: String as PropType<Switch>, | ||
default: 'off', | ||
}, | ||
lineHeight: { | ||
type: Number, | ||
default: undefined, | ||
}, | ||
}, | ||
setup(props, ctx) { | ||
const { id, height, modelValue, ...others } = toRefs(props) | ||
const option: ToRefs<CustomMonacoOption> = { value: modelValue, ...others } | ||
const editorHeight = computed(() => { | ||
return `${height.value}px` | ||
}) | ||
const { value } = useEditor(id, option) | ||
watch(value, val => { | ||
ctx.emit('update:modelValue', val) | ||
}) | ||
return { | ||
editorHeight, | ||
} | ||
}, | ||
}) | ||
</script> | ||
|
||
<style lang="scss"> | ||
.emqx-editor { | ||
border: 1px solid #d7dae1; | ||
} | ||
.emqx-editor-view { | ||
height: 100%; | ||
width: 100%; | ||
position: relative; | ||
} | ||
</style> |
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 @@ | ||
export type Lang = 'json' | 'sql' | ||
|
||
export type Switch = 'on' | 'off' | ||
|
||
export type renderLineHighlight = 'none' | 'line' | 'gutter' | 'all' | undefined | ||
|
||
export type ScrollBarStatus = 'auto' | 'visible' | 'hidden' | undefined | ||
|
||
export interface CustomMonacoOption { | ||
value: string | ||
lang: Lang | ||
fontSize: number | ||
lineNumbers: Switch | ||
renderLineHighlight: renderLineHighlight | ||
scrollbarStatus: ScrollBarStatus | ||
disabled: boolean | ||
wordWrap: Switch | ||
lineHeight: number | ||
} | ||
|
||
export default {} |
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
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