Reader.vue 31 KB

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