|
@@ -18,23 +18,17 @@
|
|
import '@vueup/vue-quill/dist/vue-quill.snow.css';
|
|
import '@vueup/vue-quill/dist/vue-quill.snow.css';
|
|
// import ImageResize from 'quill-image-resize-module';
|
|
// import ImageResize from 'quill-image-resize-module';
|
|
|
|
|
|
- import useParser from './useParser';
|
|
|
|
- import useRender from './useRender';
|
|
|
|
- import { RichTextJSON } from './types';
|
|
|
|
-
|
|
|
|
defineOptions({
|
|
defineOptions({
|
|
name: 'RichEditor',
|
|
name: 'RichEditor',
|
|
});
|
|
});
|
|
|
|
|
|
const props = withDefaults(
|
|
const props = withDefaults(
|
|
defineProps<{
|
|
defineProps<{
|
|
- modelValue: string | RichTextJSON | null;
|
|
|
|
|
|
+ modelValue: string | null;
|
|
placeholder?: string;
|
|
placeholder?: string;
|
|
- autoEmit?: boolean;
|
|
|
|
}>(),
|
|
}>(),
|
|
{
|
|
{
|
|
placeholder: '请输入',
|
|
placeholder: '请输入',
|
|
- autoEmit: false,
|
|
|
|
}
|
|
}
|
|
);
|
|
);
|
|
const emit = defineEmits(['update:modelValue', 'change']);
|
|
const emit = defineEmits(['update:modelValue', 'change']);
|
|
@@ -44,10 +38,17 @@
|
|
'bold',
|
|
'bold',
|
|
'italic',
|
|
'italic',
|
|
'underline',
|
|
'underline',
|
|
|
|
+ 'strike',
|
|
|
|
+ 'link',
|
|
|
|
+ 'image',
|
|
{ script: 'sub' },
|
|
{ script: 'sub' },
|
|
{ script: 'super' },
|
|
{ script: 'super' },
|
|
- 'image',
|
|
|
|
|
|
+ { align: [] },
|
|
|
|
+ { indent: '-1' },
|
|
|
|
+ { indent: '+1' },
|
|
],
|
|
],
|
|
|
|
+ [{ header: [1, 2, 3, 4, 5, 6, false] }],
|
|
|
|
+ [{ size: ['small', false, 'large', 'huge'] }],
|
|
['clean'],
|
|
['clean'],
|
|
];
|
|
];
|
|
|
|
|
|
@@ -62,45 +63,20 @@
|
|
const editorRef = ref();
|
|
const editorRef = ref();
|
|
const content = ref<string>('');
|
|
const content = ref<string>('');
|
|
|
|
|
|
- const { parseRichText } = useParser();
|
|
|
|
- const { renderRichText } = useRender();
|
|
|
|
-
|
|
|
|
function contentChange() {
|
|
function contentChange() {
|
|
- if (!props.autoEmit) return;
|
|
|
|
- const valJson = getValJson();
|
|
|
|
- emit('update:modelValue', valJson);
|
|
|
|
- emit('change', valJson);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- function getValJson() {
|
|
|
|
- const editorContainer = editorRef.value.getEditor();
|
|
|
|
- return parseRichText(editorContainer.childNodes[0]);
|
|
|
|
|
|
+ emit('update:modelValue', content);
|
|
|
|
+ emit('change', content);
|
|
}
|
|
}
|
|
|
|
|
|
watch(
|
|
watch(
|
|
() => props.modelValue,
|
|
() => props.modelValue,
|
|
(val) => {
|
|
(val) => {
|
|
- let valJson = {} as RichTextJSON;
|
|
|
|
- if (val === null || val === '') {
|
|
|
|
- valJson = { sections: [] };
|
|
|
|
- } else if (typeof val === 'string') {
|
|
|
|
- try {
|
|
|
|
- valJson = JSON.parse(val);
|
|
|
|
- } catch (error) {
|
|
|
|
- console.error(error);
|
|
|
|
- valJson = { sections: [] };
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- valJson = val;
|
|
|
|
- }
|
|
|
|
- content.value = renderRichText(valJson);
|
|
|
|
|
|
+ content.value = val || '';
|
|
},
|
|
},
|
|
{
|
|
{
|
|
immediate: true,
|
|
immediate: true,
|
|
}
|
|
}
|
|
);
|
|
);
|
|
-
|
|
|
|
- defineExpose({ getValJson });
|
|
|
|
</script>
|
|
</script>
|
|
|
|
|
|
<style>
|
|
<style>
|