Reader.vue 19 KB

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