微信小程序
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.

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