|
|
@@ -0,0 +1,157 @@
|
|
|
+<template>
|
|
|
+ <div class="main-container">
|
|
|
+ <QuillEditor ref="myQuillEditorRef"
|
|
|
+ theme="snow"
|
|
|
+ v-model:content="data.content"
|
|
|
+ :options="data.editorOption"
|
|
|
+ contentType="delta"
|
|
|
+ @update:content="setValue()"
|
|
|
+ />
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script setup>
|
|
|
+ import { QuillEditor, Quill } from '@vueup/vue-quill'
|
|
|
+ import { reactive, onMounted, ref, toRaw, watch, defineProps } from 'vue'
|
|
|
+ import '@vueup/vue-quill/dist/vue-quill.snow.css'
|
|
|
+ import emitter from '@/utils/emitter'
|
|
|
+ import dayjs from 'dayjs'
|
|
|
+
|
|
|
+ const props = defineProps(['value', 'onTextChangeRef', 'onSelectionChangeRef', 'onGetHTML', 'onGetText', 'onGetQuill'])
|
|
|
+ const emit = defineEmits(['updateValue'])
|
|
|
+
|
|
|
+ const content = ref('')
|
|
|
+ const myQuillEditorRef = ref()
|
|
|
+ const fileBtn = ref()
|
|
|
+
|
|
|
+ const data = reactive({
|
|
|
+ content: ``,
|
|
|
+ editorOption: {
|
|
|
+ modules: {
|
|
|
+ toolbar: [
|
|
|
+ ['bold', 'italic', 'underline', 'strike'],
|
|
|
+ [{ 'size': ['small', false, 'large', 'huge'] }],
|
|
|
+ [{ 'font': [] }],
|
|
|
+ [{ 'align': [] }],
|
|
|
+ [{ 'list': 'ordered' }, { 'list': 'bullet' }],
|
|
|
+ [{ 'indent': '-1' }, { 'indent': '+1' }],
|
|
|
+ [{ 'header': 1 }, { 'header': 2 }],
|
|
|
+ ['image'],
|
|
|
+ [{ 'direction': 'rtl' }],
|
|
|
+ [{ 'color': [] }, { 'background': [] }]
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ placeholder: '请输入内容...'
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ // 抛出更改内容
|
|
|
+ const setValue = () => {
|
|
|
+ const html = toRaw(myQuillEditorRef.value).getHTML()
|
|
|
+ emitter.emit('get-html', html)
|
|
|
+ const text = toRaw(myQuillEditorRef.value).getText()
|
|
|
+ emitter.emit('get-text', text)
|
|
|
+ const content1 = toRaw(myQuillEditorRef.value).getContents()
|
|
|
+ emitter.emit('get-quill-content', content1)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //初始化Quill及行为监控
|
|
|
+ const initQuill = () => {
|
|
|
+ const quill = toRaw(myQuillEditorRef.value).getQuill()
|
|
|
+ if (myQuillEditorRef.value) {
|
|
|
+ quill.getModule('toolbar')
|
|
|
+ }
|
|
|
+
|
|
|
+ // 文本变化监控
|
|
|
+ quill.on(Quill.events.TEXT_CHANGE, (...args) => {
|
|
|
+ console.log("文本变化监控")
|
|
|
+ console.log(args)
|
|
|
+ Object.assign(props.onTextChangeRef, {...args})
|
|
|
+ emitter.emit('change-record', {type: "TEXT_CHANGE", ...args["0"], time: dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss.SSS")})
|
|
|
+ })
|
|
|
+
|
|
|
+ // 光标变化监控
|
|
|
+ quill.on(Quill.events.SELECTION_CHANGE, (...args) => {
|
|
|
+ console.log("光标变化监控")
|
|
|
+ console.log(args)
|
|
|
+ Object.assign(props.onSelectionChangeRef, {...args})
|
|
|
+ emitter.emit('change-record', {type: "SELECTION_CHANGE", ...args["0"], time: dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss.SSS")})
|
|
|
+ });
|
|
|
+
|
|
|
+ // 粘贴行为监控
|
|
|
+ quill.root.addEventListener('paste', function (event) {
|
|
|
+ console.log("粘贴行为监控")
|
|
|
+ var clipboardData = event.clipboardData;
|
|
|
+ if (clipboardData && clipboardData.types && clipboardData.types.includes('text/plain')) {
|
|
|
+ var pastedText = clipboardData.getData('text/plain');
|
|
|
+ console.log('粘贴的纯文本内容:', pastedText);
|
|
|
+ emitter.emit('change-record', {type: "COPY", ops: pastedText, time: dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss.SSS")})
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+// 初始化编辑器
|
|
|
+ onMounted(() => {
|
|
|
+ initQuill()
|
|
|
+ });
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 暂不启用
|
|
|
+ * 用于处理图片
|
|
|
+ * @param state
|
|
|
+ */
|
|
|
+ const imgHandler = (state) => {
|
|
|
+ if (state) {
|
|
|
+ fileBtn.value.click()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 暂不启用
|
|
|
+ * 用于文件上传
|
|
|
+ * @param e
|
|
|
+ */
|
|
|
+ const handleUpload = (e) => {
|
|
|
+ const files = Array.prototype.slice.call(e.target.files)
|
|
|
+ if (!files) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ const formdata = new FormData()
|
|
|
+ formdata.append('file', files[0])
|
|
|
+ backsite.uploadFile(formdata) // 此处使用服务端提供上传接口
|
|
|
+ .then(res => {
|
|
|
+ if (res.data.url) {
|
|
|
+ const quill = toRaw(myQuillEditor.value).getQuill()
|
|
|
+ const length = quill.getSelection().index
|
|
|
+ quill.insertEmbed(length, 'image', res.data.url)
|
|
|
+ quill.setSelection(length + 1)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+</script>
|
|
|
+
|
|
|
+<style scoped lang="scss">
|
|
|
+// 调整样式
|
|
|
+.main-container {
|
|
|
+ height: 85vh;
|
|
|
+ border: solid 2px rgba(199, 199, 199, 0.3);
|
|
|
+}
|
|
|
+
|
|
|
+.main-container :deep(.ql-container) {
|
|
|
+ height: 95%;
|
|
|
+ max-height: 95%;
|
|
|
+ background: #fff;
|
|
|
+}
|
|
|
+
|
|
|
+.main-container :deep(.ql-toolbar) {
|
|
|
+ height: 5%;
|
|
|
+ background: #fff;
|
|
|
+}
|
|
|
+
|
|
|
+.main-container :deep(.ql-formats) {
|
|
|
+ height: 21px;
|
|
|
+ line-height: 21px;
|
|
|
+}
|
|
|
+</style>
|