Reader.vue 34 KB

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