Reader.vue 33 KB

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