Reader.vue 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. async bookManagerEvent(eventName) {
  262. const serverStorage = this.$refs.serverStorage;
  263. if (eventName == 'load-meta-finish') {
  264. serverStorage.init();
  265. const result = await bookManager.cleanRecentBooks();
  266. if (result)
  267. this.debouncedSaveRecent();
  268. }
  269. if (eventName == 'recent-changed') {
  270. (async() => {
  271. const oldBook = this.mostRecentBookReactive;
  272. const newBook = bookManager.mostRecentBook();
  273. if (oldBook && newBook) {
  274. if (oldBook.key != newBook.key) {
  275. await this.loadBook(newBook);
  276. } else if (oldBook.bookPos != newBook.bookPos) {
  277. this.bookPosChanged({bookPos: newBook.bookPos});
  278. }
  279. }
  280. this.debouncedSaveRecent();
  281. })();
  282. }
  283. }
  284. get toolBarActive() {
  285. return this.reader.toolBarActive;
  286. }
  287. mostRecentBook() {
  288. const result = bookManager.mostRecentBook();
  289. this.mostRecentBookReactive = result;
  290. return result;
  291. }
  292. get settings() {
  293. return this.$store.state.reader.settings;
  294. }
  295. addAction(pos) {
  296. let a = this.actionList;
  297. if (!a.length || a[a.length - 1] != pos) {
  298. a.push(pos);
  299. if (a.length > 20)
  300. a.shift();
  301. this.actionCur = a.length - 1;
  302. }
  303. }
  304. toolBarToggle() {
  305. this.commit('reader/setToolBarActive', !this.toolBarActive);
  306. this.$root.$emit('resize');
  307. }
  308. fullScreenToggle() {
  309. this.fullScreenActive = !this.fullScreenActive;
  310. if (this.fullScreenActive) {
  311. const element = document.documentElement;
  312. if (element.requestFullscreen) {
  313. element.requestFullscreen();
  314. } else if (element.webkitrequestFullscreen) {
  315. element.webkitRequestFullscreen();
  316. } else if (element.mozRequestFullscreen) {
  317. element.mozRequestFullScreen();
  318. }
  319. } else {
  320. if (document.cancelFullScreen) {
  321. document.cancelFullScreen();
  322. } else if (document.mozCancelFullScreen) {
  323. document.mozCancelFullScreen();
  324. } else if (document.webkitCancelFullScreen) {
  325. document.webkitCancelFullScreen();
  326. }
  327. }
  328. }
  329. closeAllTextPages() {
  330. this.setPositionActive = false;
  331. this.copyTextActive = false;
  332. this.historyActive = false;
  333. this.settingsActive = false;
  334. this.stopScrolling();
  335. this.stopSearch();
  336. this.helpActive = false;
  337. }
  338. loaderToggle() {
  339. this.loaderActive = !this.loaderActive;
  340. if (this.loaderActive) {
  341. this.closeAllTextPages();
  342. }
  343. }
  344. setPositionToggle() {
  345. this.setPositionActive = !this.setPositionActive;
  346. if (this.setPositionActive && this.activePage == 'TextPage' && this.mostRecentBook()) {
  347. this.closeAllTextPages();
  348. this.setPositionActive = true;
  349. this.$nextTick(() => {
  350. const recent = this.mostRecentBook();
  351. this.$refs.setPositionPage.init(recent.bookPos, recent.textLength - 1);
  352. });
  353. } else {
  354. this.setPositionActive = false;
  355. }
  356. }
  357. stopScrolling() {
  358. if (this.scrollingActive)
  359. this.scrollingToggle();
  360. }
  361. scrollingToggle() {
  362. this.scrollingActive = !this.scrollingActive;
  363. if (this.activePage == 'TextPage') {
  364. const page = this.$refs.page;
  365. if (this.scrollingActive) {
  366. page.startTextScrolling();
  367. } else {
  368. page.stopTextScrolling();
  369. }
  370. }
  371. }
  372. stopSearch() {
  373. if (this.searchActive)
  374. this.searchToggle();
  375. }
  376. startTextSearch(opts) {
  377. if (this.activePage == 'TextPage')
  378. this.$refs.page.startSearch(opts.needle);
  379. }
  380. stopTextSearch() {
  381. if (this.activePage == 'TextPage')
  382. this.$refs.page.stopSearch();
  383. }
  384. searchToggle() {
  385. this.searchActive = !this.searchActive;
  386. const page = this.$refs.page;
  387. if (this.searchActive && this.activePage == 'TextPage' && page.parsed) {
  388. this.closeAllTextPages();
  389. this.searchActive = true;
  390. this.$nextTick(() => {
  391. this.$refs.searchPage.init(page.parsed);
  392. });
  393. } else {
  394. this.stopTextSearch();
  395. this.searchActive = false;
  396. }
  397. }
  398. copyTextToggle() {
  399. this.copyTextActive = !this.copyTextActive;
  400. const page = this.$refs.page;
  401. if (this.copyTextActive && this.activePage == 'TextPage' && page.parsed) {
  402. this.closeAllTextPages();
  403. this.copyTextActive = true;
  404. this.$nextTick(() => {
  405. this.$refs.copyTextPage.init(this.mostRecentBook().bookPos, page.parsed, this.copyFullText);
  406. });
  407. } else {
  408. this.copyTextActive = false;
  409. }
  410. }
  411. historyToggle() {
  412. this.historyActive = !this.historyActive;
  413. if (this.historyActive) {
  414. this.closeAllTextPages();
  415. this.$refs.historyPage.init();
  416. this.historyActive = true;
  417. } else {
  418. this.historyActive = false;
  419. }
  420. }
  421. settingsToggle() {
  422. this.settingsActive = !this.settingsActive;
  423. if (this.settingsActive) {
  424. this.closeAllTextPages();
  425. this.settingsActive = true;
  426. } else {
  427. this.settingsActive = false;
  428. }
  429. }
  430. helpToggle() {
  431. this.helpActive = !this.helpActive;
  432. if (this.helpActive) {
  433. this.closeAllTextPages();
  434. this.helpActive = true;
  435. }
  436. }
  437. donateToggle() {
  438. this.helpToggle();
  439. if (this.helpActive) {
  440. this.$nextTick(() => {
  441. this.$refs.helpPage.activateDonateHelpPage();
  442. });
  443. }
  444. }
  445. refreshBook() {
  446. if (this.mostRecentBook()) {
  447. this.loadBook({url: this.mostRecentBook().url, force: true});
  448. }
  449. }
  450. buttonClick(button) {
  451. const activeClass = this.buttonActiveClass(button);
  452. this.$refs[button].$el.blur();
  453. if (activeClass['tool-button-disabled'])
  454. return;
  455. switch (button) {
  456. case 'loader':
  457. this.loaderToggle();
  458. break;
  459. case 'undoAction':
  460. if (this.actionCur > 0) {
  461. this.actionCur--;
  462. this.bookPosChanged({bookPos: this.actionList[this.actionCur]});
  463. }
  464. break;
  465. case 'redoAction':
  466. if (this.actionCur < this.actionList.length - 1) {
  467. this.actionCur++;
  468. this.bookPosChanged({bookPos: this.actionList[this.actionCur]});
  469. }
  470. break;
  471. case 'fullScreen':
  472. this.fullScreenToggle();
  473. break;
  474. case 'setPosition':
  475. this.setPositionToggle();
  476. break;
  477. case 'scrolling':
  478. this.scrollingToggle();
  479. break;
  480. case 'search':
  481. this.searchToggle();
  482. break;
  483. case 'copyText':
  484. this.copyTextToggle();
  485. break;
  486. case 'history':
  487. this.historyToggle();
  488. break;
  489. case 'refresh':
  490. this.refreshBook();
  491. break;
  492. case 'settings':
  493. this.settingsToggle();
  494. break;
  495. }
  496. }
  497. buttonActiveClass(button) {
  498. const classActive = { 'tool-button-active': true, 'tool-button-active:hover': true };
  499. const classDisabled = { 'tool-button-disabled': true, 'tool-button-disabled:hover': true };
  500. let classResult = {};
  501. switch (button) {
  502. case 'loader':
  503. case 'fullScreen':
  504. case 'setPosition':
  505. case 'scrolling':
  506. case 'search':
  507. case 'copyText':
  508. case 'history':
  509. case 'settings':
  510. if (this[`${button}Active`])
  511. classResult = classActive;
  512. break;
  513. }
  514. switch (button) {
  515. case 'undoAction':
  516. if (this.actionCur <= 0)
  517. classResult = classDisabled;
  518. break;
  519. case 'redoAction':
  520. if (this.actionCur == this.actionList.length - 1)
  521. classResult = classDisabled;
  522. break;
  523. }
  524. if (this.activePage == 'LoaderPage' || !this.mostRecentBook()) {
  525. switch (button) {
  526. case 'undoAction':
  527. case 'redoAction':
  528. case 'setPosition':
  529. case 'scrolling':
  530. case 'search':
  531. case 'copyText':
  532. classResult = classDisabled;
  533. break;
  534. case 'history':
  535. case 'refresh':
  536. if (!this.mostRecentBook())
  537. classResult = classDisabled;
  538. break;
  539. }
  540. }
  541. return classResult;
  542. }
  543. async activateClickMapPage() {
  544. if (this.clickControl && this.showClickMapPage && !this.clickMapActive) {
  545. this.clickMapActive = true;
  546. await this.$refs.clickMapPage.slowDisappear();
  547. this.clickMapActive = false;
  548. }
  549. }
  550. get activePage() {
  551. let result = '';
  552. if (this.progressActive)
  553. result = 'ProgressPage';
  554. else if (this.loaderActive)
  555. result = 'LoaderPage';
  556. else if (this.mostRecentBookReactive)
  557. result = 'TextPage';
  558. if (!result && !this.loading) {
  559. this.loaderActive = true;
  560. result = 'LoaderPage';
  561. }
  562. if (result != 'TextPage') {
  563. this.$root.$emit('set-app-title');
  564. }
  565. // на LoaderPage всегда показываем toolBar
  566. if (result == 'LoaderPage' && !this.toolBarActive) {
  567. this.toolBarToggle();
  568. }
  569. if (this.lastActivePage != result && result == 'TextPage') {
  570. //акивируем страницу с текстом
  571. this.$nextTick(async() => {
  572. const last = this.mostRecentBookReactive;
  573. const isParsed = bookManager.hasBookParsed(last);
  574. if (!isParsed) {
  575. this.$root.$emit('set-app-title');
  576. return;
  577. }
  578. this.updateRoute();
  579. const textPage = this.$refs.page;
  580. if (textPage.showBook) {
  581. textPage.lastBook = last;
  582. textPage.bookPos = (last.bookPos !== undefined ? last.bookPos : 0);
  583. textPage.showBook();
  584. }
  585. });
  586. }
  587. this.lastActivePage = result;
  588. return result;
  589. }
  590. async loadBook(opts) {
  591. if (!opts || !opts.url) {
  592. this.mostRecentBook();
  593. return;
  594. }
  595. let url = opts.url;
  596. if ((url.indexOf('http://') != 0) && (url.indexOf('https://') != 0) &&
  597. (url.indexOf('file://') != 0))
  598. url = 'http://' + url;
  599. // уже просматривается сейчас
  600. const lastBook = (this.$refs.page ? this.$refs.page.lastBook : null);
  601. if (!opts.force && lastBook && lastBook.url == url && bookManager.hasBookParsed(lastBook)) {
  602. this.loaderActive = false;
  603. return;
  604. }
  605. this.progressActive = true;
  606. await this.$nextTick()
  607. const progress = this.$refs.page;
  608. this.actionList = [];
  609. this.actionCur = -1;
  610. try {
  611. progress.show();
  612. progress.setState({state: 'parse'});
  613. // есть ли среди недавних
  614. const key = bookManager.keyFromUrl(url);
  615. let wasOpened = await bookManager.getRecentBook({key});
  616. wasOpened = (wasOpened ? wasOpened : {});
  617. const bookPos = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPos);
  618. const bookPosSeen = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPosSeen);
  619. let book = null;
  620. if (!opts.force) {
  621. // пытаемся загрузить и распарсить книгу в менеджере из локального кэша
  622. const bookParsed = await bookManager.getBook({url}, (prog) => {
  623. progress.setState({progress: prog});
  624. });
  625. // если есть в локальном кэше
  626. if (bookParsed) {
  627. await bookManager.setRecentBook(Object.assign({bookPos, bookPosSeen}, bookParsed));
  628. this.mostRecentBook();
  629. this.addAction(bookPos);
  630. this.loaderActive = false;
  631. progress.hide(); this.progressActive = false;
  632. this.blinkCachedLoadMessage();
  633. await this.activateClickMapPage();
  634. return;
  635. }
  636. // иначе идем на сервер
  637. // пытаемся загрузить готовый файл с сервера
  638. if (wasOpened.path) {
  639. try {
  640. const resp = await readerApi.loadCachedBook(wasOpened.path, (state) => {
  641. progress.setState(state);
  642. });
  643. book = Object.assign({}, wasOpened, {data: resp.data});
  644. } catch (e) {
  645. //молчим
  646. }
  647. }
  648. }
  649. progress.setState({totalSteps: 5});
  650. // не удалось, скачиваем книгу полностью с конвертацией
  651. let loadCached = true;
  652. if (!book) {
  653. book = await readerApi.loadBook(url, (state) => {
  654. progress.setState(state);
  655. });
  656. loadCached = false;
  657. }
  658. // добавляем в bookManager
  659. progress.setState({state: 'parse', step: 5});
  660. const addedBook = await bookManager.addBook(book, (prog) => {
  661. progress.setState({progress: prog});
  662. });
  663. // добавляем в историю
  664. await bookManager.setRecentBook(Object.assign({bookPos, bookPosSeen}, addedBook));
  665. this.mostRecentBook();
  666. this.addAction(bookPos);
  667. this.updateRoute(true);
  668. this.loaderActive = false;
  669. progress.hide(); this.progressActive = false;
  670. if (loadCached) {
  671. this.blinkCachedLoadMessage();
  672. } else
  673. this.stopBlink = true;
  674. await this.activateClickMapPage();
  675. } catch (e) {
  676. progress.hide(); this.progressActive = false;
  677. this.loaderActive = true;
  678. this.$alert(e.message, 'Ошибка', {type: 'error'});
  679. }
  680. }
  681. async loadFile(opts) {
  682. this.progressActive = true;
  683. await this.$nextTick();
  684. const progress = this.$refs.page;
  685. try {
  686. progress.show();
  687. progress.setState({state: 'upload'});
  688. const url = await readerApi.uploadFile(opts.file, this.config.maxUploadFileSize, (state) => {
  689. progress.setState(state);
  690. });
  691. progress.hide(); this.progressActive = false;
  692. await this.loadBook({url});
  693. } catch (e) {
  694. progress.hide(); this.progressActive = false;
  695. this.loaderActive = true;
  696. this.$alert(e.message, 'Ошибка', {type: 'error'});
  697. }
  698. }
  699. blinkCachedLoadMessage() {
  700. if (!this.blinkCachedLoad)
  701. return;
  702. this.blinkCount = 30;
  703. if (!this.inBlink) {
  704. this.inBlink = true;
  705. this.stopBlink = false;
  706. this.$nextTick(async() => {
  707. let page = this.$refs.page;
  708. while (this.blinkCount) {
  709. this.showRefreshIcon = !this.showRefreshIcon;
  710. if (page.blinkCachedLoadMessage)
  711. page.blinkCachedLoadMessage(this.showRefreshIcon);
  712. await utils.sleep(500);
  713. if (this.stopBlink)
  714. break;
  715. this.blinkCount--;
  716. page = this.$refs.page;
  717. }
  718. this.showRefreshIcon = true;
  719. this.inBlink = false;
  720. if (page.blinkCachedLoadMessage)
  721. page.blinkCachedLoadMessage('finish');
  722. });
  723. }
  724. }
  725. keyHook(event) {
  726. if (this.$root.rootRoute == '/reader') {
  727. let handled = false;
  728. if (!handled && this.helpActive)
  729. handled = this.$refs.helpPage.keyHook(event);
  730. if (!handled && this.settingsActive)
  731. handled = this.$refs.settingsPage.keyHook(event);
  732. if (!handled && this.historyActive)
  733. handled = this.$refs.historyPage.keyHook(event);
  734. if (!handled && this.setPositionActive)
  735. handled = this.$refs.setPositionPage.keyHook(event);
  736. if (!handled && this.searchActive)
  737. handled = this.$refs.searchPage.keyHook(event);
  738. if (!handled && this.copyTextActive)
  739. handled = this.$refs.copyTextPage.keyHook(event);
  740. if (!handled && this.$refs.page && this.$refs.page.keyHook)
  741. handled = this.$refs.page.keyHook(event);
  742. if (!handled && event.type == 'keydown') {
  743. if (event.code == 'Escape')
  744. this.loaderToggle();
  745. if (this.activePage == 'TextPage') {
  746. switch (event.code) {
  747. case 'KeyH':
  748. case 'F1':
  749. this.helpToggle();
  750. event.preventDefault();
  751. event.stopPropagation();
  752. break;
  753. case 'KeyZ':
  754. this.scrollingToggle();
  755. break;
  756. case 'KeyP':
  757. this.setPositionToggle();
  758. break;
  759. case 'KeyF':
  760. if (event.ctrlKey) {
  761. this.searchToggle();
  762. event.preventDefault();
  763. event.stopPropagation();
  764. }
  765. break;
  766. case 'KeyC':
  767. if (event.ctrlKey) {
  768. this.copyTextToggle();
  769. event.preventDefault();
  770. event.stopPropagation();
  771. }
  772. break;
  773. case 'KeyR':
  774. this.refreshBook();
  775. break;
  776. case 'KeyX':
  777. this.historyToggle();
  778. event.preventDefault();
  779. event.stopPropagation();
  780. break;
  781. case 'KeyS':
  782. this.settingsToggle();
  783. break;
  784. }
  785. }
  786. }
  787. }
  788. }
  789. }
  790. //-----------------------------------------------------------------------------
  791. </script>
  792. <style scoped>
  793. .el-container {
  794. padding: 0;
  795. margin: 0;
  796. height: 100%;
  797. }
  798. .el-header {
  799. padding-left: 5px;
  800. padding-right: 5px;
  801. background-color: #1B695F;
  802. color: #000;
  803. overflow-x: auto;
  804. overflow-y: hidden;
  805. }
  806. .header {
  807. display: flex;
  808. justify-content: space-between;
  809. min-width: 550px;
  810. }
  811. .el-main {
  812. position: relative;
  813. display: flex;
  814. padding: 0;
  815. margin: 0;
  816. background-color: #EBE2C9;
  817. color: #000;
  818. }
  819. .tool-button {
  820. margin: 0 2px 0 2px;
  821. padding: 0;
  822. color: #3E843E;
  823. background-color: #E6EDF4;
  824. margin-top: 5px;
  825. height: 38px;
  826. width: 38px;
  827. border: 0;
  828. box-shadow: 3px 3px 5px black;
  829. }
  830. .tool-button:hover {
  831. background-color: white;
  832. }
  833. .tool-button-active {
  834. box-shadow: 0 0 0;
  835. color: white;
  836. background-color: #8AB45F;
  837. position: relative;
  838. top: 1px;
  839. left: 1px;
  840. }
  841. .tool-button-active:hover {
  842. color: white;
  843. background-color: #81C581;
  844. }
  845. .tool-button-disabled {
  846. color: lightgray;
  847. background-color: gray;
  848. }
  849. .tool-button-disabled:hover {
  850. color: lightgray;
  851. background-color: gray;
  852. }
  853. i {
  854. font-size: 200%;
  855. }
  856. .space {
  857. width: 10px;
  858. display: inline-block;
  859. }
  860. .clear {
  861. color: rgba(0,0,0,0);
  862. }
  863. </style>