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.

form-rich.js 3.7KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // components/form/form-rich/form-rich.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. label: String,
  8. value: String,
  9. },
  10. observers: {
  11. 'value': function (val) {
  12. let self = this;
  13. if (val) {
  14. self.setData({
  15. descriptionStorage: val
  16. })
  17. self.createSelectorQuery().select('#editorContent').context(function (res) {
  18. self.editorCtx = res.context;
  19. self.editorCtx.setContents({ html: self.data.descriptionStorage })
  20. }).exec()
  21. }
  22. },
  23. },
  24. lifetimes: {
  25. attached() {
  26. const platform = wx.getSystemInfoSync().platform
  27. const isIOS = platform === 'ios'
  28. this.setData({
  29. isIOS
  30. })
  31. const self = 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. self.updatePosition(keyboardHeight)
  43. self.editorCtx.scrollIntoView()
  44. }
  45. })
  46. }, duration)
  47. })
  48. }
  49. },
  50. data: {
  51. styleCss: 'padding: 38rpx 0; line-height: 36rpx font-size: 28rpx;background-color: #ffffff;',
  52. keyboardHeight: 0,
  53. isIOS: false,
  54. formats: {},
  55. descriptionStorage: ''
  56. },
  57. /**
  58. * 组件的方法列表
  59. */
  60. methods: {
  61. // 个人简介内容
  62. onEditorContentReady() {
  63. const self = this;
  64. self.createSelectorQuery().select('#editorContent').context(function (res) {
  65. self.editorCtx = res.context;
  66. self.editorCtx.setContents({ html: self.data.descriptionStorage })
  67. }).exec()
  68. },
  69. onEditorContentInput(e) {
  70. var self = this;
  71. self.setData({
  72. descriptionStorage: e.detail.html.replace(/wx:nodeid="\d+"/g, '')
  73. })
  74. },
  75. click() {
  76. var self = this;
  77. wx.showModal({
  78. title: '清空描述内容',
  79. content: '是否要清空描述内容',
  80. confirmText: '清空',
  81. showCancel: true,
  82. success(res) {
  83. if (res.confirm) {
  84. self.setData({
  85. descriptionStorage: ''
  86. })
  87. self.editorCtx.setContents({
  88. html: self.data.descriptionStorage
  89. })
  90. self.triggerEvent('delValue')
  91. }
  92. }
  93. })
  94. },
  95. send(e) {
  96. var self = this;
  97. if (self.data.descriptionStorage) {
  98. self.triggerEvent('getValue', {
  99. val: self.data.descriptionStorage
  100. })
  101. } else {
  102. wx.showToast({
  103. icon: 'none',
  104. title: '未填写描述',
  105. })
  106. self.triggerEvent('getValue', {
  107. val: ''
  108. })
  109. }
  110. },
  111. updatePosition(keyboardHeight) {
  112. const toolbarHeight = 148;
  113. const {
  114. windowHeight,
  115. platform
  116. } = wx.getSystemInfoSync()
  117. let editorHeight = keyboardHeight > 0 ? (windowHeight - keyboardHeight - toolbarHeight) : windowHeight
  118. this.setData({
  119. editorHeight,
  120. keyboardHeight
  121. })
  122. },
  123. calNavigationBarAndStatusBar() {
  124. const systemInfo = wx.getSystemInfoSync()
  125. const {
  126. statusBarHeight,
  127. platform
  128. } = systemInfo
  129. const isIOS = platform === 'ios'
  130. const navigationBarHeight = isIOS ? 44 : 48
  131. return statusBarHeight + navigationBarHeight
  132. },
  133. onStatusChange(e) {
  134. const formats = e.detail
  135. this.setData({
  136. formats
  137. })
  138. },
  139. format(e) {
  140. let {
  141. name,
  142. value
  143. } = e.target.dataset
  144. console.log(name)
  145. return this.editorCtx.format(name, value)
  146. },
  147. }
  148. })