Reader.vue 16 KB

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