2026-05-31 15:42:52 +09:00
|
|
|
<script setup>
|
|
|
|
|
import { ref, onMounted, onBeforeUnmount, watch } from 'vue'
|
|
|
|
|
import Editor from '@toast-ui/editor'
|
|
|
|
|
import '@toast-ui/editor/dist/toastui-editor.css'
|
|
|
|
|
import codeSyntaxHighlight from '@toast-ui/editor-plugin-code-syntax-highlight/dist/toastui-editor-plugin-code-syntax-highlight-all'
|
|
|
|
|
import 'prismjs/themes/prism-tomorrow.css'
|
|
|
|
|
import '@toast-ui/editor-plugin-code-syntax-highlight/dist/toastui-editor-plugin-code-syntax-highlight.css'
|
|
|
|
|
|
|
|
|
|
const props = defineProps({
|
|
|
|
|
modelValue: { type: String, default: '' },
|
|
|
|
|
height: { type: String, default: '400px' },
|
|
|
|
|
placeholder: { type: String, default: '' },
|
|
|
|
|
initialEditType: { type: String, default: 'wysiwyg' }, // 'markdown' | 'wysiwyg'
|
|
|
|
|
})
|
|
|
|
|
const emit = defineEmits(['update:modelValue'])
|
|
|
|
|
|
2026-06-07 17:50:43 +09:00
|
|
|
// PC(Electron)는 위지윅(contenteditable)에서 한글 IME 첫 자음이 중복되는 Chromium 버그가 있어
|
|
|
|
|
// 마크다운(텍스트 기반) 모드로 입력받는다. 웹/모바일(APK)은 위지윅 유지.
|
|
|
|
|
const isElectron = typeof navigator !== 'undefined' && /electron/i.test(navigator.userAgent)
|
|
|
|
|
const editType = isElectron ? 'markdown' : props.initialEditType
|
|
|
|
|
|
2026-05-31 15:42:52 +09:00
|
|
|
const el = ref(null)
|
|
|
|
|
let editor = null
|
2026-06-07 17:50:43 +09:00
|
|
|
// 에디터가 마지막으로 emit 한 값. 부모가 되돌려준 echo 를 구분해 입력 중 setMarkdown 으로 끊기지 않게.
|
2026-05-31 19:52:23 +09:00
|
|
|
let lastEmitted = props.modelValue || ''
|
2026-05-31 15:42:52 +09:00
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
editor = new Editor({
|
|
|
|
|
el: el.value,
|
|
|
|
|
height: props.height,
|
2026-06-07 17:50:43 +09:00
|
|
|
initialEditType: editType,
|
|
|
|
|
hideModeSwitch: true,
|
2026-05-31 15:42:52 +09:00
|
|
|
plugins: [codeSyntaxHighlight], // 코드 구문 강조
|
|
|
|
|
initialValue: props.modelValue || '',
|
|
|
|
|
placeholder: props.placeholder,
|
|
|
|
|
usageStatistics: false,
|
|
|
|
|
autofocus: false,
|
|
|
|
|
events: {
|
2026-05-31 19:52:23 +09:00
|
|
|
change: () => {
|
2026-06-07 17:50:43 +09:00
|
|
|
lastEmitted = editor.getMarkdown()
|
|
|
|
|
emit('update:modelValue', lastEmitted)
|
2026-05-31 19:52:23 +09:00
|
|
|
},
|
2026-05-31 15:42:52 +09:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2026-06-07 17:50:43 +09:00
|
|
|
// 외부 값 변경(수정 로드, 제출 후 초기화 등)만 에디터에 반영. echo 는 무시.
|
2026-05-31 15:42:52 +09:00
|
|
|
watch(
|
|
|
|
|
() => props.modelValue,
|
|
|
|
|
(val) => {
|
2026-05-31 19:52:23 +09:00
|
|
|
const v = val || ''
|
2026-06-07 17:50:43 +09:00
|
|
|
if (v === lastEmitted) return
|
2026-05-31 19:52:23 +09:00
|
|
|
if (editor && v !== editor.getMarkdown()) {
|
|
|
|
|
lastEmitted = v
|
|
|
|
|
editor.setMarkdown(v)
|
2026-05-31 15:42:52 +09:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
|
editor?.destroy()
|
|
|
|
|
editor = null
|
|
|
|
|
})
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
|
|
|
|
<div ref="el"></div>
|
|
|
|
|
</template>
|