123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- // components/form/form-rich/form-rich.js
- Component({
-
- /**
- * 组件的属性列表
- */
- properties: {
- label: String,
- value: String,
- },
-
- observers: {
- 'value': function (val) {
- let self = this;
- if (val) {
- self.setData({
- descriptionStorage: val
- })
- self.createSelectorQuery().select('#editorContent').context(function (res) {
- self.editorCtx = res.context;
- self.editorCtx.setContents({ html: self.data.descriptionStorage })
- }).exec()
- }
- },
- },
-
- lifetimes: {
- attached() {
- const platform = wx.getSystemInfoSync().platform
- const isIOS = platform === 'ios'
- this.setData({
- isIOS
- })
- const self = this
- this.updatePosition(0)
- let keyboardHeight = 0
- wx.onKeyboardHeightChange(res => {
- if (res.height === keyboardHeight) return
- const duration = res.height > 0 ? res.duration * 1000 : 0
- keyboardHeight = res.height
- setTimeout(() => {
- wx.pageScrollTo({
- scrollTop: 0,
- success() {
- self.updatePosition(keyboardHeight)
- self.editorCtx.scrollIntoView()
- }
- })
- }, duration)
- })
- }
- },
- data: {
- styleCss: 'padding: 38rpx 0; line-height: 36rpx font-size: 28rpx;background-color: #ffffff;',
- keyboardHeight: 0,
- isIOS: false,
- formats: {},
- descriptionStorage: ''
- },
-
- /**
- * 组件的方法列表
- */
- methods: {
- // 个人简介内容
- onEditorContentReady() {
- const self = this;
- self.createSelectorQuery().select('#editorContent').context(function (res) {
- self.editorCtx = res.context;
- self.editorCtx.setContents({ html: self.data.descriptionStorage })
- }).exec()
- },
- onEditorContentInput(e) {
- var self = this;
- self.setData({
- descriptionStorage: e.detail.html.replace(/wx:nodeid="\d+"/g, '')
- })
- },
-
- click() {
- var self = this;
- wx.showModal({
- title: '清空描述内容',
- content: '是否要清空描述内容',
- confirmText: '清空',
- showCancel: true,
- success(res) {
- if (res.confirm) {
- self.setData({
- descriptionStorage: ''
- })
- self.editorCtx.setContents({
- html: self.data.descriptionStorage
- })
- self.triggerEvent('delValue')
- }
- }
- })
- },
-
- send(e) {
- var self = this;
- if (self.data.descriptionStorage) {
- self.triggerEvent('getValue', {
- val: self.data.descriptionStorage
- })
- } else {
- wx.showToast({
- icon: 'none',
- title: '未填写描述',
- })
- self.triggerEvent('getValue', {
- val: ''
- })
- }
- },
-
-
- updatePosition(keyboardHeight) {
- const toolbarHeight = 148;
- const {
- windowHeight,
- platform
- } = wx.getSystemInfoSync()
- let editorHeight = keyboardHeight > 0 ? (windowHeight - keyboardHeight - toolbarHeight) : windowHeight
- this.setData({
- editorHeight,
- keyboardHeight
- })
- },
- calNavigationBarAndStatusBar() {
- const systemInfo = wx.getSystemInfoSync()
- const {
- statusBarHeight,
- platform
- } = systemInfo
- const isIOS = platform === 'ios'
- const navigationBarHeight = isIOS ? 44 : 48
- return statusBarHeight + navigationBarHeight
- },
- onStatusChange(e) {
- const formats = e.detail
- this.setData({
- formats
- })
- },
- format(e) {
- let {
- name,
- value
- } = e.target.dataset
- console.log(name)
- return this.editorCtx.format(name, value)
- },
- }
- })
|