Reader.vue 14 KB

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