Reader.vue 32 KB

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