Reader.vue 14 KB

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