Reader.vue 15 KB

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