Reader.vue 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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. @load-file="loadFile"
  51. @book-pos-changed="bookPosChanged"
  52. @tool-bar-toggle="toolBarToggle"
  53. @full-screen-toogle="fullScreenToggle"
  54. @stop-scrolling="stopScrolling"
  55. @scrolling-toggle="scrollingToggle"
  56. @help-toggle="helpToggle"
  57. @donate-toggle="donateToggle"
  58. ></component>
  59. </keep-alive>
  60. <SetPositionPage v-if="setPositionActive" ref="setPositionPage" @set-position-toggle="setPositionToggle" @book-pos-changed="bookPosChanged"></SetPositionPage>
  61. <SearchPage v-show="searchActive" ref="searchPage"
  62. @search-toggle="searchToggle"
  63. @book-pos-changed="bookPosChanged"
  64. @start-text-search="startTextSearch"
  65. @stop-text-search="stopTextSearch">
  66. </SearchPage>
  67. <CopyTextPage v-if="copyTextActive" ref="copyTextPage" @copy-text-toggle="copyTextToggle"></CopyTextPage>
  68. <HistoryPage v-show="historyActive" ref="historyPage" @load-book="loadBook" @history-toggle="historyToggle"></HistoryPage>
  69. <SettingsPage v-if="settingsActive" ref="settingsPage" @settings-toggle="settingsToggle"></SettingsPage>
  70. <HelpPage v-if="helpActive" ref="helpPage" @help-toggle="helpToggle"></HelpPage>
  71. <ClickMapPage v-show="clickMapActive" ref="clickMapPage"></ClickMapPage>
  72. <ServerStorage v-show="hidden" ref="serverStorage"></ServerStorage>
  73. </el-main>
  74. </el-container>
  75. </template>
  76. <script>
  77. //-----------------------------------------------------------------------------
  78. import Vue from 'vue';
  79. import Component from 'vue-class-component';
  80. import _ from 'lodash';
  81. import {Buffer} from 'safe-buffer';
  82. import LoaderPage from './LoaderPage/LoaderPage.vue';
  83. import TextPage from './TextPage/TextPage.vue';
  84. import ProgressPage from './ProgressPage/ProgressPage.vue';
  85. import SetPositionPage from './SetPositionPage/SetPositionPage.vue';
  86. import SearchPage from './SearchPage/SearchPage.vue';
  87. import CopyTextPage from './CopyTextPage/CopyTextPage.vue';
  88. import HistoryPage from './HistoryPage/HistoryPage.vue';
  89. import SettingsPage from './SettingsPage/SettingsPage.vue';
  90. import HelpPage from './HelpPage/HelpPage.vue';
  91. import ClickMapPage from './ClickMapPage/ClickMapPage.vue';
  92. import ServerStorage from './ServerStorage/ServerStorage.vue';
  93. import bookManager from './share/bookManager';
  94. import readerApi from '../../api/reader';
  95. import * as utils from '../../share/utils';
  96. export default @Component({
  97. components: {
  98. LoaderPage,
  99. TextPage,
  100. ProgressPage,
  101. SetPositionPage,
  102. SearchPage,
  103. CopyTextPage,
  104. HistoryPage,
  105. SettingsPage,
  106. HelpPage,
  107. ClickMapPage,
  108. ServerStorage,
  109. },
  110. watch: {
  111. bookPos: function(newValue) {
  112. if (newValue !== undefined && this.activePage == 'TextPage') {
  113. const textPage = this.$refs.page;
  114. if (textPage.bookPos != newValue) {
  115. textPage.bookPos = newValue;
  116. }
  117. this.debouncedSetRecentBook(newValue);
  118. }
  119. },
  120. routeParamPos: function(newValue) {
  121. if (newValue !== undefined && newValue != this.bookPos) {
  122. this.bookPos = newValue;
  123. }
  124. },
  125. routeParamUrl: function(newValue) {
  126. if (newValue !== '' && newValue !== this.mostRecentBook().url) {
  127. this.loadBook({url: newValue, bookPos: this.routeParamPos});
  128. }
  129. },
  130. settings: function() {
  131. this.loadSettings();
  132. this.updateRoute();
  133. },
  134. loaderActive: function(newValue) {
  135. const recent = this.mostRecentBook();
  136. if (!newValue && !this.loading && recent && !bookManager.hasBookParsed(recent)) {
  137. this.loadBook(recent);
  138. }
  139. },
  140. },
  141. })
  142. class Reader extends Vue {
  143. loaderActive = false;
  144. progressActive = false;
  145. fullScreenActive = false;
  146. scrollingActive = false;
  147. setPositionActive = false;
  148. searchActive = false;
  149. copyTextActive = false;
  150. historyActive = false;
  151. settingsActive = false;
  152. helpActive = false;
  153. clickMapActive = false;
  154. bookPos = null;
  155. allowUrlParamBookPos = false;
  156. showRefreshIcon = true;
  157. mostRecentBookReactive = null;
  158. actionList = [];
  159. actionCur = -1;
  160. hidden = false;
  161. created() {
  162. this.loading = true;
  163. this.commit = this.$store.commit;
  164. this.dispatch = this.$store.dispatch;
  165. this.reader = this.$store.state.reader;
  166. this.config = this.$store.state.config;
  167. this.$root.addKeyHook(this.keyHook);
  168. this.lastActivePage = false;
  169. this.debouncedUpdateRoute = _.debounce(() => {
  170. this.updateRoute();
  171. }, 1000);
  172. this.debouncedSetRecentBook = _.debounce(async(newValue) => {
  173. const recent = this.mostRecentBook();
  174. if (recent && (recent.bookPos != newValue || recent.bookPosSeen !== this.bookPosSeen)) {
  175. await bookManager.setRecentBook(Object.assign({}, recent, {bookPos: newValue, bookPosSeen: this.bookPosSeen}));
  176. if (this.actionCur < 0 || (this.actionCur >= 0 && this.actionList[this.actionCur] != newValue))
  177. this.addAction(newValue);
  178. }
  179. }, 500);
  180. this.debouncedSaveRecent = _.debounce(async() => {
  181. const serverStorage = this.$refs.serverStorage;
  182. while (!serverStorage.inited) await utils.sleep(1000);
  183. await serverStorage.saveRecent();
  184. }, 1000);
  185. document.addEventListener('fullscreenchange', () => {
  186. this.fullScreenActive = (document.fullscreenElement !== null);
  187. });
  188. this.loadSettings();
  189. }
  190. mounted() {
  191. (async() => {
  192. await bookManager.init(this.settings);
  193. bookManager.addEventListener(this.bookManagerEvent);
  194. if (this.$root.rootRoute == '/reader') {
  195. if (this.routeParamUrl) {
  196. await this.loadBook({url: this.routeParamUrl, bookPos: this.routeParamPos});
  197. } else {
  198. this.loaderActive = true;
  199. }
  200. }
  201. this.checkSetStorageAccessKey();
  202. this.loading = false;
  203. })();
  204. }
  205. loadSettings() {
  206. const settings = this.settings;
  207. this.allowUrlParamBookPos = settings.allowUrlParamBookPos;
  208. this.copyFullText = settings.copyFullText;
  209. this.showClickMapPage = settings.showClickMapPage;
  210. this.clickControl = settings.clickControl;
  211. this.blinkCachedLoad = settings.blinkCachedLoad;
  212. }
  213. checkSetStorageAccessKey() {
  214. const q = this.$route.query;
  215. if (q['setStorageAccessKey']) {
  216. this.$router.replace(`/reader`);
  217. this.settingsToggle();
  218. this.$nextTick(() => {
  219. this.$refs.settingsPage.enterServerStorageKey(
  220. Buffer.from(utils.fromBase58(q['setStorageAccessKey'])).toString()
  221. );
  222. });
  223. }
  224. }
  225. get routeParamPos() {
  226. let result = undefined;
  227. const q = this.$route.query;
  228. if (q['__p']) {
  229. result = q['__p'];
  230. if (Array.isArray(result))
  231. result = result[0];
  232. }
  233. return (result ? parseInt(result, 10) || 0 : result);
  234. }
  235. updateRoute(isNewRoute) {
  236. if (this.loading)
  237. return;
  238. const recent = this.mostRecentBook();
  239. const pos = (recent && recent.bookPos && this.allowUrlParamBookPos ? `__p=${recent.bookPos}&` : '');
  240. const url = (recent ? `url=${recent.url}` : '');
  241. if (isNewRoute)
  242. this.$router.push(`/reader?${pos}${url}`);
  243. else
  244. this.$router.replace(`/reader?${pos}${url}`);
  245. }
  246. get routeParamUrl() {
  247. let result = '';
  248. const path = this.$route.fullPath;
  249. const i = path.indexOf('url=');
  250. if (i >= 0) {
  251. result = path.substr(i + 4);
  252. }
  253. return decodeURIComponent(result);
  254. }
  255. bookPosChanged(event) {
  256. if (event.bookPosSeen !== undefined)
  257. this.bookPosSeen = event.bookPosSeen;
  258. this.bookPos = event.bookPos;
  259. this.debouncedUpdateRoute();
  260. }
  261. bookManagerEvent(eventName) {
  262. const serverStorage = this.$refs.serverStorage;
  263. if (eventName == 'load-meta-finish') {
  264. serverStorage.init();
  265. }
  266. if (eventName == 'recent-changed') {
  267. (async() => {
  268. const oldBook = this.mostRecentBookReactive;
  269. const newBook = bookManager.mostRecentBook();
  270. if (oldBook && newBook && oldBook.key != newBook.key) {
  271. await this.loadBook(newBook);
  272. }
  273. this.debouncedSaveRecent();
  274. })();
  275. }
  276. }
  277. get toolBarActive() {
  278. return this.reader.toolBarActive;
  279. }
  280. mostRecentBook() {
  281. const result = bookManager.mostRecentBook();
  282. this.mostRecentBookReactive = result;
  283. return result;
  284. }
  285. get settings() {
  286. return this.$store.state.reader.settings;
  287. }
  288. addAction(pos) {
  289. let a = this.actionList;
  290. if (!a.length || a[a.length - 1] != pos) {
  291. a.push(pos);
  292. if (a.length > 20)
  293. a.shift();
  294. this.actionCur = a.length - 1;
  295. }
  296. }
  297. toolBarToggle() {
  298. this.commit('reader/setToolBarActive', !this.toolBarActive);
  299. this.$root.$emit('resize');
  300. }
  301. fullScreenToggle() {
  302. this.fullScreenActive = !this.fullScreenActive;
  303. if (this.fullScreenActive) {
  304. const element = document.documentElement;
  305. if (element.requestFullscreen) {
  306. element.requestFullscreen();
  307. } else if (element.webkitrequestFullscreen) {
  308. element.webkitRequestFullscreen();
  309. } else if (element.mozRequestFullscreen) {
  310. element.mozRequestFullScreen();
  311. }
  312. } else {
  313. if (document.cancelFullScreen) {
  314. document.cancelFullScreen();
  315. } else if (document.mozCancelFullScreen) {
  316. document.mozCancelFullScreen();
  317. } else if (document.webkitCancelFullScreen) {
  318. document.webkitCancelFullScreen();
  319. }
  320. }
  321. }
  322. closeAllTextPages() {
  323. this.setPositionActive = false;
  324. this.copyTextActive = false;
  325. this.historyActive = false;
  326. this.settingsActive = false;
  327. this.stopScrolling();
  328. this.stopSearch();
  329. this.helpActive = false;
  330. }
  331. loaderToggle() {
  332. this.loaderActive = !this.loaderActive;
  333. if (this.loaderActive) {
  334. this.closeAllTextPages();
  335. }
  336. }
  337. setPositionToggle() {
  338. this.setPositionActive = !this.setPositionActive;
  339. if (this.setPositionActive && this.activePage == 'TextPage' && this.mostRecentBook()) {
  340. this.closeAllTextPages();
  341. this.setPositionActive = true;
  342. this.$nextTick(() => {
  343. const recent = this.mostRecentBook();
  344. this.$refs.setPositionPage.init(recent.bookPos, recent.textLength - 1);
  345. });
  346. } else {
  347. this.setPositionActive = false;
  348. }
  349. }
  350. stopScrolling() {
  351. if (this.scrollingActive)
  352. this.scrollingToggle();
  353. }
  354. scrollingToggle() {
  355. this.scrollingActive = !this.scrollingActive;
  356. if (this.activePage == 'TextPage') {
  357. const page = this.$refs.page;
  358. if (this.scrollingActive) {
  359. page.startTextScrolling();
  360. } else {
  361. page.stopTextScrolling();
  362. }
  363. }
  364. }
  365. stopSearch() {
  366. if (this.searchActive)
  367. this.searchToggle();
  368. }
  369. startTextSearch(opts) {
  370. if (this.activePage == 'TextPage')
  371. this.$refs.page.startSearch(opts.needle);
  372. }
  373. stopTextSearch() {
  374. if (this.activePage == 'TextPage')
  375. this.$refs.page.stopSearch();
  376. }
  377. searchToggle() {
  378. this.searchActive = !this.searchActive;
  379. const page = this.$refs.page;
  380. if (this.searchActive && this.activePage == 'TextPage' && page.parsed) {
  381. this.closeAllTextPages();
  382. this.searchActive = true;
  383. this.$nextTick(() => {
  384. this.$refs.searchPage.init(page.parsed);
  385. });
  386. } else {
  387. this.stopTextSearch();
  388. this.searchActive = false;
  389. }
  390. }
  391. copyTextToggle() {
  392. this.copyTextActive = !this.copyTextActive;
  393. const page = this.$refs.page;
  394. if (this.copyTextActive && this.activePage == 'TextPage' && page.parsed) {
  395. this.closeAllTextPages();
  396. this.copyTextActive = true;
  397. this.$nextTick(() => {
  398. this.$refs.copyTextPage.init(this.mostRecentBook().bookPos, page.parsed, this.copyFullText);
  399. });
  400. } else {
  401. this.copyTextActive = false;
  402. }
  403. }
  404. historyToggle() {
  405. this.historyActive = !this.historyActive;
  406. if (this.historyActive) {
  407. this.closeAllTextPages();
  408. this.$refs.historyPage.init();
  409. this.historyActive = true;
  410. } else {
  411. this.historyActive = false;
  412. }
  413. }
  414. settingsToggle() {
  415. this.settingsActive = !this.settingsActive;
  416. if (this.settingsActive) {
  417. this.closeAllTextPages();
  418. this.settingsActive = true;
  419. } else {
  420. this.settingsActive = false;
  421. }
  422. }
  423. helpToggle() {
  424. this.helpActive = !this.helpActive;
  425. if (this.helpActive) {
  426. this.closeAllTextPages();
  427. this.helpActive = true;
  428. }
  429. }
  430. donateToggle() {
  431. this.helpToggle();
  432. if (this.helpActive) {
  433. this.$nextTick(() => {
  434. this.$refs.helpPage.activateDonateHelpPage();
  435. });
  436. }
  437. }
  438. refreshBook() {
  439. if (this.mostRecentBook()) {
  440. this.loadBook({url: this.mostRecentBook().url, force: true});
  441. }
  442. }
  443. buttonClick(button) {
  444. const activeClass = this.buttonActiveClass(button);
  445. this.$refs[button].$el.blur();
  446. if (activeClass['tool-button-disabled'])
  447. return;
  448. switch (button) {
  449. case 'loader':
  450. this.loaderToggle();
  451. break;
  452. case 'undoAction':
  453. if (this.actionCur > 0) {
  454. this.actionCur--;
  455. this.bookPosChanged({bookPos: this.actionList[this.actionCur]});
  456. }
  457. break;
  458. case 'redoAction':
  459. if (this.actionCur < this.actionList.length - 1) {
  460. this.actionCur++;
  461. this.bookPosChanged({bookPos: this.actionList[this.actionCur]});
  462. }
  463. break;
  464. case 'fullScreen':
  465. this.fullScreenToggle();
  466. break;
  467. case 'setPosition':
  468. this.setPositionToggle();
  469. break;
  470. case 'scrolling':
  471. this.scrollingToggle();
  472. break;
  473. case 'search':
  474. this.searchToggle();
  475. break;
  476. case 'copyText':
  477. this.copyTextToggle();
  478. break;
  479. case 'history':
  480. this.historyToggle();
  481. break;
  482. case 'refresh':
  483. this.refreshBook();
  484. break;
  485. case 'settings':
  486. this.settingsToggle();
  487. break;
  488. }
  489. }
  490. buttonActiveClass(button) {
  491. const classActive = { 'tool-button-active': true, 'tool-button-active:hover': true };
  492. const classDisabled = { 'tool-button-disabled': true, 'tool-button-disabled:hover': true };
  493. let classResult = {};
  494. switch (button) {
  495. case 'loader':
  496. case 'fullScreen':
  497. case 'setPosition':
  498. case 'scrolling':
  499. case 'search':
  500. case 'copyText':
  501. case 'history':
  502. case 'settings':
  503. if (this[`${button}Active`])
  504. classResult = classActive;
  505. break;
  506. }
  507. switch (button) {
  508. case 'undoAction':
  509. if (this.actionCur <= 0)
  510. classResult = classDisabled;
  511. break;
  512. case 'redoAction':
  513. if (this.actionCur == this.actionList.length - 1)
  514. classResult = classDisabled;
  515. break;
  516. }
  517. if (this.activePage == 'LoaderPage' || !this.mostRecentBook()) {
  518. switch (button) {
  519. case 'undoAction':
  520. case 'redoAction':
  521. case 'setPosition':
  522. case 'scrolling':
  523. case 'search':
  524. case 'copyText':
  525. classResult = classDisabled;
  526. break;
  527. case 'history':
  528. case 'refresh':
  529. if (!this.mostRecentBook())
  530. classResult = classDisabled;
  531. break;
  532. }
  533. }
  534. return classResult;
  535. }
  536. async activateClickMapPage() {
  537. if (this.clickControl && this.showClickMapPage && !this.clickMapActive) {
  538. this.clickMapActive = true;
  539. await this.$refs.clickMapPage.slowDisappear();
  540. this.clickMapActive = false;
  541. }
  542. }
  543. get activePage() {
  544. let result = '';
  545. if (this.progressActive)
  546. result = 'ProgressPage';
  547. else if (this.loaderActive)
  548. result = 'LoaderPage';
  549. else if (this.mostRecentBookReactive)
  550. result = 'TextPage';
  551. if (!result && !this.loading) {
  552. this.loaderActive = true;
  553. result = 'LoaderPage';
  554. }
  555. if (result != 'TextPage') {
  556. this.$root.$emit('set-app-title');
  557. }
  558. // на LoaderPage всегда показываем toolBar
  559. if (result == 'LoaderPage' && !this.toolBarActive) {
  560. this.toolBarToggle();
  561. }
  562. if (this.lastActivePage != result && result == 'TextPage') {
  563. //акивируем страницу с текстом
  564. this.$nextTick(async() => {
  565. const last = this.mostRecentBookReactive;
  566. const isParsed = bookManager.hasBookParsed(last);
  567. if (!isParsed) {
  568. this.$root.$emit('set-app-title');
  569. return;
  570. }
  571. this.updateRoute();
  572. const textPage = this.$refs.page;
  573. if (textPage.showBook) {
  574. textPage.lastBook = last;
  575. textPage.bookPos = (last.bookPos !== undefined ? last.bookPos : 0);
  576. textPage.showBook();
  577. }
  578. });
  579. }
  580. this.lastActivePage = result;
  581. return result;
  582. }
  583. async loadBook(opts) {
  584. if (!opts || !opts.url) {
  585. this.mostRecentBook();
  586. return;
  587. }
  588. let url = opts.url;
  589. if ((url.indexOf('http://') != 0) && (url.indexOf('https://') != 0) &&
  590. (url.indexOf('file://') != 0))
  591. url = 'http://' + url;
  592. // уже просматривается сейчас
  593. const lastBook = (this.$refs.page ? this.$refs.page.lastBook : null);
  594. if (!opts.force && lastBook && lastBook.url == url && bookManager.hasBookParsed(lastBook)) {
  595. this.loaderActive = false;
  596. return;
  597. }
  598. this.progressActive = true;
  599. await this.$nextTick()
  600. const progress = this.$refs.page;
  601. this.actionList = [];
  602. this.actionCur = -1;
  603. try {
  604. progress.show();
  605. progress.setState({state: 'parse'});
  606. // есть ли среди недавних
  607. const key = bookManager.keyFromUrl(url);
  608. let wasOpened = await bookManager.getRecentBook({key});
  609. wasOpened = (wasOpened ? wasOpened : {});
  610. const bookPos = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPos);
  611. const bookPosSeen = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPosSeen);
  612. let book = null;
  613. if (!opts.force) {
  614. // пытаемся загрузить и распарсить книгу в менеджере из локального кэша
  615. const bookParsed = await bookManager.getBook({url}, (prog) => {
  616. progress.setState({progress: prog});
  617. });
  618. // если есть в локальном кэше
  619. if (bookParsed) {
  620. await bookManager.setRecentBook(Object.assign({bookPos, bookPosSeen}, bookParsed));
  621. this.mostRecentBook();
  622. this.addAction(bookPos);
  623. this.loaderActive = false;
  624. progress.hide(); this.progressActive = false;
  625. this.blinkCachedLoadMessage();
  626. await this.activateClickMapPage();
  627. return;
  628. }
  629. // иначе идем на сервер
  630. // пытаемся загрузить готовый файл с сервера
  631. if (wasOpened.path) {
  632. try {
  633. const resp = await readerApi.loadCachedBook(wasOpened.path, (state) => {
  634. progress.setState(state);
  635. });
  636. book = Object.assign({}, wasOpened, {data: resp.data});
  637. } catch (e) {
  638. //молчим
  639. }
  640. }
  641. }
  642. progress.setState({totalSteps: 5});
  643. // не удалось, скачиваем книгу полностью с конвертацией
  644. let loadCached = true;
  645. if (!book) {
  646. book = await readerApi.loadBook(url, (state) => {
  647. progress.setState(state);
  648. });
  649. loadCached = false;
  650. }
  651. // добавляем в bookManager
  652. progress.setState({state: 'parse', step: 5});
  653. const addedBook = await bookManager.addBook(book, (prog) => {
  654. progress.setState({progress: prog});
  655. });
  656. // добавляем в историю
  657. await bookManager.setRecentBook(Object.assign({bookPos, bookPosSeen}, addedBook));
  658. this.mostRecentBook();
  659. this.addAction(bookPos);
  660. this.updateRoute(true);
  661. this.loaderActive = false;
  662. progress.hide(); this.progressActive = false;
  663. if (loadCached) {
  664. this.blinkCachedLoadMessage();
  665. } else
  666. this.stopBlink = true;
  667. await this.activateClickMapPage();
  668. } catch (e) {
  669. progress.hide(); this.progressActive = false;
  670. this.loaderActive = true;
  671. this.$alert(e.message, 'Ошибка', {type: 'error'});
  672. }
  673. }
  674. async loadFile(opts) {
  675. this.progressActive = true;
  676. await this.$nextTick();
  677. const progress = this.$refs.page;
  678. try {
  679. progress.show();
  680. progress.setState({state: 'upload'});
  681. const url = await readerApi.uploadFile(opts.file, this.config.maxUploadFileSize, (state) => {
  682. progress.setState(state);
  683. });
  684. progress.hide(); this.progressActive = false;
  685. await this.loadBook({url});
  686. } catch (e) {
  687. progress.hide(); this.progressActive = false;
  688. this.loaderActive = true;
  689. this.$alert(e.message, 'Ошибка', {type: 'error'});
  690. }
  691. }
  692. blinkCachedLoadMessage() {
  693. if (!this.blinkCachedLoad)
  694. return;
  695. this.blinkCount = 30;
  696. if (!this.inBlink) {
  697. this.inBlink = true;
  698. this.stopBlink = false;
  699. this.$nextTick(async() => {
  700. let page = this.$refs.page;
  701. while (this.blinkCount) {
  702. this.showRefreshIcon = !this.showRefreshIcon;
  703. if (page.blinkCachedLoadMessage)
  704. page.blinkCachedLoadMessage(this.showRefreshIcon);
  705. await utils.sleep(500);
  706. if (this.stopBlink)
  707. break;
  708. this.blinkCount--;
  709. page = this.$refs.page;
  710. }
  711. this.showRefreshIcon = true;
  712. this.inBlink = false;
  713. if (page.blinkCachedLoadMessage)
  714. page.blinkCachedLoadMessage('finish');
  715. });
  716. }
  717. }
  718. keyHook(event) {
  719. if (this.$root.rootRoute == '/reader') {
  720. let handled = false;
  721. if (!handled && this.helpActive)
  722. handled = this.$refs.helpPage.keyHook(event);
  723. if (!handled && this.settingsActive)
  724. handled = this.$refs.settingsPage.keyHook(event);
  725. if (!handled && this.historyActive)
  726. handled = this.$refs.historyPage.keyHook(event);
  727. if (!handled && this.setPositionActive)
  728. handled = this.$refs.setPositionPage.keyHook(event);
  729. if (!handled && this.searchActive)
  730. handled = this.$refs.searchPage.keyHook(event);
  731. if (!handled && this.copyTextActive)
  732. handled = this.$refs.copyTextPage.keyHook(event);
  733. if (!handled && this.$refs.page && this.$refs.page.keyHook)
  734. handled = this.$refs.page.keyHook(event);
  735. if (!handled && event.type == 'keydown') {
  736. if (event.code == 'Escape')
  737. this.loaderToggle();
  738. if (this.activePage == 'TextPage') {
  739. switch (event.code) {
  740. case 'KeyH':
  741. case 'F1':
  742. this.helpToggle();
  743. event.preventDefault();
  744. event.stopPropagation();
  745. break;
  746. case 'KeyZ':
  747. this.scrollingToggle();
  748. break;
  749. case 'KeyP':
  750. this.setPositionToggle();
  751. break;
  752. case 'KeyF':
  753. if (event.ctrlKey) {
  754. this.searchToggle();
  755. event.preventDefault();
  756. event.stopPropagation();
  757. }
  758. break;
  759. case 'KeyC':
  760. if (event.ctrlKey) {
  761. this.copyTextToggle();
  762. event.preventDefault();
  763. event.stopPropagation();
  764. }
  765. break;
  766. case 'KeyR':
  767. this.refreshBook();
  768. break;
  769. case 'KeyX':
  770. this.historyToggle();
  771. event.preventDefault();
  772. event.stopPropagation();
  773. break;
  774. case 'KeyS':
  775. this.settingsToggle();
  776. break;
  777. }
  778. }
  779. }
  780. }
  781. }
  782. }
  783. //-----------------------------------------------------------------------------
  784. </script>
  785. <style scoped>
  786. .el-container {
  787. padding: 0;
  788. margin: 0;
  789. height: 100%;
  790. }
  791. .el-header {
  792. padding-left: 5px;
  793. padding-right: 5px;
  794. background-color: #1B695F;
  795. color: #000;
  796. overflow-x: auto;
  797. overflow-y: hidden;
  798. }
  799. .header {
  800. display: flex;
  801. justify-content: space-between;
  802. min-width: 550px;
  803. }
  804. .el-main {
  805. position: relative;
  806. display: flex;
  807. padding: 0;
  808. margin: 0;
  809. background-color: #EBE2C9;
  810. color: #000;
  811. }
  812. .tool-button {
  813. margin: 0 2px 0 2px;
  814. padding: 0;
  815. color: #3E843E;
  816. background-color: #E6EDF4;
  817. margin-top: 5px;
  818. height: 38px;
  819. width: 38px;
  820. border: 0;
  821. box-shadow: 3px 3px 5px black;
  822. }
  823. .tool-button:hover {
  824. background-color: white;
  825. }
  826. .tool-button-active {
  827. box-shadow: 0 0 0;
  828. color: white;
  829. background-color: #8AB45F;
  830. position: relative;
  831. top: 1px;
  832. left: 1px;
  833. }
  834. .tool-button-active:hover {
  835. color: white;
  836. background-color: #81C581;
  837. }
  838. .tool-button-disabled {
  839. color: lightgray;
  840. background-color: gray;
  841. }
  842. .tool-button-disabled:hover {
  843. color: lightgray;
  844. background-color: gray;
  845. }
  846. i {
  847. font-size: 200%;
  848. }
  849. .space {
  850. width: 10px;
  851. display: inline-block;
  852. }
  853. .clear {
  854. color: rgba(0,0,0,0);
  855. }
  856. </style>