Reader.vue 39 KB

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