Reader.vue 38 KB

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