Reader.vue 32 KB

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