Comments (2)
富文本图片尽量不要用base64编码,加载慢。 建议使用wangeditor
可以自己修改的:
<!--
* @Description: 对vue-quill-editor封装
* @Author: kcz
* @Date: 2020-03-30 12:44:03
* @LastEditors: your name
* @LastEditTime: 2022-06-08 17:10:12
-->
<template>
<quillEditor
:style="{ height: `${record.options.height}px` }"
:value="value"
ref="vueQuillEditor"
class="ql-editor-class"
:class="{ chinesization: record.options.chinesization, 'disabled-editor': record.options.disabled || parentDisabled }"
:options="editorOption"
:disabled="record.options.disabled || parentDisabled"
@paste.native.stop="imagePaste"
@blur="onEditorBlur($event)"
@focus="onEditorFocus($event)"
@change="onEditorChange($event)"
>
</quillEditor>
</template>
<script>
import { quillEditor } from 'vue-quill-editor' //调用编辑器
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// @jayce
import API from '@api/upload.js'
// 三方插件
/**
* 参考:
* https://github.com/surmon-china/vue-quill-editor/issues/322#issuecomment-490780441
* https://github.com/visualjerk/quill-magic-url
*/
import Quill from 'quill'
import MagicUrl from 'quill-magic-url'
Quill.register('modules/magicUrl', MagicUrl)
var toolbarOptions = [
['bold', 'italic', 'underline', 'strike'], // toggled buttons
['blockquote', 'code-block'],
[{ header: 1 }, { header: 2 }], // custom button values
[{ list: 'ordered' }, { list: 'bullet' }],
[{ script: 'sub' }, { script: 'super' }], // superscript/subscript
[{ indent: '-1' }, { indent: '+1' }], // outdent/indent
// [{ direction: 'rtl' }], // text direction
[{ size: ['small', false, 'large', 'huge'] }], // custom dropdown
[{ header: [1, 2, 3, 4, 5, 6, false] }],
[{ color: [] }, { background: [] }], // dropdown with defaults from theme
// [{ font: [] }],
[{ align: [] }],
// ['clean'], // remove formatting button
['image'],
]
export default {
name: 'editor',
props: ['value', 'record', 'parentDisabled'],
components: { quillEditor },
data() {
return {
editorOption: {
placeholder: this.record.options.placeholder,
modules: {
// 预览状态无toolbar,编辑状态有toolbar
toolbar: toolbarOptions,
// 这里不能响应式控制,改用css 加类名实现toolbar的编辑显示,和预览隐藏
// toolbar: this.record.options.disabled || this.parentDisabled ? false : toolbarOptions,
magicUrl: {
// Regex used to check URLs during typing
urlRegularExpression: /(https?:\/\/[\S]+)|(www.[\S]+)|(tel:[\S]+)/g,
// Regex used to check URLs on paste
globalRegularExpression: /(https?:\/\/|www\.|tel:)[\S]+/g,
},
},
},
}
},
mounted() {
this.overWriteToolbarImageHandler()
},
methods: {
onEditorBlur() {}, // 失去焦点事件
onEditorFocus() {
console.log(this.record.options.disabled, this.parentDisabled, '--line90')
}, // 获得焦点事件
onEditorChange(e) {
this.$emit('change', e.html)
},
/**
* 图片粘贴处理
* 参考: https://github.com/surmon-china/vue-quill-editor/issues/364
* @param event
*/
imagePaste(event) {
var items = event.clipboardData && event.clipboardData.items
var file = null
if (items && items.length) {
// 检索剪切板items
for (var i = 0; i < items.length; i++) {
console.log(items[i])
if (items[i].type.indexOf('image') !== -1) {
file = items[i].getAsFile()
break
}
}
}
if (file && file.type.match(/^image\//i)) {
event.preventDefault()
this.uploadImage(file, true)
}
},
/*
* 图片上传
*/
async uploadImage(data, isPaste) {
let formData = new FormData()
formData.append('file', data.file || data)
// let params = {
// action: 'uploadimage',
// target: this.configData.type,
// }
let res = await API.upload(formData)
let url = window.location.href.split(window.location.port)[0]
url += window.location.port
url += '/file'
// window.open(file.url ? file.url : url + file.response.fileLocation, '_blank')
// return
let resURL = url + (res.success == true && res.fileLocation)
this.insertContent(resURL)
},
insertContent(resURL) {
let quill = this.$refs['vueQuillEditor'].quill
// 获取光标所在位置
let length = quill.getSelection().index
// 插入图片 res.info为服务器返回的图片地址
quill.insertEmbed(length, 'image', resURL)
// 调整光标到最后
quill.setSelection(length + 1)
},
/**
* 覆写 工具栏 image handler
* https://github.com/surmon-china/vue-quill-editor/issues/43#issuecomment-1147189697
*/
overWriteToolbarImageHandler() {
let quill = this.$refs['vueQuillEditor'].quill
var toolbar = quill.getModule('toolbar')
console.log(this.$refs['vueQuillEditor'], '--line133')
// 如果没有toolbar,则是在预览状态,也就不需要去重写ImageHandler
if (!toolbar) return
// toolbar.handlers.image = function () {
// console.log(this)
// }
let _this = this //vue instance
toolbar.addHandler('image', function () {
let fileInput = document.createElement('input')
fileInput.setAttribute('type', 'file')
fileInput.setAttribute('accept', 'image/png, image/gif, image/jpeg, image/bmp, image/x-icon')
fileInput.classList.add('ql-image')
fileInput.addEventListener('change', async function () {
if (fileInput.files != null && fileInput.files[0] != null) {
let formData = new FormData()
formData.append('file', fileInput.files[0])
let res = await API.upload(formData)
let url = window.location.href.split(window.location.port)[0]
url += window.location.port
url += '/file'
let resURL = url + (res.success == true && res.fileLocation)
_this.insertContent(resURL)
}
})
this.container.appendChild(fileInput)
fileInput.click()
})
},
},
}
</script>
<style lang="less" scoped>
.ql-editor-class {
-webkit-box-sizing: border-box;
box-sizing: border-box;
line-height: 1.42;
height: 100%;
outline: none;
padding: 0 0 66px;
tab-size: 4;
-moz-tab-size: 4;
text-align: left;
word-wrap: break-word;
}
/deep/ .ql-snow .ql-editor img {
max-width: 35% !important;
}
/deep/ pre.ql-syntax {
height: auto !important;
}
/deep/ .ql-container {
font-size: 16px;
}
.disabled-editor {
/deep/ .ql-toolbar {
display: none !important;
}
/deep/ .ql-container {
border: none !important;
}
/deep/ .ql-editor {
padding: 0;
margin: 12px 0;
line-height: 1.8;
}
/deep/ .ql-snow .ql-editor blockquote {
margin: 12px 0;
}
}
</style>
from k-form-design.
可以尝试自定义组件扩展富文本或者自定义组件使用其他富文本
from k-form-design.
Related Issues (20)
- 自定义问题 HOT 1
- 选择输入列中嵌套输入框,默认值不加载 HOT 1
- 动态表格列宽调整 HOT 1
- 设计器怎么实现表单嵌套子表单的功能呢? HOT 1
- 使用build cdn加载自定义组件失败 HOT 1
- 表格布局怎么删除行或者列?
- 动态表格数据检验功能 HOT 3
- 3.8.16版本添加动态表格组建后无法获取数据 HOT 2
- 添加动态表格, 在里面插入input组件 选择必选,填写数据后无法获取数据
- 加一个类似步骤表单的控件 HOT 2
- 下拉框或者多选表单的如何setData?
- 动态表格组件里加入文本框组件,操作列会出现高度错位 HOT 1
- 设置json时,下拉选择器不显示,而且报错 HOT 2
- vite 4.x 版本 构建的项目引入k-form-design会报错 目前是vite ^4.3.9 HOT 2
- 自定义组件如何getData
- 当我基于栅格控件自定义了一个加强版的栅格控件时,我每次拖动自定义的栅格控件,里面的元素总是同一个数组的,改变其中一个,同样使用自定义栅格控件的元素也会改变
- 请问自定义控件之后如何配置右侧的控件属性呢,比如是否必填、最大镇等等.......
- 动态表单动态检验有bug HOT 1
- 上传图片头部信息支持动态token吗,目前头部信息是写死的 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from k-form-design.