微信小程序官方组件展示之表单组件editor源码

以下将展示微信小程序之表单组件editor源码官方组件能力,组件样式仅供参考,开发者可根据自身需求定义组件样式,具体属性参数详见小程序开发文档。

功能描述:

富文本编辑器,可以对图片、文字进行编辑。

编辑器导出内容支持带标签的 html和纯文本的 text,编辑器内部采用 delta 格式进行存储。

通过setContents接口设置内容时,解析插入的 html 可能会由于一些非法标签导致解析错误,建议开发者在小程序内使用时通过 delta 进行插入。

富文本组件内部引入了一些基本的样式使得内容可以正确的展示,开发时可以进行覆盖。需要注意的是,在其它组件或环境中使用富文本组件导出的 html 时,需要额外引入 这段样式,并维护<ql-container><ql-editor></ql-editor></ql-container>的结构。

图片控件仅初始化时设置有效。

相关 api:EditorContext

属性说明:

编辑器内支持部分 HTML 标签和内联样式,不支持class和id

支持的标签

不满足的标签会被忽略,<div>会被转行为<p>储存。


支持的内联样式

内联样式仅能设置在行内元素或块级元素上,不能同时设置。例如 font-size 归类为行内元素属性,在 p 标签上设置是无效的。


Bug& Tip

1.tip:使用catchtouchend 绑定事件则不会使编辑器失去焦点(2.8.3)

2.tip:插入的 html 中事件绑定会被移除

3.tip:formats 中的color 属性会统一以 hex 格式返回

4.tip:粘贴时仅纯文本内容会被拷贝进编辑器

5.tip:插入 html 到编辑器内时,编辑器会删除一些不必要的标签,以保证内容的统一。例如<p><span>xxx</span></p>会改写为

xxx

6.tip:编辑器聚焦时页面会被上推,系统行为以保证编辑区可见





示例代码:

JAVASCRIPT

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')

      }

    })

  },

  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: '80%',

          success: function () {

            console.log('insert image success')

          }

        })

      }

    })

  }

})


WXML

<view class="container" style="height:{{editorHeight}}px;">

  <editor id="editor" class="ql-container" placeholder="{{placeholder}}" bindstatuschange="onStatusChange" bindready="onEditorReady">

  </editor>

</view>

<view class="toolbar" catchtouchend="format" hidden="{{keyboardHeight > 0 ? false : true}}" style="bottom: {{isIOS ? keyboardHeight : 0}}px">

  <i class="iconfont icon-charutupian" catchtouchend="insertImage"></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>

 

WXSS

.container {

  position: absolute;

  top: 0;

  left: 0;

  width: 100%;

}

.ql-container {

  box-sizing: border-box;

  width: 100%;

  height: 100%;

  font-size: 16px;

  line-height: 1.5;

  overflow: auto;

  padding: 10px 10px 20px 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;

}


版权声明:本站所有内容均由互联网收集整理、上传,如涉及版权问题,请联系我们第一时间处理。

原文链接地址:https://developers.weixin.qq.com/miniprogram/dev/component/editor.html

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,612评论 5 471
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,345评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,625评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,022评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,974评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,227评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,688评论 3 392
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,358评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,490评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,402评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,446评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,126评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,721评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,802评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,013评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,504评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,080评论 2 341

推荐阅读更多精彩内容