Reader.vue 32 KB

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