Reader.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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" @book-pos-changed="bookPosChanged"></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. watch: {
  64. bookPos: function(newValue) {
  65. if (newValue !== undefined && this.pageActive == 'TextPage') {
  66. const textPage = this.$refs.page;
  67. if (textPage.bookPos != newValue) {
  68. textPage.bookPos = newValue;
  69. this.commit('reader/setOpenedBook', Object.assign({}, this.lastOpenedBook, {bookPos: newValue}));
  70. }
  71. }
  72. },
  73. routeParamPos: function(newValue) {
  74. if (newValue !== undefined && newValue != this.bookPos) {
  75. this.bookPos = newValue;
  76. }
  77. },
  78. routeParamUrl: function(newValue) {
  79. if (newValue !== '' && newValue !== this.lastOpenedBook.url) {
  80. this.loadBook({url: newValue});
  81. }
  82. }
  83. },
  84. })
  85. class Reader extends Vue {
  86. loaderActive = false;
  87. progressActive = false;
  88. bookPos = null;
  89. created() {
  90. this.commit = this.$store.commit;
  91. this.dispatch = this.$store.dispatch;
  92. this.reader = this.$store.state.reader;
  93. this.$root.addKeyHook(this.keyHook);
  94. this.lastActivePage = false;
  95. }
  96. mounted() {
  97. /*while (this.lastOpenedBook) {
  98. this.commit('reader/delOpenedBook', this.lastOpenedBook);
  99. }*/
  100. const lastUrl = (this.lastOpenedBook ? this.lastOpenedBook.url : '');
  101. if (this.$root.rootRoute == '/reader' && this.routeParamUrl && this.routeParamUrl != lastUrl) {
  102. this.loaderActive = true;
  103. this.loadBook({url: this.routeParamUrl, bookPos: this.routeParamPos});
  104. }
  105. }
  106. get routeParamPos() {
  107. let result = undefined;
  108. const q = this.$route.query;
  109. if (q['__p']) {
  110. result = q['__p'];
  111. if (Array.isArray(result))
  112. result = result[0];
  113. }
  114. return (result ? parseInt(result, 10) || 0 : result);
  115. }
  116. updateRoute() {
  117. const pos = (this.bookPos != undefined ? `__p=${this.bookPos}&` : '');
  118. this.$router.replace(`/reader?${pos}url=${this.lastOpenedBook.url}`);
  119. }
  120. get routeParamUrl() {
  121. let result = '';
  122. const path = this.$route.fullPath;
  123. const i = path.indexOf('url=');
  124. if (i >= 0) {
  125. result = path.substr(i + 4);
  126. }
  127. return decodeURIComponent(result);
  128. }
  129. bookPosChanged(event) {
  130. this.bookPos = event.bookPos;
  131. this.updateRoute();
  132. }
  133. get fullScreenActive() {
  134. return this.reader.fullScreenActive;
  135. }
  136. get lastOpenedBook() {
  137. return this.$store.getters['reader/lastOpenedBook'];
  138. }
  139. buttonClick(button) {
  140. switch (button) {
  141. case 'loader': this.loaderActive = !this.loaderActive; break;
  142. case 'fullscreen': this.commit('reader/setFullScreenActive', !this.fullScreenActive); break;
  143. }
  144. }
  145. buttonActiveClass(button) {
  146. const classActive = { 'tool-button-active': true, 'tool-button-active:hover': true };
  147. switch (button) {
  148. case 'loader': return (this.loaderActive ? classActive : {});
  149. case 'fullscreen': return (this.fullScreenActive ? classActive : {});
  150. }
  151. return {};
  152. }
  153. get pageActive() {
  154. let result = '';
  155. if (this.progressActive)
  156. result = 'ProgressPage';
  157. else if (this.loaderActive)
  158. result = 'LoaderPage';
  159. else if (this.lastOpenedBook)
  160. result = 'TextPage';
  161. if (!result) {
  162. this.loaderActive = true;
  163. result = 'LoaderPage';
  164. }
  165. if (result != 'TextPage') {
  166. this.$root.$emit('set-app-title');
  167. }
  168. if (this.lastActivePage != result && result == 'TextPage') {
  169. //акивируем страницу с текстом
  170. this.$nextTick(async() => {
  171. const last = this.lastOpenedBook;
  172. const isParsed = await bookManager.hasBookParsed(last);
  173. if (!isParsed) {
  174. this.$root.$emit('set-app-title');
  175. this.loadBook({url: last.url, bookPos: last.bookPos});
  176. return;
  177. } else {
  178. this.bookPos = last.bookPos;
  179. }
  180. this.updateRoute();
  181. const textPage = this.$refs.page;
  182. textPage.lastBook = last;
  183. textPage.bookPos = (this.bookPos !== undefined ? this.bookPos : 0);
  184. textPage.showBook();
  185. });
  186. }
  187. this.lastActivePage = result;
  188. return result;
  189. }
  190. loadBook(opts) {
  191. this.progressActive = true;
  192. this.$nextTick(async() => {
  193. const progress = this.$refs.page;
  194. try {
  195. progress.show();
  196. progress.setState({state: 'parse'});
  197. const bookParsed = await bookManager.getBook({url: opts.url}, (prog) => {
  198. progress.setState({progress: prog});
  199. });
  200. if (bookParsed) {
  201. let isOpened = this.reader.openedBook[bookParsed.key];
  202. isOpened = (isOpened ? isOpened : {});
  203. const bookPos = (opts.bookPos !== undefined ? opts.bookPos : isOpened.bookPos);
  204. this.commit('reader/setOpenedBook', Object.assign({bookPos}, bookManager.metaOnly(bookParsed)));
  205. this.loaderActive = false;
  206. progress.hide(); this.progressActive = false;
  207. return;
  208. }
  209. progress.setState({totalSteps: 5});
  210. const book = await readerApi.loadBook(opts.url, (state) => {
  211. progress.setState(state);
  212. });
  213. progress.setState({state: 'parse', step: 5});
  214. const addedBook = await bookManager.addBook(book, (prog) => {
  215. progress.setState({progress: prog});
  216. });
  217. let isOpened = this.reader.openedBook[addedBook.key];
  218. isOpened = (isOpened ? isOpened : {});
  219. const bookPos = (opts.bookPos !== undefined ? opts.bookPos : isOpened.bookPos);
  220. this.commit('reader/setOpenedBook', Object.assign({bookPos}, bookManager.metaOnly(addedBook)));
  221. this.loaderActive = false;
  222. progress.hide(); this.progressActive = false;
  223. } catch (e) {
  224. progress.hide(); this.progressActive = false;
  225. this.loaderActive = true;
  226. this.$alert(e.message, 'Ошибка', {type: 'error'});
  227. }
  228. });
  229. }
  230. keyHook(event) {
  231. if (this.$root.rootRoute == '/reader') {
  232. if (this.$refs.page && this.$refs.page.keyHook)
  233. this.$refs.page.keyHook(event);
  234. }
  235. }
  236. }
  237. //-----------------------------------------------------------------------------
  238. </script>
  239. <style scoped>
  240. .el-container {
  241. padding: 0;
  242. margin: 0;
  243. height: 100%;
  244. }
  245. .el-header {
  246. padding-left: 5px;
  247. padding-right: 5px;
  248. background-color: #1B695F;
  249. color: #000;
  250. overflow-x: auto;
  251. overflow-y: hidden;
  252. }
  253. .header {
  254. display: flex;
  255. justify-content: space-between;
  256. min-width: 500px;
  257. }
  258. .el-main {
  259. display: flex;
  260. padding: 0;
  261. margin: 0;
  262. background-color: #EBE2C9;
  263. color: #000;
  264. }
  265. .tool-button {
  266. margin: 0;
  267. margin-left: 2px;
  268. margin-right: 2px;
  269. padding: 0;
  270. color: #3E843E;
  271. background-color: #E6EDF4;
  272. margin-top: 5px;
  273. height: 38px;
  274. width: 38px;
  275. border: 0;
  276. box-shadow: 3px 3px 5px black;
  277. }
  278. .tool-button:hover {
  279. background-color: white;
  280. }
  281. .tool-button-active {
  282. box-shadow: 0 0 0;
  283. color: white;
  284. background-color: #8AB45F;
  285. position: relative;
  286. top: 1px;
  287. left: 1px;
  288. }
  289. .tool-button-active:hover {
  290. color: white;
  291. background-color: #81C581;
  292. }
  293. i {
  294. font-size: 200%;
  295. }
  296. .space {
  297. width: 10px;
  298. display: inline-block;
  299. }
  300. </style>