Reader.vue 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  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="mode == 'liberama.top' && 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" @libs-close="libsClose" @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. libsClose() {
  617. if (this.libsActive)
  618. this.libsToogle();
  619. }
  620. libsToogle() {
  621. this.libsActive = !this.libsActive;
  622. if (this.libsActive) {
  623. this.$refs.libsPage.init();
  624. } else {
  625. this.$refs.libsPage.done();
  626. }
  627. }
  628. offlineModeToggle() {
  629. this.offlineModeActive = !this.offlineModeActive;
  630. this.$refs.serverStorage.offlineModeActive = this.offlineModeActive;
  631. }
  632. settingsToggle() {
  633. this.settingsActive = !this.settingsActive;
  634. if (this.settingsActive) {
  635. this.closeAllWindows();
  636. this.settingsActive = true;
  637. this.$nextTick(() => {
  638. this.$refs.settingsPage.init();
  639. });
  640. } else {
  641. this.settingsActive = false;
  642. }
  643. }
  644. helpToggle() {
  645. this.helpActive = !this.helpActive;
  646. if (this.helpActive) {
  647. this.closeAllWindows();
  648. this.helpActive = true;
  649. }
  650. }
  651. donateToggle() {
  652. this.helpToggle();
  653. if (this.helpActive) {
  654. this.$nextTick(() => {
  655. this.$refs.helpPage.activateDonateHelpPage();
  656. });
  657. }
  658. }
  659. versionHistoryToggle() {
  660. this.helpToggle();
  661. if (this.helpActive) {
  662. this.$nextTick(() => {
  663. this.$refs.helpPage.activateVersionHistoryHelpPage();
  664. });
  665. }
  666. }
  667. refreshBook() {
  668. if (this.mostRecentBook()) {
  669. this.loadBook({url: this.mostRecentBook().url, force: true});
  670. }
  671. }
  672. undoAction() {
  673. if (this.actionCur > 0) {
  674. this.actionCur--;
  675. this.bookPosChanged({bookPos: this.actionList[this.actionCur]});
  676. }
  677. }
  678. redoAction() {
  679. if (this.actionCur < this.actionList.length - 1) {
  680. this.actionCur++;
  681. this.bookPosChanged({bookPos: this.actionList[this.actionCur]});
  682. }
  683. }
  684. buttonClick(action) {
  685. const activeClass = this.buttonActiveClass(action);
  686. this.$refs[action].blur();
  687. if (activeClass['tool-button-disabled'])
  688. return;
  689. this.doAction({action});
  690. }
  691. buttonActiveClass(action) {
  692. const classActive = { 'tool-button-active': true, 'tool-button-active:hover': true };
  693. const classDisabled = { 'tool-button-disabled': true, 'tool-button-disabled:hover': true };
  694. let classResult = {};
  695. switch (action) {
  696. case 'loader':
  697. case 'fullScreen':
  698. case 'setPosition':
  699. case 'scrolling':
  700. case 'search':
  701. case 'copyText':
  702. case 'refresh':
  703. case 'libs':
  704. case 'recentBooks':
  705. case 'offlineMode':
  706. case 'settings':
  707. if (this.progressActive) {
  708. classResult = classDisabled;
  709. } else if (this[`${action}Active`]) {
  710. classResult = classActive;
  711. }
  712. break;
  713. case 'undoAction':
  714. if (this.actionCur <= 0)
  715. classResult = classDisabled;
  716. break;
  717. case 'redoAction':
  718. if (this.actionCur == this.actionList.length - 1)
  719. classResult = classDisabled;
  720. break;
  721. }
  722. if (this.activePage == 'LoaderPage' || !this.mostRecentBookReactive) {
  723. switch (action) {
  724. case 'undoAction':
  725. case 'redoAction':
  726. case 'setPosition':
  727. case 'scrolling':
  728. case 'search':
  729. case 'copyText':
  730. classResult = classDisabled;
  731. break;
  732. case 'recentBooks':
  733. case 'refresh':
  734. if (!this.mostRecentBookReactive)
  735. classResult = classDisabled;
  736. break;
  737. }
  738. }
  739. return classResult;
  740. }
  741. async activateClickMapPage() {
  742. if (this.clickControl && this.showClickMapPage && !this.clickMapActive) {
  743. this.clickMapActive = true;
  744. await this.$refs.clickMapPage.slowDisappear();
  745. this.clickMapActive = false;
  746. }
  747. }
  748. get activePage() {
  749. let result = '';
  750. if (this.progressActive)
  751. result = 'ProgressPage';
  752. else if (this.loaderActive)
  753. result = 'LoaderPage';
  754. else if (this.mostRecentBookReactive)
  755. result = 'TextPage';
  756. if (!result && !this.loading) {
  757. this.loaderActive = true;
  758. result = 'LoaderPage';
  759. }
  760. if (result != 'TextPage') {
  761. this.$root.$emit('set-app-title');
  762. }
  763. // на LoaderPage всегда показываем toolBar
  764. if (result == 'LoaderPage' && !this.toolBarActive) {
  765. this.toolBarToggle();
  766. }
  767. if (this.lastActivePage != result && result == 'TextPage') {
  768. //акивируем страницу с текстом
  769. this.$nextTick(async() => {
  770. const last = this.mostRecentBookReactive;
  771. const isParsed = await bookManager.hasBookParsed(last);
  772. if (!isParsed) {
  773. this.$root.$emit('set-app-title');
  774. return;
  775. }
  776. this.updateRoute();
  777. const textPage = this.$refs.page;
  778. if (textPage.showBook) {
  779. this.textPage = textPage;
  780. textPage.lastBook = last;
  781. textPage.bookPos = (last.bookPos !== undefined ? last.bookPos : 0);
  782. textPage.showBook();
  783. }
  784. });
  785. }
  786. this.lastActivePage = result;
  787. return result;
  788. }
  789. async loadBook(opts) {
  790. if (!opts || !opts.url) {
  791. this.mostRecentBook();
  792. return;
  793. }
  794. this.closeAllWindows();
  795. let url = encodeURI(decodeURI(opts.url));
  796. if ((url.indexOf('http://') != 0) && (url.indexOf('https://') != 0) &&
  797. (url.indexOf('file://') != 0))
  798. url = 'http://' + url;
  799. // уже просматривается сейчас
  800. const lastBook = (this.textPage ? this.textPage.lastBook : null);
  801. if (!opts.force && lastBook && lastBook.url == url &&
  802. (!opts.path || opts.path == lastBook.path) &&
  803. await bookManager.hasBookParsed(lastBook)) {
  804. this.loaderActive = false;
  805. return;
  806. }
  807. this.progressActive = true;
  808. await this.$nextTick();
  809. const progress = this.$refs.page;
  810. this.actionList = [];
  811. this.actionCur = -1;
  812. try {
  813. progress.show();
  814. progress.setState({state: 'parse'});
  815. // есть ли среди недавних
  816. const key = bookManager.keyFromUrl(url);
  817. let wasOpened = await bookManager.getRecentBook({key});
  818. wasOpened = (wasOpened ? wasOpened : {});
  819. const bookPos = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPos);
  820. const bookPosSeen = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPosSeen);
  821. let book = null;
  822. if (!opts.force) {
  823. // пытаемся загрузить и распарсить книгу в менеджере из локального кэша
  824. const bookParsed = await bookManager.getBook({url, path: opts.path}, (prog) => {
  825. progress.setState({progress: prog});
  826. });
  827. // если есть в локальном кэше
  828. if (bookParsed) {
  829. await bookManager.setRecentBook(Object.assign({bookPos, bookPosSeen}, bookParsed));
  830. this.mostRecentBook();
  831. this.addAction(bookPos);
  832. this.loaderActive = false;
  833. progress.hide(); this.progressActive = false;
  834. this.blinkCachedLoadMessage();
  835. this.checkBookPosPercent();
  836. await this.activateClickMapPage();
  837. return;
  838. }
  839. // иначе идем на сервер
  840. // пытаемся загрузить готовый файл с сервера
  841. if (wasOpened.path) {
  842. progress.setState({totalSteps: 5});
  843. try {
  844. const resp = await readerApi.loadCachedBook(wasOpened.path, (state) => {
  845. progress.setState(state);
  846. });
  847. book = Object.assign({}, wasOpened, {data: resp.data});
  848. } catch (e) {
  849. //молчим
  850. }
  851. }
  852. }
  853. progress.setState({totalSteps: 5});
  854. // не удалось, скачиваем книгу полностью с конвертацией
  855. let loadCached = true;
  856. if (!book) {
  857. book = await readerApi.loadBook({url, enableSitesFilter: this.enableSitesFilter}, (state) => {
  858. progress.setState(state);
  859. });
  860. loadCached = false;
  861. }
  862. // добавляем в bookManager
  863. progress.setState({state: 'parse', step: 5});
  864. const addedBook = await bookManager.addBook(book, (prog) => {
  865. progress.setState({progress: prog});
  866. });
  867. // добавляем в историю
  868. await bookManager.setRecentBook(Object.assign({bookPos, bookPosSeen}, addedBook));
  869. this.mostRecentBook();
  870. this.addAction(bookPos);
  871. this.updateRoute(true);
  872. this.loaderActive = false;
  873. progress.hide(); this.progressActive = false;
  874. if (loadCached) {
  875. this.blinkCachedLoadMessage();
  876. } else
  877. this.stopBlink = true;
  878. this.checkBookPosPercent();
  879. await this.activateClickMapPage();
  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. async loadFile(opts) {
  887. this.progressActive = true;
  888. await this.$nextTick();
  889. const progress = this.$refs.page;
  890. try {
  891. progress.show();
  892. progress.setState({state: 'upload'});
  893. const url = await readerApi.uploadFile(opts.file, this.config.maxUploadFileSize, (state) => {
  894. progress.setState(state);
  895. });
  896. progress.hide(); this.progressActive = false;
  897. await this.loadBook({url});
  898. } catch (e) {
  899. progress.hide(); this.progressActive = false;
  900. this.loaderActive = true;
  901. this.$root.stdDialog.alert(e.message, 'Ошибка', {color: 'negative'});
  902. }
  903. }
  904. blinkCachedLoadMessage() {
  905. if (!this.blinkCachedLoad)
  906. return;
  907. this.blinkCount = 30;
  908. if (!this.inBlink) {
  909. this.inBlink = true;
  910. this.stopBlink = false;
  911. this.$nextTick(async() => {
  912. let page = this.$refs.page;
  913. while (this.blinkCount) {
  914. this.showRefreshIcon = !this.showRefreshIcon;
  915. if (page && page.blinkCachedLoadMessage)
  916. page.blinkCachedLoadMessage(this.showRefreshIcon);
  917. await utils.sleep(500);
  918. if (this.stopBlink)
  919. break;
  920. this.blinkCount--;
  921. page = this.$refs.page;
  922. }
  923. this.showRefreshIcon = true;
  924. this.inBlink = false;
  925. if (page && page.blinkCachedLoadMessage)
  926. page.blinkCachedLoadMessage('finish');
  927. });
  928. }
  929. }
  930. doAction(opts) {
  931. let result = true;
  932. let {action = '', event = false} = opts;
  933. switch (action) {
  934. case 'loader':
  935. this.loaderToggle();
  936. break;
  937. case 'help':
  938. this.helpToggle();
  939. break;
  940. case 'undoAction':
  941. this.undoAction();
  942. break;
  943. case 'redoAction':
  944. this.redoAction();
  945. break;
  946. case 'fullScreen':
  947. this.fullScreenToggle();
  948. break;
  949. case 'scrolling':
  950. this.scrollingToggle();
  951. break;
  952. case 'stopScrolling':
  953. this.stopScrolling();
  954. break;
  955. case 'setPosition':
  956. this.setPositionToggle();
  957. break;
  958. case 'search':
  959. this.searchToggle();
  960. break;
  961. case 'copyText':
  962. this.copyTextToggle();
  963. break;
  964. case 'refresh':
  965. this.refreshBook();
  966. break;
  967. case 'libs':
  968. this.libsToogle();
  969. break;
  970. case 'recentBooks':
  971. this.recentBooksToggle();
  972. break;
  973. case 'offlineMode':
  974. this.offlineModeToggle();
  975. break;
  976. case 'settings':
  977. this.settingsToggle();
  978. break;
  979. case 'switchToolbar':
  980. this.toolBarToggle();
  981. break;
  982. case 'donate':
  983. this.donateToggle();
  984. break;
  985. default:
  986. result = false;
  987. break;
  988. }
  989. if (!result && this.activePage == 'TextPage' && this.$refs.page) {
  990. result = true;
  991. const textPage = this.$refs.page;
  992. switch (action) {
  993. case 'bookBegin':
  994. textPage.doHome();
  995. break;
  996. case 'bookEnd':
  997. textPage.doEnd();
  998. break;
  999. case 'pageBack':
  1000. textPage.doPageUp();
  1001. break;
  1002. case 'pageForward':
  1003. textPage.doPageDown();
  1004. break;
  1005. case 'lineBack':
  1006. textPage.doUp();
  1007. break;
  1008. case 'lineForward':
  1009. textPage.doDown();
  1010. break;
  1011. case 'incFontSize':
  1012. textPage.doFontSizeInc();
  1013. break;
  1014. case 'decFontSize':
  1015. textPage.doFontSizeDec();
  1016. break;
  1017. case 'scrollingSpeedUp':
  1018. textPage.doScrollingSpeedUp();
  1019. break;
  1020. case 'scrollingSpeedDown':
  1021. textPage.doScrollingSpeedDown();
  1022. break;
  1023. default:
  1024. result = false;
  1025. break;
  1026. }
  1027. }
  1028. if (result && event) {
  1029. event.preventDefault();
  1030. event.stopPropagation();
  1031. }
  1032. return result;
  1033. }
  1034. keyHook(event) {
  1035. let result = false;
  1036. if (this.$root.rootRoute() == '/reader') {
  1037. if (this.$root.stdDialog.active || this.$refs.dialog1.active || this.$refs.dialog2.active)
  1038. return result;
  1039. if (!result && this.helpActive)
  1040. result = this.$refs.helpPage.keyHook(event);
  1041. if (!result && this.settingsActive)
  1042. result = this.$refs.settingsPage.keyHook(event);
  1043. if (!result && this.recentBooksActive)
  1044. result = this.$refs.recentBooksPage.keyHook(event);
  1045. if (!result && this.setPositionActive)
  1046. result = this.$refs.setPositionPage.keyHook(event);
  1047. if (!result && this.searchActive)
  1048. result = this.$refs.searchPage.keyHook(event);
  1049. if (!result && this.copyTextActive)
  1050. result = this.$refs.copyTextPage.keyHook(event);
  1051. if (!result && this.$refs.page && this.$refs.page.keyHook)
  1052. result = this.$refs.page.keyHook(event);
  1053. if (!result && event.type == 'keydown') {
  1054. const action = this.$root.readerActionByKeyEvent(event);
  1055. if (action == 'loader') {
  1056. result = this.doAction({action, event});
  1057. }
  1058. if (!result && this.activePage == 'TextPage') {
  1059. result = this.doAction({action, event});
  1060. }
  1061. }
  1062. }
  1063. return result;
  1064. }
  1065. }
  1066. //-----------------------------------------------------------------------------
  1067. </script>
  1068. <style scoped>
  1069. .header {
  1070. padding-left: 5px;
  1071. padding-right: 5px;
  1072. background-color: #1B695F;
  1073. color: #000;
  1074. overflow: hidden;
  1075. height: 50px;
  1076. }
  1077. .main {
  1078. background-color: #EBE2C9;
  1079. color: #000;
  1080. }
  1081. .tool-button {
  1082. margin: 0px 2px 0 2px;
  1083. padding: 0;
  1084. color: #3E843E;
  1085. background-color: #E6EDF4;
  1086. margin-top: 5px;
  1087. height: 38px;
  1088. width: 38px;
  1089. border: 0;
  1090. border-radius: 6px;
  1091. box-shadow: 3px 3px 5px black;
  1092. outline: 0;
  1093. }
  1094. .tool-button:hover {
  1095. background-color: white;
  1096. cursor: pointer;
  1097. }
  1098. .tool-button-active {
  1099. box-shadow: 0 0 0;
  1100. color: white;
  1101. background-color: #8AB45F;
  1102. position: relative;
  1103. top: 1px;
  1104. left: 1px;
  1105. }
  1106. .tool-button-active:hover {
  1107. color: white;
  1108. background-color: #81C581;
  1109. cursor: pointer;
  1110. }
  1111. .tool-button-disabled {
  1112. color: lightgray;
  1113. background-color: gray;
  1114. cursor: default;
  1115. }
  1116. .tool-button-disabled:hover {
  1117. color: lightgray;
  1118. background-color: gray;
  1119. cursor: default;
  1120. }
  1121. .space {
  1122. width: 10px;
  1123. display: inline-block;
  1124. }
  1125. .clear {
  1126. color: rgba(0,0,0,0);
  1127. }
  1128. .clickable {
  1129. color: blue;
  1130. text-decoration: underline;
  1131. cursor: pointer;
  1132. }
  1133. .copy-icon {
  1134. cursor: pointer;
  1135. font-size: 120%;
  1136. color: blue;
  1137. }
  1138. </style>