Reader.vue 42 KB

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