Reader.vue 45 KB

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