123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <script setup lang="ts">
- import { onBeforeUnmount, computed, PropType, unref, nextTick, ref, watch, shallowRef } from 'vue'
- import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
- import { IDomEditor, IEditorConfig, i18nChangeLanguage } from '@wangeditor/editor'
- import { propTypes } from '@/utils/propTypes'
- import { isNumber } from '@/utils/is'
- import { ElMessage } from 'element-plus'
- import { useLocaleStore } from '@/store/modules/locale'
- const localeStore = useLocaleStore()
- const currentLocale = computed(() => localeStore.getCurrentLocale)
- i18nChangeLanguage(unref(currentLocale).lang)
- const props = defineProps({
- editorId: propTypes.string.def('wangeEditor-1'),
- height: propTypes.oneOfType([Number, String]).def('500px'),
- editorConfig: {
- type: Object as PropType<IEditorConfig>,
- default: () => undefined
- },
- defaultHtml: propTypes.string.def('')
- })
- // 编辑器实例,必须用 shallowRef
- const editorRef = shallowRef<IDomEditor>()
- const handleCreated = (editor: IDomEditor) => {
- editorRef.value = editor
- }
- const emit = defineEmits(['change'])
- // 编辑器配置
- const editorConfig = computed((): IEditorConfig => {
- return Object.assign(
- {
- readOnly: false,
- customAlert: (s: string, t: string) => {
- switch (t) {
- case 'success':
- ElMessage.success(s)
- break
- case 'info':
- ElMessage.info(s)
- break
- case 'warning':
- ElMessage.warning(s)
- break
- case 'error':
- ElMessage.error(s)
- break
- default:
- ElMessage.info(s)
- break
- }
- },
- autoFocus: false,
- scroll: true,
- uploadImgShowBase64: true
- },
- props.editorConfig || {}
- )
- })
- const editorStyle = computed(() => {
- return {
- height: isNumber(props.height) ? `${props.height}px` : props.height
- }
- })
- // 回调函数
- const handleChange = (editor: IDomEditor) => {
- emit('change', editor)
- }
- // 组件销毁时,及时销毁编辑器
- onBeforeUnmount(() => {
- const editor = unref(editorRef.value)
- if (editor === null) return
- // 销毁,并移除 editor
- editor?.destroy()
- })
- const getEditorRef = async (): Promise<IDomEditor> => {
- await nextTick()
- return unref(editorRef.value) as IDomEditor
- }
- defineExpose({
- getEditorRef
- })
- const show = ref(true)
- watch(
- () => props.defaultHtml,
- () => {
- show.value = false
- setTimeout(() => {
- show.value = true
- }, 0)
- }
- )
- </script>
- <template>
- <div v-if="show" class="border-1 border-solid border-[var(--tags-view-border-color)]">
- <!-- 工具栏 -->
- <Toolbar
- :editor="editorRef"
- :editorId="editorId"
- class="border-bottom-1 border-solid border-[var(--tags-view-border-color)]"
- />
- <!-- 编辑器 -->
- <Editor
- :editorId="editorId"
- :defaultConfig="editorConfig"
- :defaultHtml="defaultHtml"
- :style="editorStyle"
- @on-change="handleChange"
- @onCreated="handleCreated"
- />
- </div>
- </template>
- <style src="@wangeditor/editor/dist/css/style.css"></style>
|