|
@@ -5,18 +5,19 @@
|
|
|
import {
|
|
|
defineComponent,
|
|
|
ref,
|
|
|
- onMounted,
|
|
|
unref,
|
|
|
- onUnmounted,
|
|
|
nextTick,
|
|
|
computed,
|
|
|
watch,
|
|
|
+ onBeforeUnmount,
|
|
|
+ onDeactivated,
|
|
|
} from 'vue';
|
|
|
import Vditor from 'vditor';
|
|
|
import 'vditor/dist/index.css';
|
|
|
import { useLocale } from '/@/locales/useLocale';
|
|
|
import { useModalContext } from '../../Modal';
|
|
|
import { useRootSetting } from '/@/hooks/setting/useRootSetting';
|
|
|
+ import { onMountedOrActivated } from '/@/hooks/core/onMountedOrActivated';
|
|
|
|
|
|
type Lang = 'zh_CN' | 'en_US' | 'ja_JP' | 'ko_KR' | undefined;
|
|
|
|
|
@@ -26,7 +27,7 @@
|
|
|
height: { type: Number, default: 360 },
|
|
|
value: { type: String, default: '' },
|
|
|
},
|
|
|
- emits: ['change', 'get'],
|
|
|
+ emits: ['change', 'get', 'update:value'],
|
|
|
setup(props, { attrs, emit }) {
|
|
|
const wrapRef = ref<ElRef>(null);
|
|
|
const vditorRef = ref<Nullable<Vditor>>(null);
|
|
@@ -36,17 +37,16 @@
|
|
|
|
|
|
const { getLocale } = useLocale();
|
|
|
const { getDarkMode } = useRootSetting();
|
|
|
+ const valueRef = ref('');
|
|
|
|
|
|
watch(
|
|
|
[() => getDarkMode.value, () => initedRef.value],
|
|
|
- ([val]) => {
|
|
|
- const vditor = unref(vditorRef);
|
|
|
-
|
|
|
- if (!vditor) {
|
|
|
+ ([val, inited]) => {
|
|
|
+ if (!inited) {
|
|
|
return;
|
|
|
}
|
|
|
- const theme = val === 'dark' ? 'dark' : undefined;
|
|
|
- vditor.setTheme(theme as 'dark');
|
|
|
+ const theme = val === 'dark' ? 'dark' : 'classic';
|
|
|
+ instance.getVditor()?.setTheme(theme);
|
|
|
},
|
|
|
{
|
|
|
immediate: true,
|
|
@@ -54,6 +54,16 @@
|
|
|
}
|
|
|
);
|
|
|
|
|
|
+ watch(
|
|
|
+ () => props.value,
|
|
|
+ (v) => {
|
|
|
+ if (v !== valueRef.value) {
|
|
|
+ instance.getVditor()?.setValue(v);
|
|
|
+ valueRef.value = v;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
const getCurrentLang = computed((): 'zh_CN' | 'en_US' | 'ja_JP' | 'ko_KR' => {
|
|
|
let lang: Lang;
|
|
|
switch (unref(getLocale)) {
|
|
@@ -72,54 +82,59 @@
|
|
|
return lang;
|
|
|
});
|
|
|
function init() {
|
|
|
- const wrapEl = unref(wrapRef);
|
|
|
+ const wrapEl = unref(wrapRef) as HTMLElement;
|
|
|
if (!wrapEl) return;
|
|
|
const bindValue = { ...attrs, ...props };
|
|
|
vditorRef.value = new Vditor(wrapEl, {
|
|
|
- theme: 'classic',
|
|
|
+ theme: getDarkMode.value === 'dark' ? 'dark' : 'classic',
|
|
|
lang: unref(getCurrentLang),
|
|
|
mode: 'sv',
|
|
|
preview: {
|
|
|
actions: [],
|
|
|
},
|
|
|
input: (v) => {
|
|
|
- // emit('update:value', v);
|
|
|
+ valueRef.value = v;
|
|
|
+ emit('update:value', v);
|
|
|
emit('change', v);
|
|
|
},
|
|
|
+ after: () => {
|
|
|
+ nextTick(() => {
|
|
|
+ modalFn?.redoModalHeight?.();
|
|
|
+ initedRef.value = true;
|
|
|
+ });
|
|
|
+ },
|
|
|
blur: () => {
|
|
|
- unref(vditorRef)?.setValue(props.value);
|
|
|
+ //unref(vditorRef)?.setValue(props.value);
|
|
|
},
|
|
|
...bindValue,
|
|
|
cache: {
|
|
|
enable: false,
|
|
|
},
|
|
|
});
|
|
|
- initedRef.value = true;
|
|
|
}
|
|
|
|
|
|
const instance = {
|
|
|
getVditor: (): Vditor => vditorRef.value!,
|
|
|
};
|
|
|
|
|
|
- onMounted(() => {
|
|
|
- nextTick(() => {
|
|
|
- init();
|
|
|
- setTimeout(() => {
|
|
|
- modalFn?.redoModalHeight?.();
|
|
|
- }, 200);
|
|
|
- });
|
|
|
-
|
|
|
- emit('get', instance);
|
|
|
- });
|
|
|
-
|
|
|
- onUnmounted(() => {
|
|
|
+ function destroy() {
|
|
|
const vditorInstance = unref(vditorRef);
|
|
|
if (!vditorInstance) return;
|
|
|
try {
|
|
|
vditorInstance?.destroy?.();
|
|
|
} catch (error) {}
|
|
|
+ vditorRef.value = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ onMountedOrActivated(() => {
|
|
|
+ nextTick(() => {
|
|
|
+ init();
|
|
|
+ });
|
|
|
+ emit('get', instance);
|
|
|
});
|
|
|
|
|
|
+ onBeforeUnmount(destroy);
|
|
|
+ onDeactivated(destroy);
|
|
|
return {
|
|
|
wrapRef,
|
|
|
...instance,
|