index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 { name, value } = e.target.dataset
  95. if (!name) return
  96. console.log('format', name, value)
  97. this.editorCtx.format(name, value)
  98. },
  99. onStatusChange(e) {
  100. const formats = e.detail
  101. this.setData({
  102. formats
  103. })
  104. },
  105. insertDivider() {
  106. this.editorCtx.insertDivider({
  107. success: function() {
  108. console.log('insert divider success')
  109. }
  110. })
  111. },
  112. clear() {
  113. this.editorCtx.clear({
  114. success: function(res) {
  115. console.log("clear success")
  116. }
  117. })
  118. },
  119. removeFormat() {
  120. this.editorCtx.removeFormat()
  121. },
  122. insertDate() {
  123. const date = new Date()
  124. const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
  125. this.editorCtx.insertText({
  126. text: formatDate
  127. })
  128. },
  129. insertImage() {
  130. this.triggerEvent('insertImage'); //触发父组件方法
  131. },
  132. insertSrc(src) { //接受图片返回地址
  133. this.editorCtx.insertImage({
  134. src,
  135. data: {
  136. id: 'abcd',
  137. role: 'god'
  138. },
  139. width: '100%',
  140. fail: (err) => {
  141. console.log(`图片插入失败:${err}`);
  142. }
  143. })
  144. },
  145. getContent(e) { //获得文本内容
  146. this.triggerEvent('Content', {
  147. content: e.detail
  148. })
  149. },
  150. setHtml(html) { //回显
  151. if (html) {
  152. this.createSelectorQuery().select('#editor').context((res) => {
  153. this.editorCtx = res.context
  154. this.editorCtx.setContents({
  155. html,
  156. fail: (err) => {
  157. console.log(`内容回显失败:${err}`);
  158. }
  159. })
  160. }).exec()
  161. }
  162. },
  163. }
  164. })