Reader.vue 46 KB

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