Reader.vue 37 KB

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