๊ฐœ๋ฐœ์ผ์ง€

tui-editor (TOAST UI Editor) vue3 <script setup> ์ ์šฉ๋ฒ•

yyza 2023. 7. 13. 15:15
๋ฐ˜์‘ํ˜•

๐Ÿฅ‘tui-editor ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜

https://github.com/nhn/tui.editor

 

GitHub - nhn/tui.editor: ๐Ÿž๐Ÿ“ Markdown WYSIWYG Editor. GFM Standard + Chart & UML Extensible.

๐Ÿž๐Ÿ“ Markdown WYSIWYG Editor. GFM Standard + Chart & UML Extensible. - GitHub - nhn/tui.editor: ๐Ÿž๐Ÿ“ Markdown WYSIWYG Editor. GFM Standard + Chart & UML Extensible.

github.com

npm install --save @toast-ui/vue-editor

๐Ÿฅ‘ Vue3 ํ”„๋กœ์ ํŠธ์— tui-editor๊ตฌ์„ฑํ•˜๊ธฐ

import Editor from '@toast-ui/editor';
import '@toast-ui/editor/dist/toastui-editor.css';

๐Ÿฅ‘ tui-editor ์‚ฌ์šฉ ์ด์œ 

 ์ฒ˜์Œ์œผ๋กœ HTML Editor๋ฅผ ์‚ฌ์šฉํ•œ๊ฑด Ck-editor์˜€๋Š”๋ฐ tui-editor๊ฐ€ ์„ค์น˜๋‚˜ ์‚ฌ์šฉ๋ฒ•์— ์žˆ์–ด ๋” ๊ฐ„๋‹จํ•˜๊ณ  ๋ช…๋ฃŒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿฅ‘tui-editor ์ ์šฉ ๋ฐฉ๋ฒ•

tui-editor๊ฐ€ ๊ธฐ์กด vue2๊นŒ์ง€๋Š” ์ง€์›์„ ํ–ˆ์—‡์œผ๋‚˜ vue3๋ถ€ํ„ฐ๋Š” vue3์™€ ํ•จ๊ป˜ Tui.Editor๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์ง์ ‘ ๋ž˜ํผ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

 

<template>
		// ref์‚ฌ์šฉํ•˜์—ฌ ์—๋””ํ„ฐ value ์ ์šฉ
		<div ref="editor" />		
        //์ž‘์„ฑํ•œ ๋‚ด์šฉ ๋ถˆ๋Ÿฌ์™€์„œ html ์ ์šฉ
        <div v-html="testHtml.value"></div>
</template>

<scipt setup>
import { onMounted, ref, watch, onUnmounted } from 'vue';
import Editor from '@toast-ui/editor';
import '@toast-ui/editor/dist/toastui-editor.css';

const props = defineProps({
	modelValue: {
		type: String,
		required: false,
		default: '',
	},
});

const emit = defineEmits(['update:modelValue']);
const editor = ref();
const editorValid = ref();
const testHtml = ref();

//๋งˆ์šดํŠธ๋ ๋•Œ Editor ์ƒ์„ฑ
onMounted(() => {
		editorValid.value = new Editor({
			el: editor.value,
			height: '500px',
            //'wysiwyg', 'markdown' ํƒ 1
			initialEditType: 'wysiwyg',
			events: {
				change: () => emit('update:modelValue', editorValid.value.getMarkdown()),
			},
		});
});

//์ž‘์„ฑํ•œ ๋‚ด์šฉ ๋ถˆ๋Ÿฌ์™€์„œ html ์ ์šฉ
const testValid = (e) => {
	testHtml.value = editorValid.value.getHTML()
};
</script>

์ฃผ์˜ํ•  ์ ์€ onMounted ๋  ๋•Œ editor๋ฅผ ์ ์šฉํ•˜๋ฏ€๋กœ ์ ์šฉํ•  ํ•ด๋‹น elements๊ฐ€ Hide๋˜์–ด์žˆ์„์‹œ ์—๋Ÿฌ ๋ฐœ์ƒ
๋ฐ˜์‘ํ˜•