(精华)2020年7月30日 微信小程序 富文本编辑器的使用
发布日期:2021-06-29 15:08:33 浏览次数:3 分类:技术文章

本文共 3132 字,大约阅读时间需要 10 分钟。

Page({
data: {
formats: {
}, readOnly: false, placeholder: '在这里尽情创作吧!', editorHeight: 300, keyboardHeight: 0, isIOS: false }, readOnlyChange() {
this.setData({
readOnly: !this.data.readOnly }) }, onLoad() {
const platform = wx.getSystemInfoSync().platform const isIOS = platform === 'ios' this.setData({
isIOS}) const that = this this.updatePosition(0) let keyboardHeight = 0 wx.onKeyboardHeightChange(res => {
if (res.height === keyboardHeight) return const duration = res.height > 0 ? res.duration * 1000 : 0 keyboardHeight = res.height setTimeout(() => {
wx.pageScrollTo({
scrollTop: 0, success() {
that.updatePosition(keyboardHeight) that.editorCtx.scrollIntoView() } }) }, duration) }) }, updatePosition(keyboardHeight) {
const toolbarHeight = 50 const {
windowHeight, platform } = wx.getSystemInfoSync() let editorHeight = keyboardHeight > 0 ? (windowHeight - keyboardHeight - toolbarHeight) : windowHeight this.setData({
editorHeight, keyboardHeight }) }, calNavigationBarAndStatusBar() {
const systemInfo = wx.getSystemInfoSync() const {
statusBarHeight, platform } = systemInfo const isIOS = platform === 'ios' const navigationBarHeight = isIOS ? 44 : 48 return statusBarHeight + navigationBarHeight }, onEditorReady() {
const that = this wx.createSelectorQuery().select('#editor').context(function (res) {
that.editorCtx = res.context }).exec() }, blur() {
this.editorCtx.blur() }, format(e) {
let {
name, value } = e.target.dataset if (!name) return // console.log('format', name, value) this.editorCtx.format(name, value) }, onStatusChange(e) {
const formats = e.detail this.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 = this wx.chooseImage({
count: 1, success: function (res) {
that.editorCtx.insertImage({
src: res.tempFilePaths[0], data: {
id: 'abcd', role: 'god' }, width: '100%', success: function () {
console.log('insert image success') } }) } }) }})

转载地址:https://codeboy.blog.csdn.net/article/details/107686266 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:(精华)2020年7月30日 微信小程序 选择器的使用
下一篇:(精华)2020年7月30日 微信小程序 富文本和文本的使用

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月20日 12时01分19秒