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

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

软件事业部
软件事业部原创
2022年08月25日 12:58:30856浏览

以下将展示微信小程序之表单组件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>会改写为<p>xxx</p>
6.tip: 编辑器聚焦时页面会被上推,系统行为以保证编辑区可见

示例代码:
JAVASCRIPT:

  1. Page({
  2. data: {
  3. formats: {},
  4. readOnly: false,
  5. placeholder: '开始输入...',
  6. editorHeight: 300,
  7. keyboardHeight: 0,
  8. isIOS: false
  9. },
  10. readOnlyChange() {
  11. this.setData({
  12. readOnly: !this.data.readOnly
  13. })
  14. },
  15. onLoad() {
  16. const platform = wx.getSystemInfoSync().platform
  17. const isIOS = platform === 'ios'
  18. this.setData({ isIOS})
  19. const that = this
  20. this.updatePosition(0)
  21. let keyboardHeight = 0
  22. wx.onKeyboardHeightChange(res => {
  23. if (res.height === keyboardHeight) return
  24. const duration = res.height > 0 ? res.duration * 1000 : 0
  25. keyboardHeight = res.height
  26. setTimeout(() => {
  27. wx.pageScrollTo({
  28. scrollTop: 0,
  29. success() {
  30. that.updatePosition(keyboardHeight)
  31. that.editorCtx.scrollIntoView()
  32. }
  33. })
  34. }, duration)
  35. })
  36. },
  37. updatePosition(keyboardHeight) {
  38. const toolbarHeight = 50
  39. const { windowHeight, platform } = wx.getSystemInfoSync()
  40. let editorHeight = keyboardHeight > 0 ? (windowHeight - keyboardHeight - toolbarHeight) : windowHeight
  41. this.setData({ editorHeight, keyboardHeight })
  42. },
  43. calNavigationBarAndStatusBar() {
  44. const systemInfo = wx.getSystemInfoSync()
  45. const { statusBarHeight, platform } = systemInfo
  46. const isIOS = platform === 'ios'
  47. const navigationBarHeight = isIOS ? 44 : 48
  48. return statusBarHeight + navigationBarHeight
  49. },
  50. onEditorReady() {
  51. const that = this
  52. wx.createSelectorQuery().select('#editor').context(function (res) {
  53. that.editorCtx = res.context
  54. }).exec()
  55. },
  56. blur() {
  57. this.editorCtx.blur()
  58. },
  59. format(e) {
  60. let { name, value } = e.target.dataset
  61. if (!name) return
  62. // console.log('format', name, value)
  63. this.editorCtx.format(name, value)
  64. },
  65. onStatusChange(e) {
  66. const formats = e.detail
  67. this.setData({ formats })
  68. },
  69. insertDivider() {
  70. this.editorCtx.insertDivider({
  71. success: function () {
  72. console.log('insert divider success')
  73. }
  74. })
  75. },
  76. clear() {
  77. this.editorCtx.clear({
  78. success: function (res) {
  79. console.log("clear success")
  80. }
  81. })
  82. },
  83. removeFormat() {
  84. this.editorCtx.removeFormat()
  85. },
  86. insertDate() {
  87. const date = new Date()
  88. const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
  89. this.editorCtx.insertText({
  90. text: formatDate
  91. })
  92. },
  93. insertImage() {
  94. const that = this
  95. wx.chooseImage({
  96. count: 1,
  97. success: function (res) {
  98. that.editorCtx.insertImage({
  99. src: res.tempFilePaths[0],
  100. data: {
  101. id: 'abcd',
  102. role: 'god'
  103. },
  104. width: '80%',
  105. success: function () {
  106. console.log('insert image success')
  107. }
  108. })
  109. }
  110. })
  111. }
  112. })

WXML:

  1. <view class="container" style="height:{{editorHeight}}px;">
  2. <editor id="editor" class="ql-container" placeholder="{{placeholder}}" bindstatuschange="onStatusChange" bindready="onEditorReady">
  3. </editor>
  4. </view>
  5. <view class="toolbar" catchtouchend="format" hidden="{{keyboardHeight > 0 ? false : true}}" style="bottom: {{isIOS ? keyboardHeight : 0}}px">
  6. <i class="iconfont icon-charutupian" catchtouchend="insertImage"></i>
  7. <i class="iconfont icon-format-header-2 {{formats.header === 2 ? 'ql-active' : ''}}" data-name="header" data-value="{{2}}"></i>
  8. <i class="iconfont icon-format-header-3 {{formats.header === 3 ? 'ql-active' : ''}}" data-name="header" data-value="{{3}}"></i>
  9. <i class="iconfont icon-zitijiacu {{formats.bold ? 'ql-active' : ''}}" data-name="bold"></i>
  10. <i class="iconfont icon-zitixieti {{formats.italic ? 'ql-active' : ''}}" data-name="italic"></i>
  11. <i class="iconfont icon-zitixiahuaxian {{formats.underline ? 'ql-active' : ''}}" data-name="underline"></i>
  12. <i class="iconfont icon--checklist" data-name="list" data-value="check"></i>
  13. <i class="iconfont icon-youxupailie {{formats.list === 'ordered' ? 'ql-active' : ''}}" data-name="list" data-value="ordered"></i>
  14. <i class="iconfont icon-wuxupailie {{formats.list === 'bullet' ? 'ql-active' : ''}}" data-name="list" data-value="bullet"></i>
  15. </view>

WXSS:

  1. .container {
  2. position: absolute;
  3. top: 0;
  4. left: 0;
  5. width: 100%;
  6. }
  7. .ql-container {
  8. box-sizing: border-box;
  9. width: 100%;
  10. height: 100%;
  11. font-size: 16px;
  12. line-height: 1.5;
  13. overflow: auto;
  14. padding: 10px 10px 20px 10px;
  15. border: 1px solid #ECECEC;
  16. }
  17. .ql-active {
  18. color: #22C704;
  19. }
  20. .iconfont {
  21. display: inline-block;
  22. width: 30px;
  23. height: 30px;
  24. cursor: pointer;
  25. font-size: 20px;
  26. }
  27. .toolbar {
  28. box-sizing: border-box;
  29. padding: 0 10px;
  30. height: 50px;
  31. width: 100%;
  32. position: fixed;
  33. left: 0;
  34. right: 100%;
  35. bottom: 0;
  36. display: flex;
  37. align-items: center;
  38. justify-content: space-between;
  39. border: 1px solid #ECECEC;
  40. border-left: none;
  41. border-right: none;
  42. }

版权声明: 本站所有内容均由互联网收集整理、上传,如涉及版权问题,我们第一时间处理。
原文链接地址:https://developers.weixin.qq.com/miniprogram/dev/component/editor.html

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议