Reader.vue 32 KB

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