Reader.vue 33 KB

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