Reader.vue 45 KB

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