Reader.vue 46 KB

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