Reader.vue 13 KB

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