Reader.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. <template>
  2. <el-container>
  3. <el-header v-show="toolBarActive" height='50px'>
  4. <div class="header">
  5. <el-tooltip content="Загрузить книгу" :open-delay="1000" effect="light">
  6. <el-button ref="loader" class="tool-button" :class="buttonActiveClass('loader')" @click="buttonClick('loader')"><i class="el-icon-back"></i></el-button>
  7. </el-tooltip>
  8. <div>
  9. <el-tooltip content="Действие назад" :open-delay="1000" effect="light">
  10. <el-button ref="undoAction" class="tool-button" :class="buttonActiveClass('undoAction')" @click="buttonClick('undoAction')" ><i class="el-icon-arrow-left"></i></el-button>
  11. </el-tooltip>
  12. <el-tooltip content="Действие вперед" :open-delay="1000" effect="light">
  13. <el-button ref="redoAction" class="tool-button" :class="buttonActiveClass('redoAction')" @click="buttonClick('redoAction')" ><i class="el-icon-arrow-right"></i></el-button>
  14. </el-tooltip>
  15. <div class="space"></div>
  16. <el-tooltip content="На весь экран" :open-delay="1000" effect="light">
  17. <el-button ref="fullScreen" class="tool-button" :class="buttonActiveClass('fullScreen')" @click="buttonClick('fullScreen')"><i class="el-icon-rank"></i></el-button>
  18. </el-tooltip>
  19. <el-tooltip content="Прокрутка книги" :open-delay="1000" effect="light">
  20. <el-button ref="setPosition" class="tool-button" :class="buttonActiveClass('setPosition')" @click="buttonClick('setPosition')"><i class="el-icon-d-arrow-right"></i></el-button>
  21. </el-tooltip>
  22. <el-tooltip content="Плавный скроллинг" :open-delay="1000" effect="light">
  23. <el-button ref="scrolling" class="tool-button" :class="buttonActiveClass('scrolling')" @click="buttonClick('scrolling')"><i class="el-icon-sort"></i></el-button>
  24. </el-tooltip>
  25. <el-tooltip content="Найти в тексте" :open-delay="1000" effect="light">
  26. <el-button ref="search" class="tool-button" :class="buttonActiveClass('search')" @click="buttonClick('search')"><i class="el-icon-search"></i></el-button>
  27. </el-tooltip>
  28. <el-tooltip content="Скопировать текст со страницы" :open-delay="1000" effect="light">
  29. <el-button ref="copyText" class="tool-button" :class="buttonActiveClass('copyText')" @click="buttonClick('copyText')"><i class="el-icon-edit-outline"></i></el-button>
  30. </el-tooltip>
  31. <el-tooltip content="Принудительно обновить книгу в обход кеша" :open-delay="1000" effect="light">
  32. <el-button ref="refresh" class="tool-button" :class="buttonActiveClass('refresh')" @click="buttonClick('refresh')">
  33. <i class="el-icon-refresh" :class="{clear: !showRefreshIcon}"></i>
  34. </el-button>
  35. </el-tooltip>
  36. <div class="space"></div>
  37. <el-tooltip content="История" :open-delay="1000" effect="light">
  38. <el-button ref="history" class="tool-button" :class="buttonActiveClass('history')" @click="buttonClick('history')"><i class="el-icon-document"></i></el-button>
  39. </el-tooltip>
  40. </div>
  41. <el-tooltip content="Настроить" :open-delay="1000" effect="light">
  42. <el-button ref="settings" class="tool-button" @click="buttonClick('settings')"><i class="el-icon-setting"></i></el-button>
  43. </el-tooltip>
  44. </div>
  45. </el-header>
  46. <el-main>
  47. <keep-alive>
  48. <component ref="page" :is="activePage"
  49. @load-book="loadBook"
  50. @book-pos-changed="bookPosChanged"
  51. @tool-bar-toggle="toolBarToggle"
  52. @full-screen-toogle="fullScreenToggle"
  53. ></component>
  54. </keep-alive>
  55. <HistoryPage v-if="historyActive" ref="historyPage" @load-book="loadBook" @history-toggle="historyToggle"></HistoryPage>
  56. <SetPositionPage v-if="setPositionActive" ref="setPositionPage" @set-position-toggle="setPositionToggle" @book-pos-changed="bookPosChanged"></SetPositionPage>
  57. </el-main>
  58. </el-container>
  59. </template>
  60. <script>
  61. //-----------------------------------------------------------------------------
  62. import Vue from 'vue';
  63. import Component from 'vue-class-component';
  64. import LoaderPage from './LoaderPage/LoaderPage.vue';
  65. import TextPage from './TextPage/TextPage.vue';
  66. import ProgressPage from './ProgressPage/ProgressPage.vue';
  67. import HistoryPage from './HistoryPage/HistoryPage.vue';
  68. import SetPositionPage from './SetPositionPage/SetPositionPage.vue';
  69. import bookManager from './share/bookManager';
  70. import readerApi from '../../api/reader';
  71. import {sleep} from '../../share/utils';
  72. export default @Component({
  73. components: {
  74. LoaderPage,
  75. TextPage,
  76. ProgressPage,
  77. HistoryPage,
  78. SetPositionPage,
  79. },
  80. watch: {
  81. bookPos: function(newValue) {
  82. if (newValue !== undefined && this.activePage == 'TextPage') {
  83. const textPage = this.$refs.page;
  84. if (textPage.bookPos != newValue) {
  85. textPage.bookPos = newValue;
  86. }
  87. if (this.lastOpenedBook && this.lastOpenedBook.bookPos != newValue) {
  88. this.commit('reader/setOpenedBook', Object.assign({}, this.lastOpenedBook, {bookPos: newValue, bookPosSeen: this.bookPosSeen}));
  89. }
  90. }
  91. },
  92. routeParamPos: function(newValue) {
  93. if (newValue !== undefined && newValue != this.bookPos) {
  94. this.bookPos = newValue;
  95. }
  96. },
  97. routeParamUrl: function(newValue) {
  98. if (newValue !== '' && newValue !== this.lastOpenedBook.url) {
  99. this.loadBook({url: newValue, bookPos: this.routeParamPos});
  100. }
  101. },
  102. },
  103. })
  104. class Reader extends Vue {
  105. loaderActive = false;
  106. progressActive = false;
  107. fullScreenActive = false;
  108. scrollingActive = false;
  109. searchActive = false;
  110. copyTextActive = false;
  111. historyActive = false;
  112. setPositionActive = false;
  113. bookPos = null;
  114. allowUrlParamBookPos = true;
  115. showRefreshIcon = true;
  116. created() {
  117. this.commit = this.$store.commit;
  118. this.dispatch = this.$store.dispatch;
  119. this.reader = this.$store.state.reader;
  120. this.$root.addKeyHook(this.keyHook);
  121. this.lastActivePage = false;
  122. document.addEventListener('fullscreenchange', () => {
  123. this.fullScreenActive = (document.fullscreenElement !== null);
  124. });
  125. }
  126. mounted() {
  127. /*while (this.lastOpenedBook) {
  128. this.commit('reader/delOpenedBook', this.lastOpenedBook);
  129. }*/
  130. if (this.$root.rootRoute == '/reader') {
  131. if (this.routeParamUrl) {
  132. this.loadBook({url: this.routeParamUrl, bookPos: this.routeParamPos});
  133. } else if (this.lastOpenedBook) {
  134. this.loadBook({url: this.lastOpenedBook.url});
  135. } else {
  136. this.loaderActive = true;
  137. }
  138. }
  139. }
  140. get routeParamPos() {
  141. let result = undefined;
  142. const q = this.$route.query;
  143. if (q['__p']) {
  144. result = q['__p'];
  145. if (Array.isArray(result))
  146. result = result[0];
  147. }
  148. return (result ? parseInt(result, 10) || 0 : result);
  149. }
  150. updateRoute(isNewRoute) {
  151. const pos = (this.bookPos != undefined && this.allowUrlParamBookPos ? `__p=${this.bookPos}&` : '');
  152. if (isNewRoute)
  153. this.$router.push(`/reader?${pos}url=${this.lastOpenedBook.url}`);
  154. else
  155. this.$router.replace(`/reader?${pos}url=${this.lastOpenedBook.url}`);
  156. }
  157. get routeParamUrl() {
  158. let result = '';
  159. const path = this.$route.fullPath;
  160. const i = path.indexOf('url=');
  161. if (i >= 0) {
  162. result = path.substr(i + 4);
  163. }
  164. return decodeURIComponent(result);
  165. }
  166. bookPosChanged(event) {
  167. if (event.bookPosSeen !== undefined)
  168. this.bookPosSeen = event.bookPosSeen;
  169. this.bookPos = event.bookPos;
  170. this.updateRoute();
  171. }
  172. get toolBarActive() {
  173. return this.reader.toolBarActive;
  174. }
  175. get lastOpenedBook() {
  176. const result = this.$store.getters['reader/lastOpenedBook'];
  177. if (!result)
  178. this.closeAllTextPages()
  179. return result;
  180. }
  181. toolBarToggle() {
  182. this.commit('reader/setToolBarActive', !this.toolBarActive);
  183. this.$root.$emit('resize');
  184. }
  185. fullScreenToggle() {
  186. this.fullScreenActive = !this.fullScreenActive;
  187. if (this.fullScreenActive) {
  188. const element = document.documentElement;
  189. if (element.requestFullscreen) {
  190. element.requestFullscreen();
  191. } else if (element.webkitrequestFullscreen) {
  192. element.webkitRequestFullscreen();
  193. } else if (element.mozRequestFullscreen) {
  194. element.mozRequestFullScreen();
  195. }
  196. } else {
  197. if (document.cancelFullScreen) {
  198. document.cancelFullScreen();
  199. } else if (document.mozCancelFullScreen) {
  200. document.mozCancelFullScreen();
  201. } else if (document.webkitCancelFullScreen) {
  202. document.webkitCancelFullScreen();
  203. }
  204. }
  205. }
  206. closeAllTextPages() {
  207. this.historyActive = false;
  208. this.setPositionActive = false;
  209. }
  210. loaderToggle() {
  211. this.loaderActive = !this.loaderActive;
  212. if (this.loaderActive) {
  213. this.closeAllTextPages();
  214. }
  215. }
  216. historyToggle() {
  217. this.historyActive = !this.historyActive;
  218. if (this.historyActive && this.activePage == 'TextPage') {
  219. this.closeAllTextPages();
  220. this.historyActive = true;
  221. } else {
  222. this.historyActive = false;
  223. }
  224. }
  225. setPositionToggle() {
  226. this.setPositionActive = !this.setPositionActive;
  227. if (this.setPositionActive && this.activePage == 'TextPage' && this.lastOpenedBook) {
  228. this.closeAllTextPages();
  229. this.setPositionActive = true;
  230. this.$nextTick(() => {
  231. this.$refs.setPositionPage.sliderMax = this.lastOpenedBook.textLength - 1;
  232. this.$refs.setPositionPage.sliderValue = this.lastOpenedBook.bookPos;
  233. });
  234. } else {
  235. this.setPositionActive = false;
  236. }
  237. }
  238. buttonClick(button) {
  239. switch (button) {
  240. case 'loader':
  241. this.loaderToggle();
  242. break;
  243. case 'fullScreen':
  244. this.fullScreenToggle();
  245. break;
  246. case 'history':
  247. this.historyToggle();
  248. break;
  249. case 'setPosition':
  250. this.setPositionToggle();
  251. break;
  252. case 'refresh':
  253. if (this.lastOpenedBook) {
  254. this.loadBook({url: this.lastOpenedBook.url, force: true});
  255. }
  256. break;
  257. }
  258. this.$refs[button].$el.blur();
  259. }
  260. buttonActiveClass(button) {
  261. const classActive = { 'tool-button-active': true, 'tool-button-active:hover': true };
  262. const classDisabled = { 'tool-button-disabled': true, 'tool-button-disabled:hover': true };
  263. let classResult = {};
  264. switch (button) {
  265. case 'loader':
  266. case 'fullScreen':
  267. case 'setPosition':
  268. case 'scrolling':
  269. case 'search':
  270. case 'copyText':
  271. case 'history':
  272. if (this[`${button}Active`])
  273. classResult = classActive;
  274. break;
  275. }
  276. if (this.activePage == 'LoaderPage' || !this.lastOpenedBook) {
  277. switch (button) {
  278. case 'undoAction':
  279. case 'redoAction':
  280. case 'setPosition':
  281. case 'scrolling':
  282. case 'search':
  283. case 'copyText':
  284. case 'history':
  285. classResult = classDisabled;
  286. break;
  287. case 'refresh':
  288. if (!this.lastOpenedBook)
  289. classResult = classDisabled;
  290. break;
  291. }
  292. }
  293. return classResult;
  294. }
  295. get activePage() {
  296. let result = '';
  297. if (this.progressActive)
  298. result = 'ProgressPage';
  299. else if (this.loaderActive)
  300. result = 'LoaderPage';
  301. else if (this.lastOpenedBook)
  302. result = 'TextPage';
  303. if (!result) {
  304. this.loaderActive = true;
  305. result = 'LoaderPage';
  306. }
  307. if (result != 'TextPage') {
  308. this.$root.$emit('set-app-title');
  309. }
  310. if (this.lastActivePage != result && result == 'TextPage') {
  311. //акивируем страницу с текстом
  312. this.$nextTick(async() => {
  313. const last = this.lastOpenedBook;
  314. const isParsed = await bookManager.hasBookParsed(last);
  315. if (!isParsed) {
  316. this.$root.$emit('set-app-title');
  317. return;
  318. }
  319. this.updateRoute();
  320. const textPage = this.$refs.page;
  321. if (textPage.showBook) {
  322. textPage.lastBook = last;
  323. textPage.bookPos = (last.bookPos !== undefined ? last.bookPos : 0);
  324. textPage.showBook();
  325. }
  326. });
  327. }
  328. this.lastActivePage = result;
  329. return result;
  330. }
  331. loadBook(opts) {
  332. this.closeAllTextPages();
  333. this.progressActive = true;
  334. this.$nextTick(async() => {
  335. const progress = this.$refs.page;
  336. try {
  337. progress.show();
  338. progress.setState({state: 'parse'});
  339. // есть ли среди истории OpenedBook
  340. const key = bookManager.keyFromUrl(opts.url);
  341. let wasOpened = this.reader.openedBook[key];
  342. wasOpened = (wasOpened ? wasOpened : {});
  343. const bookPos = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPos);
  344. const bookPosSeen = (opts.bookPos !== undefined ? opts.bookPos : wasOpened.bookPosSeen);
  345. let book = null;
  346. if (!opts.force) {
  347. // пытаемся загрузить и распарсить книгу в менеджере из локального кеша
  348. const bookParsed = await bookManager.getBook({url: opts.url}, (prog) => {
  349. progress.setState({progress: prog});
  350. });
  351. // если есть в локальном кеше
  352. if (bookParsed) {
  353. this.commit('reader/setOpenedBook', Object.assign({bookPos, bookPosSeen}, bookManager.metaOnly(bookParsed)));
  354. this.loaderActive = false;
  355. progress.hide(); this.progressActive = false;
  356. this.blinkCachedLoadMessage();
  357. return;
  358. }
  359. // иначе идем на сервер
  360. // пытаемся загрузить готовый файл с сервера
  361. if (wasOpened.path) {
  362. try {
  363. const resp = await readerApi.loadCachedBook(wasOpened.path, (state) => {
  364. progress.setState(state);
  365. });
  366. book = Object.assign({}, wasOpened, {data: resp.data});
  367. } catch (e) {
  368. //молчим
  369. }
  370. }
  371. }
  372. progress.setState({totalSteps: 5});
  373. // не удалось, скачиваем книгу полностью с конвертацией
  374. let loadCached = true;
  375. if (!book) {
  376. book = await readerApi.loadBook(opts.url, (state) => {
  377. progress.setState(state);
  378. });
  379. loadCached = false;
  380. }
  381. // добавляем в bookManager
  382. progress.setState({state: 'parse', step: 5});
  383. const addedBook = await bookManager.addBook(book, (prog) => {
  384. progress.setState({progress: prog});
  385. });
  386. // добавляем в историю
  387. this.commit('reader/setOpenedBook', Object.assign({bookPos, bookPosSeen}, bookManager.metaOnly(addedBook)));
  388. this.updateRoute(true);
  389. this.loaderActive = false;
  390. progress.hide(); this.progressActive = false;
  391. if (loadCached) {
  392. this.blinkCachedLoadMessage();
  393. } else
  394. this.stopBlink = true;
  395. } catch (e) {
  396. progress.hide(); this.progressActive = false;
  397. this.loaderActive = true;
  398. this.$alert(e.message, 'Ошибка', {type: 'error'});
  399. }
  400. });
  401. }
  402. blinkCachedLoadMessage() {
  403. this.blinkCount = 30;
  404. if (!this.inBlink) {
  405. this.inBlink = true;
  406. this.stopBlink = false;
  407. this.$nextTick(async() => {
  408. let page = this.$refs.page;
  409. while (this.blinkCount) {
  410. this.showRefreshIcon = !this.showRefreshIcon;
  411. if (page.blinkCachedLoadMessage)
  412. page.blinkCachedLoadMessage(this.showRefreshIcon);
  413. await sleep(500);
  414. if (this.stopBlink)
  415. break;
  416. this.blinkCount--;
  417. page = this.$refs.page;
  418. }
  419. this.showRefreshIcon = true;
  420. this.inBlink = false;
  421. if (page.blinkCachedLoadMessage)
  422. page.blinkCachedLoadMessage('finish');
  423. });
  424. }
  425. }
  426. keyHook(event) {
  427. if (this.$root.rootRoute == '/reader') {
  428. let handled = false;
  429. if (!handled && this.historyActive)
  430. handled = this.$refs.historyPage.keyHook(event);
  431. if (!handled && this.setPositionActive)
  432. handled = this.$refs.setPositionPage.keyHook(event);
  433. if (!handled && this.$refs.page && this.$refs.page.keyHook)
  434. handled = this.$refs.page.keyHook(event);
  435. if (!handled && event.type == 'keydown') {
  436. switch (event.code) {
  437. case 'Escape':
  438. this.loaderToggle();
  439. break;
  440. case 'KeyH':
  441. this.historyToggle();
  442. break;
  443. case 'KeyP':
  444. this.setPositionToggle();
  445. break;
  446. }
  447. }
  448. }
  449. }
  450. }
  451. //-----------------------------------------------------------------------------
  452. </script>
  453. <style scoped>
  454. .el-container {
  455. padding: 0;
  456. margin: 0;
  457. height: 100%;
  458. }
  459. .el-header {
  460. padding-left: 5px;
  461. padding-right: 5px;
  462. background-color: #1B695F;
  463. color: #000;
  464. overflow-x: auto;
  465. overflow-y: hidden;
  466. }
  467. .header {
  468. display: flex;
  469. justify-content: space-between;
  470. min-width: 550px;
  471. }
  472. .el-main {
  473. position: relative;
  474. display: flex;
  475. padding: 0;
  476. margin: 0;
  477. background-color: #EBE2C9;
  478. color: #000;
  479. }
  480. .tool-button {
  481. margin: 0;
  482. margin-left: 2px;
  483. margin-right: 2px;
  484. padding: 0;
  485. color: #3E843E;
  486. background-color: #E6EDF4;
  487. margin-top: 5px;
  488. height: 38px;
  489. width: 38px;
  490. border: 0;
  491. box-shadow: 3px 3px 5px black;
  492. }
  493. .tool-button:hover {
  494. background-color: white;
  495. }
  496. .tool-button-active {
  497. box-shadow: 0 0 0;
  498. color: white;
  499. background-color: #8AB45F;
  500. position: relative;
  501. top: 1px;
  502. left: 1px;
  503. }
  504. .tool-button-active:hover {
  505. color: white;
  506. background-color: #81C581;
  507. }
  508. .tool-button-disabled {
  509. color: lightgray;
  510. background-color: gray;
  511. }
  512. .tool-button-disabled:hover {
  513. color: lightgray;
  514. background-color: gray;
  515. }
  516. i {
  517. font-size: 200%;
  518. }
  519. .space {
  520. width: 10px;
  521. display: inline-block;
  522. }
  523. .clear {
  524. color: rgba(0,0,0,0);
  525. }
  526. </style>