Reader.vue 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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. switch (button) {
  390. case 'loader':
  391. this.loaderToggle();
  392. break;
  393. case 'undoAction':
  394. if (this.actionCur > 0) {
  395. this.actionCur--;
  396. this.bookPosChanged({bookPos: this.actionList[this.actionCur]});
  397. }
  398. break;
  399. case 'redoAction':
  400. if (this.actionCur < this.actionList.length - 1) {
  401. this.actionCur++;
  402. this.bookPosChanged({bookPos: this.actionList[this.actionCur]});
  403. }
  404. break;
  405. case 'fullScreen':
  406. this.fullScreenToggle();
  407. break;
  408. case 'setPosition':
  409. this.setPositionToggle();
  410. break;
  411. case 'scrolling':
  412. this.scrollingToggle();
  413. break;
  414. case 'search':
  415. this.searchToggle();
  416. break;
  417. case 'copyText':
  418. this.copyTextToggle();
  419. break;
  420. case 'history':
  421. this.historyToggle();
  422. break;
  423. case 'refresh':
  424. if (this.mostRecentBook()) {
  425. this.loadBook({url: this.mostRecentBook().url, force: true});
  426. }
  427. break;
  428. case 'settings':
  429. this.settingsToggle();
  430. break;
  431. }
  432. this.$refs[button].$el.blur();
  433. }
  434. buttonActiveClass(button) {
  435. const classActive = { 'tool-button-active': true, 'tool-button-active:hover': true };
  436. const classDisabled = { 'tool-button-disabled': true, 'tool-button-disabled:hover': true };
  437. let classResult = {};
  438. switch (button) {
  439. case 'loader':
  440. case 'fullScreen':
  441. case 'setPosition':
  442. case 'scrolling':
  443. case 'search':
  444. case 'copyText':
  445. case 'history':
  446. case 'settings':
  447. if (this[`${button}Active`])
  448. classResult = classActive;
  449. break;
  450. }
  451. switch (button) {
  452. case 'undoAction':
  453. if (this.actionCur <= 0)
  454. classResult = classDisabled;
  455. break;
  456. case 'redoAction':
  457. if (this.actionCur == this.actionList.length - 1)
  458. classResult = classDisabled;
  459. break;
  460. }
  461. if (this.activePage == 'LoaderPage' || !this.mostRecentBook()) {
  462. switch (button) {
  463. case 'undoAction':
  464. case 'redoAction':
  465. case 'setPosition':
  466. case 'scrolling':
  467. case 'search':
  468. case 'copyText':
  469. classResult = classDisabled;
  470. break;
  471. case 'history':
  472. case 'refresh':
  473. if (!this.mostRecentBook())
  474. classResult = classDisabled;
  475. break;
  476. }
  477. }
  478. return classResult;
  479. }
  480. async acivateClickMapPage() {
  481. if (this.showClickMapPage && !this.clickMapActive) {
  482. this.clickMapActive = true;
  483. await this.$refs.clickMapPage.slowDisappear();
  484. this.clickMapActive = false;
  485. }
  486. }
  487. get activePage() {
  488. let result = '';
  489. if (this.progressActive)
  490. result = 'ProgressPage';
  491. else if (this.loaderActive)
  492. result = 'LoaderPage';
  493. else if (this.mostRecentBookReactive)
  494. result = 'TextPage';
  495. if (!result && !this.loading) {
  496. this.loaderActive = true;
  497. result = 'LoaderPage';
  498. }
  499. if (result != 'TextPage') {
  500. this.$root.$emit('set-app-title');
  501. }
  502. if (this.lastActivePage != result && result == 'TextPage') {
  503. //акивируем страницу с текстом
  504. this.$nextTick(async() => {
  505. const last = this.mostRecentBookReactive;
  506. const isParsed = bookManager.hasBookParsed(last);
  507. if (!isParsed) {
  508. this.$root.$emit('set-app-title');
  509. return;
  510. }
  511. this.updateRoute();
  512. const textPage = this.$refs.page;
  513. if (textPage.showBook) {
  514. textPage.lastBook = last;
  515. textPage.bookPos = (last.bookPos !== undefined ? last.bookPos : 0);
  516. textPage.showBook();
  517. }
  518. });
  519. }
  520. this.lastActivePage = result;
  521. return result;
  522. }
  523. loadBook(opts) {
  524. if (!opts) {
  525. this.mostRecentBook();
  526. return;
  527. }
  528. // уже просматривается сейчас
  529. const lastBook = (this.$refs.page ? this.$refs.page.lastBook : null);
  530. if (!opts.force && lastBook && lastBook.url == opts.url && bookManager.hasBookParsed(lastBook)) {
  531. this.loaderActive = false;
  532. return;
  533. }
  534. this.progressActive = true;
  535. this.$nextTick(async() => {
  536. const progress = this.$refs.page;
  537. this.actionList = [];
  538. this.actionCur = -1;
  539. try {
  540. progress.show();
  541. progress.setState({state: 'parse'});
  542. // есть ли среди недавних
  543. const key = bookManager.keyFromUrl(opts.url);
  544. let wasOpened = await bookManager.getRecentBook({key});
  545. wasOpened = (wasOpened ? wasOpened : {});
  546. const bookPos = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPos);
  547. const bookPosSeen = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPosSeen);
  548. const bookPosPercent = wasOpened.bookPosPercent;
  549. let book = null;
  550. if (!opts.force) {
  551. // пытаемся загрузить и распарсить книгу в менеджере из локального кэша
  552. const bookParsed = await bookManager.getBook({url: opts.url}, (prog) => {
  553. progress.setState({progress: prog});
  554. });
  555. // если есть в локальном кэше
  556. if (bookParsed) {
  557. await bookManager.setRecentBook(Object.assign({bookPos, bookPosSeen, bookPosPercent}, bookManager.metaOnly(bookParsed)));
  558. this.mostRecentBook();
  559. this.addAction(bookPos);
  560. this.loaderActive = false;
  561. progress.hide(); this.progressActive = false;
  562. this.blinkCachedLoadMessage();
  563. await this.acivateClickMapPage();
  564. return;
  565. }
  566. // иначе идем на сервер
  567. // пытаемся загрузить готовый файл с сервера
  568. if (wasOpened.path) {
  569. try {
  570. const resp = await readerApi.loadCachedBook(wasOpened.path, (state) => {
  571. progress.setState(state);
  572. });
  573. book = Object.assign({}, wasOpened, {data: resp.data});
  574. } catch (e) {
  575. //молчим
  576. }
  577. }
  578. }
  579. progress.setState({totalSteps: 5});
  580. // не удалось, скачиваем книгу полностью с конвертацией
  581. let loadCached = true;
  582. if (!book) {
  583. book = await readerApi.loadBook(opts.url, (state) => {
  584. progress.setState(state);
  585. });
  586. loadCached = false;
  587. }
  588. // добавляем в bookManager
  589. progress.setState({state: 'parse', step: 5});
  590. const addedBook = await bookManager.addBook(book, (prog) => {
  591. progress.setState({progress: prog});
  592. });
  593. // добавляем в историю
  594. await bookManager.setRecentBook(Object.assign({bookPos, bookPosSeen, bookPosPercent}, bookManager.metaOnly(addedBook)));
  595. this.mostRecentBook();
  596. this.addAction(bookPos);
  597. this.updateRoute(true);
  598. this.loaderActive = false;
  599. progress.hide(); this.progressActive = false;
  600. if (loadCached) {
  601. this.blinkCachedLoadMessage();
  602. } else
  603. this.stopBlink = true;
  604. await this.acivateClickMapPage();
  605. } catch (e) {
  606. progress.hide(); this.progressActive = false;
  607. this.loaderActive = true;
  608. this.$alert(e.message, 'Ошибка', {type: 'error'});
  609. }
  610. });
  611. }
  612. loadFile(opts) {
  613. this.progressActive = true;
  614. this.$nextTick(async() => {
  615. const progress = this.$refs.page;
  616. try {
  617. progress.show();
  618. progress.setState({state: 'upload'});
  619. const url = await readerApi.uploadFile(opts.file, (state) => {
  620. progress.setState(state);
  621. });
  622. progress.hide(); this.progressActive = false;
  623. this.loadBook({url});
  624. } catch (e) {
  625. progress.hide(); this.progressActive = false;
  626. this.loaderActive = true;
  627. this.$alert(e.message, 'Ошибка', {type: 'error'});
  628. }
  629. });
  630. }
  631. blinkCachedLoadMessage() {
  632. this.blinkCount = 30;
  633. if (!this.inBlink) {
  634. this.inBlink = true;
  635. this.stopBlink = false;
  636. this.$nextTick(async() => {
  637. let page = this.$refs.page;
  638. while (this.blinkCount) {
  639. this.showRefreshIcon = !this.showRefreshIcon;
  640. if (page.blinkCachedLoadMessage)
  641. page.blinkCachedLoadMessage(this.showRefreshIcon);
  642. await sleep(500);
  643. if (this.stopBlink)
  644. break;
  645. this.blinkCount--;
  646. page = this.$refs.page;
  647. }
  648. this.showRefreshIcon = true;
  649. this.inBlink = false;
  650. if (page.blinkCachedLoadMessage)
  651. page.blinkCachedLoadMessage('finish');
  652. });
  653. }
  654. }
  655. keyHook(event) {
  656. if (this.$root.rootRoute == '/reader') {
  657. let handled = false;
  658. if (!handled && this.helpActive)
  659. handled = this.$refs.helpPage.keyHook(event);
  660. if (!handled && this.settingsActive)
  661. handled = this.$refs.settingsPage.keyHook(event);
  662. if (!handled && this.historyActive)
  663. handled = this.$refs.historyPage.keyHook(event);
  664. if (!handled && this.setPositionActive)
  665. handled = this.$refs.setPositionPage.keyHook(event);
  666. if (!handled && this.searchActive)
  667. handled = this.$refs.searchPage.keyHook(event);
  668. if (!handled && this.copyTextActive)
  669. handled = this.$refs.copyTextPage.keyHook(event);
  670. if (!handled && this.$refs.page && this.$refs.page.keyHook)
  671. handled = this.$refs.page.keyHook(event);
  672. if (!handled && event.type == 'keydown') {
  673. if (event.code == 'Escape')
  674. this.loaderToggle();
  675. if (this.activePage == 'TextPage') {
  676. switch (event.code) {
  677. case 'KeyH':
  678. case 'F1':
  679. this.helpToggle();
  680. event.preventDefault();
  681. event.stopPropagation();
  682. break;
  683. case 'KeyP':
  684. this.setPositionToggle();
  685. break;
  686. case 'KeyF':
  687. if (event.ctrlKey) {
  688. this.searchToggle();
  689. event.preventDefault();
  690. event.stopPropagation();
  691. }
  692. break;
  693. case 'KeyC':
  694. if (event.ctrlKey) {
  695. this.copyTextToggle();
  696. event.preventDefault();
  697. event.stopPropagation();
  698. }
  699. break;
  700. case 'KeyZ':
  701. this.scrollingToggle();
  702. break;
  703. case 'KeyX':
  704. this.historyToggle();
  705. break;
  706. case 'KeyS':
  707. this.settingsToggle();
  708. break;
  709. }
  710. }
  711. }
  712. }
  713. }
  714. }
  715. //-----------------------------------------------------------------------------
  716. </script>
  717. <style scoped>
  718. .el-container {
  719. padding: 0;
  720. margin: 0;
  721. height: 100%;
  722. }
  723. .el-header {
  724. padding-left: 5px;
  725. padding-right: 5px;
  726. background-color: #1B695F;
  727. color: #000;
  728. overflow-x: auto;
  729. overflow-y: hidden;
  730. }
  731. .header {
  732. display: flex;
  733. justify-content: space-between;
  734. min-width: 550px;
  735. }
  736. .el-main {
  737. position: relative;
  738. display: flex;
  739. padding: 0;
  740. margin: 0;
  741. background-color: #EBE2C9;
  742. color: #000;
  743. }
  744. .tool-button {
  745. margin: 0 2px 0 2px;
  746. padding: 0;
  747. color: #3E843E;
  748. background-color: #E6EDF4;
  749. margin-top: 5px;
  750. height: 38px;
  751. width: 38px;
  752. border: 0;
  753. box-shadow: 3px 3px 5px black;
  754. }
  755. .tool-button:hover {
  756. background-color: white;
  757. }
  758. .tool-button-active {
  759. box-shadow: 0 0 0;
  760. color: white;
  761. background-color: #8AB45F;
  762. position: relative;
  763. top: 1px;
  764. left: 1px;
  765. }
  766. .tool-button-active:hover {
  767. color: white;
  768. background-color: #81C581;
  769. }
  770. .tool-button-disabled {
  771. color: lightgray;
  772. background-color: gray;
  773. }
  774. .tool-button-disabled:hover {
  775. color: lightgray;
  776. background-color: gray;
  777. }
  778. i {
  779. font-size: 200%;
  780. }
  781. .space {
  782. width: 10px;
  783. display: inline-block;
  784. }
  785. .clear {
  786. color: rgba(0,0,0,0);
  787. }
  788. </style>