Reader.vue 39 KB

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