Reader.vue 36 KB

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