Reader.vue 31 KB

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