强大的富文本编辑器:quill github:32k start++,:https://github.com/quilljs/quill
quill粘贴图片上传服务器
<link href="//cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">
<div id="editor" style="height: 370px">
<p>Hello World!</p>
</div>
<script src="//cdn.quilljs.com/1.3.6/quill.min.js"></script>
<script>
var toolbarOptions = [
['bold', 'italic', 'underline', 'strike'],
['blockquote', 'code-block'],
[{'header': 1}, {'header': 2}],
[{'list': 'ordered'}, {'list': 'bullet'}],
[{'script': 'sub'}, {'script': 'super'}],
[{'indent': '-1'}, {'indent': '+1'}],
[{'direction': 'rtl'}],
[{'size': ['small', false, 'large', 'huge']}],
[{'header': [1, 2, 3, 4, 5, 6, false]}],
[{'color': []}, {'background': []}],
[{'font': []}],
[{'align': []}],
['image'],
['clean']
];
var editor = new Quill('#editor', {
modules: {toolbar: toolbarOptions},
theme: 'snow',
});
editor.root.addEventListener("paste", (e) => {
console.log(e)
console.log(this)
const clipboardData = e.clipboardData
const isImage = clipboardData.types.length && clipboardData.types.join('').includes('Files');
if (!isImage) {
return;
}
const file = clipboardData.files[0];
if (!file || !file.name || !(file.name.toLowerCase().indexOf(".png") !== -1 || file.name.toLowerCase().indexOf(".gif") !== -1
|| file.name.toLowerCase().indexOf(".jpg") !== -1)) {
console.log('粘贴的不是图片')
return;
}
var formData = new FormData;
formData.append('file', file)
$.ajax({
data: formData,
processData: false,
contentType: false,
sync: false,
url: '/file/up/img',
type: 'post',
success(data) {
console.log(data)
if (data.code==0){
const range = editor.getSelection(true);
editor.insertEmbed(range.index, 'image', data.data);
}else {
layer.msg(data.msg)
}
}
})
})
editor.clipboard.addMatcher('IMG', (node, delta) => {
const Delta = Quill.import('delta')
return new Delta().insert('')
})
</script>
上传图片到服务器返回url处理
完整代码
<link href="//cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">
<script src="//cdn.quilljs.com/1.3.6/quill.min.js"></script>
<div id="editor" style="height: 370px">
<p>Hello World!</p>
</div>
<input id="opImg" style="display: none;" type="file" onchange="addImg(this)"
accept="image/gif,image/jpeg,image/jpg,image/png,image/svg">
<script>
var editor,$
function addImg(e) {
const upImg = e.files[0];
console.log(editor)
var formData = new FormData;
formData.append('file', upImg)
$.ajax({
data: formData,
processData: false,
contentType: false,
sync: false,
url: '/file/up/img',
type: 'post',
success(data) {
console.log(data)
if (data.code == 0) {
const range = editor.getSelection(true);
editor.insertEmbed(range.index, 'image', data.data);
} else {
layer.msg(data.msg)
}
}
})
}
function imageHandler(e) {
document.getElementById('opImg').click()
}
layui.use(['layer', 'jquery', 'form'], function () {
$ = layui.jquery
var layer = layui.layer
, form = layui.form
var toolbarOptions = [
['bold', 'italic', 'underline', 'strike'],
['blockquote', 'code-block'],
[{'header': 1}, {'header': 2}],
[{'list': 'ordered'}, {'list': 'bullet'}],
[{'script': 'sub'}, {'script': 'super'}],
[{'indent': '-1'}, {'indent': '+1'}],
[{'direction': 'rtl'}],
[{'size': ['small', false, 'large', 'huge']}],
[{'header': [1, 2, 3, 4, 5, 6, false]}],
[{'color': []}, {'background': []}],
[{'font': ['宋体']}],
[{'align': []}],
['image'],
['clean']
];
editor = new Quill('#editor', {
modules: {
toolbar: {
container: toolbarOptions,
handlers: {
image: imageHandler
}
}
},
theme: 'snow',
});
editor.root.addEventListener("paste", (e) => {
console.log(e)
console.log(this)
const clipboardData = e.clipboardData
const isImage = clipboardData.types.length && clipboardData.types.join('').includes('Files');
if (!isImage) {
return;
}
const file = clipboardData.files[0];
if (!file || !file.name || !(file.name.toLowerCase().indexOf(".png") !== -1 || file.name.toLowerCase().indexOf(".gif") !== -1
|| file.name.toLowerCase().indexOf(".jpg") !== -1 || file.name.toLowerCase().indexOf(".jpeg") !== -1)) {
console.log('粘贴的不是图片')
return;
}
console.log(file)
var formData = new FormData;
formData.append('file', file)
$.ajax({
data: formData,
processData: false,
contentType: false,
sync: false,
url: '/file/up/img',
type: 'post',
success(data) {
console.log(data)
if (data.code == 0) {
const range = editor.getSelection(true);
editor.insertEmbed(range.index, 'image', data.data);
} else {
layer.msg(data.msg)
}
}
})
})
editor.clipboard.addMatcher('IMG', (node, delta) => {
const Delta = Quill.import('delta')
return new Delta().insert('')
})
});
</script>
|