hg-editor.js 3.4 KB

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