Reader.vue 24 KB

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