Reader.vue 13 KB

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