Reader.vue 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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" :class="buttonActiveClass('undoAction')" @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" :class="buttonActiveClass('redoAction')" @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" :class="buttonActiveClass('setPosition')" @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" :class="buttonActiveClass('scrolling')" @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" :class="buttonActiveClass('search')" @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" :class="buttonActiveClass('copyText')" @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" :class="buttonActiveClass('refresh')" @click="buttonClick('refresh')">
  33. <i class="el-icon-refresh" :class="{clear: !showRefreshIcon}"></i>
  34. </el-button>
  35. </el-tooltip>
  36. <div class="space"></div>
  37. <el-tooltip content="Открыть недавние" :open-delay="1000" effect="light">
  38. <el-button ref="history" class="tool-button" :class="buttonActiveClass('history')" @click="buttonClick('history')"><i class="el-icon-document"></i></el-button>
  39. </el-tooltip>
  40. </div>
  41. <el-tooltip content="Настроить" :open-delay="1000" effect="light">
  42. <el-button ref="settings" class="tool-button" :class="buttonActiveClass('settings')" @click="buttonClick('settings')"><i class="el-icon-setting"></i></el-button>
  43. </el-tooltip>
  44. </div>
  45. </el-header>
  46. <el-main>
  47. <keep-alive>
  48. <component ref="page" :is="activePage"
  49. @load-book="loadBook"
  50. @book-pos-changed="bookPosChanged"
  51. @tool-bar-toggle="toolBarToggle"
  52. @full-screen-toogle="fullScreenToggle"
  53. @stop-scrolling="stopScrolling"
  54. @scrolling-toggle="scrollingToggle"
  55. ></component>
  56. </keep-alive>
  57. <SetPositionPage v-if="setPositionActive" ref="setPositionPage" @set-position-toggle="setPositionToggle" @book-pos-changed="bookPosChanged"></SetPositionPage>
  58. <HistoryPage v-if="historyActive" ref="historyPage" @load-book="loadBook" @history-toggle="historyToggle"></HistoryPage>
  59. <SettingsPage v-if="settingsActive" ref="settingsPage" @settings-toggle="settingsToggle"></SettingsPage>
  60. </el-main>
  61. </el-container>
  62. </template>
  63. <script>
  64. //-----------------------------------------------------------------------------
  65. import Vue from 'vue';
  66. import Component from 'vue-class-component';
  67. import LoaderPage from './LoaderPage/LoaderPage.vue';
  68. import TextPage from './TextPage/TextPage.vue';
  69. import ProgressPage from './ProgressPage/ProgressPage.vue';
  70. import SetPositionPage from './SetPositionPage/SetPositionPage.vue';
  71. import HistoryPage from './HistoryPage/HistoryPage.vue';
  72. import SettingsPage from './SettingsPage/SettingsPage.vue';
  73. import bookManager from './share/bookManager';
  74. import readerApi from '../../api/reader';
  75. import {sleep} from '../../share/utils';
  76. export default @Component({
  77. components: {
  78. LoaderPage,
  79. TextPage,
  80. ProgressPage,
  81. SetPositionPage,
  82. HistoryPage,
  83. SettingsPage,
  84. },
  85. watch: {
  86. bookPos: function(newValue) {
  87. if (newValue !== undefined && this.activePage == 'TextPage') {
  88. const textPage = this.$refs.page;
  89. if (textPage.bookPos != newValue) {
  90. textPage.bookPos = newValue;
  91. }
  92. if (this.lastOpenedBook && this.lastOpenedBook.bookPos != newValue) {
  93. this.commit('reader/setOpenedBook', Object.assign({}, this.lastOpenedBook, {bookPos: newValue, bookPosSeen: this.bookPosSeen}));
  94. }
  95. }
  96. },
  97. routeParamPos: function(newValue) {
  98. if (newValue !== undefined && newValue != this.bookPos) {
  99. this.bookPos = newValue;
  100. }
  101. },
  102. routeParamUrl: function(newValue) {
  103. if (newValue !== '' && newValue !== this.lastOpenedBook.url) {
  104. this.loadBook({url: newValue, bookPos: this.routeParamPos});
  105. }
  106. },
  107. settings: function(newValue) {
  108. this.allowUrlParamBookPos = newValue.allowUrlParamBookPos;
  109. this.updateRoute();
  110. },
  111. },
  112. })
  113. class Reader extends Vue {
  114. loaderActive = false;
  115. progressActive = false;
  116. fullScreenActive = false;
  117. setPositionActive = false;
  118. scrollingActive = false;
  119. searchActive = false;
  120. copyTextActive = false;
  121. historyActive = false;
  122. settingsActive = false;
  123. bookPos = null;
  124. allowUrlParamBookPos = false;
  125. showRefreshIcon = true;
  126. created() {
  127. this.commit = this.$store.commit;
  128. this.dispatch = this.$store.dispatch;
  129. this.reader = this.$store.state.reader;
  130. this.$root.addKeyHook(this.keyHook);
  131. this.lastActivePage = false;
  132. document.addEventListener('fullscreenchange', () => {
  133. this.fullScreenActive = (document.fullscreenElement !== null);
  134. });
  135. this.allowUrlParamBookPos = this.settings.allowUrlParamBookPos;
  136. }
  137. mounted() {
  138. /*while (this.lastOpenedBook) {
  139. this.commit('reader/delOpenedBook', this.lastOpenedBook);
  140. }*/
  141. if (this.$root.rootRoute == '/reader') {
  142. if (this.routeParamUrl) {
  143. this.loadBook({url: this.routeParamUrl, bookPos: this.routeParamPos});
  144. } else if (this.lastOpenedBook) {
  145. this.loadBook({url: this.lastOpenedBook.url});
  146. } else {
  147. this.loaderActive = true;
  148. }
  149. }
  150. }
  151. get routeParamPos() {
  152. let result = undefined;
  153. const q = this.$route.query;
  154. if (q['__p']) {
  155. result = q['__p'];
  156. if (Array.isArray(result))
  157. result = result[0];
  158. }
  159. return (result ? parseInt(result, 10) || 0 : result);
  160. }
  161. updateRoute(isNewRoute) {
  162. const pos = (this.bookPos != undefined && this.allowUrlParamBookPos ? `__p=${this.bookPos}&` : '');
  163. if (isNewRoute)
  164. this.$router.push(`/reader?${pos}url=${this.lastOpenedBook.url}`);
  165. else
  166. this.$router.replace(`/reader?${pos}url=${this.lastOpenedBook.url}`);
  167. }
  168. get routeParamUrl() {
  169. let result = '';
  170. const path = this.$route.fullPath;
  171. const i = path.indexOf('url=');
  172. if (i >= 0) {
  173. result = path.substr(i + 4);
  174. }
  175. return decodeURIComponent(result);
  176. }
  177. bookPosChanged(event) {
  178. if (event.bookPosSeen !== undefined)
  179. this.bookPosSeen = event.bookPosSeen;
  180. this.bookPos = event.bookPos;
  181. this.updateRoute();
  182. }
  183. get toolBarActive() {
  184. return this.reader.toolBarActive;
  185. }
  186. get lastOpenedBook() {
  187. const result = this.$store.getters['reader/lastOpenedBook'];
  188. if (!result)
  189. this.closeAllTextPages();
  190. return result;
  191. }
  192. get settings() {
  193. return this.$store.state.reader.settings;
  194. }
  195. toolBarToggle() {
  196. this.commit('reader/setToolBarActive', !this.toolBarActive);
  197. this.$root.$emit('resize');
  198. }
  199. fullScreenToggle() {
  200. this.fullScreenActive = !this.fullScreenActive;
  201. if (this.fullScreenActive) {
  202. const element = document.documentElement;
  203. if (element.requestFullscreen) {
  204. element.requestFullscreen();
  205. } else if (element.webkitrequestFullscreen) {
  206. element.webkitRequestFullscreen();
  207. } else if (element.mozRequestFullscreen) {
  208. element.mozRequestFullScreen();
  209. }
  210. } else {
  211. if (document.cancelFullScreen) {
  212. document.cancelFullScreen();
  213. } else if (document.mozCancelFullScreen) {
  214. document.mozCancelFullScreen();
  215. } else if (document.webkitCancelFullScreen) {
  216. document.webkitCancelFullScreen();
  217. }
  218. }
  219. }
  220. closeAllTextPages() {
  221. this.setPositionActive = false;
  222. this.historyActive = false;
  223. this.settingsActive = false;
  224. this.stopScrolling();
  225. }
  226. loaderToggle() {
  227. this.loaderActive = !this.loaderActive;
  228. if (this.loaderActive) {
  229. this.closeAllTextPages();
  230. }
  231. }
  232. setPositionToggle() {
  233. this.setPositionActive = !this.setPositionActive;
  234. if (this.setPositionActive && this.activePage == 'TextPage' && this.lastOpenedBook) {
  235. this.closeAllTextPages();
  236. this.setPositionActive = true;
  237. this.$nextTick(() => {
  238. this.$refs.setPositionPage.sliderMax = this.lastOpenedBook.textLength - 1;
  239. this.$refs.setPositionPage.sliderValue = this.lastOpenedBook.bookPos;
  240. });
  241. } else {
  242. this.setPositionActive = false;
  243. }
  244. }
  245. stopScrolling() {
  246. if (this.scrollingActive)
  247. this.scrollingToggle();
  248. }
  249. scrollingToggle() {
  250. this.scrollingActive = !this.scrollingActive;
  251. if (this.activePage == 'TextPage') {
  252. const page = this.$refs.page;
  253. if (this.scrollingActive) {
  254. page.startTextScrolling();
  255. } else {
  256. page.stopTextScrolling();
  257. }
  258. }
  259. }
  260. historyToggle() {
  261. this.historyActive = !this.historyActive;
  262. if (this.historyActive) {
  263. this.closeAllTextPages();
  264. this.historyActive = true;
  265. } else {
  266. this.historyActive = false;
  267. }
  268. }
  269. settingsToggle() {
  270. this.settingsActive = !this.settingsActive;
  271. if (this.settingsActive) {
  272. this.closeAllTextPages();
  273. this.settingsActive = true;
  274. } else {
  275. this.settingsActive = false;
  276. }
  277. }
  278. buttonClick(button) {
  279. switch (button) {
  280. case 'loader':
  281. this.loaderToggle();
  282. break;
  283. case 'fullScreen':
  284. this.fullScreenToggle();
  285. break;
  286. case 'setPosition':
  287. this.setPositionToggle();
  288. break;
  289. case 'scrolling':
  290. this.scrollingToggle()
  291. break;
  292. case 'history':
  293. this.historyToggle();
  294. break;
  295. case 'refresh':
  296. if (this.lastOpenedBook) {
  297. this.loadBook({url: this.lastOpenedBook.url, force: true});
  298. }
  299. break;
  300. case 'settings':
  301. this.settingsToggle();
  302. break;
  303. }
  304. this.$refs[button].$el.blur();
  305. }
  306. buttonActiveClass(button) {
  307. const classActive = { 'tool-button-active': true, 'tool-button-active:hover': true };
  308. const classDisabled = { 'tool-button-disabled': true, 'tool-button-disabled:hover': true };
  309. let classResult = {};
  310. switch (button) {
  311. case 'loader':
  312. case 'fullScreen':
  313. case 'setPosition':
  314. case 'scrolling':
  315. case 'search':
  316. case 'copyText':
  317. case 'history':
  318. case 'settings':
  319. if (this[`${button}Active`])
  320. classResult = classActive;
  321. break;
  322. }
  323. if (this.activePage == 'LoaderPage' || !this.lastOpenedBook) {
  324. switch (button) {
  325. case 'undoAction':
  326. case 'redoAction':
  327. case 'setPosition':
  328. case 'scrolling':
  329. case 'search':
  330. case 'copyText':
  331. classResult = classDisabled;
  332. break;
  333. case 'history':
  334. case 'refresh':
  335. if (!this.lastOpenedBook)
  336. classResult = classDisabled;
  337. break;
  338. }
  339. }
  340. return classResult;
  341. }
  342. get activePage() {
  343. let result = '';
  344. if (this.progressActive)
  345. result = 'ProgressPage';
  346. else if (this.loaderActive)
  347. result = 'LoaderPage';
  348. else if (this.lastOpenedBook)
  349. result = 'TextPage';
  350. if (!result) {
  351. this.loaderActive = true;
  352. result = 'LoaderPage';
  353. }
  354. if (result != 'TextPage') {
  355. this.$root.$emit('set-app-title');
  356. }
  357. if (this.lastActivePage != result && result == 'TextPage') {
  358. //акивируем страницу с текстом
  359. this.$nextTick(async() => {
  360. const last = this.lastOpenedBook;
  361. const isParsed = await bookManager.hasBookParsed(last);
  362. if (!isParsed) {
  363. this.$root.$emit('set-app-title');
  364. return;
  365. }
  366. this.updateRoute();
  367. const textPage = this.$refs.page;
  368. if (textPage.showBook) {
  369. textPage.lastBook = last;
  370. textPage.bookPos = (last.bookPos !== undefined ? last.bookPos : 0);
  371. textPage.showBook();
  372. }
  373. });
  374. }
  375. this.lastActivePage = result;
  376. return result;
  377. }
  378. loadBook(opts) {
  379. this.closeAllTextPages();
  380. this.progressActive = true;
  381. this.$nextTick(async() => {
  382. const progress = this.$refs.page;
  383. try {
  384. progress.show();
  385. progress.setState({state: 'parse'});
  386. // есть ли среди истории OpenedBook
  387. const key = bookManager.keyFromUrl(opts.url);
  388. let wasOpened = this.reader.openedBook[key];
  389. wasOpened = (wasOpened ? wasOpened : {});
  390. const bookPos = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPos);
  391. const bookPosSeen = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPosSeen);
  392. let book = null;
  393. if (!opts.force) {
  394. // пытаемся загрузить и распарсить книгу в менеджере из локального кеша
  395. const bookParsed = await bookManager.getBook({url: opts.url}, (prog) => {
  396. progress.setState({progress: prog});
  397. });
  398. // если есть в локальном кеше
  399. if (bookParsed) {
  400. this.commit('reader/setOpenedBook', Object.assign({bookPos, bookPosSeen}, bookManager.metaOnly(bookParsed)));
  401. this.loaderActive = false;
  402. progress.hide(); this.progressActive = false;
  403. this.blinkCachedLoadMessage();
  404. return;
  405. }
  406. // иначе идем на сервер
  407. // пытаемся загрузить готовый файл с сервера
  408. if (wasOpened.path) {
  409. try {
  410. const resp = await readerApi.loadCachedBook(wasOpened.path, (state) => {
  411. progress.setState(state);
  412. });
  413. book = Object.assign({}, wasOpened, {data: resp.data});
  414. } catch (e) {
  415. //молчим
  416. }
  417. }
  418. }
  419. progress.setState({totalSteps: 5});
  420. // не удалось, скачиваем книгу полностью с конвертацией
  421. let loadCached = true;
  422. if (!book) {
  423. book = await readerApi.loadBook(opts.url, (state) => {
  424. progress.setState(state);
  425. });
  426. loadCached = false;
  427. }
  428. // добавляем в bookManager
  429. progress.setState({state: 'parse', step: 5});
  430. const addedBook = await bookManager.addBook(book, (prog) => {
  431. progress.setState({progress: prog});
  432. });
  433. // добавляем в историю
  434. this.commit('reader/setOpenedBook', Object.assign({bookPos, bookPosSeen}, bookManager.metaOnly(addedBook)));
  435. this.updateRoute(true);
  436. this.loaderActive = false;
  437. progress.hide(); this.progressActive = false;
  438. if (loadCached) {
  439. this.blinkCachedLoadMessage();
  440. } else
  441. this.stopBlink = true;
  442. } catch (e) {
  443. progress.hide(); this.progressActive = false;
  444. this.loaderActive = true;
  445. this.$alert(e.message, 'Ошибка', {type: 'error'});
  446. }
  447. });
  448. }
  449. blinkCachedLoadMessage() {
  450. this.blinkCount = 30;
  451. if (!this.inBlink) {
  452. this.inBlink = true;
  453. this.stopBlink = false;
  454. this.$nextTick(async() => {
  455. let page = this.$refs.page;
  456. while (this.blinkCount) {
  457. this.showRefreshIcon = !this.showRefreshIcon;
  458. if (page.blinkCachedLoadMessage)
  459. page.blinkCachedLoadMessage(this.showRefreshIcon);
  460. await sleep(500);
  461. if (this.stopBlink)
  462. break;
  463. this.blinkCount--;
  464. page = this.$refs.page;
  465. }
  466. this.showRefreshIcon = true;
  467. this.inBlink = false;
  468. if (page.blinkCachedLoadMessage)
  469. page.blinkCachedLoadMessage('finish');
  470. });
  471. }
  472. }
  473. keyHook(event) {
  474. if (this.$root.rootRoute == '/reader') {
  475. let handled = false;
  476. if (!handled && this.settingsActive)
  477. handled = this.$refs.settingsPage.keyHook(event);
  478. if (!handled && this.historyActive)
  479. handled = this.$refs.historyPage.keyHook(event);
  480. if (!handled && this.setPositionActive)
  481. handled = this.$refs.setPositionPage.keyHook(event);
  482. if (!handled && this.$refs.page && this.$refs.page.keyHook)
  483. handled = this.$refs.page.keyHook(event);
  484. if (!handled && event.type == 'keydown') {
  485. if (event.code == 'Escape')
  486. this.loaderToggle();
  487. if (this.activePage == 'TextPage') {
  488. switch (event.code) {
  489. case 'KeyP':
  490. this.setPositionToggle();
  491. break;
  492. case 'KeyZ':
  493. this.scrollingToggle();
  494. break;
  495. case 'KeyH':
  496. this.historyToggle();
  497. break;
  498. case 'KeyS':
  499. this.settingsToggle();
  500. break;
  501. }
  502. }
  503. }
  504. }
  505. }
  506. }
  507. //-----------------------------------------------------------------------------
  508. </script>
  509. <style scoped>
  510. .el-container {
  511. padding: 0;
  512. margin: 0;
  513. height: 100%;
  514. }
  515. .el-header {
  516. padding-left: 5px;
  517. padding-right: 5px;
  518. background-color: #1B695F;
  519. color: #000;
  520. overflow-x: auto;
  521. overflow-y: hidden;
  522. }
  523. .header {
  524. display: flex;
  525. justify-content: space-between;
  526. min-width: 550px;
  527. }
  528. .el-main {
  529. position: relative;
  530. display: flex;
  531. padding: 0;
  532. margin: 0;
  533. background-color: #EBE2C9;
  534. color: #000;
  535. }
  536. .tool-button {
  537. margin: 0;
  538. margin-left: 2px;
  539. margin-right: 2px;
  540. padding: 0;
  541. color: #3E843E;
  542. background-color: #E6EDF4;
  543. margin-top: 5px;
  544. height: 38px;
  545. width: 38px;
  546. border: 0;
  547. box-shadow: 3px 3px 5px black;
  548. }
  549. .tool-button:hover {
  550. background-color: white;
  551. }
  552. .tool-button-active {
  553. box-shadow: 0 0 0;
  554. color: white;
  555. background-color: #8AB45F;
  556. position: relative;
  557. top: 1px;
  558. left: 1px;
  559. }
  560. .tool-button-active:hover {
  561. color: white;
  562. background-color: #81C581;
  563. }
  564. .tool-button-disabled {
  565. color: lightgray;
  566. background-color: gray;
  567. }
  568. .tool-button-disabled:hover {
  569. color: lightgray;
  570. background-color: gray;
  571. }
  572. i {
  573. font-size: 200%;
  574. }
  575. .space {
  576. width: 10px;
  577. display: inline-block;
  578. }
  579. .clear {
  580. color: rgba(0,0,0,0);
  581. }
  582. </style>