Reader.vue 37 KB

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