addItem.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import common from '../../utils/util';
  2. import main from '../../utils/main';
  3. const app = getApp();
  4. var tempCursor = 0,
  5. btnName = "";
  6. var arrSoundMark = [];
  7. var isUnload = true;
  8. var isEdit = true;
  9. Page({
  10. data: {
  11. Field: "",
  12. SoundSign: "[读]",
  13. LineSign: "[线]",
  14. BtnArray: ["+", "-", "×", "÷", "="],
  15. },
  16. onLoad: function (options) {
  17. wx.hideShareMenu();
  18. var id = options.id;
  19. var fieldid = options.fieldid;
  20. tempCursor = 0, btnName = "", arrSoundMark = [];
  21. var that = this;
  22. that.setData({
  23. MiaoguoCardID: id,
  24. Focus: true,
  25. FieldNumber: fieldid,
  26. Containnerheight: main.getWindowHeight(),
  27. });
  28. wx.setNavigationBarTitle({
  29. title: '编辑 段落' + fieldid,
  30. });
  31. },
  32. onShow: function () {
  33. console.log("onShow");
  34. var that = this;
  35. console.log("onShow:" + isEdit);
  36. if (app.globalData.IsAndroid && !isEdit) {
  37. return;
  38. }
  39. var list = wx.getStorageSync("CardList");
  40. var card = {};
  41. for (var i = 0; i < list.length; i++) {
  42. if (that.data.MiaoguoCardID == list[i].MiaoguoCardID) {
  43. var content = main.encryptUrl(list[i].Content[this.data.FieldNumber].Content);
  44. this.setData({
  45. Field: content,
  46. });
  47. if (!wx.getStorageSync("TempCardInfoAddItem")) {
  48. wx.setStorageSync("TempCardInfoAddItem", content);
  49. }
  50. break;
  51. }
  52. }
  53. isUnload = true;
  54. },
  55. onUnload: function () {
  56. if (isUnload) {
  57. var content = wx.getStorageSync("TempCardInfoAddItem");
  58. if (content != undefined) {
  59. this.updateField(content);
  60. wx.removeStorageSync("TempCardInfoAddItem");
  61. }
  62. }
  63. isEdit = true;
  64. },
  65. bindinputField: function (e) {
  66. var that = this;
  67. if (app.globalData.IsAndroid && !isEdit) {
  68. return;
  69. }
  70. this.setData({
  71. Field: e.detail.value,
  72. });
  73. console.log("bindinputField:" + e.detail.value);
  74. //this.updateField();
  75. },
  76. addSymbol: function (e) {
  77. console.log("addSymbol");
  78. isEdit = true;
  79. var that = this;
  80. btnName = e.currentTarget.dataset.id;
  81. if (btnName == "[读]") {
  82. this.setData({
  83. SoundSign: "[/读]",
  84. });
  85. } else if (btnName == "[/读]") {
  86. this.setData({
  87. SoundSign: "[读]",
  88. });
  89. } else if (btnName == "[线]") {
  90. this.setData({
  91. LineSign: "[/线]",
  92. });
  93. } else if (btnName == "[/线]") {
  94. this.setData({
  95. LineSign: "[线]",
  96. });
  97. }
  98. if (!this.data.Focus) {
  99. this.data.Field = this.data.Field + btnName;
  100. this.setData({
  101. Field: this.data.Field,
  102. });
  103. }
  104. //console.log("addSymbol:" + this.data.Field);
  105. setTimeout(function () {
  106. that.setData({
  107. Focus: true,
  108. });
  109. }, 300);
  110. },
  111. onBindFocus: function () {
  112. this.setData({
  113. Focus: true,
  114. });
  115. },
  116. onBindblur: function (e) {
  117. var that = this;
  118. console.log("onBindblur:" + isEdit + " Filed:" + this.data.Field);
  119. if (app.globalData.IsAndroid && !isEdit) {
  120. setTimeout(function () {
  121. isEdit = true;
  122. }, 500);
  123. return;
  124. }
  125. isEdit = false;
  126. console.log("onBindblur:"+isEdit);
  127. this.updateField();
  128. tempCursor = e.detail.cursor;
  129. //console.log("tempCursor:" + tempCursor);
  130. if (tempCursor == undefined)
  131. tempCursor = this.data["Field"].length;
  132. var str1 = "",
  133. str2 = "";
  134. if (this.data.Field.length > 0) {
  135. str1 = this.data.Field.substr(0, tempCursor);
  136. str2 = this.data.Field.substring(tempCursor, this.data.Field.length);
  137. }
  138. //console.log("str1:" + str1);
  139. //console.log("str2:" + str2);
  140. var cur = 1;
  141. if (btnName == "[读]" || btnName == "[线]") {
  142. cur = 3;
  143. } else if (btnName == "[/读]" || btnName == "[/线]") {
  144. cur = 4;
  145. } else if (btnName == "( )") {
  146. cur = 7;
  147. } else if (btnName == "_____") {
  148. cur = 5;
  149. }
  150. this.data.Field = str1 + btnName + str2;
  151. this.setData({
  152. Field: this.data.Field,
  153. Focus: false,
  154. Cursor: tempCursor + cur,
  155. });
  156. btnName = "";
  157. },
  158. onSearch: function (e) {
  159. wx.setStorageSync("TempFieldNumber", this.data.FieldNumber);
  160. wx.navigateTo({
  161. url: './searchWeb',
  162. });
  163. this.updateField();
  164. setTimeout(function () {
  165. isEdit = true;
  166. console.log("onSearchEnd");
  167. }, 1000);
  168. },
  169. clear: function () {
  170. var that = this;
  171. console.log("clear");
  172. this.setData({
  173. Field: "",
  174. Focus: true,
  175. Cursor: 0,
  176. });
  177. btnName = "";
  178. isEdit = false;
  179. this.updateField();
  180. },
  181. closeAddItem: function (e) {
  182. var isSave = e.currentTarget.dataset.idsave;
  183. if (isSave == "true") {
  184. isUnload = false;
  185. wx.removeStorageSync("TempCardInfoAddItem");
  186. this.updateField();
  187. }
  188. wx.navigateBack({
  189. delta: 1,
  190. });
  191. },
  192. uploadImage: function () {
  193. console.log("uploadImageStart");
  194. var that = this;
  195. wx.chooseImage({
  196. count: 1,
  197. sizeType: ['compressed'],
  198. sourceType: ['album', 'camera'],
  199. success(res) {
  200. // tempFilePath可以作为img标签的src属性显示图片
  201. const tempFilePaths = res.tempFilePaths;
  202. //console.log(tempFilePaths[0]);
  203. //that.data.Field = that.data.Field + "[图 url='" + tempFilePaths[0] + "']" + tempFilePaths[0] + "[/图]";
  204. if (tempCursor == undefined)
  205. tempCursor = that.data.Field.length;
  206. var str1 = "",
  207. str2 = "";
  208. if (that.data.Field.length > 0) {
  209. str1 = that.data.Field.substr(0, tempCursor);
  210. str2 = that.data.Field.substring(tempCursor, that.data.Field.length);
  211. }
  212. var str0 = "[图 url='" + tempFilePaths[0] + "'][/图]";
  213. that.data.Field = str1 + str0 + str2;
  214. console.log("uploadImageEnd:" + that.data.Field);
  215. that.setData({
  216. Field: that.data.Field,
  217. Focus: false,
  218. });
  219. that.updateField();
  220. },
  221. fail: function () {
  222. }
  223. });
  224. },
  225. updateField: function (history) {
  226. var str = this.data.Field;
  227. str = main.decryptUrl(str);
  228. str = str.replace(/\[读\]/g, "\n[读]");
  229. str = str.replace(/\[图\]/g, "\n[图]");
  230. str = str.replace(/\[\/读\]/g, "[\/读]\n");
  231. str = str.replace(/\[\/图\]/g, "[\/图]\n");
  232. str = str.replace(/\n\n/g, "\n");
  233. var list = wx.getStorageSync("CardList");
  234. for (var i = 0; i < list.length; i++) {
  235. if (this.data.MiaoguoCardID == list[i].MiaoguoCardID) {
  236. if (history != undefined)
  237. list[i].Content[this.data.FieldNumber].Content = history;
  238. else
  239. list[i].Content[this.data.FieldNumber].Content = str;
  240. break;
  241. }
  242. }
  243. wx.setStorageSync("CardList", list);
  244. },
  245. onShareAppMessage: function () {
  246. return {
  247. title: app.globalData.ShareTitle,
  248. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  249. imageUrl: app.globalData.ShareImage,
  250. }
  251. },
  252. })