hg-editor.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.data.name;
  45. if (html) that.editorCtx.setContents({ html })
  46. }).exec()
  47. },
  48. //插入图片
  49. _addImage: function (event) {
  50. let _this = this;
  51. wx.chooseImage({
  52. count: 1,
  53. sizeType: ['compressed'],
  54. sourceType: ['album'],
  55. success: function (res) {
  56. _this._uploadImage(res.tempFilePaths[0]);
  57. }
  58. });
  59. },
  60. _uploadImage: function (tempFilePath) {
  61. let _this = this;
  62. wx.uploadFile({
  63. url: `${app.globalData.fileUrl}/files/court/elimg/upload`,
  64. filePath: tempFilePath,
  65. name: 'file',
  66. formData: {},
  67. success: (res) => {
  68. let arr = JSON.parse(res.data);
  69. if (arr.errcode == '0') {
  70. _this.editorCtx.insertImage({ src: app.globalData.fileUrl + arr.uri });
  71. } else {
  72. wx.showToast({ title: `${arr.errmsg}`, icon: 'fail', duration: 2000 })
  73. }
  74. },
  75. })
  76. },
  77. //设置斜体
  78. _addItalic: function () {
  79. this.editorCtx.format("italic")
  80. },
  81. //添加粗体样式
  82. _addBold: function () {
  83. this.editorCtx.format("bold")
  84. },
  85. //设置标题
  86. _addHeader: function (e) {
  87. let headerType = e.currentTarget.dataset.header;
  88. this.editorCtx.format("header", headerType)
  89. },
  90. //设置文字的排列方式
  91. _addAlign: function (e) {
  92. let alignType = e.currentTarget.dataset.align;
  93. this.editorCtx.format("align", alignType);
  94. },
  95. //设置列表
  96. _addList: function (e) {
  97. let listType = e.currentTarget.dataset.list;
  98. this.editorCtx.format("list", listType);
  99. },
  100. //撤销
  101. _undo: function () {
  102. this.editorCtx.undo();
  103. },
  104. //监控输入
  105. _onInputting: function (e) {
  106. let html = e.detail.html;
  107. let text = e.detail.text;
  108. this.triggerEvent("input", { html: html, text: text }, {});
  109. }
  110. }
  111. })