Reader.vue 45 KB

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