Reader.vue 16 KB

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