Vue3使用的 vite 腳手架
更新日志
2023-8-29更新: 關于打包,報錯 【“default” 不是由 “node_modules/quill-image-resize-module/image-resize.min.js”導出的 】 ,只需要直接導入就可以 (最下方已給出最新代碼)
import 'quill-image-resize-module/image-resize.min.js';
安裝這幾個庫
富文本
yarn add @vueup/vue-quill
圖片拖拽插件
yarn add quill-image-drop-module
圖片縮放大小插件
yarn add quill-image-resize-module
使用這個庫
這個庫可以實現 webpack.ProvidePlugin({....}) 的方式
yarn add @rollup/plugin-inject
在 Vite 中 plugins 節點下
plugins[
// ..... 一些其他插件配置
inject({
'window.Quill': ['@vueup/vue-quill', 'Quill'],
Quill: ['@vueup/vue-quill', 'Quill'],
}),
]
Quill 富文本注冊插件 ImageDrop 、imageResize
import { QuillEditor, Quill } from '@vueup/vue-quill';
import '@vueup/vue-quill/dist/vue-quill.snow.css';
import { ImageDrop } from 'quill-image-drop-module';
import imageResize from 'quill-image-resize-module';
Quill.register('modules/ImageDrop', ImageDrop);
Quill.register('modules/imageResize', imageResize);
效果
image.png
完整代碼:
<template>
<div>
<QuillEditor
ref="quillEditor"
:options="editorOption"
:content="modelValue"
:style="{ height: height }"
class="quillEditor"
contentType="html"
@update:content="textChange"
/>
</div>
</template>
<script>
import { defineComponent } from 'vue';
import { QuillEditor, Quill } from '@vueup/vue-quill';
import '@vueup/vue-quill/dist/vue-quill.snow.css';
import { ImageDrop } from 'quill-image-drop-module';
// import imageResize from 'quill-image-resize-module';
import 'quill-image-resize-module/image-resize.min.js';
// import QuillBetterTable from 'quill-better-table'; // 需要升級 quill 版本 2.0 以上
// import "quill-better-table/dist/quill-better-table.css";
Quill.register('modules/ImageDrop', ImageDrop);
//Quill.register('modules/imageResize', imageResize);
// Quill.register({'modules/better-table': QuillBetterTable}, true)
// 工具欄配置項
const toolbarOptions = [
// 加粗 斜體 下劃線 刪除線 -----['bold', 'italic', 'underline', 'strike']
['bold', 'italic', 'underline', 'strike'],
// 引用 代碼塊-----['blockquote', 'code-block']
['blockquote', 'code-block'],
// 1、2 級標題-----[{ header: 1 }, { header: 2 }]
[{ header: 1 }, { header: 2 }],
// 有序、無序列表-----[{ list: 'ordered' }, { list: 'bullet' }]
[{ list: 'ordered' }, { list: 'bullet' }],
// 上標/下標-----[{ script: 'sub' }, { script: 'super' }]
[{ script: 'sub' }, { script: 'super' }],
// 縮進-----[{ indent: '-1' }, { indent: '+1' }]
[{ indent: '-1' }, { indent: '+1' }],
// 文本方向-----[{'direction': 'rtl'}]
[{ direction: 'rtl' }],
// 字體大小-----[{ size: ['small', false, 'large', 'huge'] }]
[{ size: ['small', false, 'large', 'huge'] }],
// 標題-----[{ header: [1, 2, 3, 4, 5, 6, false] }]
[{ header: [1, 2, 3, 4, 5, 6, false] }],
// 字體顏色、字體背景顏色-----[{ color: [] }, { background: [] }]
[{ color: [] }, { background: [] }],
// 字體種類-----[{ font: [] }]
[{ font: [] }],
// 對齊方式-----[{ align: [] }]
[{ align: [] }],
// 清除文本格式-----['clean']
['clean'],
// 鏈接、圖片、視頻-----['link', 'image', 'video']
['link', 'image', 'video'],
// ['table'] // 表格
];
export default defineComponent({
name: 'QuillRichtext',
components: {
QuillEditor,
},
props: {
height: {
type: String,
default: '350px',
},
modelValue: String
},
watch: {
},
data() {
return {
myContent: '',
editorOption: {
modules: {
toolbar: toolbarOptions,
history: {
delay: 1000,
maxStack: 50,
userOnly: false,
},
ImageDrop: true,
imageResize: {
displayStyles: {
backgroundColor: 'black',
border: 'none',
color: 'white',
},
modules: ['Resize', 'DisplaySize', 'Toolbar'],
},
// table: false, // disable table module
// 'better-table': {
// operationMenu: {
// items: {
// insertColumnRight: { text: '右邊插入一列' },
// insertColumnLeft: { text: '左邊插入一列' },
// insertRowUp: { text: '上邊插入一行' },
// insertRowDown: { text: '下邊插入一行' },
// mergeCells: { text: '合并單元格' },
// unmergeCells: { text: '拆分單元格' },
// deleteColumn: { text: '刪除列' },
// deleteRow: { text: '刪除行' },
// deleteTable: { text: '刪除表格' },
// },
// background: {
// color: '#333'
// },
// color: {
// colors: ['green', 'red', 'yellow', 'blue', 'white'],
// text: 'background:'
// }
// }
// },
// keyboard: {
// bindings: QuillBetterTable.keyboardBindings
// }
},
theme: 'snow',
placeholder: '請輸入正文',
// Some Quill optiosn...
},
};
},
methods: {
textChange(val) {
// console.log(val)
this.$emit('update:modelValue', val)
}
}
});
</script>
<style lang="less"></style>