微信小程序
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

183 行
4.3 KiB

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