hg-editor.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // components/hg-editor/hg-editor.js
  2. const app = getApp()
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. properties: {
  8. /**是否显示工具栏 */
  9. showTabBar: {
  10. type: 'Boolean',
  11. value: true
  12. },
  13. placeholder: {
  14. type: 'String',
  15. value: '请输入相关内容'
  16. },
  17. name: {
  18. type: 'String',
  19. value: ''
  20. },
  21. },
  22. // 生命周期函数,可以为函数,或一个在methods段中定义的方法名
  23. attached: function () { }, // 此处attached的声明会被lifetimes字段中的声明覆盖
  24. ready: function () { },
  25. pageLifetimes: {
  26. // 组件所在页面的生命周期函数
  27. show: function () { },
  28. hide: function () { },
  29. resize: function () { },
  30. },
  31. /**
  32. * 组件的初始数据
  33. */
  34. data: {
  35. },
  36. /**
  37. * 组件的方法列表
  38. */
  39. methods: {
  40. _onEditorReady: async function () {
  41. const that = this;
  42. that.createSelectorQuery().select('#editor').context(function (res) {
  43. that.editorCtx = res.context
  44. let html = that;
  45. if (html) {
  46. that.editorCtx.setContents({
  47. html: html,
  48. success: (res) => {
  49. },
  50. fail: (res) => { console.log(res) }
  51. })
  52. }
  53. }).exec()
  54. },
  55. //插入图片
  56. _addImage: function (event) {
  57. let _this = this;
  58. wx.chooseImage({
  59. count: 1,
  60. sizeType: ['compressed'],
  61. sourceType: ['album'],
  62. success: function (res) {
  63. _this._uploadImage(res.tempFilePaths[0]);
  64. }
  65. });
  66. },
  67. _uploadImage: function (tempFilePath) {
  68. let _this = this;
  69. wx.uploadFile({
  70. url: `${app.globalData.fileUrl}/files/court/elimg/upload`,
  71. filePath: tempFilePath,
  72. name: 'file',
  73. formData: {},
  74. success: (res) => {
  75. let arr = JSON.parse(res.data);
  76. if (arr.errcode == '0') {
  77. _this.editorCtx.insertImage({ src: app.globalData.fileUrl + arr.uri });
  78. } else {
  79. wx.showToast({ title: `${arr.errmsg}`, icon: 'fail', duration: 2000 })
  80. }
  81. },
  82. })
  83. },
  84. //设置斜体
  85. _addItalic: function () {
  86. this.editorCtx.format("italic")
  87. },
  88. //添加粗体样式
  89. _addBold: function () {
  90. this.editorCtx.format("bold")
  91. },
  92. //设置标题
  93. _addHeader: function (e) {
  94. let headerType = e.currentTarget.dataset.header;
  95. this.editorCtx.format("header", headerType)
  96. },
  97. //设置文字的排列方式
  98. _addAlign: function (e) {
  99. let alignType = e.currentTarget.dataset.align;
  100. this.editorCtx.format("align", alignType);
  101. },
  102. //设置列表
  103. _addList: function (e) {
  104. let listType = e.currentTarget.dataset.list;
  105. this.editorCtx.format("list", listType);
  106. },
  107. //撤销
  108. _undo: function () {
  109. this.editorCtx.undo();
  110. },
  111. //监控输入
  112. _onInputting: function (e) {
  113. let html = e.detail.html;
  114. let text = e.detail.text;
  115. this.triggerEvent("input", { html: html, text: text }, {});
  116. }
  117. }
  118. })