# tui-editor

TuiEditor (opens new window) 组件基于 tui-editor 封装,是一款 Markdown 编辑器;tui-editor 官网 (opens new window)GitHub (opens new window)

# Editor

# Props

名称 类型 默认值 说明
v-model String " " 编辑器的内容.
toolbars string[] toolbarItems tui.editor 工具栏的配置.
height String '300px' 编辑器的高度.
initialEditType String 'markdown'
previewStyle 'tab' | 'vertical' | undefined undefined
const toolbarItems: string[] = [
  'heading',
  'bold',
  'italic',
  'strike',
  'divider',
  'hr',
  'quote',
  'divider',
  'ul',
  'ol',
  'task',
  'indent',
  'outdent',
  'divider',
  'table',
  'image',
  'link',
  'divider',
  'code',
  'codeblock',
];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# Viewer

# Props

名称 类型 默认值 说明
value String " " 显示的内容.

# 上传图片

组件统一了整个后台编辑器的上传图片api地址。相关代码如下,你可以自定义修改:

<template>
    <div ref="editorDiv"></div>
</template>
<script lang="ts">
import { defineComponent, onMounted, PropType, Ref, ref } from "vue";
import request from '@/utils/request';
import Editor from '@toast-ui/editor';
import 'codemirror/lib/codemirror.css'; // Editor's Dependency Style
import '@toast-ui/editor/dist/toastui-editor.css'; // Editor's Style

const toolbarItems: string[] = [
  'heading',
  'bold',
  'italic',
  'strike',
  'divider',
  'hr',
  'quote',
  'divider',
  'ul',
  'ol',
  'task',
  'indent',
  'outdent',
  'divider',
  'table',
  'image',
  'link',
  'divider',
  'code',
  'codeblock',
];

interface TuiEditorSetupData {
    editorDiv: Ref;
}

export default defineComponent({
    name: 'TuiEditor',
    props: {
        modelValue: {
            type: String,
            default: ''
        },
        toolbars: {
            type: Array as PropType<string[]>,
            default: toolbarItems
        },
        height: {
            type: String,
            default: '300px'
        },
        previewStyle: {
            type: String as PropType<'tab' | 'vertical'>,
            default: ''
        },
        initialEditType: {
            type: String as PropType<'wysiwyg' | 'markdown'>,
            default: 'markdown'
        },
        useCommandShortcut: {
            type: Boolean,
            default: true
        }
    },
    setup(props, { emit }): TuiEditorSetupData {

        const editorDiv = ref<HTMLElement>();

        onMounted(()=> {
            if(editorDiv.value) {
                const editor = new Editor({
                    el: editorDiv.value,
                    toolbarItems: props.toolbars,
                    initialValue: props.modelValue,
                    height: props.height,
                    previewStyle: props.previewStyle,
                    initialEditType: props.initialEditType,
                    useCommandShortcut: props.useCommandShortcut,
                    events: {
                        /* load: (editor: any) => {
                        }, */
                        change: (/* param: { source: SourceType | 'viewer'; data: MouseEvent } */) => {
                            const value = editor.getMarkdown();
                            emit('update:modelValue', value);
                        },
                    },
                    hooks: {
                        addImageBlobHook: (fileOrBlob, callback) => {
                            const param = new FormData();
                            param.append('file', fileOrBlob);

                            request({
                                headers: { 'Content-Type': 'multipart/form-data' },
                                url: '/uploads',
                                method: 'POST',
                                data: param,
                            })
                            .then(res => {
                                const { data } = res;
                                const { url, name } = data;
                                callback(url, name);
                            })
                            .catch(err => {
                                console.log(err);
                            });
                        },
                    }
                })
            }
        })

        return {
            editorDiv
        }

    }
})
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

# Example

在线链接 (opens new window)

# 卸载

如果你不需要此组件,然后代码打包感觉不需要,可以卸载此组件。

1、CMD 运行

npm uninstall @toast-ui/editor
1

2、删除组件文件目录 @/components/TuiEditor (opens new window)