FileEditScreen.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import {FsUtils} from "../lib/FsUtils";
  2. import {t, extendLocale} from "../lib/i18n";
  3. extendLocale({
  4. "file_view_as_image": {
  5. "en-US": "View as image",
  6. "zh-CN": "以图片形式查看",
  7. "zh-TW": "以圖片形式檢視",
  8. "ru-RU": "Прсм. изображение",
  9. "de-DE": "Zeige als Bild"
  10. },
  11. "file_view_as_text": {
  12. "en-US": "View as text",
  13. "zh-CN": "以文本形式查看",
  14. "zh-TW": "以文字形式檢視",
  15. "ru-RU": "Просм. текст",
  16. "de-DE": "Zeige als Text"
  17. },
  18. "file_view_as_bin": {
  19. "en-US": "View as binary",
  20. "zh-CN": "以文字形式檢視",
  21. "zh-TW": "以二進制形式查看",
  22. "ru-RU": "Просм. бинарно",
  23. "de-DE": "Zeige binär"
  24. },
  25. "file_delete": {
  26. "en-US": "Delete",
  27. "zh-CN": "删除",
  28. "zh-TW": "刪除",
  29. "ru-RU": "Удалить",
  30. "de-DE": "Löschen"
  31. },
  32. })
  33. class FileEditScreen {
  34. STYLE_BUTTON = {
  35. normal_color: 0x111111,
  36. press_color: 0x222222,
  37. x: 12,
  38. w: 168,
  39. h: 56
  40. }
  41. constructor(data) {
  42. this.path = data;
  43. }
  44. start() {
  45. // Stats
  46. let posY = 72;
  47. let text = this.path + "\n", fileSize = 0;
  48. try {
  49. const [st, e] = FsUtils.stat(this.path);
  50. if(st.size) {
  51. text += "Size: " + FsUtils.printBytes(st.size) + "\n";
  52. fileSize = st.size;
  53. }
  54. } catch(e) {
  55. console.warn(e);
  56. }
  57. let textLayout = hmUI.getTextLayout(text, {text_size: 18, text_width: 168})
  58. let textHeight = textLayout.height;
  59. hmUI.createWidget(hmUI.widget.TEXT, {
  60. x: 12,
  61. y: posY,
  62. w: 168,
  63. h: textHeight,
  64. text_size: 18,
  65. color: 0xffffff,
  66. text_style: hmUI.text_style.WRAP,
  67. text
  68. });
  69. posY += textHeight + 12;
  70. // Open btns
  71. if(fileSize > 0) {
  72. if(this.path.endsWith(".png")) {
  73. this.addViewAsImageButton(posY);
  74. posY += 64;
  75. }
  76. this.addViewAsTextButton(posY);
  77. posY += 64;
  78. this.addViewAsBinaryButton(posY);
  79. posY += 64;
  80. }
  81. // Delete btn
  82. if(this.path.startsWith("/storage/")) hmUI.createWidget(hmUI.widget.BUTTON, {
  83. ...this.STYLE_BUTTON,
  84. y: posY,
  85. color: 0xff0000,
  86. text: t("file_delete"),
  87. click_func: () => this.delete()
  88. })
  89. }
  90. addViewAsImageButton(y) {
  91. hmUI.createWidget(hmUI.widget.BUTTON, {
  92. ...this.STYLE_BUTTON,
  93. y,
  94. text: t("file_view_as_image"),
  95. click_func: () => {
  96. hmApp.gotoPage({
  97. url: "page/ImageViewScreen",
  98. param: this.prepareTempFile(this.path)
  99. });
  100. }
  101. });
  102. }
  103. addViewAsTextButton(y) {
  104. hmUI.createWidget(hmUI.widget.BUTTON, {
  105. ...this.STYLE_BUTTON,
  106. y,
  107. text: t("file_view_as_text"),
  108. click_func: () => {
  109. hmApp.gotoPage({
  110. url: "page/TextViewScreen",
  111. param: this.path
  112. });
  113. }
  114. });
  115. }
  116. addViewAsBinaryButton(y) {
  117. hmUI.createWidget(hmUI.widget.BUTTON, {
  118. ...this.STYLE_BUTTON,
  119. y,
  120. text: t("file_view_as_bin"),
  121. click_func: () => {
  122. hmApp.gotoPage({
  123. url: "page/HexdumpScreen",
  124. param: this.path
  125. });
  126. }
  127. })
  128. }
  129. delete() {
  130. FsUtils.rmTree(this.path);
  131. hmApp.goBack();
  132. }
  133. prepareTempFile(sourcePath) {
  134. const current = hmFS.SysProGetChars("mmk_tb_temp");
  135. if(current) {
  136. const path = FsUtils.fullPath(current);
  137. hmFS.remove(path);
  138. }
  139. const data = FsUtils.read(sourcePath);
  140. const newFile = "temp_" + Math.round(Math.random() * 100000) + ".png";
  141. const dest = hmFS.open_asset(newFile, hmFS.O_WRONLY | hmFS.O_CREAT);
  142. hmFS.seek(dest, 0, hmFS.SEEK_SET);
  143. hmFS.write(dest, data, 0, data.byteLength);
  144. hmFS.close(dest);
  145. hmFS.SysProSetChars("mmk_tb_temp", newFile);
  146. return newFile;
  147. }
  148. }
  149. let __$$app$$__ = __$$hmAppManager$$__.currentApp;
  150. let __$$module$$__ = __$$app$$__.current;
  151. __$$module$$__.module = DeviceRuntimeCore.Page({
  152. onInit(p) {
  153. new FileEditScreen(p).start();
  154. }
  155. });