微信小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 3 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. Component({
  2. properties: {
  3. width: {
  4. type: String
  5. },
  6. height: {
  7. type: String
  8. },
  9. insertPicture: {
  10. type: Boolean,
  11. value: true
  12. },
  13. placeholder: {
  14. type: String,
  15. value: '输入文字...'
  16. }
  17. },
  18. data: {
  19. formats: {},
  20. readOnly: false,
  21. // editorHeight: 300,
  22. keyboardHeight: 0,
  23. isIOS: false
  24. },
  25. ready() {
  26. const platform = wx.getSystemInfoSync().platform
  27. const isIOS = platform === 'ios'
  28. this.setData({
  29. isIOS
  30. })
  31. const that = this
  32. this.updatePosition(0)
  33. let keyboardHeight = 0
  34. wx.onKeyboardHeightChange(res => {
  35. if (res.height === keyboardHeight) return
  36. const duration = res.height > 0 ? res.duration * 1000 : 0
  37. keyboardHeight = res.height
  38. setTimeout(() => {
  39. wx.pageScrollTo({
  40. scrollTop: 0,
  41. success() {
  42. that.updatePosition(keyboardHeight)
  43. that.editorCtx.scrollIntoView()
  44. }
  45. })
  46. }, duration)
  47. })
  48. },
  49. methods: {
  50. readOnlyChange() {
  51. this.setData({
  52. readOnly: !this.data.readOnly
  53. })
  54. },
  55. updatePosition(keyboardHeight) {
  56. const toolbarHeight = 100
  57. const {
  58. windowHeight,
  59. platform
  60. } = wx.getSystemInfoSync()
  61. // let editorHeight = keyboardHeight > 0 ? (windowHeight - keyboardHeight - toolbarHeight) : windowHeight
  62. this.setData({
  63. // editorHeight,
  64. keyboardHeight
  65. })
  66. },
  67. calNavigationBarAndStatusBar() {
  68. const systemInfo = wx.getSystemInfoSync()
  69. const {
  70. statusBarHeight,
  71. platform
  72. } = systemInfo
  73. const isIOS = platform === 'ios'
  74. const navigationBarHeight = isIOS ? 44 : 48
  75. return statusBarHeight + navigationBarHeight
  76. },
  77. onEditorReady() {
  78. const that = this
  79. //组件使用createSelectorQuery加上in(this)
  80. wx.createSelectorQuery().in(that).select('#editor').context(function (res) {
  81. that.editorCtx = res.context
  82. }).exec()
  83. },
  84. undo() {
  85. this.editorCtx.undo()
  86. },
  87. redo() {
  88. this.editorCtx.redo()
  89. },
  90. blur() {
  91. this.editorCtx.blur()
  92. },
  93. format(e) {
  94. let {
  95. name,
  96. value
  97. } = e.target.dataset
  98. if (!name) return
  99. // console.log('format', name, value)
  100. if (name === "backgroundColor" && value === "#ff0000") { //设置字体颜色为白色
  101. this.editorCtx.format("color", "#ffffff")
  102. }
  103. if (name === "backgroundColor" && value === "#ffffff") {
  104. this.editorCtx.format("color", "#000000")
  105. }
  106. if (name === "color") { //清除字体样式
  107. this.editorCtx.removeFormat()
  108. }
  109. this.editorCtx.format(name, value)
  110. },
  111. onStatusChange(e) {
  112. const formats = e.detail
  113. this.setData({
  114. formats
  115. })
  116. },
  117. insertDivider() {
  118. this.editorCtx.insertDivider({
  119. success: function () {
  120. console.log('insert divider success')
  121. }
  122. })
  123. },
  124. clear() {
  125. this.editorCtx.clear({
  126. success: function (res) {
  127. console.log("clear success")
  128. }
  129. })
  130. },
  131. removeFormat() {
  132. this.editorCtx.removeFormat()
  133. },
  134. insertDate() {
  135. const date = new Date()
  136. const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
  137. this.editorCtx.insertText({
  138. text: formatDate
  139. })
  140. },
  141. insertImage() {
  142. this.triggerEvent('insertImage'); //触发父组件方法
  143. },
  144. insertSrc(src) { //接受图片返回地址
  145. this.editorCtx.insertImage({
  146. src,
  147. data: {
  148. id: 'abcd',
  149. role: 'god'
  150. },
  151. width: '80%',
  152. fail: (err) => {
  153. console.log(`图片插入失败:${err}`);
  154. }
  155. })
  156. },
  157. getContent(e) { //获得文本内容
  158. this.triggerEvent('Content', {
  159. content: e.detail
  160. })
  161. },
  162. setHtml(html) { //回显
  163. if (html) {
  164. this.createSelectorQuery().select('#editor').context((res) => {
  165. this.editorCtx = res.context
  166. this.editorCtx.setContents({
  167. html,
  168. fail: (err) => {
  169. console.log(`内容回显失败:${err}`);
  170. }
  171. })
  172. }).exec()
  173. }
  174. },
  175. }
  176. })