Reader.vue 23 KB

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