Reader.vue 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  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. recentBooksClose() {
  518. this.recentBooksActive = false;
  519. }
  520. recentBooksToggle() {
  521. this.recentBooksActive = !this.recentBooksActive;
  522. if (this.recentBooksActive) {
  523. this.closeAllWindows();
  524. this.$refs.recentBooksPage.init();
  525. this.recentBooksActive = true;
  526. } else {
  527. this.recentBooksActive = false;
  528. }
  529. }
  530. contentsPageToggle() {
  531. this.contentsActive = !this.contentsActive;
  532. const page = this.$refs.page;
  533. if (this.contentsActive && this.activePage == 'TextPage' && page.parsed) {
  534. this.closeAllWindows();
  535. this.contentsActive = true;
  536. this.$nextTick(() => {
  537. this.$refs.contentsPage.init(this.mostRecentBook(), page.parsed);
  538. });
  539. } else {
  540. this.contentsActive = false;
  541. }
  542. }
  543. libsClose() {
  544. if (this.libsActive)
  545. this.libsToogle();
  546. }
  547. libsToogle() {
  548. this.libsActive = !this.libsActive;
  549. if (this.libsActive) {
  550. this.$refs.libsPage.init();
  551. } else {
  552. this.$refs.libsPage.done();
  553. }
  554. }
  555. offlineModeToggle() {
  556. this.offlineModeActive = !this.offlineModeActive;
  557. this.$refs.serverStorage.offlineModeActive = this.offlineModeActive;
  558. }
  559. settingsToggle() {
  560. this.settingsActive = !this.settingsActive;
  561. if (this.settingsActive) {
  562. this.closeAllWindows();
  563. this.settingsActive = true;
  564. this.$nextTick(() => {
  565. this.$refs.settingsPage.init();
  566. });
  567. } else {
  568. this.settingsActive = false;
  569. }
  570. }
  571. helpToggle() {
  572. this.helpActive = !this.helpActive;
  573. if (this.helpActive) {
  574. this.closeAllWindows();
  575. this.helpActive = true;
  576. }
  577. }
  578. donateToggle() {
  579. this.helpToggle();
  580. if (this.helpActive) {
  581. this.$nextTick(() => {
  582. this.$refs.helpPage.activateDonateHelpPage();
  583. });
  584. }
  585. }
  586. versionHistoryToggle() {
  587. this.helpToggle();
  588. if (this.helpActive) {
  589. this.$nextTick(() => {
  590. this.$refs.helpPage.activateVersionHistoryHelpPage();
  591. });
  592. }
  593. }
  594. refreshBook(mode) {
  595. const mrb = this.mostRecentBook();
  596. if (mrb) {
  597. if (mode && mode == 'split') {
  598. this.loadBook({url: mrb.url, uploadFileName: mrb.uploadFileName, skipCheck: true, isText: true, force: true});
  599. } else {
  600. this.loadBook({url: mrb.url, uploadFileName: mrb.uploadFileName, force: true});
  601. }
  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. const uploadFileName = (opts.uploadFileName ? opts.uploadFileName : '');
  761. let book = null;
  762. if (!opts.force) {
  763. // пытаемся загрузить и распарсить книгу в менеджере из локального кэша
  764. const bookParsed = await bookManager.getBook({url, path: opts.path}, (prog) => {
  765. progress.setState({progress: prog});
  766. });
  767. // если есть в локальном кэше
  768. if (bookParsed) {
  769. await bookManager.setRecentBook(Object.assign({bookPos, bookPosSeen}, bookParsed));
  770. this.mostRecentBook();
  771. this.addAction(bookPos);
  772. this.loaderActive = false;
  773. progress.hide(); this.progressActive = false;
  774. this.blinkCachedLoadMessage();
  775. this.checkBookPosPercent();
  776. await this.activateClickMapPage();
  777. return;
  778. }
  779. // иначе идем на сервер
  780. // пытаемся загрузить готовый файл с сервера
  781. if (wasOpened.path) {
  782. progress.setState({totalSteps: 5});
  783. try {
  784. const resp = await readerApi.loadCachedBook(wasOpened.path, (state) => {
  785. progress.setState(state);
  786. });
  787. book = Object.assign({}, wasOpened, {data: resp.data});
  788. } catch (e) {
  789. //молчим
  790. }
  791. }
  792. }
  793. progress.setState({totalSteps: 5});
  794. // не удалось, скачиваем книгу полностью с конвертацией
  795. let loadCached = true;
  796. if (!book) {
  797. book = await readerApi.loadBook({
  798. url,
  799. skipCheck: (opts.skipCheck ? true : false),
  800. isText: (opts.isText ? true : false),
  801. enableSitesFilter: this.enableSitesFilter,
  802. uploadFileName
  803. },
  804. (state) => {
  805. progress.setState(state);
  806. }
  807. );
  808. loadCached = false;
  809. }
  810. // добавляем в bookManager
  811. progress.setState({state: 'parse', step: 5});
  812. const addedBook = await bookManager.addBook(book, (prog) => {
  813. progress.setState({progress: prog});
  814. });
  815. // добавляем в историю
  816. await bookManager.setRecentBook(Object.assign({bookPos, bookPosSeen, uploadFileName}, addedBook));
  817. this.mostRecentBook();
  818. this.addAction(bookPos);
  819. this.updateRoute(true);
  820. this.loaderActive = false;
  821. progress.hide(); this.progressActive = false;
  822. if (loadCached) {
  823. this.blinkCachedLoadMessage();
  824. } else
  825. this.stopBlink = true;
  826. this.checkBookPosPercent();
  827. await this.activateClickMapPage();
  828. } catch (e) {
  829. progress.hide(); this.progressActive = false;
  830. this.loaderActive = true;
  831. this.$root.stdDialog.alert(e.message, 'Ошибка', {color: 'negative'});
  832. }
  833. }
  834. async loadFile(opts) {
  835. this.progressActive = true;
  836. await this.$nextTick();
  837. const progress = this.$refs.page;
  838. try {
  839. progress.show();
  840. progress.setState({state: 'upload'});
  841. const url = await readerApi.uploadFile(opts.file, this.config.maxUploadFileSize, (state) => {
  842. progress.setState(state);
  843. });
  844. progress.hide(); this.progressActive = false;
  845. await this.loadBook({url, uploadFileName: opts.file.name, force: true});
  846. } catch (e) {
  847. progress.hide(); this.progressActive = false;
  848. this.loaderActive = true;
  849. this.$root.stdDialog.alert(e.message, 'Ошибка', {color: 'negative'});
  850. }
  851. }
  852. blinkCachedLoadMessage() {
  853. if (!this.blinkCachedLoad)
  854. return;
  855. this.blinkCount = 30;
  856. if (!this.inBlink) {
  857. this.inBlink = true;
  858. this.stopBlink = false;
  859. this.$nextTick(async() => {
  860. let page = this.$refs.page;
  861. while (this.blinkCount) {
  862. this.showRefreshIcon = !this.showRefreshIcon;
  863. if (page && page.blinkCachedLoadMessage)
  864. page.blinkCachedLoadMessage(this.showRefreshIcon);
  865. await utils.sleep(500);
  866. if (this.stopBlink)
  867. break;
  868. this.blinkCount--;
  869. page = this.$refs.page;
  870. }
  871. this.showRefreshIcon = true;
  872. this.inBlink = false;
  873. if (page && page.blinkCachedLoadMessage)
  874. page.blinkCachedLoadMessage('finish');
  875. });
  876. }
  877. }
  878. doAction(opts) {
  879. let result = true;
  880. let {action = '', event = false} = opts;
  881. switch (action) {
  882. case 'loader':
  883. this.loaderToggle();
  884. break;
  885. case 'help':
  886. this.helpToggle();
  887. break;
  888. case 'undoAction':
  889. this.undoAction();
  890. break;
  891. case 'redoAction':
  892. this.redoAction();
  893. break;
  894. case 'fullScreen':
  895. this.fullScreenToggle();
  896. break;
  897. case 'scrolling':
  898. this.scrollingToggle();
  899. break;
  900. case 'stopScrolling':
  901. this.stopScrolling();
  902. break;
  903. case 'setPosition':
  904. this.setPositionToggle();
  905. break;
  906. case 'search':
  907. this.searchToggle();
  908. break;
  909. case 'copyText':
  910. this.copyTextToggle();
  911. break;
  912. case 'splitToPara':
  913. this.refreshBook('split');
  914. break;
  915. case 'refresh':
  916. this.refreshBook();
  917. break;
  918. case 'contents':
  919. this.contentsPageToggle();
  920. break;
  921. case 'libs':
  922. this.libsToogle();
  923. break;
  924. case 'recentBooks':
  925. this.recentBooksToggle();
  926. break;
  927. case 'offlineMode':
  928. this.offlineModeToggle();
  929. break;
  930. case 'settings':
  931. this.settingsToggle();
  932. break;
  933. case 'switchToolbar':
  934. this.toolBarToggle();
  935. break;
  936. case 'donate':
  937. this.donateToggle();
  938. break;
  939. default:
  940. result = false;
  941. break;
  942. }
  943. if (!result && this.activePage == 'TextPage' && this.$refs.page) {
  944. result = true;
  945. const textPage = this.$refs.page;
  946. switch (action) {
  947. case 'bookBegin':
  948. textPage.doHome();
  949. break;
  950. case 'bookEnd':
  951. textPage.doEnd();
  952. break;
  953. case 'pageBack':
  954. textPage.doPageUp();
  955. break;
  956. case 'pageForward':
  957. textPage.doPageDown();
  958. break;
  959. case 'lineBack':
  960. textPage.doUp();
  961. break;
  962. case 'lineForward':
  963. textPage.doDown();
  964. break;
  965. case 'incFontSize':
  966. textPage.doFontSizeInc();
  967. break;
  968. case 'decFontSize':
  969. textPage.doFontSizeDec();
  970. break;
  971. case 'scrollingSpeedUp':
  972. textPage.doScrollingSpeedUp();
  973. break;
  974. case 'scrollingSpeedDown':
  975. textPage.doScrollingSpeedDown();
  976. break;
  977. default:
  978. result = false;
  979. break;
  980. }
  981. }
  982. if (result && event) {
  983. event.preventDefault();
  984. event.stopPropagation();
  985. }
  986. return result;
  987. }
  988. keyHook(event) {
  989. let result = false;
  990. if (this.$root.rootRoute() == '/reader') {
  991. if (this.$root.stdDialog.active)
  992. return result;
  993. if (!result)
  994. result = this.$refs.dialogs.keyHook(event);
  995. if (!result && this.helpActive)
  996. result = this.$refs.helpPage.keyHook(event);
  997. if (!result && this.settingsActive)
  998. result = this.$refs.settingsPage.keyHook(event);
  999. if (!result && this.recentBooksActive)
  1000. result = this.$refs.recentBooksPage.keyHook(event);
  1001. if (!result && this.setPositionActive)
  1002. result = this.$refs.setPositionPage.keyHook(event);
  1003. if (!result && this.searchActive)
  1004. result = this.$refs.searchPage.keyHook(event);
  1005. if (!result && this.copyTextActive)
  1006. result = this.$refs.copyTextPage.keyHook(event);
  1007. if (!result && this.contentsActive)
  1008. result = this.$refs.contentsPage.keyHook(event);
  1009. if (!result && this.$refs.page && this.$refs.page.keyHook)
  1010. result = this.$refs.page.keyHook(event);
  1011. if (!result && event.type == 'keydown') {
  1012. const action = this.$root.readerActionByKeyEvent(event);
  1013. if (action == 'loader') {
  1014. result = this.doAction({action, event});
  1015. }
  1016. if (!result && this.activePage == 'TextPage') {
  1017. result = this.doAction({action, event});
  1018. }
  1019. }
  1020. }
  1021. return result;
  1022. }
  1023. }
  1024. //-----------------------------------------------------------------------------
  1025. </script>
  1026. <style scoped>
  1027. .header {
  1028. padding-left: 5px;
  1029. padding-right: 5px;
  1030. background-color: #1B695F;
  1031. color: #000;
  1032. overflow: hidden;
  1033. height: 50px;
  1034. }
  1035. .main {
  1036. background-color: #EBE2C9;
  1037. color: #000;
  1038. }
  1039. .tool-button {
  1040. margin: 0px 2px 0 2px;
  1041. padding: 0;
  1042. color: #3E843E;
  1043. background-color: #E6EDF4;
  1044. margin-top: 5px;
  1045. height: 38px;
  1046. width: 38px;
  1047. border: 0;
  1048. border-radius: 6px;
  1049. box-shadow: 3px 3px 5px black;
  1050. outline: 0;
  1051. }
  1052. .tool-button:hover {
  1053. background-color: white;
  1054. cursor: pointer;
  1055. }
  1056. .tool-button-active {
  1057. box-shadow: 0 0 0;
  1058. color: white;
  1059. background-color: #8AB45F;
  1060. position: relative;
  1061. top: 1px;
  1062. left: 1px;
  1063. }
  1064. .tool-button-active:hover {
  1065. color: white;
  1066. background-color: #81C581;
  1067. cursor: pointer;
  1068. }
  1069. .tool-button-disabled {
  1070. color: lightgray;
  1071. background-color: gray;
  1072. cursor: default;
  1073. }
  1074. .tool-button-disabled:hover {
  1075. color: lightgray;
  1076. background-color: gray;
  1077. cursor: default;
  1078. }
  1079. .space {
  1080. width: 10px;
  1081. display: inline-block;
  1082. }
  1083. .clear {
  1084. color: rgba(0,0,0,0);
  1085. }
  1086. </style>