Reader.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <template>
  2. <el-container>
  3. <el-header height='50px'>
  4. <div class="header">
  5. <el-tooltip content="Загрузить книгу" :open-delay="1000" effect="light">
  6. <el-button class="tool-button" :class="buttonActiveClass('loader')" @click="buttonClick('loader')"><i class="el-icon-back"></i></el-button>
  7. </el-tooltip>
  8. <div>
  9. <el-tooltip content="Действие назад" :open-delay="1000" effect="light">
  10. <el-button class="tool-button" @click="buttonClick('undoAction')" ><i class="el-icon-arrow-left"></i></el-button>
  11. </el-tooltip>
  12. <el-tooltip content="Действие вперед" :open-delay="1000" effect="light">
  13. <el-button class="tool-button" @click="buttonClick('redoAction')" ><i class="el-icon-arrow-right"></i></el-button>
  14. </el-tooltip>
  15. <div class="space"></div>
  16. <el-tooltip content="На весь экран" :open-delay="1000" effect="light">
  17. <el-button class="tool-button" :class="buttonActiveClass('fullscreen')" @click="buttonClick('fullscreen')"><i class="el-icon-rank"></i></el-button>
  18. </el-tooltip>
  19. <el-tooltip content="Прокрутка книги" :open-delay="1000" effect="light">
  20. <el-button class="tool-button" @click="buttonClick('setPosition')"><i class="el-icon-d-arrow-right"></i></el-button>
  21. </el-tooltip>
  22. <el-tooltip content="Плавный скроллинг" :open-delay="1000" effect="light">
  23. <el-button class="tool-button" @click="buttonClick('scrolling')"><i class="el-icon-sort"></i></el-button>
  24. </el-tooltip>
  25. <el-tooltip content="Найти в тексте" :open-delay="1000" effect="light">
  26. <el-button class="tool-button" @click="buttonClick('search')"><i class="el-icon-search"></i></el-button>
  27. </el-tooltip>
  28. <el-tooltip content="Принудительно обновить книгу в обход кеша" :open-delay="1000" effect="light">
  29. <el-button class="tool-button" @click="buttonClick('refresh')"><i class="el-icon-refresh"></i></el-button>
  30. </el-tooltip>
  31. <div class="space"></div>
  32. <el-tooltip content="История" :open-delay="1000" effect="light">
  33. <el-button class="tool-button" @click="buttonClick('history')"><i class="el-icon-document"></i></el-button>
  34. </el-tooltip>
  35. </div>
  36. <el-tooltip content="Настроить" :open-delay="1000" effect="light">
  37. <el-button class="tool-button" @click="buttonClick('settings')"><i class="el-icon-setting"></i></el-button>
  38. </el-tooltip>
  39. </div>
  40. </el-header>
  41. <el-main>
  42. <keep-alive>
  43. <component ref="page" :is="pageActive" @load-book="loadBook" @parse-book="parseBook"></component>
  44. </keep-alive>
  45. </el-main>
  46. </el-container>
  47. </template>
  48. <script>
  49. //-----------------------------------------------------------------------------
  50. import Vue from 'vue';
  51. import Component from 'vue-class-component';
  52. import LoaderPage from './LoaderPage/LoaderPage.vue';
  53. import TextPage from './TextPage/TextPage.vue';
  54. import ProgressPage from './ProgressPage/ProgressPage.vue';
  55. import bookManager from './share/bookManager';
  56. import readerApi from '../../api/reader';
  57. export default @Component({
  58. components: {
  59. LoaderPage,
  60. TextPage,
  61. ProgressPage
  62. },
  63. })
  64. class Reader extends Vue {
  65. progressActive = false;
  66. created() {
  67. this.commit = this.$store.commit;
  68. this.dispatch = this.$store.dispatch;
  69. this.reader = this.$store.state.reader;
  70. this.$root.addKeyHook(this.keyHook);
  71. }
  72. mounted() {
  73. /*while (this.lastOpenedBook) {
  74. this.commit('reader/delOpenedBook', this.lastOpenedBook);
  75. }*/
  76. if (this.$root.rootRoute == '/reader' && this.routeParamUrl && this.routeParamUrl != this.lastOpenedBook.url) {
  77. this.commit('reader/setLoaderActive', true);
  78. this.loadBook({url: this.routeParamUrl});
  79. }
  80. }
  81. get routeParamUrl() {
  82. let result = '';
  83. const path = this.$route.fullPath;
  84. const i = path.indexOf('url=');
  85. if (i >= 0) {
  86. result = path.substr(i + 4);
  87. }
  88. return decodeURIComponent(result);
  89. }
  90. get loaderActive() {
  91. return this.reader.loaderActive;
  92. }
  93. get fullScreenActive() {
  94. return this.reader.fullScreenActive;
  95. }
  96. get lastOpenedBook() {
  97. return this.$store.getters['reader/lastOpenedBook'];
  98. }
  99. buttonClick(button) {
  100. switch (button) {
  101. case 'loader': this.commit('reader/setLoaderActive', !this.loaderActive); break;
  102. case 'fullscreen': this.commit('reader/setFullScreenActive', !this.fullScreenActive); break;
  103. }
  104. }
  105. buttonActiveClass(button) {
  106. const classActive = { 'tool-button-active': true, 'tool-button-active:hover': true };
  107. switch (button) {
  108. case 'loader': return (this.loaderActive ? classActive : {});
  109. case 'fullscreen': return (this.fullScreenActive ? classActive : {});
  110. }
  111. return {};
  112. }
  113. get pageActive() {
  114. let result = '';
  115. if (this.progressActive)
  116. result = 'ProgressPage';
  117. else if (this.loaderActive)
  118. result = 'LoaderPage';
  119. else if (this.lastOpenedBook)
  120. result = 'TextPage';
  121. if (!result) {
  122. this.commit('reader/setLoaderActive', true);
  123. result = 'LoaderPage';
  124. }
  125. if (result != 'TextPage') {
  126. this.$root.$emit('set-app-title');
  127. }
  128. if (result == 'LoaderPage') {
  129. this.$router.replace('/reader');
  130. }
  131. return result;
  132. }
  133. loadBook(opts) {
  134. this.progressActive = true;
  135. this.$nextTick(async() => {
  136. const progress = this.$refs.page;
  137. progress.show();
  138. progress.setState({totalSteps: 5});
  139. try {
  140. const book = await readerApi.loadBook(opts.url, (state) => {
  141. progress.setState(state);
  142. });
  143. progress.setState({state: 'parse', step: 5});
  144. const addedBook = await bookManager.addBook(book, (prog) => {
  145. progress.setState({progress: prog});
  146. });
  147. this.commit('reader/setOpenedBook', bookManager.metaOnly(addedBook));
  148. this.commit('reader/setLoaderActive', false);
  149. progress.hide(); this.progressActive = false;
  150. } catch (e) {
  151. progress.hide(); this.progressActive = false;
  152. this.commit('reader/setLoaderActive', true);
  153. this.$alert(e.message, 'Ошибка', {type: 'error'});
  154. }
  155. });
  156. }
  157. parseBook(meta) {
  158. this.progressActive = true;
  159. this.$nextTick(async() => {
  160. if (bookManager.hasBookParsed(meta)) {
  161. this.progressActive = false;
  162. return;
  163. }
  164. const progress = this.$refs.page;
  165. progress.show();
  166. progress.setState({state: 'parse'});
  167. try {
  168. const isParsed = await bookManager.getBook(meta, (prog) => {
  169. progress.setState({progress: prog});
  170. });
  171. progress.hide(); this.progressActive = false;
  172. if (!isParsed) {
  173. this.loadBook({url: meta.url});
  174. }
  175. } catch (e) {
  176. progress.hide(); this.progressActive = false;
  177. this.commit('reader/setLoaderActive', true);
  178. this.$alert(e.message, 'Ошибка', {type: 'error'});
  179. }
  180. });
  181. }
  182. keyHook(event) {
  183. if (this.$root.rootRoute == '/reader') {
  184. if (this.$refs.page && this.$refs.page.keyHook)
  185. this.$refs.page.keyHook(event);
  186. }
  187. }
  188. }
  189. //-----------------------------------------------------------------------------
  190. </script>
  191. <style scoped>
  192. .el-container {
  193. padding: 0;
  194. margin: 0;
  195. height: 100%;
  196. }
  197. .el-header {
  198. padding-left: 5px;
  199. padding-right: 5px;
  200. background-color: #1B695F;
  201. color: #000;
  202. overflow-x: auto;
  203. overflow-y: hidden;
  204. }
  205. .header {
  206. display: flex;
  207. justify-content: space-between;
  208. min-width: 500px;
  209. }
  210. .el-main {
  211. display: flex;
  212. padding: 0;
  213. margin: 0;
  214. background-color: #EBE2C9;
  215. color: #000;
  216. }
  217. .tool-button {
  218. margin: 0;
  219. margin-left: 2px;
  220. margin-right: 2px;
  221. padding: 0;
  222. color: #3E843E;
  223. background-color: #E6EDF4;
  224. margin-top: 5px;
  225. height: 38px;
  226. width: 38px;
  227. border: 0;
  228. box-shadow: 3px 3px 5px black;
  229. }
  230. .tool-button:hover {
  231. background-color: white;
  232. }
  233. .tool-button-active {
  234. box-shadow: 0 0 0;
  235. color: white;
  236. background-color: #8AB45F;
  237. position: relative;
  238. top: 1px;
  239. left: 1px;
  240. }
  241. .tool-button-active:hover {
  242. color: white;
  243. background-color: #81C581;
  244. }
  245. i {
  246. font-size: 200%;
  247. }
  248. .space {
  249. width: 10px;
  250. display: inline-block;
  251. }
  252. </style>