Reader.vue 37 KB

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