Reader.vue 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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-if="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. if (this.searchActive && this.activePage == 'TextPage' && this.lastOpenedBook) {
  267. this.closeAllTextPages();
  268. this.searchActive = true;
  269. this.$nextTick(() => {
  270. //this.$refs.searchPage
  271. });
  272. } else {
  273. this.searchActive = false;
  274. }
  275. }
  276. historyToggle() {
  277. this.historyActive = !this.historyActive;
  278. if (this.historyActive) {
  279. this.closeAllTextPages();
  280. this.historyActive = true;
  281. } else {
  282. this.historyActive = false;
  283. }
  284. }
  285. settingsToggle() {
  286. this.settingsActive = !this.settingsActive;
  287. if (this.settingsActive) {
  288. this.closeAllTextPages();
  289. this.settingsActive = true;
  290. } else {
  291. this.settingsActive = false;
  292. }
  293. }
  294. buttonClick(button) {
  295. switch (button) {
  296. case 'loader':
  297. this.loaderToggle();
  298. break;
  299. case 'fullScreen':
  300. this.fullScreenToggle();
  301. break;
  302. case 'setPosition':
  303. this.setPositionToggle();
  304. break;
  305. case 'scrolling':
  306. this.scrollingToggle();
  307. break;
  308. case 'search':
  309. this.searchToggle();
  310. break;
  311. case 'history':
  312. this.historyToggle();
  313. break;
  314. case 'refresh':
  315. if (this.lastOpenedBook) {
  316. this.loadBook({url: this.lastOpenedBook.url, force: true});
  317. }
  318. break;
  319. case 'settings':
  320. this.settingsToggle();
  321. break;
  322. }
  323. this.$refs[button].$el.blur();
  324. }
  325. buttonActiveClass(button) {
  326. const classActive = { 'tool-button-active': true, 'tool-button-active:hover': true };
  327. const classDisabled = { 'tool-button-disabled': true, 'tool-button-disabled:hover': true };
  328. let classResult = {};
  329. switch (button) {
  330. case 'loader':
  331. case 'fullScreen':
  332. case 'setPosition':
  333. case 'scrolling':
  334. case 'search':
  335. case 'copyText':
  336. case 'history':
  337. case 'settings':
  338. if (this[`${button}Active`])
  339. classResult = classActive;
  340. break;
  341. }
  342. if (this.activePage == 'LoaderPage' || !this.lastOpenedBook) {
  343. switch (button) {
  344. case 'undoAction':
  345. case 'redoAction':
  346. case 'setPosition':
  347. case 'scrolling':
  348. case 'search':
  349. case 'copyText':
  350. classResult = classDisabled;
  351. break;
  352. case 'history':
  353. case 'refresh':
  354. if (!this.lastOpenedBook)
  355. classResult = classDisabled;
  356. break;
  357. }
  358. }
  359. return classResult;
  360. }
  361. get activePage() {
  362. let result = '';
  363. if (this.progressActive)
  364. result = 'ProgressPage';
  365. else if (this.loaderActive)
  366. result = 'LoaderPage';
  367. else if (this.lastOpenedBook)
  368. result = 'TextPage';
  369. if (!result) {
  370. this.loaderActive = true;
  371. result = 'LoaderPage';
  372. }
  373. if (result != 'TextPage') {
  374. this.$root.$emit('set-app-title');
  375. }
  376. if (this.lastActivePage != result && result == 'TextPage') {
  377. //акивируем страницу с текстом
  378. this.$nextTick(async() => {
  379. const last = this.lastOpenedBook;
  380. const isParsed = await bookManager.hasBookParsed(last);
  381. if (!isParsed) {
  382. this.$root.$emit('set-app-title');
  383. return;
  384. }
  385. this.updateRoute();
  386. const textPage = this.$refs.page;
  387. if (textPage.showBook) {
  388. textPage.lastBook = last;
  389. textPage.bookPos = (last.bookPos !== undefined ? last.bookPos : 0);
  390. textPage.showBook();
  391. }
  392. });
  393. }
  394. this.lastActivePage = result;
  395. return result;
  396. }
  397. loadBook(opts) {
  398. this.closeAllTextPages();
  399. this.progressActive = true;
  400. this.$nextTick(async() => {
  401. const progress = this.$refs.page;
  402. try {
  403. progress.show();
  404. progress.setState({state: 'parse'});
  405. // есть ли среди истории OpenedBook
  406. const key = bookManager.keyFromUrl(opts.url);
  407. let wasOpened = this.reader.openedBook[key];
  408. wasOpened = (wasOpened ? wasOpened : {});
  409. const bookPos = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPos);
  410. const bookPosSeen = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPosSeen);
  411. let book = null;
  412. if (!opts.force) {
  413. // пытаемся загрузить и распарсить книгу в менеджере из локального кеша
  414. const bookParsed = await bookManager.getBook({url: opts.url}, (prog) => {
  415. progress.setState({progress: prog});
  416. });
  417. // если есть в локальном кеше
  418. if (bookParsed) {
  419. this.commit('reader/setOpenedBook', Object.assign({bookPos, bookPosSeen}, bookManager.metaOnly(bookParsed)));
  420. this.loaderActive = false;
  421. progress.hide(); this.progressActive = false;
  422. this.blinkCachedLoadMessage();
  423. return;
  424. }
  425. // иначе идем на сервер
  426. // пытаемся загрузить готовый файл с сервера
  427. if (wasOpened.path) {
  428. try {
  429. const resp = await readerApi.loadCachedBook(wasOpened.path, (state) => {
  430. progress.setState(state);
  431. });
  432. book = Object.assign({}, wasOpened, {data: resp.data});
  433. } catch (e) {
  434. //молчим
  435. }
  436. }
  437. }
  438. progress.setState({totalSteps: 5});
  439. // не удалось, скачиваем книгу полностью с конвертацией
  440. let loadCached = true;
  441. if (!book) {
  442. book = await readerApi.loadBook(opts.url, (state) => {
  443. progress.setState(state);
  444. });
  445. loadCached = false;
  446. }
  447. // добавляем в bookManager
  448. progress.setState({state: 'parse', step: 5});
  449. const addedBook = await bookManager.addBook(book, (prog) => {
  450. progress.setState({progress: prog});
  451. });
  452. // добавляем в историю
  453. this.commit('reader/setOpenedBook', Object.assign({bookPos, bookPosSeen}, bookManager.metaOnly(addedBook)));
  454. this.updateRoute(true);
  455. this.loaderActive = false;
  456. progress.hide(); this.progressActive = false;
  457. if (loadCached) {
  458. this.blinkCachedLoadMessage();
  459. } else
  460. this.stopBlink = true;
  461. } catch (e) {
  462. progress.hide(); this.progressActive = false;
  463. this.loaderActive = true;
  464. this.$alert(e.message, 'Ошибка', {type: 'error'});
  465. }
  466. });
  467. }
  468. blinkCachedLoadMessage() {
  469. this.blinkCount = 30;
  470. if (!this.inBlink) {
  471. this.inBlink = true;
  472. this.stopBlink = false;
  473. this.$nextTick(async() => {
  474. let page = this.$refs.page;
  475. while (this.blinkCount) {
  476. this.showRefreshIcon = !this.showRefreshIcon;
  477. if (page.blinkCachedLoadMessage)
  478. page.blinkCachedLoadMessage(this.showRefreshIcon);
  479. await sleep(500);
  480. if (this.stopBlink)
  481. break;
  482. this.blinkCount--;
  483. page = this.$refs.page;
  484. }
  485. this.showRefreshIcon = true;
  486. this.inBlink = false;
  487. if (page.blinkCachedLoadMessage)
  488. page.blinkCachedLoadMessage('finish');
  489. });
  490. }
  491. }
  492. keyHook(event) {
  493. if (this.$root.rootRoute == '/reader') {
  494. let handled = false;
  495. if (!handled && this.settingsActive)
  496. handled = this.$refs.settingsPage.keyHook(event);
  497. if (!handled && this.historyActive)
  498. handled = this.$refs.historyPage.keyHook(event);
  499. if (!handled && this.setPositionActive)
  500. handled = this.$refs.setPositionPage.keyHook(event);
  501. if (!handled && this.searchActive)
  502. handled = this.$refs.searchPage.keyHook(event);
  503. if (!handled && this.$refs.page && this.$refs.page.keyHook)
  504. handled = this.$refs.page.keyHook(event);
  505. if (!handled && event.type == 'keydown') {
  506. if (event.code == 'Escape')
  507. this.loaderToggle();
  508. if (this.activePage == 'TextPage') {
  509. switch (event.code) {
  510. case 'KeyP':
  511. this.setPositionToggle();
  512. break;
  513. case 'KeyF':
  514. if (event.ctrlKey) {
  515. this.searchToggle();
  516. event.preventDefault();
  517. event.stopPropagation();
  518. }
  519. break;
  520. case 'KeyZ':
  521. this.scrollingToggle();
  522. break;
  523. case 'KeyH':
  524. this.historyToggle();
  525. break;
  526. case 'KeyS':
  527. this.settingsToggle();
  528. break;
  529. }
  530. }
  531. }
  532. }
  533. }
  534. }
  535. //-----------------------------------------------------------------------------
  536. </script>
  537. <style scoped>
  538. .el-container {
  539. padding: 0;
  540. margin: 0;
  541. height: 100%;
  542. }
  543. .el-header {
  544. padding-left: 5px;
  545. padding-right: 5px;
  546. background-color: #1B695F;
  547. color: #000;
  548. overflow-x: auto;
  549. overflow-y: hidden;
  550. }
  551. .header {
  552. display: flex;
  553. justify-content: space-between;
  554. min-width: 550px;
  555. }
  556. .el-main {
  557. position: relative;
  558. display: flex;
  559. padding: 0;
  560. margin: 0;
  561. background-color: #EBE2C9;
  562. color: #000;
  563. }
  564. .tool-button {
  565. margin: 0;
  566. margin-left: 2px;
  567. margin-right: 2px;
  568. padding: 0;
  569. color: #3E843E;
  570. background-color: #E6EDF4;
  571. margin-top: 5px;
  572. height: 38px;
  573. width: 38px;
  574. border: 0;
  575. box-shadow: 3px 3px 5px black;
  576. }
  577. .tool-button:hover {
  578. background-color: white;
  579. }
  580. .tool-button-active {
  581. box-shadow: 0 0 0;
  582. color: white;
  583. background-color: #8AB45F;
  584. position: relative;
  585. top: 1px;
  586. left: 1px;
  587. }
  588. .tool-button-active:hover {
  589. color: white;
  590. background-color: #81C581;
  591. }
  592. .tool-button-disabled {
  593. color: lightgray;
  594. background-color: gray;
  595. }
  596. .tool-button-disabled:hover {
  597. color: lightgray;
  598. background-color: gray;
  599. }
  600. i {
  601. font-size: 200%;
  602. }
  603. .space {
  604. width: 10px;
  605. display: inline-block;
  606. }
  607. .clear {
  608. color: rgba(0,0,0,0);
  609. }
  610. </style>