Reader.vue 37 KB

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