hg-editor.js 2.6 KB

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