Reader.vue 13 KB

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