Reader.vue 31 KB

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