Reader.vue 36 KB

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