当前位置:网站首页>Enhanced paste quill editor
Enhanced paste quill editor
2022-06-25 05:09:00 【Lijianyi】
Kung Fu brothers can have a look at my Blog www.maple.ink
Paste picture into rich text
Drag and drop the image directly to rich text , Or copy image files from the system , Paste in rich text , Can't put pictures into rich text . The former will open a new window directly in the browser , The latter has no direct reaction .
Through installation quill-image-extend-module The enhancement module enables the rich text editor to drag and drop pictures into the rich text box , But it does not have the function of copying and pasting into rich text . It can cooperate with quill-image-resize-module The module sets the size of the picture .
// Introduce picture enhancement module
import {
container, ImageExtend, QuillWatch} from 'quill-image-extend-module'
Quill.register('modules/ImageExtend', ImageExtend)
// The picture control enhancement module is introduced
import ImageResize from 'quill-image-resize-module'
Quill.register('modules/ImageResize', ImageResize)
export default {
data() {
return {
editorOptions: {
modules: {
ImageExtend: {
loading: true,
name: 'img',
action: '',
headers: (xhr) => {
},
response: (res) => {
return res.info
}
},
ImageResize: {
modules: [ 'Resize', 'DisplaySize', 'Toolbar' ]
},
}
}
}
}
}

Be careful : Although this method has the function of displaying pictures in rich text boxes , But the principle is actually to get the picture file object , adopt FileReader Object to convert it to base64 Format , Put in img Of src Properties of the , Therefore, theoretically speaking, it does not have the ability to transfer large files , There may also be some degree of distortion .
Paste file into rich text
The main principle of pasting a file is to bind the paste event of the current rich text box , Get the pasted by pasting the event file object .
// Introduce custom formats
import uploadFile from './quillPlus/uploadFile';
uploadFile.blotName = 'uploadFile';
uploadFile.tagName = 'div';
Quill.register(uploadFile);
methods: {
onEditorFocus() {
let quill = this.$refs.editor.quill,
self_ = this;
// Listen for paste events
quill.root.addEventListener('paste', (event)=> {
// Determine whether the pasted picture is
if(event.clipboardData && event.clipboardData.files && event.clipboardData.files) {
// Prevent native Events
event.preventDefault();
let cursorIndex = quill.selection.savedRange.index;
[].forEach.call(event.clipboardData.files, file => {
// Determine whether the current file is a picture
if(file.type.match(/^image\/(gif|jpe?g|a?png|bmp)/i)) {
let reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function(e) {
quill.insertEmbed(cursorIndex, 'image', e.target.result);
}
}else {
// Notice here because insertEmbed Statements can only be inserted into rich text in a well-defined format , So we need to extend the current format
quill.insertEmbed(cursorIndex, 'uploadFile', {
file: file, fileIndex: self_.fileIndex++});
}
})
}
});
},
}
Custom format components
import {
Quill} from 'vue-quill-editor';
const BlockEmbed = Quill.import('blots/block/embed');
class uploadFile extends BlockEmbed {
static create(value) {
let node = super.create();
// Set up a class Class name controls style
node.setAttribute('class', 'upload-file');
// Setting does not allow operation
node.setAttribute('contentEditable', false);
// Set up data-id Mark
node.setAttribute('data-id', value.fileIndex);
node.innerText = value.file.name;
return node;
}
static value(node) {
return {
dataId: node.getAttribute('data-id')
}
}
}
export default uploadFile;
This solution can also solve the problem of uploading pictures , Especially big pictures .
Be careful : There are two solutions for uploading files :
- Paste the file into rich text to trigger uploading , adopt HTTP Requesting a direct call to the interface will file Object to fromData Deliver to the server . After receiving the file, the server returns the link of the current file on the server , Insert link into page DOM On the property of .
- Paste the file into the rich text and the current file Object into an array , Click the upload button to upload to the server through the loop .
The first solution is recommended for uploading files , as a result of : Easy front end ~

边栏推荐
- JS function to realize simple calculator
- epplus复制模板后打印区域变小的问题
- Redis (17)
- Svg code snippet of loading animation
- File upload vulnerability (III)
- Small sample learning data set
- 2021-10-24
- Page electronic clock (use js to dynamically obtain time display)
- cuda编译报错
- Precise delay based on Cortex-M3 and M4 (systick delay of system timer can be used for STM32, aducm4050, etc.)
猜你喜欢

Personalized Federated Learning with Moreau Envelopes

Why does the SQL statement hit the index faster than it does not?

XSS (cross site script attack) summary (II)

Eyeshot 2022 Released

Two hours to take you into the software testing industry (with a full set of software testing learning routes)

【FLink】access closed classloader classloader. check-leaked-classloader

Rce code execution & command execution (V)

《QDebug 2022年6月》

dotnet-exec 0.4.0 released

How micro engine uploads remote attachments
随机推荐
parallel recovery slave next change & parallel recovery push change
parallel recovery slave next change & parallel recovery push change
ThinkPHP 5 log management
February 20ctf record
Teach you to write non maintainable PHP code step by step
win11蓝牙无法连接怎么办?win11蓝牙无法连接的解决方法
buuctf web
Get to know the drawing component of flutter - custompaint
What if the desktop computer is not connected to WiFi
基于SSH实现的学生成绩管理系统
TX Text Control 30.0 ActiveX
Creation and use of MySQL index
Opensea PHP development kit
A summary of the experiment of continue and break in C language
H5 canvas drawing circle drawing fillet [detailed explanation]
《QDebug 2022年6月》
两小时带你进入软件测试行业风口(附全套软件测试学习路线)
融合CDN,为客户打造极致服务体验!
Detailed summary of position positioning
Eyeshot Ultimate 2022 Crack By Xacker