Reader.vue 13 KB

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