Reader.vue 43 KB

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