hg-editor.js 2.6 KB

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