Reader.vue 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  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. //TODO: убрать конвертирование 'file://' после 06.2021
  733. if (url.length == 71 && url.indexOf('file://') == 0)
  734. url = url.replace(/^file/, 'disk');
  735. if ((url.indexOf('http://') != 0) && (url.indexOf('https://') != 0) &&
  736. (url.indexOf('disk://') != 0))
  737. url = 'http://' + url;
  738. // уже просматривается сейчас
  739. const lastBook = (this.textPage ? this.textPage.lastBook : null);
  740. if (!opts.force && lastBook && lastBook.url == url &&
  741. (!opts.path || opts.path == lastBook.path) &&
  742. await bookManager.hasBookParsed(lastBook)) {
  743. this.loaderActive = false;
  744. return;
  745. }
  746. this.progressActive = true;
  747. await this.$nextTick();
  748. const progress = this.$refs.page;
  749. this.actionList = [];
  750. this.actionCur = -1;
  751. try {
  752. progress.show();
  753. progress.setState({state: 'parse'});
  754. // есть ли среди недавних
  755. const key = bookManager.keyFromUrl(url);
  756. let wasOpened = await bookManager.getRecentBook({key});
  757. wasOpened = (wasOpened ? wasOpened : {});
  758. const bookPos = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPos);
  759. const bookPosSeen = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPosSeen);
  760. let book = null;
  761. if (!opts.force) {
  762. // пытаемся загрузить и распарсить книгу в менеджере из локального кэша
  763. const bookParsed = await bookManager.getBook({url, path: opts.path}, (prog) => {
  764. progress.setState({progress: prog});
  765. });
  766. // если есть в локальном кэше
  767. if (bookParsed) {
  768. await bookManager.setRecentBook(Object.assign({bookPos, bookPosSeen}, bookParsed));
  769. this.mostRecentBook();
  770. this.addAction(bookPos);
  771. this.loaderActive = false;
  772. progress.hide(); this.progressActive = false;
  773. this.blinkCachedLoadMessage();
  774. this.checkBookPosPercent();
  775. await this.activateClickMapPage();
  776. return;
  777. }
  778. // иначе идем на сервер
  779. // пытаемся загрузить готовый файл с сервера
  780. if (wasOpened.path) {
  781. progress.setState({totalSteps: 5});
  782. try {
  783. const resp = await readerApi.loadCachedBook(wasOpened.path, (state) => {
  784. progress.setState(state);
  785. });
  786. book = Object.assign({}, wasOpened, {data: resp.data});
  787. } catch (e) {
  788. //молчим
  789. }
  790. }
  791. }
  792. progress.setState({totalSteps: 5});
  793. // не удалось, скачиваем книгу полностью с конвертацией
  794. let loadCached = true;
  795. if (!book) {
  796. book = await readerApi.loadBook({
  797. url,
  798. skipCheck: (opts.skipCheck ? true : false),
  799. isText: (opts.isText ? true : false),
  800. enableSitesFilter: this.enableSitesFilter
  801. },
  802. (state) => {
  803. progress.setState(state);
  804. }
  805. );
  806. loadCached = false;
  807. }
  808. // добавляем в bookManager
  809. progress.setState({state: 'parse', step: 5});
  810. const addedBook = await bookManager.addBook(book, (prog) => {
  811. progress.setState({progress: prog});
  812. });
  813. // добавляем в историю
  814. await bookManager.setRecentBook(Object.assign({bookPos, bookPosSeen}, addedBook));
  815. this.mostRecentBook();
  816. this.addAction(bookPos);
  817. this.updateRoute(true);
  818. this.loaderActive = false;
  819. progress.hide(); this.progressActive = false;
  820. if (loadCached) {
  821. this.blinkCachedLoadMessage();
  822. } else
  823. this.stopBlink = true;
  824. this.checkBookPosPercent();
  825. await this.activateClickMapPage();
  826. } catch (e) {
  827. progress.hide(); this.progressActive = false;
  828. this.loaderActive = true;
  829. this.$root.stdDialog.alert(e.message, 'Ошибка', {color: 'negative'});
  830. }
  831. }
  832. async loadFile(opts) {
  833. this.progressActive = true;
  834. await this.$nextTick();
  835. const progress = this.$refs.page;
  836. try {
  837. progress.show();
  838. progress.setState({state: 'upload'});
  839. const url = await readerApi.uploadFile(opts.file, this.config.maxUploadFileSize, (state) => {
  840. progress.setState(state);
  841. });
  842. progress.hide(); this.progressActive = false;
  843. await this.loadBook({url});
  844. } catch (e) {
  845. progress.hide(); this.progressActive = false;
  846. this.loaderActive = true;
  847. this.$root.stdDialog.alert(e.message, 'Ошибка', {color: 'negative'});
  848. }
  849. }
  850. blinkCachedLoadMessage() {
  851. if (!this.blinkCachedLoad)
  852. return;
  853. this.blinkCount = 30;
  854. if (!this.inBlink) {
  855. this.inBlink = true;
  856. this.stopBlink = false;
  857. this.$nextTick(async() => {
  858. let page = this.$refs.page;
  859. while (this.blinkCount) {
  860. this.showRefreshIcon = !this.showRefreshIcon;
  861. if (page && page.blinkCachedLoadMessage)
  862. page.blinkCachedLoadMessage(this.showRefreshIcon);
  863. await utils.sleep(500);
  864. if (this.stopBlink)
  865. break;
  866. this.blinkCount--;
  867. page = this.$refs.page;
  868. }
  869. this.showRefreshIcon = true;
  870. this.inBlink = false;
  871. if (page && page.blinkCachedLoadMessage)
  872. page.blinkCachedLoadMessage('finish');
  873. });
  874. }
  875. }
  876. doAction(opts) {
  877. let result = true;
  878. let {action = '', event = false} = opts;
  879. switch (action) {
  880. case 'loader':
  881. this.loaderToggle();
  882. break;
  883. case 'help':
  884. this.helpToggle();
  885. break;
  886. case 'undoAction':
  887. this.undoAction();
  888. break;
  889. case 'redoAction':
  890. this.redoAction();
  891. break;
  892. case 'fullScreen':
  893. this.fullScreenToggle();
  894. break;
  895. case 'scrolling':
  896. this.scrollingToggle();
  897. break;
  898. case 'stopScrolling':
  899. this.stopScrolling();
  900. break;
  901. case 'setPosition':
  902. this.setPositionToggle();
  903. break;
  904. case 'search':
  905. this.searchToggle();
  906. break;
  907. case 'copyText':
  908. this.copyTextToggle();
  909. break;
  910. case 'splitToPara':
  911. this.refreshBookSplitToPara();
  912. break;
  913. case 'refresh':
  914. this.refreshBook();
  915. break;
  916. case 'contents':
  917. this.contentsPageToggle();
  918. break;
  919. case 'libs':
  920. this.libsToogle();
  921. break;
  922. case 'recentBooks':
  923. this.recentBooksToggle();
  924. break;
  925. case 'offlineMode':
  926. this.offlineModeToggle();
  927. break;
  928. case 'settings':
  929. this.settingsToggle();
  930. break;
  931. case 'switchToolbar':
  932. this.toolBarToggle();
  933. break;
  934. case 'donate':
  935. this.donateToggle();
  936. break;
  937. default:
  938. result = false;
  939. break;
  940. }
  941. if (!result && this.activePage == 'TextPage' && this.$refs.page) {
  942. result = true;
  943. const textPage = this.$refs.page;
  944. switch (action) {
  945. case 'bookBegin':
  946. textPage.doHome();
  947. break;
  948. case 'bookEnd':
  949. textPage.doEnd();
  950. break;
  951. case 'pageBack':
  952. textPage.doPageUp();
  953. break;
  954. case 'pageForward':
  955. textPage.doPageDown();
  956. break;
  957. case 'lineBack':
  958. textPage.doUp();
  959. break;
  960. case 'lineForward':
  961. textPage.doDown();
  962. break;
  963. case 'incFontSize':
  964. textPage.doFontSizeInc();
  965. break;
  966. case 'decFontSize':
  967. textPage.doFontSizeDec();
  968. break;
  969. case 'scrollingSpeedUp':
  970. textPage.doScrollingSpeedUp();
  971. break;
  972. case 'scrollingSpeedDown':
  973. textPage.doScrollingSpeedDown();
  974. break;
  975. default:
  976. result = false;
  977. break;
  978. }
  979. }
  980. if (result && event) {
  981. event.preventDefault();
  982. event.stopPropagation();
  983. }
  984. return result;
  985. }
  986. keyHook(event) {
  987. let result = false;
  988. if (this.$root.rootRoute() == '/reader') {
  989. if (this.$root.stdDialog.active)
  990. return result;
  991. if (!result)
  992. result = this.$refs.dialogs.keyHook(event);
  993. if (!result && this.helpActive)
  994. result = this.$refs.helpPage.keyHook(event);
  995. if (!result && this.settingsActive)
  996. result = this.$refs.settingsPage.keyHook(event);
  997. if (!result && this.recentBooksActive)
  998. result = this.$refs.recentBooksPage.keyHook(event);
  999. if (!result && this.setPositionActive)
  1000. result = this.$refs.setPositionPage.keyHook(event);
  1001. if (!result && this.searchActive)
  1002. result = this.$refs.searchPage.keyHook(event);
  1003. if (!result && this.copyTextActive)
  1004. result = this.$refs.copyTextPage.keyHook(event);
  1005. if (!result && this.contentsActive)
  1006. result = this.$refs.contentsPage.keyHook(event);
  1007. if (!result && this.$refs.page && this.$refs.page.keyHook)
  1008. result = this.$refs.page.keyHook(event);
  1009. if (!result && event.type == 'keydown') {
  1010. const action = this.$root.readerActionByKeyEvent(event);
  1011. if (action == 'loader') {
  1012. result = this.doAction({action, event});
  1013. }
  1014. if (!result && this.activePage == 'TextPage') {
  1015. result = this.doAction({action, event});
  1016. }
  1017. }
  1018. }
  1019. return result;
  1020. }
  1021. }
  1022. //-----------------------------------------------------------------------------
  1023. </script>
  1024. <style scoped>
  1025. .header {
  1026. padding-left: 5px;
  1027. padding-right: 5px;
  1028. background-color: #1B695F;
  1029. color: #000;
  1030. overflow: hidden;
  1031. height: 50px;
  1032. }
  1033. .main {
  1034. background-color: #EBE2C9;
  1035. color: #000;
  1036. }
  1037. .tool-button {
  1038. margin: 0px 2px 0 2px;
  1039. padding: 0;
  1040. color: #3E843E;
  1041. background-color: #E6EDF4;
  1042. margin-top: 5px;
  1043. height: 38px;
  1044. width: 38px;
  1045. border: 0;
  1046. border-radius: 6px;
  1047. box-shadow: 3px 3px 5px black;
  1048. outline: 0;
  1049. }
  1050. .tool-button:hover {
  1051. background-color: white;
  1052. cursor: pointer;
  1053. }
  1054. .tool-button-active {
  1055. box-shadow: 0 0 0;
  1056. color: white;
  1057. background-color: #8AB45F;
  1058. position: relative;
  1059. top: 1px;
  1060. left: 1px;
  1061. }
  1062. .tool-button-active:hover {
  1063. color: white;
  1064. background-color: #81C581;
  1065. cursor: pointer;
  1066. }
  1067. .tool-button-disabled {
  1068. color: lightgray;
  1069. background-color: gray;
  1070. cursor: default;
  1071. }
  1072. .tool-button-disabled:hover {
  1073. color: lightgray;
  1074. background-color: gray;
  1075. cursor: default;
  1076. }
  1077. .space {
  1078. width: 10px;
  1079. display: inline-block;
  1080. }
  1081. .clear {
  1082. color: rgba(0,0,0,0);
  1083. }
  1084. </style>