Reader.vue 32 KB

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