300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 小程序富文本编辑器(editor)

小程序富文本编辑器(editor)

时间:2018-11-02 11:31:49

相关推荐

小程序富文本编辑器(editor)

由于最近在做小程序 有接触到小程序的富文本编辑器 (editor)。在网上查了部分资料。感觉都比较杂。话不多说 上码。

wxml

<view class="issues"><view class="issue" bindtap="release">文章发布</view></view><view class="container" style="height:{{editorHeight}}px;"><editor id="editor" class="ql-container" placeholder="{{placeholder}}" bindstatuschange="onStatusChange" bindready="onEditorReady"></editor></view><scroll-view scroll-x="{{true}}"><view class="toolbar" catchtouchend="format" style="bottom: {{isIOS ? keyboardHeight : 0}}px"><i class="iconfont icon-charutupian" catchtouchend="insertImage"></i><i class="iconfont icon-format-header-1 {{formats.header === 1 ? 'ql-active' : ''}}" data-name="header" data-value="{{1}}"></i><i class="iconfont icon-format-header-2 {{formats.header === 2 ? 'ql-active' : ''}}" data-name="header" data-value="{{2}}"></i><i class="iconfont icon-format-header-3 {{formats.header === 3 ? 'ql-active' : ''}}" data-name="header" data-value="{{3}}"></i><i class="iconfont icon-zitijiacu {{formats.bold ? 'ql-active' : ''}}" data-name="bold"></i><i class="iconfont icon-zitixieti {{formats.italic ? 'ql-active' : ''}}" data-name="italic"></i><i class="iconfont icon-zitixiahuaxian {{formats.underline ? 'ql-active' : ''}}" data-name="underline"></i><i class="iconfont icon--checklist" data-name="list" data-value="check"></i><i class="iconfont icon-youxupailie {{formats.list === 'ordered' ? 'ql-active' : ''}}" data-name="list" data-value="ordered"></i><i class="iconfont icon-wuxupailie {{formats.list === 'bullet' ? 'ql-active' : ''}}" data-name="list" data-value="bullet"></i></view></scroll-view>

.js

const db = wx.cloud.database()const _ = mandconst util = require("../../../../util/util")Page({data: {formats: {},readOnly: false,placeholder: '在这里尽情创作吧!',editorHeight: 300,keyboardHeight: 0,isIOS: false},readOnlyChange() {this.setData({readOnly: !this.data.readOnly})},onLoad() {const platform = wx.getSystemInfoSync().platformconst isIOS = platform === 'ios'this.setData({isIOS })const that = thisthis.updatePosition(0)let keyboardHeight = 0wx.onKeyboardHeightChange(res => {if (res.height === keyboardHeight) returnconst duration = res.height > 0 ? res.duration * 1000 : 0keyboardHeight = res.heightsetTimeout(() => {wx.pageScrollTo({scrollTop: 0,success() {that.updatePosition(keyboardHeight)that.editorCtx.scrollIntoView()}})}, duration)})},updatePosition(keyboardHeight) {const toolbarHeight = 50const {windowHeight, platform } = wx.getSystemInfoSync()let editorHeight = keyboardHeight > 0 ? (windowHeight - keyboardHeight - toolbarHeight)-35 : windowHeight-35this.setData({editorHeight, keyboardHeight })},calNavigationBarAndStatusBar() {const systemInfo = wx.getSystemInfoSync()const {statusBarHeight, platform } = systemInfoconst isIOS = platform === 'ios'const navigationBarHeight = isIOS ? 44 : 48return statusBarHeight + navigationBarHeight},onEditorReady() {const that = thiswx.createSelectorQuery().select('#editor').context(function (res) {that.editorCtx = res.context}).exec()},blur() {this.editorCtx.blur()},format(e) {let {name, value } = e.target.datasetif (!name) return// console.log('format', name, value)this.editorCtx.format(name, value)},onStatusChange(e) {const formats = e.detailthis.setData({formats })},insertDivider() {this.editorCtx.insertDivider({success: function () {console.log('insert divider success')}})},clickLogText(e) {that.editorCtx.getContents({success: function (res) {console.log(res.html)wx.setStorageSync("content", res.html); // 缓存本地console.log(res.html)}})},clear() {this.editorCtx.clear({success: function (res) {// console.log("clear success")}})},removeFormat() {this.editorCtx.removeFormat()},insertDate() {const date = new Date()const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`this.editorCtx.insertText({text: formatDate})},insertImage() {const that = thiswx.chooseImage({count: 1,success: function (res) {console.log(res.tempFilePaths[0])// 上传云图片wx.cloud.uploadFile({cloudPath: "sheet/" + new Date().getTime() + ".png",filePath: res.tempFilePaths[0], // 文件路径}).then(res => {console.log(res.fileID)that.editorCtx.insertImage({src: res.fileID,data: {id: 'abcd',role: 'god'},width: '100%',success: function () {console.log('insert image success')}})})}})},// 发布release() {this.editorCtx.getContents({success: (res) => {console.log(res)db.collection('clock').where({"_id": "79550af2603f463b08172d5813948f37" }).update({data: {content: _.push({date: util.formatTime(new Date()).substring(0, 10),content: res.html }),},success: res => {wx.navigateTo({url: '../../show/show'})}})console.log("succ:", res);},});console.log("文章发布")}})

wxss

@import “…/common/lib/weui.wxss”;

@import “./assets/iconfont.wxss”;这二文件去示例代码下载

@import "../common/lib/weui.wxss";@import "./assets/iconfont.wxss";.issues {width: 100%;height: 70rpx;position: fixed;z-index: 999;}.issue {position: absolute;background: #2BDC70;width: 150rpx;margin: 10rpx 0;color: #fff;}.container {position: absolute;top: 70rpx;left: 0;width: 100%;}.ql-container {box-sizing: border-box;width: 100%;height: 90%;font-size: 16px;line-height: 1.5;overflow: auto;padding: 10px 10px 10px 10px;border: 1px solid #ECECEC;}.ql-active {color: #22C704;}.iconfont {display: inline-block;width: 30px;height: 30px;cursor: pointer;font-size: 20px;}.toolbar {box-sizing: border-box;padding: 0 10px;height: 50px;width: 100%;position: fixed;left: 0;right: 100%;bottom: 0;display: flex;align-items: center;justify-content: space-between;border: 1px solid #ECECEC;border-left: none;border-right: none;}

小程序api 在开发者工具预览效果 在原有的代码中进行更改 这样比较快捷

删除这段图标显示

显示带HTML标签

<rich-text nodes="{{带HTML标签的数据}}"></rich-text>

有什么问题欢迎留言

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。