Reader.vue 36 KB

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