Reader.vue 45 KB

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