Reader.vue 31 KB

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