Reader.vue 45 KB

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