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