微信小程序editor富文本编辑器

这篇具有很好参考价值的文章主要介绍了微信小程序editor富文本编辑器。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

        在开发小程序的时候,需要用到发布文章这个功能,于是就需要使用富文本编辑器。而微信小程序则正好有editor这个组件,不过editor组件的功能,还需要我们自己去调用富文本编辑器的api去自定义。富文本在wxml中可使用<rich-text nodes="{{ value }}"></<rich-text>标签展示。想直接用的可以跳到二、(4)。如果你也向我一样从0开始搭建,那真的是钛裤辣!

一、准备工作

先看看效果图,然后我们再进一步进行搭建。

微信小程序editor富文本编辑器

         我们先看官方文档,搭建上面的这种编辑器需要用到bindready(初始化实例编辑器)、bindfocus(光标聚焦)、bindblur(光标失焦)、bindinput(输入内容时触发)、bindstatuschange(工具栏状态发生变化时触发)这几个属性。

微信小程序editor富文本编辑器

点击相关 api:EditorContext可以看到这样一些方法,在调用这些方法之前需要创建一个实例,只有通过这个实例才能够调用以下的这些方法。创建实例需要用到 wx.createSelectorQuery ,相关创建的代码放到下文。创建完实例后,我们将会用到以下几个方法:

1、EditorContext.format(string name, string value)(修改样式,name和value需要在wxml进行绑定)

2、EditorContext.insertDivider()(插入分割线,需要绑定事件)

3、EditorContext.insertImage(Object object)(插入图片,需要绑定事件)

4、EditorContext.blur()(键盘失焦时收起键盘)

EditorContexthttps://developers.weixin.qq.com/miniprogram/dev/api/media/editor/EditorContext.html

 微信小程序editor富文本编辑器

 二、开始创建

1、wxml如下代码

        在editor中绑定bindready(初始化实例编辑器)、bindfocus(光标聚焦)、bindblur(光标失焦)、bindinput(输入内容时触发)、bindstatuschange(工具栏状态发生变化时触发)这几个属性。在toolBar中绑定catchtouchend(点击工具栏时获取状态)。我们现在只需要管这几个事件、其他的class和style样式可以忽略。

<!--pages/oneEditor/oneEditor.wxml-->
  <view>
    <editor id="editor"  bindready="onEditorReady" bindinput="onInput" bindblur="onBlur" bindstatuschange="onStatusChange">
    </editor>
  </view>
  <view class="toolBar" catchtouchend="format">
    <i></i>
    <i></i>
    <i></i>
    <i></i>
    <i></i>
    <i></i>
    <i></i>
    <i></i>
    <i></i>
    <i></i>
    <i></i>
    <i></i>
    <i></i>
    <i></i>
    <i></i>
  </view>

2、JS代码如下,我们按照函数调用的顺序进行讲解

(1)在初始化编辑器时需要用到<editor>中的id="editor"  bindready="onEditorReady",这里用到的上面讲的wx.createSelectorQuery()。oneEditor就是我们生成的实例,随便你取啥名,到时候调用方法的时候使用,例如this.oneEditor.format()。

 onEditorReady(){
    var that=this
    // 初始化一个实例
    wx.createSelectorQuery().select('#editor').context(res=>{
      that.oneEditor=res.context
      console.log(res.context)
    }).exec()
  },

(2) 初始化完成后(因为class和style还没有添加,所以你们显示的效果图和我的不一样),可以用到<editor>中的οninput="onInput"了,输入1111111可以看到输出的html。

onInput(e){
    console.log(e.detail.delta.ops)
    console.log("html",e.detail.html)
  },

微信小程序editor富文本编辑器

(3)<editor>中的 bindblur="onBlur" ,这里就用到上面的实例了,调用这个方法可以在失焦的时候收起键盘。

  onBlur(e){
    console.log(e)
    this.oneEditor.blur()
  },

 (4)<editor>中的bindstatuschange="onStatusChange"就需要我们在部署完class后讲解了,因为这里涉及到工具栏状态的变化,而我们工具栏还没有部署。下面开始部署工具栏的样式,回到组件那个页面,因为我们需要用到官方的样式和图标,所以得去官网找样式代码。

微信小程序editor富文本编辑器

 微信小程序editor富文本编辑器微信小程序editor富文本编辑器

(5) 这个代码片段其实就是一个编辑器了,但是我们从0开始搭建真的是钛裤辣!(体验的就是一个过程的乐趣)。然后我们把common移到我们自己的项目中去,需要和你的编辑器同层级(方便应用),把assets移到你的编辑器里面,就行了。

 微信小程序editor富文本编辑器微信小程序editor富文本编辑器

 然后我们的class就可以全部加上啦(再加上上面的wxml怕你们忘了)wxml代码如下:

<!--pages/oneEditor/oneEditor.wxml-->
  <view class="editor_container" style="height:{{editorHeight}}rpx">
    <editor id="editor" class="editor" placeholder="{{placeholder}}"    bindready="onEditorReady" bindinput="onInput" bindblur="onBlur" bindstatuschange="onStatusChange">
    </editor>
  </view>
  <view class="toolBar" catchtouchend="format" style="bottom:{{keyboardHeight}}px">
    <i class="iconfont icon-charutupian" catchtouchend="insertImage"></i>
    <i class="iconfont icon-fengexian" catchtouchend="addDivider"></i>
    <i class="iconfont icon-format-header-1 {{formats.header===1?'active':''}}" data-name="header" data-value="1"></i>
    <i class="iconfont icon-format-header-2 {{formats.header===2?'active':''}}" data-name="header" data-value="2"></i>
    <i class="iconfont icon-format-header-3 {{formats.header===3?'active':''}}" data-name="header" data-value="3"></i>
    <i class="iconfont icon-format-header-4 {{formats.header===4?'active':''}}" data-name="header" data-value="4"></i>
    <i class="iconfont icon-zitixiahuaxian {{formats.underline?'active':''}}" data-name="underline"></i>
    <i class="iconfont icon-zitijiacu {{formats.bold?'active':''}}" data-name="bold"></i>
    <i class="iconfont icon-zitixieti {{formats.italic=='em'?'active':''}}" data-name="italic"></i>
    <i class="iconfont icon-zuoduiqi {{formats.align=='left'?'active':''}}" data-name="align" data-value="left"></i>
    <i class="iconfont icon-juzhongduiqi {{formats.align=='center'?'active':''}}" data-name="align"
      data-value="center"></i>
    <i class="iconfont icon-youduiqi {{formats.align=='right'?'active':''}}" data-name="align" data-value="right"></i>
    <i class="iconfont icon-zuoyouduiqi {{formats.align=='justify'?'active':''}}" data-name="align"
      data-value="justify"></i>
    <i class="iconfont icon-zitishanchuxian {{formats.strike=='del'?'active':''}}" data-name="strike"></i>
    <i class="iconfont icon-clearedformat" catchtouchend="clearFormat"></i>
  </view>

(6)wxss代码入下,引入官方的样式和图标,具体的图标样式还需要自己在asserts中的iconfont去寻找引用格式为class="iconfont icon+具体样式",样式有了。那么上面的{{formats}}、data-name、datavalue是咋回事捏。下面开始讲。

/* pages/oneEditor/oneEditor.wxss */
@import "../common/lib/weui.wxss";
@import "./assets/iconfont.wxss";
.tool{
  margin-left: 10rpx;
}
.iconfont {
  display: inline-block;
  width: 30px;
  height: 30px;
  cursor: pointer;
  font-size: 20px;
}
.editor_container {
  /* position: absolute;  */
  top: 0; 
  left: 0; 
  width: 100%;
}
.editor {
  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;
}
.active{
  color: #409EFF;
}
.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;
}

(7)怕看岔了,于是把上面wxml再粘贴下来了,这里讲bindstatuschange="onStatusChange"和catchtouchend="format"这俩是一块的。

<!--pages/oneEditor/oneEditor.wxml-->
  <view class="editor_container" style="height:{{editorHeight}}rpx">
    <editor id="editor" class="editor" placeholder="{{placeholder}}"    bindready="onEditorReady" bindinput="onInput" bindblur="onBlur" bindstatuschange="onStatusChange">
    </editor>
  </view>
  <view class="toolBar" catchtouchend="format" style="bottom:{{keyboardHeight}}px">
    <i class="iconfont icon-charutupian" catchtouchend="insertImage"></i>
    <i class="iconfont icon-fengexian" catchtouchend="addDivider"></i>
    <i class="iconfont icon-format-header-1 {{formats.header===1?'active':''}}" data-name="header" data-value="1"></i>
    <i class="iconfont icon-format-header-2 {{formats.header===2?'active':''}}" data-name="header" data-value="2"></i>
    <i class="iconfont icon-format-header-3 {{formats.header===3?'active':''}}" data-name="header" data-value="3"></i>
    <i class="iconfont icon-format-header-4 {{formats.header===4?'active':''}}" data-name="header" data-value="4"></i>
    <i class="iconfont icon-zitixiahuaxian {{formats.underline?'active':''}}" data-name="underline"></i>
    <i class="iconfont icon-zitijiacu {{formats.bold?'active':''}}" data-name="bold"></i>
    <i class="iconfont icon-zitixieti {{formats.italic=='em'?'active':''}}" data-name="italic"></i>
    <i class="iconfont icon-zuoduiqi {{formats.align=='left'?'active':''}}" data-name="align" data-value="left"></i>
    <i class="iconfont icon-juzhongduiqi {{formats.align=='center'?'active':''}}" data-name="align"
      data-value="center"></i>
    <i class="iconfont icon-youduiqi {{formats.align=='right'?'active':''}}" data-name="align" data-value="right"></i>
    <i class="iconfont icon-zuoyouduiqi {{formats.align=='justify'?'active':''}}" data-name="align"
      data-value="justify"></i>
    <i class="iconfont icon-zitishanchuxian {{formats.strike=='del'?'active':''}}" data-name="strike"></i>
    <i class="iconfont icon-clearedformat" catchtouchend="clearFormat"></i>
  </view>

(8)onStatusChange,先在<i>中绑定data-name,name的名字参考官方文档,我点击了斜体(看52行),然后这个函数就把工具栏的开启状态记录下来了,于是我们只需根据formats的值来判断是否开启了工具栏的某个功能,改变class中的触发样式,比如我把active设置成了蓝色。

微信小程序editor富文本编辑器

(9) format是用来保存当前样式的,比如我点击H1,会记录一个name:header标识这个是标题,而value:1标识这是一个一级标题,其他的类似。

微信小程序editor富文本编辑器

 3、通过实例调用api

只需要在wxml绑定函数,并在函数里面调用api就可以了,例如插入下划线,我在wxml用catchtouchend="addDivider"绑定了一个事件

  addDivider(){
    this.oneEditor.insertDivider(res=>{
      console.log(res)
    })
  },

微信小程序editor富文本编辑器

 还有一些调整键盘高度和上传图片这里就不讲了吧,直接上代码。文章来源地址https://www.toymoban.com/news/detail-488088.html

 onLoad: function (options) {
    //获取键盘高度
    wx.onKeyboardHeightChange((res) => {          
      console.log("KeyboardHeight",res.height)
      this.oneEditor.scrollIntoView()
      this.setData({
        keyboradHight:res.height
      })
    })
  },
insertImage(){
    var that=this
    wx.chooseMedia({
      count:1,
      mediaType:['image'],
      sourceType:['album', 'camera'],
      success(res){
        console.log(res)
        let url=res.tempFiles[0].tempFilePath
          that.oneEditor.insertImage({
            src:url,
            extClass:"image",
            alt:"图片加载错误",
            width:"100%",
            height:"300rpx"
          })
      }
    })
   
  },

到了这里,关于微信小程序editor富文本编辑器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 小程序Editor富文本编辑器-封装使用用法

    本文章主要讲述editor中小程序的图片上传和pc端数据不同步的问题,建议多看下代码 完整代码在最下面 1、创建个用于组件封装的editor文件夹,存放三个文件  2、editor.vue文件是主要封装代码 3、editor-icon.css文件样式 5、如果上传图片失败或者是图片裂开,和pc端数据不同步的话

    2024年02月11日
    浏览(36)
  • Eclipse - Text Editors (文本编辑器)

    Window - Preferences - General - Editors - Text Editors Displayed tab witdth: 4 勾选 Insert spaces for tabs 勾选 Show line number [1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/

    2024年02月21日
    浏览(38)
  • 富文本编辑器 ck-editor5 的使用

    最近在项目中需要用到富文本编辑器,据说ck-editor5很不错,于是就使用它了,不过在期间也遇到了很多问题,这里记录下。 一、引入ck-editor5 文档地址:Predefined builds - CKEditor 5 Documentation 这里有个坑,我最初是根据文档执行下面的npm命令下载的,最后捣腾了半天发现里面功能

    2024年01月20日
    浏览(38)
  • 富文本编辑器 VUE-QUILL-EDITOR 使用教程

    1、NPM 导入 VUE-QUILL-EDITOR 2、引入 VUE-QUILL-EDITOR 在全局中引入 在指定的 vue 文件中引入 3、在 VUE 中使用 到这里一个默认的富文本编辑器已经导入使用了,如下图所视! 一般的,我们在使用的时候并不需要这么多功能,可以适当的对编辑器配置项进行配置。 可以根据自己的实际

    2024年02月09日
    浏览(58)
  • vue-quill-editor富文本编辑器使用步骤

    首先放上效果图 目录 1.安装 2.引入到项目中 3.在页面上写组件 4.配置option 5.给工具栏鼠标悬停加上中文释义 6.上传图片到七牛云 7.自定义控制图片大小 1.安装 2.引入到项目中         有两种挂载方式: 全局挂载 和 在组件中挂载,根据自己的项目需求选择,一般用到富文

    2024年02月06日
    浏览(38)
  • vue使用富文本编辑器vue-quill-editor

    问题描述: 我们在开发过程中经常会遇到使用富文本编辑器进行操作,当前插件市场上关于富文本的编辑器挺多的,我们就不一一个介绍了,现在我们主要讲解一些vue-quill-editor富文本编辑器的使用操作和注意事项。 效果图 具体操作使用 1. 安装 2. 引入到项目中 有两种挂载方

    2024年02月02日
    浏览(46)
  • 富文本编辑器 VUE-QUILL-EDITOR 使用教程 (最全)

    VUE-QUILL-EDITOR 基于 QUILL、适用于 VUE 的富文本编辑器,支持服务端渲染和单页应用,非常高效简洁。 在全局中引入 在指定的 vue 文件中引入 到这里一个默认的富文本编辑器已经导入使用了,如下图所视! 一般的,我们在使用的时候并不需要这么多功能,可以适当的对编辑器配

    2024年02月04日
    浏览(40)
  • 简版的富文本编辑器、VUE+ElementUI 富文本编辑器 element ui富文本编辑器的使用(quill-editor) 不好用你来打我!全网醉简单!要复杂的别来!

    实现效果   1.安装插件 npm install vue-quill-editor --save 2.安装成功后在package.json中查看 3.在main.js中全局引入插件 4.页面实现 感谢老哥: ElementUI生成富文本编辑器 https://blog.csdn.net/keplerm/article/details/123379511?spm=1001.2101.3001.6650.9utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCom

    2024年02月16日
    浏览(60)
  • element ui富文本编辑器的使用(quill-editor)

    可以拖拽图片大小及其位置 为了便于大家直接使用,直接把script以及css放在一个页面里,之际copy就可以使用 注意: 1、我是在elementUi使用的,上传图片以及页面的访问需要有登录权限,所以我的上传图片视频的组件里有:headers=“headers”,携带登录权限 2、需要更改自己的上

    2024年02月03日
    浏览(36)
  • vue-quill-editor富文本编辑器-扩展表格、图片调整大小

    上篇文章已经讲到、vue-quill-editor的基本配置和图片转成url 这篇文章主要使用插件来完成 图片调整大小 和 表格的插件使用( 这两个目前quill 版本并不兼容 如果有大神解决了还望指点 ) 参考文章: vue-quill-editor 富文本编辑器支持图片拖拽和放大缩小_*且听风吟的博客-CSDN博

    2024年02月04日
    浏览(46)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包