Reader.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <template>
  2. <el-container>
  3. <el-header v-show="toolBarActive" height='50px'>
  4. <div class="header">
  5. <el-tooltip content="Загрузить книгу" :open-delay="1000" effect="light">
  6. <el-button ref="loader" 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 ref="undoAction" 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 ref="redoAction" 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 ref="fullScreen" 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 ref="setPosition" 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 ref="scrolling" 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 ref="search" 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 ref="copyText" class="tool-button" @click="buttonClick('copyText')"><i class="el-icon-edit-outline"></i></el-button>
  30. </el-tooltip>
  31. <el-tooltip content="Принудительно обновить книгу в обход кеша" :open-delay="1000" effect="light">
  32. <el-button ref="refresh" class="tool-button" @click="buttonClick('refresh')"><i class="el-icon-refresh"></i></el-button>
  33. </el-tooltip>
  34. <div class="space"></div>
  35. <el-tooltip content="История" :open-delay="1000" effect="light">
  36. <el-button ref="history" class="tool-button" :class="buttonActiveClass('history')" @click="buttonClick('history')"><i class="el-icon-document"></i></el-button>
  37. </el-tooltip>
  38. </div>
  39. <el-tooltip content="Настроить" :open-delay="1000" effect="light">
  40. <el-button ref="settings" class="tool-button" @click="buttonClick('settings')"><i class="el-icon-setting"></i></el-button>
  41. </el-tooltip>
  42. </div>
  43. </el-header>
  44. <el-main>
  45. <keep-alive>
  46. <component ref="page" :is="pageActive"
  47. @load-book="loadBook"
  48. @book-pos-changed="bookPosChanged"
  49. @tool-bar-toggle="toolBarToggle"
  50. @history-toggle="historyToggle"
  51. ></component>
  52. </keep-alive>
  53. </el-main>
  54. </el-container>
  55. </template>
  56. <script>
  57. //-----------------------------------------------------------------------------
  58. import Vue from 'vue';
  59. import Component from 'vue-class-component';
  60. import LoaderPage from './LoaderPage/LoaderPage.vue';
  61. import HistoryPage from './HistoryPage/HistoryPage.vue';
  62. import TextPage from './TextPage/TextPage.vue';
  63. import ProgressPage from './ProgressPage/ProgressPage.vue';
  64. import bookManager from './share/bookManager';
  65. import readerApi from '../../api/reader';
  66. export default @Component({
  67. components: {
  68. LoaderPage,
  69. HistoryPage,
  70. TextPage,
  71. ProgressPage
  72. },
  73. watch: {
  74. bookPos: function(newValue) {
  75. if (newValue !== undefined && this.pageActive == 'TextPage') {
  76. const textPage = this.$refs.page;
  77. if (textPage.bookPos != newValue) {
  78. textPage.bookPos = newValue;
  79. }
  80. if (this.lastOpenedBook && this.lastOpenedBook.bookPos != newValue) {
  81. this.commit('reader/setOpenedBook', Object.assign({}, this.lastOpenedBook, {bookPos: newValue, bookPosSeen: this.bookPosSeen}));
  82. }
  83. }
  84. },
  85. routeParamPos: function(newValue) {
  86. if (newValue !== undefined && newValue != this.bookPos) {
  87. this.bookPos = newValue;
  88. }
  89. },
  90. routeParamUrl: function(newValue) {
  91. if (newValue !== '' && newValue !== this.lastOpenedBook.url) {
  92. this.loadBook({url: newValue, bookPos: this.routeParamPos});
  93. }
  94. },
  95. },
  96. })
  97. class Reader extends Vue {
  98. loaderActive = false;
  99. historyActive = false;
  100. progressActive = false;
  101. fullScreenActive = false;
  102. bookPos = null;
  103. allowUrlParamBookPos = true;
  104. created() {
  105. this.commit = this.$store.commit;
  106. this.dispatch = this.$store.dispatch;
  107. this.reader = this.$store.state.reader;
  108. this.$root.addKeyHook(this.keyHook);
  109. this.lastActivePage = false;
  110. }
  111. mounted() {
  112. /*while (this.lastOpenedBook) {
  113. this.commit('reader/delOpenedBook', this.lastOpenedBook);
  114. }*/
  115. if (this.$root.rootRoute == '/reader') {
  116. if (this.routeParamUrl) {
  117. this.loadBook({url: this.routeParamUrl, bookPos: this.routeParamPos});
  118. } else if (this.lastOpenedBook) {
  119. this.loadBook({url: this.lastOpenedBook.url});
  120. } else {
  121. this.loaderActive = true;
  122. }
  123. }
  124. }
  125. get routeParamPos() {
  126. let result = undefined;
  127. const q = this.$route.query;
  128. if (q['__p']) {
  129. result = q['__p'];
  130. if (Array.isArray(result))
  131. result = result[0];
  132. }
  133. return (result ? parseInt(result, 10) || 0 : result);
  134. }
  135. updateRoute(isNewRoute) {
  136. const pos = (this.bookPos != undefined && this.allowUrlParamBookPos ? `__p=${this.bookPos}&` : '');
  137. if (isNewRoute)
  138. this.$router.push(`/reader?${pos}url=${this.lastOpenedBook.url}`);
  139. else
  140. this.$router.replace(`/reader?${pos}url=${this.lastOpenedBook.url}`);
  141. }
  142. get routeParamUrl() {
  143. let result = '';
  144. const path = this.$route.fullPath;
  145. const i = path.indexOf('url=');
  146. if (i >= 0) {
  147. result = path.substr(i + 4);
  148. }
  149. return decodeURIComponent(result);
  150. }
  151. bookPosChanged(event) {
  152. this.bookPosSeen = event.bookPosSeen;
  153. this.bookPos = event.bookPos;
  154. this.updateRoute();
  155. }
  156. get toolBarActive() {
  157. return this.reader.toolBarActive;
  158. }
  159. get lastOpenedBook() {
  160. return this.$store.getters['reader/lastOpenedBook'];
  161. }
  162. toolBarToggle() {
  163. this.commit('reader/setToolBarActive', !this.toolBarActive);
  164. this.$root.$emit('resize');
  165. }
  166. fullScreenToggle() {
  167. this.fullScreenActive = !this.fullScreenActive;
  168. if (this.fullScreenActive) {
  169. const element = document.documentElement;
  170. if (element.requestFullscreen) {
  171. element.requestFullscreen();
  172. } else if (element.webkitrequestFullscreen) {
  173. element.webkitRequestFullscreen();
  174. } else if (element.mozRequestFullscreen) {
  175. element.mozRequestFullScreen();
  176. }
  177. } else {
  178. if (document.cancelFullScreen) {
  179. document.cancelFullScreen();
  180. } else if (document.mozCancelFullScreen) {
  181. document.mozCancelFullScreen();
  182. } else if (document.webkitCancelFullScreen) {
  183. document.webkitCancelFullScreen();
  184. }
  185. }
  186. }
  187. loaderToggle() {
  188. this.loaderActive = !this.loaderActive;
  189. }
  190. historyToggle() {
  191. this.historyActive = !this.historyActive;
  192. }
  193. buttonClick(button) {
  194. switch (button) {
  195. case 'loader':
  196. this.loaderToggle();
  197. break;
  198. case 'history':
  199. this.historyToggle();
  200. break;
  201. case 'fullScreen':
  202. this.fullScreenToggle();
  203. break;
  204. case 'refresh':
  205. if (this.lastOpenedBook) {
  206. this.loadBook({url: this.lastOpenedBook.url, force: true});
  207. }
  208. break;
  209. }
  210. this.$refs[button].$el.blur();
  211. }
  212. buttonActiveClass(button) {
  213. const classActive = { 'tool-button-active': true, 'tool-button-active:hover': true };
  214. switch (button) {
  215. case 'loader': return (this.loaderActive ? classActive : {});
  216. case 'history': return (this.historyActive ? classActive : {});
  217. case 'fullScreen': return (this.fullScreenActive ? classActive : {});
  218. }
  219. return {};
  220. }
  221. get pageActive() {
  222. let result = '';
  223. if (this.progressActive)
  224. result = 'ProgressPage';
  225. else if (this.loaderActive)
  226. result = 'LoaderPage';
  227. else if (this.historyActive)
  228. result = 'HistoryPage';
  229. else if (this.lastOpenedBook)
  230. result = 'TextPage';
  231. if (!result) {
  232. this.loaderActive = true;
  233. result = 'LoaderPage';
  234. }
  235. if (result != 'TextPage') {
  236. this.$root.$emit('set-app-title');
  237. }
  238. if (this.lastActivePage != result && result == 'TextPage') {
  239. //акивируем страницу с текстом
  240. this.$nextTick(async() => {
  241. const last = this.lastOpenedBook;
  242. const isParsed = await bookManager.hasBookParsed(last);
  243. if (!isParsed) {
  244. this.$root.$emit('set-app-title');
  245. return;
  246. }
  247. this.updateRoute();
  248. const textPage = this.$refs.page;
  249. if (textPage.showBook) {
  250. textPage.lastBook = last;
  251. textPage.bookPos = (last.bookPos !== undefined ? last.bookPos : 0);
  252. textPage.showBook();
  253. }
  254. });
  255. }
  256. this.$nextTick(() => {
  257. if (this.$refs.page)
  258. this.$refs.page.fullScreenToggle = this.fullScreenToggle;
  259. });
  260. this.lastActivePage = result;
  261. return result;
  262. }
  263. loadBook(opts) {
  264. this.progressActive = true;
  265. this.$nextTick(async() => {
  266. const progress = this.$refs.page;
  267. try {
  268. progress.show();
  269. progress.setState({state: 'parse'});
  270. // есть ли среди истории OpenedBook
  271. const key = bookManager.keyFromUrl(opts.url);
  272. let wasOpened = this.reader.openedBook[key];
  273. wasOpened = (wasOpened ? wasOpened : {});
  274. const bookPos = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPos);
  275. const bookPosSeen = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPosSeen);
  276. let book = null;
  277. if (!opts.force) {
  278. // пытаемся загрузить и распарсить книгу в менеджере из локального кеша
  279. const bookParsed = await bookManager.getBook({url: opts.url}, (prog) => {
  280. progress.setState({progress: prog});
  281. });
  282. // если есть в локальном кеше
  283. if (bookParsed) {
  284. this.commit('reader/setOpenedBook', Object.assign({bookPos, bookPosSeen}, bookManager.metaOnly(bookParsed)));
  285. this.loaderActive = false;
  286. progress.hide(); this.progressActive = false;
  287. return;
  288. }
  289. // иначе идем на сервер
  290. // пытаемся загрузить готовый файл с сервера
  291. if (wasOpened.path) {
  292. try {
  293. const resp = await readerApi.loadCachedBook(wasOpened.path, (state) => {
  294. progress.setState(state);
  295. });
  296. book = Object.assign({}, wasOpened, {data: resp.data});
  297. } catch (e) {
  298. //молчим
  299. }
  300. }
  301. }
  302. progress.setState({totalSteps: 5});
  303. // не удалось, скачиваем книгу полностью с конвертацией
  304. if (!book) {
  305. book = await readerApi.loadBook(opts.url, (state) => {
  306. progress.setState(state);
  307. });
  308. }
  309. // добавляем в bookManager
  310. progress.setState({state: 'parse', step: 5});
  311. const addedBook = await bookManager.addBook(book, (prog) => {
  312. progress.setState({progress: prog});
  313. });
  314. // добавляем в историю
  315. this.commit('reader/setOpenedBook', Object.assign({bookPos, bookPosSeen}, bookManager.metaOnly(addedBook)));
  316. this.updateRoute(true);
  317. this.loaderActive = false;
  318. progress.hide(); this.progressActive = false;
  319. } catch (e) {
  320. progress.hide(); this.progressActive = false;
  321. this.loaderActive = true;
  322. this.$alert(e.message, 'Ошибка', {type: 'error'});
  323. }
  324. });
  325. }
  326. keyHook(event) {
  327. if (this.$root.rootRoute == '/reader') {
  328. let handled = false;
  329. if (this.$refs.page && this.$refs.page.keyHook)
  330. handled = this.$refs.page.keyHook(event);
  331. if (!handled && event.type == 'keydown' && event.code == 'Escape') {
  332. this.loaderToggle();
  333. }
  334. }
  335. }
  336. }
  337. //-----------------------------------------------------------------------------
  338. </script>
  339. <style scoped>
  340. .el-container {
  341. padding: 0;
  342. margin: 0;
  343. height: 100%;
  344. }
  345. .el-header {
  346. padding-left: 5px;
  347. padding-right: 5px;
  348. background-color: #1B695F;
  349. color: #000;
  350. overflow-x: auto;
  351. overflow-y: hidden;
  352. }
  353. .header {
  354. display: flex;
  355. justify-content: space-between;
  356. min-width: 550px;
  357. }
  358. .el-main {
  359. display: flex;
  360. padding: 0;
  361. margin: 0;
  362. background-color: #EBE2C9;
  363. color: #000;
  364. }
  365. .tool-button {
  366. margin: 0;
  367. margin-left: 2px;
  368. margin-right: 2px;
  369. padding: 0;
  370. color: #3E843E;
  371. background-color: #E6EDF4;
  372. margin-top: 5px;
  373. height: 38px;
  374. width: 38px;
  375. border: 0;
  376. box-shadow: 3px 3px 5px black;
  377. }
  378. .tool-button:hover {
  379. background-color: white;
  380. }
  381. .tool-button-active {
  382. box-shadow: 0 0 0;
  383. color: white;
  384. background-color: #8AB45F;
  385. position: relative;
  386. top: 1px;
  387. left: 1px;
  388. }
  389. .tool-button-active:hover {
  390. color: white;
  391. background-color: #81C581;
  392. }
  393. i {
  394. font-size: 200%;
  395. }
  396. .space {
  397. width: 10px;
  398. display: inline-block;
  399. }
  400. </style>