editor.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. Page({
  2. onShareAppMessage() {
  3. return {
  4. title: 'editor',
  5. path: 'packageComponent/pages/form/editor/editor'
  6. }
  7. },
  8. data: {
  9. formats: {},
  10. readOnly: false,
  11. placeholder: '开始输入...',
  12. editorHeight: 300,
  13. keyboardHeight: 0,
  14. isIOS: false,
  15. safeHeight: 0,
  16. toolBarHeight: 50,
  17. },
  18. readOnlyChange() {
  19. this.setData({
  20. readOnly: !this.data.readOnly
  21. })
  22. },
  23. onLoad() {
  24. this.setData({
  25. theme: wx.getSystemInfoSync().theme || 'light'
  26. })
  27. if (wx.onThemeChange) {
  28. wx.onThemeChange(({theme}) => {
  29. this.setData({theme})
  30. })
  31. }
  32. const {
  33. platform, safeArea, screenHeight
  34. } = wx.getSystemInfoSync()
  35. let safeHeight
  36. if (safeArea) {
  37. safeHeight = (screenHeight - safeArea.bottom)
  38. } else {
  39. safeHeight = 32
  40. }
  41. this._safeHeight = safeHeight
  42. const isIOS = platform === 'ios'
  43. this.setData({isIOS, safeHeight, toolBarHeight: isIOS ? safeHeight + 50 : 50})
  44. const that = this
  45. this.updatePosition(0)
  46. let keyboardHeight = 0
  47. wx.onKeyboardHeightChange(res => {
  48. if (res.height === keyboardHeight) {
  49. return
  50. }
  51. const duration = res.height > 0 ? res.duration * 1000 : 0
  52. keyboardHeight = res.height
  53. setTimeout(() => {
  54. wx.pageScrollTo({
  55. scrollTop: 0,
  56. success() {
  57. that.updatePosition(keyboardHeight)
  58. that.editorCtx.scrollIntoView()
  59. }
  60. })
  61. }, duration)
  62. })
  63. },
  64. updatePosition(keyboardHeight) {
  65. const toolbarHeight = 50
  66. const {windowHeight} = wx.getSystemInfoSync()
  67. let editorHeight = windowHeight
  68. if (keyboardHeight > 0) {
  69. editorHeight = windowHeight - keyboardHeight - toolbarHeight
  70. }
  71. if (keyboardHeight === 0) {
  72. this.setData({
  73. editorHeight,
  74. keyboardHeight,
  75. toolBarHeight: this.data.isIOS ? 50 + this._safeHeight : 50,
  76. safeHeight: this._safeHeight,
  77. })
  78. } else {
  79. this.setData({
  80. editorHeight,
  81. keyboardHeight,
  82. toolBarHeight: 50,
  83. safeHeight: 0,
  84. })
  85. }
  86. },
  87. calNavigationBarAndStatusBar() {
  88. const systemInfo = wx.getSystemInfoSync()
  89. const {statusBarHeight, platform} = systemInfo
  90. const isIOS = platform === 'ios'
  91. const navigationBarHeight = isIOS ? 44 : 48
  92. return statusBarHeight + navigationBarHeight
  93. },
  94. onEditorReady() {
  95. const that = this
  96. wx.createSelectorQuery().select('#editor').context(function (res) {
  97. that.editorCtx = res.context
  98. }).exec()
  99. },
  100. blur() {
  101. this.editorCtx.blur()
  102. },
  103. format(e) {
  104. const {name, value} = e.target.dataset
  105. if (!name) return
  106. // console.log('format', name, value)
  107. this.editorCtx.format(name, value)
  108. },
  109. onStatusChange(e) {
  110. const formats = e.detail
  111. this.setData({formats})
  112. },
  113. insertDivider() {
  114. this.editorCtx.insertDivider({
  115. success() {
  116. console.log('insert divider success')
  117. }
  118. })
  119. },
  120. clear() {
  121. this.editorCtx.clear({
  122. success() {
  123. console.log('clear success')
  124. }
  125. })
  126. },
  127. removeFormat() {
  128. this.editorCtx.removeFormat()
  129. },
  130. insertDate() {
  131. const date = new Date()
  132. const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
  133. this.editorCtx.insertText({
  134. text: formatDate
  135. })
  136. },
  137. insertImage() {
  138. const that = this
  139. wx.chooseImage({
  140. count: 1,
  141. success(res) {
  142. that.editorCtx.insertImage({
  143. src: res.tempFilePaths[0],
  144. data: {
  145. id: 'abcd',
  146. role: 'god'
  147. },
  148. width: '80%',
  149. success() {
  150. console.log('insert image success')
  151. }
  152. })
  153. }
  154. })
  155. }
  156. })