Reader.vue 44 KB

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