Reader.vue 14 KB

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