|
9 月之前 | |
---|---|---|
.. | ||
components | 9 月之前 | |
icons | 9 月之前 | |
static | 9 月之前 | |
utils | 9 月之前 | |
changelog.md | 9 月之前 | |
package.json | 9 月之前 | |
readme.md | 9 月之前 |
<template>
<view class="home">
<view class="editor-box">
<sp-editor
:toolbar-config="{
excludeKeys: ['direction', 'date', 'lineHeight', 'letterSpacing', 'listCheck'],
iconSize: '18px'
}"
@init="initEditor"
@input="inputOver"
@upinImage="upinImage"
@overMax="overMax"
@addLink="addLink"
@exportHtml="exportHtml"
></sp-editor>
</view>
</view>
</template>
<script>
export default {
data() {
return {
editorIns: null
}
},
methods: {
/**
* 获取输入内容
* @param {Object} e {html,text} 内容的html文本,和text文本
*/
inputOver(e) {
// 可以在此处获取到编辑器已编辑的内容
console.log('==== inputOver :', e)
},
/**
* 超出最大内容限制
* @param {Object} e {html,text} 内容的html文本,和text文本
*/
overMax(e) {
// 若设置了最大字数限制,可在此处触发超出限制的回调
console.log('==== overMax :', e)
},
/**
* 编辑器就绪
* @param {Object} editor 编辑器实例,你可以自定义调用editor实例的方法
* @tutorial editor组件 https://uniapp.dcloud.net.cn/component/editor.html#editor-%E7%BB%84%E4%BB%B6
* @tutorial 相关api https://uniapp.dcloud.net.cn/api/media/editor-context.html
*/
initEditor(editor) {
this.editorIns = editor // 保存编辑器实例
// 保存编辑器实例后,可以在此处获取后端数据,并赋值给编辑器初始化内容
this.preRender()
},
preRender() {
setTimeout(() => {
// 异步获取后端数据后,初始化编辑器内容
this.editorIns.setContents({
html: `<div> 猫猫<img src="https://img.yzcdn.cn/vant/cat.jpeg"/>
<img src="https://img.yzcdn.cn/vant/cat.jpeg"/>
<img src="https://img.yzcdn.cn/vant/cat.jpeg"/>yaho giao</div>`
})
}, 1000)
},
/**
* 直接运行示例工程插入图片无法正常显示的看这里
* 因为插件默认采用云端存储图片的方式
* 以$emit('upinImage', tempFiles, this.editorCtx)的方式回调
* @param {Object} tempFiles
* @param {Object} editorCtx
*/
upinImage(tempFiles, editorCtx) {
/**
* 本地临时插入图片预览
* 注意:这里仅是示例本地图片预览,因为需要将图片先上传到云端,再将图片插入到编辑器中
* 正式开发时,还请将此处注释,并解开下面 使用 uniCloud.uploadFile 上传图片的示例方法 的注释
* @tutorial https://uniapp.dcloud.net.cn/api/media/editor-context.html#editorcontext-insertimage
*/
// #ifdef MP-WEIXIN
// 注意微信小程序的图片路径是在tempFilePath字段中
editorCtx.insertImage({
src: tempFiles[0].tempFilePath,
width: '80%', // 默认不建议铺满宽度100%,预留一点空隙以便用户编辑
success: function () {}
})
// #endif
// #ifndef MP-WEIXIN
editorCtx.insertImage({
src: tempFiles[0].path,
width: '80%', // 默认不建议铺满宽度100%,预留一点空隙以便用户编辑
success: function () {}
})
// #endif
/**
* 使用 uniCloud.uploadFile 上传图片的示例方法(可适用多选上传)
* 正式开发环境中,请将上面 本地临时插入图片预览 注释后,模仿以下写法
*/
// tempFiles.forEach(async (item) => {
// uni.showLoading({
// title: '上传中请稍后',
// mask: true
// })
// let upfile = await uniCloud.uploadFile({
// filePath: item.path,
// // 同名会导致报错 policy_does_not_allow_file_overwrite
// // cloudPath可由 想要存储的文件夹/文件名 拼接,若不拼文件夹名则默认存储在cloudstorage文件夹中
// cloudPath: `cloudstorage/${item.name}`,
// cloudPathAsRealPath: true
// })
// editorCtx.insertImage({
// src: upfile.fileID,
// width: '80%', // 默认不建议铺满宽度100%,预留一点空隙以便用户编辑
// success: function () {
// uni.hideLoading()
// }
// })
// })
},
/**
* 导出 - toolbar需要开启export工具
* @param {string} e 导出的html内容
*/
exportHtml(e) {
uni.navigateTo({
url: '/pages/out/out',
success(res) {
// 传至导出页面解析即可
res.eventChannel.emit('e-transmit-html', {
data: e
})
}
})
},
/**
* 添加超链接
* @param {Object} e { text: '链接描述', href: '链接地址' }
*/
addLink(e) {
console.log('==== addLink :', e)
}
}
}
</script>
// 空白占位字样
placeholder: {
type: String,
default: '写点什么吧 ~'
},
// 是否只读
readOnly: {
type: Boolean,
default: false
},
// 最大字数限制,默认-1不限
maxlength: {
type: Number,
default: -1
},
// 工具栏配置 - 默认全工具栏 keys与excludeKeys两者皆为空,即为全工具
toolbarConfig: {
type: Object,
default: () => {
return {
keys: [], // 要显示的工具,优先级最大
excludeKeys: [], // 除这些指定的工具外,其他都显示 // 默认两者皆为空,即为全工具
iconSize: '20px', // 工具栏字体大小
iconColumns: 10 // 工具栏列数
}
}
}
建议按需引入对应的工具,尽量简化富文本工具
toolbarAllList: [
'bold', // 加粗
'italic', // 斜体
'underline', // 下划线
'strike', // 删除线
'alignLeft', // 左对齐
'alignCenter', // 居中对齐
'alignRight', // 右对齐
'alignJustify', // 两端对齐
'lineHeight', // 行间距
'letterSpacing', // 字间距
'marginTop', // 段前距
'marginBottom', // 段后距
'fontFamily', // 字体
'fontSize', // 字号
'color', // 文字颜色
'backgroundColor', // 背景颜色
'date', // 日期
'listCheck', // 待办
'listOrdered', // 有序列表
'listBullet', // 无序列表
'indentInc', // 增加缩进
'indentDec', // 减少缩进
'divider', // 分割线
'header', // 标题
'scriptSub', // 下标
'scriptSuper', // 上标
'direction', // 文本方向
'image', // 图片
'link', // 超链接
'undo', // 撤销
'redo', // 重做
'removeFormat', // 清除格式
'clear', // 清空
'export' // 导出
]
// 如上述使用方式示例中,已有详细描述,此处不再赘述
@init="initEditor"
@input="inputOver"
@upinImage="upinImage"
@overMax="overMax"
@exportHtml="exportHtml" // toolbar需要开启export工具
@addLink="addLink" // toolbar需要开启link工具
在H5中,由于官方editor内部是使用unpkg加载quill.min.js、image-resize.min.js两个依赖,可能会存在国内无法访问unpkg的资源,导致editor不正常的问题,在此我已将该两个依赖包放在了组件的 uni_modules/sp-editor/static 文件夹下,希望在H5端正常且稳定使用的小伙伴可以在index.html或template.h5.html中引入该本地cdn资源
npm i quill@1.3.7
由于官方editor组件中的insertText方法在不同平台是基于各平台对应的富文本技术实现,可能存在部分问题:
请阅读了解 editor支持的标签
关于添加插入超链接的问题:
若对插件有任何疑问或者优化建议,欢迎在评论区留言,在插件市场中的私信消息本人可能不经常留意,导致没能及时回复, 可以加入本人的插件问答QQ交流群: 852637893,欢迎进群交流。