edit.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. Tencent is pleased to support the open source community by making Face-2-Face Translator available.
  3. Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved.
  4. Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
  5. http://opensource.org/licenses/MIT
  6. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  7. */
  8. const initBottomHeight = 0
  9. var app = getApp()
  10. Page({
  11. /**
  12. * 页面的初始数据
  13. */
  14. data: {
  15. edit_text_max: 200,
  16. remain_length: 200,
  17. edit_text: "",
  18. is_focus: false,
  19. tips: "",
  20. index: -1,
  21. bottomHeight: initBottomHeight
  22. },
  23. /**
  24. * 获得最大文本长度
  25. */
  26. getEditTextMax: function () {
  27. return this.data.edit_text_max
  28. },
  29. /**
  30. * 更新剩余长度
  31. */
  32. updateRemainLength: function (now_content) {
  33. this.data.remain_length = this.getEditTextMax() - now_content.length
  34. this.data.tips = "还可以输入" + this.data.remain_length + "字..."
  35. this.setData({ tips: this.data.tips })
  36. },
  37. /**
  38. * 设置初始内容
  39. */
  40. setEditText: function (text) {
  41. this.data.edit_text = text
  42. this.setData({ edit_text: this.data.edit_text })
  43. //更新剩余长度显示
  44. this.updateRemainLength(text)
  45. this.setData({ is_focus: true })
  46. },
  47. /**
  48. * bindinput
  49. */
  50. editInput: function (event) {
  51. console.log(event)
  52. if (event.detail.value.length > this.getEditTextMax()) {
  53. } else {
  54. this.data.edit_text = event.detail.value
  55. this.updateRemainLength(this.data.edit_text)
  56. }
  57. },
  58. /**
  59. * bindconfirm
  60. */
  61. editConfirm: function (event) {
  62. if (this.data.edit_text.length > 0 && this.data.edit_text != this.data.oldText) {
  63. // 得到页面栈
  64. let pages = getCurrentPages();
  65. let prevPage = pages[pages.length - 2]; //上一个页面
  66. let dialogList = prevPage.data.dialogList.slice(0)
  67. let editItem = dialogList[dialogList.length - 1]
  68. editItem.text = this.data.edit_text
  69. prevPage.setData({
  70. dialogList: dialogList,
  71. recordStatus: 2,
  72. })
  73. wx.navigateBack()
  74. } else {
  75. //文本输入为空时提示
  76. }
  77. },
  78. /**
  79. * 点击文本输入框修改底下按钮行高,让提示和按钮始终在键盘上方 *
  80. */
  81. editFocus: function(e) {
  82. let {value, height} = e.detail
  83. console.log(value, height)
  84. if(!isNaN(height)) {
  85. this.setData({
  86. bottomHeight: height + initBottomHeight
  87. })
  88. }
  89. },
  90. /**
  91. * 输入框失焦
  92. */
  93. editBlur: function() {
  94. this.setData({
  95. bottomHeight: initBottomHeight
  96. })
  97. },
  98. /**
  99. * 清空内容
  100. */
  101. deleteContent: function () {
  102. this.setEditText("")
  103. this.setData({
  104. is_focus: true
  105. })
  106. },
  107. /**
  108. * 生命周期函数--监听页面加载
  109. */
  110. onLoad: function (options) {
  111. this.setEditText(options.content)
  112. let index = parseInt(options.index)
  113. this.setData({
  114. index: index,
  115. oldText:options.content,
  116. })
  117. },
  118. })