Reader.vue 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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 || !opts.url) {
  527. this.mostRecentBook();
  528. return;
  529. }
  530. let url = opts.url;
  531. if ((url.indexOf('http://') != 0) && (url.indexOf('https://') != 0) &&
  532. (url.indexOf('file://') != 0))
  533. url = 'http://' + url;
  534. // уже просматривается сейчас
  535. const lastBook = (this.$refs.page ? this.$refs.page.lastBook : null);
  536. if (!opts.force && lastBook && lastBook.url == url && bookManager.hasBookParsed(lastBook)) {
  537. this.loaderActive = false;
  538. return;
  539. }
  540. this.progressActive = true;
  541. this.$nextTick(async() => {
  542. const progress = this.$refs.page;
  543. this.actionList = [];
  544. this.actionCur = -1;
  545. try {
  546. progress.show();
  547. progress.setState({state: 'parse'});
  548. // есть ли среди недавних
  549. const key = bookManager.keyFromUrl(url);
  550. let wasOpened = await bookManager.getRecentBook({key});
  551. wasOpened = (wasOpened ? wasOpened : {});
  552. const bookPos = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPos);
  553. const bookPosSeen = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPosSeen);
  554. const bookPosPercent = wasOpened.bookPosPercent;
  555. let book = null;
  556. if (!opts.force) {
  557. // пытаемся загрузить и распарсить книгу в менеджере из локального кэша
  558. const bookParsed = await bookManager.getBook({url}, (prog) => {
  559. progress.setState({progress: prog});
  560. });
  561. // если есть в локальном кэше
  562. if (bookParsed) {
  563. await bookManager.setRecentBook(Object.assign({bookPos, bookPosSeen, bookPosPercent}, bookManager.metaOnly(bookParsed)));
  564. this.mostRecentBook();
  565. this.addAction(bookPos);
  566. this.loaderActive = false;
  567. progress.hide(); this.progressActive = false;
  568. this.blinkCachedLoadMessage();
  569. await this.acivateClickMapPage();
  570. return;
  571. }
  572. // иначе идем на сервер
  573. // пытаемся загрузить готовый файл с сервера
  574. if (wasOpened.path) {
  575. try {
  576. const resp = await readerApi.loadCachedBook(wasOpened.path, (state) => {
  577. progress.setState(state);
  578. });
  579. book = Object.assign({}, wasOpened, {data: resp.data});
  580. } catch (e) {
  581. //молчим
  582. }
  583. }
  584. }
  585. progress.setState({totalSteps: 5});
  586. // не удалось, скачиваем книгу полностью с конвертацией
  587. let loadCached = true;
  588. if (!book) {
  589. book = await readerApi.loadBook(url, (state) => {
  590. progress.setState(state);
  591. });
  592. loadCached = false;
  593. }
  594. // добавляем в bookManager
  595. progress.setState({state: 'parse', step: 5});
  596. const addedBook = await bookManager.addBook(book, (prog) => {
  597. progress.setState({progress: prog});
  598. });
  599. // добавляем в историю
  600. await bookManager.setRecentBook(Object.assign({bookPos, bookPosSeen, bookPosPercent}, bookManager.metaOnly(addedBook)));
  601. this.mostRecentBook();
  602. this.addAction(bookPos);
  603. this.updateRoute(true);
  604. this.loaderActive = false;
  605. progress.hide(); this.progressActive = false;
  606. if (loadCached) {
  607. this.blinkCachedLoadMessage();
  608. } else
  609. this.stopBlink = true;
  610. await this.acivateClickMapPage();
  611. } catch (e) {
  612. progress.hide(); this.progressActive = false;
  613. this.loaderActive = true;
  614. this.$alert(e.message, 'Ошибка', {type: 'error'});
  615. }
  616. });
  617. }
  618. loadFile(opts) {
  619. this.progressActive = true;
  620. this.$nextTick(async() => {
  621. const progress = this.$refs.page;
  622. try {
  623. progress.show();
  624. progress.setState({state: 'upload'});
  625. const url = await readerApi.uploadFile(opts.file, (state) => {
  626. progress.setState(state);
  627. });
  628. progress.hide(); this.progressActive = false;
  629. this.loadBook({url});
  630. } catch (e) {
  631. progress.hide(); this.progressActive = false;
  632. this.loaderActive = true;
  633. this.$alert(e.message, 'Ошибка', {type: 'error'});
  634. }
  635. });
  636. }
  637. blinkCachedLoadMessage() {
  638. this.blinkCount = 30;
  639. if (!this.inBlink) {
  640. this.inBlink = true;
  641. this.stopBlink = false;
  642. this.$nextTick(async() => {
  643. let page = this.$refs.page;
  644. while (this.blinkCount) {
  645. this.showRefreshIcon = !this.showRefreshIcon;
  646. if (page.blinkCachedLoadMessage)
  647. page.blinkCachedLoadMessage(this.showRefreshIcon);
  648. await sleep(500);
  649. if (this.stopBlink)
  650. break;
  651. this.blinkCount--;
  652. page = this.$refs.page;
  653. }
  654. this.showRefreshIcon = true;
  655. this.inBlink = false;
  656. if (page.blinkCachedLoadMessage)
  657. page.blinkCachedLoadMessage('finish');
  658. });
  659. }
  660. }
  661. keyHook(event) {
  662. if (this.$root.rootRoute == '/reader') {
  663. let handled = false;
  664. if (!handled && this.helpActive)
  665. handled = this.$refs.helpPage.keyHook(event);
  666. if (!handled && this.settingsActive)
  667. handled = this.$refs.settingsPage.keyHook(event);
  668. if (!handled && this.historyActive)
  669. handled = this.$refs.historyPage.keyHook(event);
  670. if (!handled && this.setPositionActive)
  671. handled = this.$refs.setPositionPage.keyHook(event);
  672. if (!handled && this.searchActive)
  673. handled = this.$refs.searchPage.keyHook(event);
  674. if (!handled && this.copyTextActive)
  675. handled = this.$refs.copyTextPage.keyHook(event);
  676. if (!handled && this.$refs.page && this.$refs.page.keyHook)
  677. handled = this.$refs.page.keyHook(event);
  678. if (!handled && event.type == 'keydown') {
  679. if (event.code == 'Escape')
  680. this.loaderToggle();
  681. if (this.activePage == 'TextPage') {
  682. switch (event.code) {
  683. case 'KeyH':
  684. case 'F1':
  685. this.helpToggle();
  686. event.preventDefault();
  687. event.stopPropagation();
  688. break;
  689. case 'KeyP':
  690. this.setPositionToggle();
  691. break;
  692. case 'KeyF':
  693. if (event.ctrlKey) {
  694. this.searchToggle();
  695. event.preventDefault();
  696. event.stopPropagation();
  697. }
  698. break;
  699. case 'KeyC':
  700. if (event.ctrlKey) {
  701. this.copyTextToggle();
  702. event.preventDefault();
  703. event.stopPropagation();
  704. }
  705. break;
  706. case 'KeyZ':
  707. this.scrollingToggle();
  708. break;
  709. case 'KeyX':
  710. this.historyToggle();
  711. break;
  712. case 'KeyS':
  713. this.settingsToggle();
  714. break;
  715. }
  716. }
  717. }
  718. }
  719. }
  720. }
  721. //-----------------------------------------------------------------------------
  722. </script>
  723. <style scoped>
  724. .el-container {
  725. padding: 0;
  726. margin: 0;
  727. height: 100%;
  728. }
  729. .el-header {
  730. padding-left: 5px;
  731. padding-right: 5px;
  732. background-color: #1B695F;
  733. color: #000;
  734. overflow-x: auto;
  735. overflow-y: hidden;
  736. }
  737. .header {
  738. display: flex;
  739. justify-content: space-between;
  740. min-width: 550px;
  741. }
  742. .el-main {
  743. position: relative;
  744. display: flex;
  745. padding: 0;
  746. margin: 0;
  747. background-color: #EBE2C9;
  748. color: #000;
  749. }
  750. .tool-button {
  751. margin: 0 2px 0 2px;
  752. padding: 0;
  753. color: #3E843E;
  754. background-color: #E6EDF4;
  755. margin-top: 5px;
  756. height: 38px;
  757. width: 38px;
  758. border: 0;
  759. box-shadow: 3px 3px 5px black;
  760. }
  761. .tool-button:hover {
  762. background-color: white;
  763. }
  764. .tool-button-active {
  765. box-shadow: 0 0 0;
  766. color: white;
  767. background-color: #8AB45F;
  768. position: relative;
  769. top: 1px;
  770. left: 1px;
  771. }
  772. .tool-button-active:hover {
  773. color: white;
  774. background-color: #81C581;
  775. }
  776. .tool-button-disabled {
  777. color: lightgray;
  778. background-color: gray;
  779. }
  780. .tool-button-disabled:hover {
  781. color: lightgray;
  782. background-color: gray;
  783. }
  784. i {
  785. font-size: 200%;
  786. }
  787. .space {
  788. width: 10px;
  789. display: inline-block;
  790. }
  791. .clear {
  792. color: rgba(0,0,0,0);
  793. }
  794. </style>