|
| 1 | +<script lang="ts"> |
| 2 | +import { defineComponent, ref, onMounted, watchEffect } from 'vue' |
| 3 | +import theme from '../theme' |
| 4 | +import * as monaco from 'monaco-editor' |
| 5 | +import type { PropType } from 'vue' |
| 6 | +import type { CompileError } from 'vue-i18n' |
| 7 | +
|
| 8 | +/* eslint-disable */ |
| 9 | +function debounce<T extends (...args: any[]) => any>( |
| 10 | + fn: T, |
| 11 | + delay: number = 300 |
| 12 | +): T { |
| 13 | + let prevTimer: number | null = null |
| 14 | + return ((...args: any[]) => { |
| 15 | + if (prevTimer) { |
| 16 | + clearTimeout(prevTimer) |
| 17 | + } |
| 18 | + prevTimer = window.setTimeout(() => { |
| 19 | + fn(...args) |
| 20 | + prevTimer = null |
| 21 | + }, delay) |
| 22 | + }) as any |
| 23 | +} |
| 24 | +/* eslint-enable */ |
| 25 | +
|
| 26 | +export type Foo = number |
| 27 | +
|
| 28 | +export default defineComponent({ |
| 29 | + name: 'Editor', |
| 30 | +
|
| 31 | + props: { |
| 32 | + code: { |
| 33 | + type: String, |
| 34 | + default: () => '' |
| 35 | + }, |
| 36 | + debounce: { |
| 37 | + type: Boolean, |
| 38 | + default: () => false |
| 39 | + }, |
| 40 | + language: { |
| 41 | + type: String, |
| 42 | + default: () => 'javascript' |
| 43 | + }, |
| 44 | + errors: { |
| 45 | + type: Array as PropType<CompileError[]>, |
| 46 | + default: () => [] |
| 47 | + } |
| 48 | + }, |
| 49 | +
|
| 50 | + emits: { |
| 51 | + change: null |
| 52 | + }, |
| 53 | +
|
| 54 | + setup(props, { emit }) { |
| 55 | + const container = ref<HTMLElement | null>(null) |
| 56 | +
|
| 57 | + function formatError(err: CompileError) { |
| 58 | + const loc = err.location! |
| 59 | + return { |
| 60 | + severity: monaco.MarkerSeverity.Error, |
| 61 | + startLineNumber: loc.start.line, |
| 62 | + startColumn: loc.start.column, |
| 63 | + endLineNumber: loc.end.line, |
| 64 | + endColumn: loc.end.column, |
| 65 | + message: `intlify message compilation error: ${err.message}`, |
| 66 | + code: String(err.code) |
| 67 | + } |
| 68 | + } |
| 69 | +
|
| 70 | + onMounted(() => { |
| 71 | + if (container.value == null) { |
| 72 | + return |
| 73 | + } |
| 74 | +
|
| 75 | + monaco.editor.defineTheme('my-theme', theme) |
| 76 | + monaco.editor.setTheme('my-theme') |
| 77 | +
|
| 78 | + const editor = monaco.editor.create(container.value, { |
| 79 | + value: [props.code].join('\n'), |
| 80 | + wordWrap: 'bounded', |
| 81 | + language: props.language, |
| 82 | + fontSize: 14, |
| 83 | + scrollBeyondLastLine: false, |
| 84 | + renderWhitespace: 'selection', |
| 85 | + tabSize: 2, |
| 86 | + minimap: { |
| 87 | + enabled: false |
| 88 | + } |
| 89 | + }) |
| 90 | + window.addEventListener('resize', () => editor.layout()) |
| 91 | +
|
| 92 | + const changeEmitter = props.debounce |
| 93 | + ? debounce(() => emit('change', editor.getValue())) |
| 94 | + : () => emit('change', editor.getValue()) |
| 95 | +
|
| 96 | + editor.onDidChangeModelContent(changeEmitter) |
| 97 | +
|
| 98 | + watchEffect(() => editor.setValue(props.code!)) |
| 99 | + watchEffect(() => { |
| 100 | + monaco.editor.setModelMarkers( |
| 101 | + editor.getModel()!, |
| 102 | + `vue-i18n`, |
| 103 | + props.errors.filter(e => e.location).map(formatError) |
| 104 | + ) |
| 105 | + }) |
| 106 | + }) |
| 107 | +
|
| 108 | + return { container } |
| 109 | + } |
| 110 | +}) |
| 111 | +</script> |
| 112 | + |
| 113 | +<template> |
| 114 | + <div ref="container" /> |
| 115 | +</template> |
0 commit comments