Reader.vue 46 KB

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