TextPage.vue 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. <template>
  2. <div ref="main" class="main">
  3. <div class="layout back" @wheel.prevent.stop="onMouseWheel">
  4. <div v-html="background"></div>
  5. <!-- img -->
  6. </div>
  7. <div ref="scrollBox1" class="layout over-hidden" @wheel.prevent.stop="onMouseWheel">
  8. <div ref="scrollingPage1" class="layout over-hidden" @transitionend="onPage1TransitionEnd" @animationend="onPage1AnimationEnd">
  9. <div v-html="page1"></div>
  10. </div>
  11. </div>
  12. <div ref="scrollBox2" class="layout over-hidden" @wheel.prevent.stop="onMouseWheel">
  13. <div ref="scrollingPage2" class="layout over-hidden" @transitionend="onPage2TransitionEnd" @animationend="onPage2AnimationEnd">
  14. <div v-html="page2"></div>
  15. </div>
  16. </div>
  17. <div v-show="showStatusBar" ref="statusBar" class="layout">
  18. <div v-html="statusBar"></div>
  19. </div>
  20. <div v-show="clickControl" ref="layoutEvents" class="layout events" @mousedown.prevent.stop="onMouseDown" @mouseup.prevent.stop="onMouseUp"
  21. @wheel.prevent.stop="onMouseWheel"
  22. @touchstart.stop="onTouchStart" @touchend.stop="onTouchEnd" @touchmove.stop="onTouchMove" @touchcancel.prevent.stop="onTouchCancel"
  23. oncontextmenu="return false;">
  24. <div v-show="showStatusBar" v-html="statusBarClickable" @mousedown.prevent.stop @touchstart.stop
  25. @click.prevent.stop="onStatusBarClick"></div>
  26. </div>
  27. <div v-show="!clickControl && showStatusBar" class="layout" v-html="statusBarClickable" @mousedown.prevent.stop @touchstart.stop
  28. @click.prevent.stop="onStatusBarClick"></div>
  29. <!-- невидимым делать нельзя, вовремя не подгружаютя шрифты -->
  30. <canvas ref="offscreenCanvas" class="layout" style="width: 0px; height: 0px"></canvas>
  31. </div>
  32. </template>
  33. <script>
  34. //-----------------------------------------------------------------------------
  35. import Vue from 'vue';
  36. import Component from 'vue-class-component';
  37. import {loadCSS} from 'fg-loadcss';
  38. import _ from 'lodash';
  39. import {sleep} from '../../../share/utils';
  40. import bookManager from '../share/bookManager';
  41. import DrawHelper from './DrawHelper';
  42. import rstore from '../../../store/modules/reader';
  43. import {clickMap} from '../share/clickMap';
  44. const minLayoutWidth = 100;
  45. export default @Component({
  46. watch: {
  47. bookPos: function() {
  48. this.$emit('book-pos-changed', {bookPos: this.bookPos, bookPosSeen: this.bookPosSeen});
  49. this.draw();
  50. },
  51. bookPosSeen: function() {
  52. this.$emit('book-pos-changed', {bookPos: this.bookPos, bookPosSeen: this.bookPosSeen});
  53. },
  54. settings: function() {
  55. this.debouncedLoadSettings();
  56. },
  57. toggleLayout: function() {
  58. this.updateLayout();
  59. },
  60. inAnimation: function() {
  61. this.updateLayout();
  62. },
  63. },
  64. })
  65. class TextPage extends Vue {
  66. toggleLayout = false;
  67. showStatusBar = false;
  68. clickControl = true;
  69. background = null;
  70. page1 = null;
  71. page2 = null;
  72. statusBar = null;
  73. statusBarClickable = null;
  74. lastBook = null;
  75. bookPos = 0;
  76. bookPosSeen = null;
  77. fontStyle = null;
  78. fontSize = null;
  79. fontName = null;
  80. fontWeight = null;
  81. inAnimation = false;
  82. meta = null;
  83. created() {
  84. this.drawHelper = new DrawHelper();
  85. this.commit = this.$store.commit;
  86. this.dispatch = this.$store.dispatch;
  87. this.config = this.$store.state.config;
  88. this.reader = this.$store.state.reader;
  89. this.debouncedStartClickRepeat = _.debounce((x, y) => {
  90. this.startClickRepeat(x, y);
  91. }, 800);
  92. this.debouncedPrepareNextPage = _.debounce(() => {
  93. this.prepareNextPage();
  94. }, 100);
  95. this.debouncedDrawStatusBar = _.throttle(() => {
  96. this.drawStatusBar();
  97. }, 60);
  98. this.debouncedLoadSettings = _.debounce(() => {
  99. this.loadSettings();
  100. }, 50);
  101. this.debouncedUpdatePage = _.debounce(async(lines) => {
  102. if (!this.pageChangeAnimation)
  103. this.toggleLayout = !this.toggleLayout;
  104. else {
  105. this.page2 = this.page1;
  106. this.toggleLayout = true;
  107. }
  108. if (this.toggleLayout)
  109. this.page1 = this.drawHelper.drawPage(lines);
  110. else
  111. this.page2 = this.drawHelper.drawPage(lines);
  112. await this.doPageAnimation();
  113. }, 10);
  114. this.$root.$on('resize', () => {this.$nextTick(this.onResize)});
  115. }
  116. mounted() {
  117. this.context = this.$refs.offscreenCanvas.getContext('2d');
  118. }
  119. hex2rgba(hex, alpha = 1) {
  120. const [r, g, b] = hex.match(/\w\w/g).map(x => parseInt(x, 16));
  121. return `rgba(${r},${g},${b},${alpha})`;
  122. }
  123. calcDrawProps() {
  124. //preloaded fonts
  125. this.fontList = [`12px ${this.fontName}`];
  126. //widths
  127. this.realWidth = this.$refs.main.clientWidth;
  128. this.realHeight = this.$refs.main.clientHeight;
  129. this.$refs.layoutEvents.style.width = this.realWidth + 'px';
  130. this.$refs.layoutEvents.style.height = this.realHeight + 'px';
  131. this.w = this.realWidth - 2*this.indentLR;
  132. this.scrollHeight = this.realHeight - (this.showStatusBar ? this.statusBarHeight : 0);
  133. this.h = this.scrollHeight - 2*this.indentTB;
  134. this.lineHeight = this.fontSize + this.lineInterval;
  135. this.pageLineCount = 1 + Math.floor((this.h - this.lineHeight + this.lineInterval/2)/this.lineHeight);
  136. this.$refs.scrollingPage1.style.width = this.w + 'px';
  137. this.$refs.scrollingPage2.style.width = this.w + 'px';
  138. //stuff
  139. this.currentAnimation = '';
  140. this.pageChangeDirectionDown = true;
  141. this.fontShift = this.fontVertShift/100;
  142. this.textShift = this.textVertShift/100 + this.fontShift;
  143. //statusBar
  144. this.$refs.statusBar.style.left = '0px';
  145. this.$refs.statusBar.style.top = (this.statusBarTop ? 1 : this.realHeight - this.statusBarHeight) + 'px';
  146. this.statusBarColor = this.hex2rgba(this.textColor || '#000000', this.statusBarColorAlpha);
  147. //drawHelper
  148. this.drawHelper.realWidth = this.realWidth;
  149. this.drawHelper.realHeight = this.realHeight;
  150. this.drawHelper.lastBook = this.lastBook;
  151. this.drawHelper.book = this.book;
  152. this.drawHelper.parsed = this.parsed;
  153. this.drawHelper.pageLineCount = this.pageLineCount;
  154. this.drawHelper.backgroundColor = this.backgroundColor;
  155. this.drawHelper.statusBarColor = this.statusBarColor;
  156. this.drawHelper.fontStyle = this.fontStyle;
  157. this.drawHelper.fontWeight = this.fontWeight;
  158. this.drawHelper.fontSize = this.fontSize;
  159. this.drawHelper.fontName = this.fontName;
  160. this.drawHelper.fontShift = this.fontShift;
  161. this.drawHelper.textColor = this.textColor;
  162. this.drawHelper.textShift = this.textShift;
  163. this.drawHelper.p = this.p;
  164. this.drawHelper.w = this.w;
  165. this.drawHelper.h = this.h;
  166. this.drawHelper.indentLR = this.indentLR;
  167. this.drawHelper.textAlignJustify = this.textAlignJustify;
  168. this.drawHelper.lineHeight = this.lineHeight;
  169. this.drawHelper.context = this.context;
  170. //statusBar
  171. this.statusBarClickable = this.drawHelper.statusBarClickable(this.statusBarTop, this.statusBarHeight);
  172. //parsed
  173. if (this.parsed) {
  174. this.parsed.p = this.p;
  175. this.parsed.w = this.w;// px, ширина текста
  176. this.parsed.font = this.font;
  177. this.parsed.fontSize = this.fontSize;
  178. this.parsed.wordWrap = this.wordWrap;
  179. this.parsed.cutEmptyParagraphs = this.cutEmptyParagraphs;
  180. this.parsed.addEmptyParagraphs = this.addEmptyParagraphs;
  181. let t = '';
  182. while (this.drawHelper.measureText(t, {}) < this.w) t += 'Щ';
  183. this.parsed.maxWordLength = t.length - 1;
  184. this.parsed.measureText = this.drawHelper.measureText.bind(this.drawHelper);
  185. this.parsed.lineHeight = this.lineHeight;
  186. this.parsed.showImages = this.showImages;
  187. this.parsed.showInlineImagesInCenter = this.showInlineImagesInCenter;
  188. this.parsed.imageHeightLines = this.imageHeightLines;
  189. this.parsed.imageFitWidth = this.imageFitWidth;
  190. this.parsed.compactTextPerc = this.compactTextPerc;
  191. }
  192. //scrolling page
  193. const pageSpace = this.scrollHeight - this.pageLineCount*this.lineHeight;
  194. let top = pageSpace/2;
  195. if (this.showStatusBar)
  196. top += this.statusBarHeight*(this.statusBarTop ? 1 : 0);
  197. let page1 = this.$refs.scrollBox1.style;
  198. let page2 = this.$refs.scrollBox2.style;
  199. page1.perspective = page2.perspective = '3072px';
  200. page1.width = page2.width = this.w + this.indentLR + 'px';
  201. page1.height = page2.height = this.scrollHeight - (pageSpace > 0 ? pageSpace : 0) + 'px';
  202. page1.top = page2.top = top + 'px';
  203. page1.left = page2.left = this.indentLR + 'px';
  204. page1 = this.$refs.scrollingPage1.style;
  205. page2 = this.$refs.scrollingPage2.style;
  206. page1.width = page2.width = this.w + this.indentLR + 'px';
  207. page1.height = page2.height = this.scrollHeight + this.lineHeight + 'px';
  208. }
  209. async checkLoadedFonts() {
  210. let loaded = await Promise.all(this.fontList.map(font => document.fonts.check(font)));
  211. if (loaded.some(r => !r)) {
  212. loaded = await Promise.all(this.fontList.map(font => document.fonts.load(font)));
  213. if (loaded.some(r => !r.length))
  214. throw new Error('some font not loaded');
  215. }
  216. }
  217. async loadFonts() {
  218. this.fontsLoading = true;
  219. let inst = null;
  220. (async() => {
  221. await sleep(500);
  222. if (this.fontsLoading)
  223. inst = this.$notify({
  224. title: '',
  225. dangerouslyUseHTMLString: true,
  226. message: 'Загрузка шрифта &nbsp;<i class="el-icon-loading"></i>',
  227. duration: 0
  228. });
  229. })();
  230. if (!this.fontsLoaded)
  231. this.fontsLoaded = {};
  232. //загрузка дин.шрифта
  233. const loaded = this.fontsLoaded[this.fontCssUrl];
  234. if (this.fontCssUrl && !loaded) {
  235. loadCSS(this.fontCssUrl);
  236. this.fontsLoaded[this.fontCssUrl] = 1;
  237. }
  238. const waitingTime = 10*1000;
  239. const delay = 100;
  240. let i = 0;
  241. //ждем шрифты
  242. while (i < waitingTime/delay) {
  243. i++;
  244. try {
  245. await this.checkLoadedFonts();
  246. i = waitingTime;
  247. } catch (e) {
  248. await sleep(delay);
  249. }
  250. }
  251. if (i !== waitingTime) {
  252. this.$notify.error({
  253. title: 'Ошибка загрузки',
  254. message: 'Некоторые шрифты не удалось загрузить'
  255. });
  256. }
  257. this.fontsLoading = false;
  258. if (inst)
  259. inst.close();
  260. }
  261. getSettings() {
  262. const settings = this.settings;
  263. for (let prop in rstore.settingDefaults) {
  264. this[prop] = settings[prop];
  265. }
  266. const wf = this.webFontName;
  267. const i = _.findIndex(rstore.webFonts, ['name', wf]);
  268. if (wf && i >= 0) {
  269. this.fontName = wf;
  270. this.fontCssUrl = rstore.webFonts[i].css;
  271. this.fontVertShift = settings.fontShifts[wf] || 0;
  272. }
  273. }
  274. async calcPropsAndLoadFonts(omitLoadFonts) {
  275. this.calcDrawProps();
  276. this.setBackground();
  277. if (!omitLoadFonts)
  278. await this.loadFonts();
  279. this.draw();
  280. // ширина шрифта некоторое время выдается неверно, поэтому
  281. if (!omitLoadFonts) {
  282. const parsed = this.parsed;
  283. await sleep(100);
  284. if (this.parsed === parsed) {
  285. parsed.force = true;
  286. this.draw();
  287. parsed.force = false;
  288. }
  289. }
  290. }
  291. loadSettings() {
  292. (async() => {
  293. let fontName = this.fontName;
  294. this.getSettings();
  295. await this.calcPropsAndLoadFonts(fontName == this.fontName);
  296. })();
  297. }
  298. showBook() {
  299. this.$refs.main.focus();
  300. this.toggleLayout = false;
  301. this.updateLayout();
  302. this.book = null;
  303. this.meta = null;
  304. this.fb2 = null;
  305. this.parsed = null;
  306. this.linesUp = null;
  307. this.linesDown = null;
  308. this.statusBarMessage = '';
  309. this.getSettings();
  310. this.calcDrawProps();
  311. this.draw();// пока не загрузили, очистим канвас
  312. if (this.lastBook) {
  313. (async() => {
  314. //подождем ленивый парсинг
  315. this.stopLazyParse = true;
  316. while (this.doingLazyParse) await sleep(10);
  317. const isParsed = await bookManager.hasBookParsed(this.lastBook);
  318. if (!isParsed) {
  319. return;
  320. }
  321. this.book = await bookManager.getBook(this.lastBook);
  322. this.meta = bookManager.metaOnly(this.book);
  323. this.fb2 = this.meta.fb2;
  324. let authorNames = [];
  325. if (this.fb2.author) {
  326. authorNames = this.fb2.author.map(a => _.compact([
  327. a.lastName,
  328. a.firstName,
  329. a.middleName
  330. ]).join(' '));
  331. }
  332. this.title = _.compact([
  333. authorNames.join(', '),
  334. this.fb2.bookTitle
  335. ]).join(' - ');
  336. this.$root.$emit('set-app-title', this.title);
  337. this.parsed = this.book.parsed;
  338. this.page1 = null;
  339. this.page2 = null;
  340. this.statusBar = null;
  341. await this.stopTextScrolling();
  342. this.calcPropsAndLoadFonts();
  343. this.refreshTime();
  344. if (this.lazyParseEnabled)
  345. this.lazyParsePara();
  346. })();
  347. }
  348. }
  349. updateLayout() {
  350. if (this.inAnimation) {
  351. this.$refs.scrollBox1.style.visibility = 'visible';
  352. this.$refs.scrollBox2.style.visibility = 'visible';
  353. } else if (this.toggleLayout) {
  354. this.$refs.scrollBox1.style.visibility = 'visible';
  355. this.$refs.scrollBox2.style.visibility = 'hidden';
  356. } else {
  357. this.$refs.scrollBox1.style.visibility = 'hidden';
  358. this.$refs.scrollBox2.style.visibility = 'visible';
  359. }
  360. }
  361. setBackground() {
  362. this.background = `<div class="layout ${this.wallpaper}" style="width: ${this.realWidth}px; height: ${this.realHeight}px;` +
  363. ` background-color: ${this.backgroundColor}"></div>`;
  364. }
  365. async onResize() {
  366. /*this.page1 = null;
  367. this.page2 = null;
  368. this.statusBar = null;*/
  369. this.calcDrawProps();
  370. this.setBackground();
  371. this.draw();
  372. }
  373. get settings() {
  374. return this.$store.state.reader.settings;
  375. }
  376. get font() {
  377. return `${this.fontStyle} ${this.fontWeight} ${this.fontSize}px ${this.fontName}`;
  378. }
  379. onPage1TransitionEnd() {
  380. if (this.resolveTransition1Finish)
  381. this.resolveTransition1Finish();
  382. }
  383. onPage2TransitionEnd() {
  384. if (this.resolveTransition2Finish)
  385. this.resolveTransition2Finish();
  386. }
  387. startSearch(needle) {
  388. this.drawHelper.needle = needle;
  389. this.drawHelper.searching = true;
  390. this.draw();
  391. }
  392. stopSearch() {
  393. this.drawHelper.searching = false;
  394. this.draw();
  395. }
  396. generateWaitingFunc(waitingHandlerName, stopPropertyName) {
  397. const func = (timeout) => {
  398. return new Promise((resolve, reject) => { (async() => {
  399. this[waitingHandlerName] = resolve;
  400. let wait = (timeout + 201)/100;
  401. while (wait > 0 && !this[stopPropertyName]) {
  402. wait--;
  403. await sleep(100);
  404. }
  405. resolve();
  406. })().catch(reject); });
  407. };
  408. return func;
  409. }
  410. async startTextScrolling() {
  411. if (this.doingScrolling || !this.book || !this.parsed.textLength || !this.linesDown || this.pageLineCount < 1 ||
  412. this.linesDown.length <= this.pageLineCount) {
  413. this.$emit('stop-scrolling');
  414. return;
  415. }
  416. //ждем анимацию
  417. while (this.inAnimation) await sleep(10);
  418. this.stopScrolling = false;
  419. this.doingScrolling = true;
  420. const transitionFinish = this.generateWaitingFunc('resolveTransition1Finish', 'stopScrolling');
  421. if (!this.toggleLayout)
  422. this.page1 = this.page2;
  423. this.toggleLayout = true;
  424. await this.$nextTick();
  425. await sleep(50);
  426. this.cachedPos = -1;
  427. this.draw();
  428. const page = this.$refs.scrollingPage1;
  429. let i = 0;
  430. while (!this.stopScrolling) {
  431. page.style.transition = `${this.scrollingDelay}ms ${this.scrollingType}`;
  432. page.style.transform = `translateY(-${this.lineHeight}px)`;
  433. if (i > 0) {
  434. this.doDown();
  435. if (this.linesDown.length <= this.pageLineCount + 1) {
  436. this.stopScrolling = true;
  437. }
  438. }
  439. await transitionFinish(this.scrollingDelay);
  440. page.style.transition = '';
  441. page.style.transform = 'none';
  442. page.offsetHeight;
  443. i++;
  444. }
  445. this.resolveTransition1Finish = null;
  446. this.doingScrolling = false;
  447. this.$emit('stop-scrolling');
  448. this.draw();
  449. }
  450. async stopTextScrolling() {
  451. this.stopScrolling = true;
  452. const page = this.$refs.scrollingPage1;
  453. page.style.transition = '';
  454. page.style.transform = 'none';
  455. page.offsetHeight;
  456. while (this.doingScrolling) await sleep(10);
  457. }
  458. draw() {
  459. //scrolling
  460. if (this.doingScrolling) {
  461. this.currentAnimation = '';
  462. if (this.cachedPos == this.bookPos) {
  463. this.linesDown = this.linesCached.linesDown;
  464. this.linesUp = this.linesCached.linesUp;
  465. this.page1 = this.pageCached;
  466. } else {
  467. const lines = this.getLines(this.bookPos);
  468. this.linesDown = lines.linesDown;
  469. this.linesUp = lines.linesUp;
  470. this.page1 = this.drawHelper.drawPage(lines.linesDown, true);
  471. }
  472. //caching next
  473. if (this.cachedPageTimer)
  474. clearTimeout(this.cachedPageTimer);
  475. this.cachedPageTimer = setTimeout(() => {
  476. if (this.linesDown && this.linesDown.length > this.pageLineCount && this.pageLineCount > 0) {
  477. this.cachedPos = this.linesDown[1].begin;
  478. this.linesCached = this.getLines(this.cachedPos);
  479. this.pageCached = this.drawHelper.drawPage(this.linesCached.linesDown, true);
  480. }
  481. this.cachedPageTimer = null;
  482. }, 20);
  483. this.debouncedDrawStatusBar();
  484. return;
  485. }
  486. //check
  487. if (this.w < minLayoutWidth) {
  488. this.page1 = null;
  489. this.page2 = null;
  490. this.statusBar = null;
  491. return;
  492. }
  493. if (this.book && this.bookPos > 0 && this.bookPos >= this.parsed.textLength) {
  494. this.doEnd(true);
  495. return;
  496. }
  497. //fast draw prepared
  498. if (!this.pageChangeAnimation && this.pageChangeDirectionDown && this.pagePrepared && this.bookPos == this.bookPosPrepared) {
  499. this.toggleLayout = !this.toggleLayout;
  500. this.linesDown = this.linesDownNext;
  501. this.linesUp = this.linesUpNext;
  502. } else {//normal debounced draw
  503. const lines = this.getLines(this.bookPos);
  504. this.linesDown = lines.linesDown;
  505. this.linesUp = lines.linesUp;
  506. this.debouncedUpdatePage(lines.linesDown);
  507. }
  508. this.pagePrepared = false;
  509. if (!this.pageChangeAnimation)
  510. this.debouncedPrepareNextPage();
  511. this.debouncedDrawStatusBar();
  512. if (this.book && this.linesDown && this.linesDown.length < this.pageLineCount) {
  513. this.doEnd(true);
  514. return;
  515. }
  516. }
  517. onPage1AnimationEnd() {
  518. if (this.resolveAnimation1Finish)
  519. this.resolveAnimation1Finish();
  520. }
  521. onPage2AnimationEnd() {
  522. if (this.resolveAnimation2Finish)
  523. this.resolveAnimation2Finish();
  524. }
  525. async doPageAnimation() {
  526. if (this.currentAnimation && !this.inAnimation) {
  527. this.inAnimation = true;
  528. const animation1Finish = this.generateWaitingFunc('resolveAnimation1Finish', 'stopAnimation');
  529. const animation2Finish = this.generateWaitingFunc('resolveAnimation2Finish', 'stopAnimation');
  530. const transition1Finish = this.generateWaitingFunc('resolveTransition1Finish', 'stopAnimation');
  531. const transition2Finish = this.generateWaitingFunc('resolveTransition2Finish', 'stopAnimation');
  532. const duration = Math.round(3000*(1 - this.pageChangeAnimationSpeed/100));
  533. let page1 = this.$refs.scrollingPage1;
  534. let page2 = this.$refs.scrollingPage2;
  535. switch (this.currentAnimation) {
  536. case 'thaw':
  537. await this.drawHelper.doPageAnimationThaw(page1, page2,
  538. duration, this.pageChangeDirectionDown, animation1Finish);
  539. break;
  540. case 'blink':
  541. await this.drawHelper.doPageAnimationBlink(page1, page2,
  542. duration, this.pageChangeDirectionDown, animation1Finish, animation2Finish);
  543. break;
  544. case 'rightShift':
  545. await this.drawHelper.doPageAnimationRightShift(page1, page2,
  546. duration, this.pageChangeDirectionDown, transition1Finish);
  547. break;
  548. case 'downShift':
  549. page1.style.height = this.scrollHeight + 'px';
  550. page2.style.height = this.scrollHeight + 'px';
  551. await this.drawHelper.doPageAnimationDownShift(page1, page2,
  552. duration, this.pageChangeDirectionDown, transition1Finish);
  553. page1.style.height = this.scrollHeight + this.lineHeight + 'px';
  554. page2.style.height = this.scrollHeight + this.lineHeight + 'px';
  555. break;
  556. case 'rotate':
  557. await this.drawHelper.doPageAnimationRotate(page1, page2,
  558. duration, this.pageChangeDirectionDown, transition1Finish, transition2Finish);
  559. break;
  560. case 'flip':
  561. await this.drawHelper.doPageAnimationFlip(page1, page2,
  562. duration, this.pageChangeDirectionDown, transition1Finish, transition2Finish, this.backgroundColor);
  563. break;
  564. }
  565. this.resolveAnimation1Finish = null;
  566. this.resolveAnimation2Finish = null;
  567. this.resolveTransition1Finish = null;
  568. this.resolveTransition2Finish = null;
  569. page1.style.animation = '';
  570. page2.style.animation = '';
  571. page1.style.transition = '';
  572. page1.style.transform = 'none';
  573. page1.offsetHeight;
  574. page2.style.transition = '';
  575. page2.style.transform = 'none';
  576. page2.offsetHeight;
  577. this.currentAnimation = '';
  578. this.pageChangeDirectionDown = false;//true только если PgDown
  579. this.inAnimation = false;
  580. this.stopAnimation = false;
  581. }
  582. }
  583. getLines(bookPos) {
  584. if (!this.parsed || this.pageLineCount < 1)
  585. return {};
  586. return {
  587. linesDown: this.parsed.getLines(bookPos, 2*this.pageLineCount),
  588. linesUp: this.parsed.getLines(bookPos, -2*this.pageLineCount)
  589. };
  590. }
  591. drawStatusBar(message) {
  592. if (this.w < minLayoutWidth) {
  593. this.statusBar = null;
  594. return;
  595. }
  596. if (this.showStatusBar && this.linesDown && this.pageLineCount > 0) {
  597. const lines = this.linesDown;
  598. let i = this.pageLineCount;
  599. if (this.keepLastToFirst)
  600. i--;
  601. i = (i > lines.length - 1 ? lines.length - 1 : i);
  602. if (i >= 0) {
  603. if (!message)
  604. message = this.statusBarMessage;
  605. if (!message)
  606. message = this.title;
  607. this.statusBar = this.drawHelper.drawStatusBar(this.statusBarTop, this.statusBarHeight,
  608. lines[i].end, this.parsed.textLength, message);
  609. this.bookPosSeen = lines[i].end;
  610. }
  611. } else {
  612. this.statusBar = '';
  613. }
  614. }
  615. blinkCachedLoadMessage(state) {
  616. if (state === 'finish') {
  617. this.statusBarMessage = '';
  618. } else if (state) {
  619. this.statusBarMessage = 'Книга загружена из кэша';
  620. } else {
  621. this.statusBarMessage = ' ';
  622. }
  623. this.drawStatusBar();
  624. }
  625. async lazyParsePara() {
  626. if (!this.parsed || this.doingLazyParse)
  627. return;
  628. this.doingLazyParse = true;
  629. let j = 0;
  630. let k = 0;
  631. let prevPerc = 0;
  632. this.stopLazyParse = false;
  633. for (let i = 0; i < this.parsed.para.length; i++) {
  634. j++;
  635. if (j > 1) {
  636. await sleep(1);
  637. j = 0;
  638. }
  639. if (this.stopLazyParse)
  640. break;
  641. this.parsed.parsePara(i);
  642. k++;
  643. if (k > 100) {
  644. let perc = Math.round(i/this.parsed.para.length*100);
  645. if (perc != prevPerc)
  646. this.drawStatusBar(`Обработка текста ${perc}%`);
  647. prevPerc = perc;
  648. k = 0;
  649. }
  650. }
  651. this.drawStatusBar();
  652. this.doingLazyParse = false;
  653. }
  654. async refreshTime() {
  655. if (!this.timeRefreshing) {
  656. this.timeRefreshing = true;
  657. await sleep(60*1000);
  658. if (this.book && this.parsed.textLength) {
  659. this.debouncedDrawStatusBar();
  660. }
  661. this.timeRefreshing = false;
  662. this.refreshTime();
  663. }
  664. }
  665. prepareNextPage() {
  666. // подготовка следующей страницы заранее
  667. if (!this.book || !this.parsed.textLength || !this.linesDown || this.pageLineCount < 1)
  668. return;
  669. let i = this.pageLineCount;
  670. if (this.keepLastToFirst)
  671. i--;
  672. if (i >= 0 && this.linesDown.length > i) {
  673. this.bookPosPrepared = this.linesDown[i].begin;
  674. const lines = this.getLines(this.bookPosPrepared);
  675. this.linesDownNext = lines.linesDown;
  676. this.linesUpNext = lines.linesUp;
  677. if (this.toggleLayout)
  678. this.page2 = this.drawHelper.drawPage(lines.linesDown);//наоборот
  679. else
  680. this.page1 = this.drawHelper.drawPage(lines.linesDown);
  681. this.pagePrepared = true;
  682. }
  683. }
  684. doDown() {
  685. if (this.linesDown && this.linesDown.length > this.pageLineCount && this.pageLineCount > 0) {
  686. this.bookPos = this.linesDown[1].begin;
  687. }
  688. }
  689. doUp() {
  690. if (this.linesUp && this.linesUp.length > 1 && this.pageLineCount > 0) {
  691. this.bookPos = this.linesUp[1].begin;
  692. }
  693. }
  694. doPageDown() {
  695. if (this.linesDown && this.pageLineCount > 0) {
  696. let i = this.pageLineCount;
  697. if (this.keepLastToFirst)
  698. i--;
  699. if (i >= 0 && this.linesDown.length >= 2*i) {
  700. this.currentAnimation = this.pageChangeAnimation;
  701. this.pageChangeDirectionDown = true;
  702. this.bookPos = this.linesDown[i].begin;
  703. } else
  704. this.doEnd();
  705. }
  706. }
  707. doPageUp() {
  708. if (this.linesUp && this.pageLineCount > 0) {
  709. let i = this.pageLineCount;
  710. if (this.keepLastToFirst)
  711. i--;
  712. i = (i > this.linesUp.length - 1 ? this.linesUp.length - 1 : i);
  713. if (i >= 0 && this.linesUp.length > i) {
  714. this.currentAnimation = this.pageChangeAnimation;
  715. this.pageChangeDirectionDown = false;
  716. this.bookPos = this.linesUp[i].begin;
  717. }
  718. }
  719. }
  720. doHome() {
  721. this.currentAnimation = this.pageChangeAnimation;
  722. this.pageChangeDirectionDown = false;
  723. this.bookPos = 0;
  724. }
  725. doEnd(noAni) {
  726. if (this.parsed.para.length && this.pageLineCount > 0) {
  727. let i = this.parsed.para.length - 1;
  728. let lastPos = this.parsed.para[i].offset + this.parsed.para[i].length - 1;
  729. const lines = this.parsed.getLines(lastPos, -this.pageLineCount);
  730. if (lines) {
  731. i = this.pageLineCount - 1;
  732. i = (i > lines.length - 1 ? lines.length - 1 : i);
  733. if (!noAni)
  734. this.currentAnimation = this.pageChangeAnimation;
  735. this.pageChangeDirectionDown = true;
  736. this.bookPos = lines[i].begin;
  737. }
  738. }
  739. }
  740. doToolBarToggle() {
  741. this.$emit('tool-bar-toggle');
  742. }
  743. doScrollingToggle() {
  744. this.$emit('scrolling-toggle');
  745. }
  746. doFullScreenToggle() {
  747. this.$emit('full-screen-toogle');
  748. }
  749. async doFontSizeInc() {
  750. if (!this.settingsChanging) {
  751. this.settingsChanging = true;
  752. const newSize = (this.settings.fontSize + 1 < 100 ? this.settings.fontSize + 1 : 100);
  753. const newSettings = Object.assign({}, this.settings, {fontSize: newSize});
  754. this.commit('reader/setSettings', newSettings);
  755. await sleep(50);
  756. this.settingsChanging = false;
  757. }
  758. }
  759. async doFontSizeDec() {
  760. if (!this.settingsChanging) {
  761. this.settingsChanging = true;
  762. const newSize = (this.settings.fontSize - 1 > 5 ? this.settings.fontSize - 1 : 5);
  763. const newSettings = Object.assign({}, this.settings, {fontSize: newSize});
  764. this.commit('reader/setSettings', newSettings);
  765. await sleep(50);
  766. this.settingsChanging = false;
  767. }
  768. }
  769. async doScrollingSpeedUp() {
  770. if (!this.settingsChanging) {
  771. this.settingsChanging = true;
  772. const newDelay = (this.settings.scrollingDelay - 50 > 1 ? this.settings.scrollingDelay - 50 : 1);
  773. const newSettings = Object.assign({}, this.settings, {scrollingDelay: newDelay});
  774. this.commit('reader/setSettings', newSettings);
  775. await sleep(50);
  776. this.settingsChanging = false;
  777. }
  778. }
  779. async doScrollingSpeedDown() {
  780. if (!this.settingsChanging) {
  781. this.settingsChanging = true;
  782. const newDelay = (this.settings.scrollingDelay + 50 < 10000 ? this.settings.scrollingDelay + 50 : 10000);
  783. const newSettings = Object.assign({}, this.settings, {scrollingDelay: newDelay});
  784. this.commit('reader/setSettings', newSettings);
  785. await sleep(50);
  786. this.settingsChanging = false;
  787. }
  788. }
  789. keyHook(event) {
  790. let result = false;
  791. if (event.type == 'keydown' && !event.ctrlKey && !event.altKey) {
  792. result = true;
  793. switch (event.code) {
  794. case 'ArrowDown':
  795. if (event.shiftKey)
  796. this.doScrollingSpeedUp();
  797. else
  798. this.doDown();
  799. break;
  800. case 'ArrowUp':
  801. if (event.shiftKey)
  802. this.doScrollingSpeedDown();
  803. else
  804. this.doUp();
  805. break;
  806. case 'PageDown':
  807. case 'ArrowRight':
  808. this.doPageDown();
  809. break;
  810. case 'Space':
  811. if (event.shiftKey)
  812. this.doPageUp();
  813. else
  814. this.doPageDown();
  815. break;
  816. case 'PageUp':
  817. case 'ArrowLeft':
  818. case 'Backspace':
  819. this.doPageUp();
  820. break;
  821. case 'Home':
  822. this.doHome();
  823. break;
  824. case 'End':
  825. this.doEnd();
  826. break;
  827. case 'KeyA':
  828. if (event.shiftKey)
  829. this.doFontSizeDec();
  830. else
  831. this.doFontSizeInc();
  832. break;
  833. case 'Enter':
  834. case 'Backquote'://`
  835. case 'KeyF':
  836. this.doFullScreenToggle();
  837. break;
  838. case 'Tab':
  839. case 'KeyQ':
  840. this.doToolBarToggle();
  841. event.preventDefault();
  842. event.stopPropagation();
  843. break;
  844. default:
  845. result = false;
  846. break;
  847. }
  848. }
  849. return result;
  850. }
  851. async startClickRepeat(pointX, pointY) {
  852. this.repX = pointX;
  853. this.repY = pointY;
  854. if (!this.repInit && this.repDoing) {
  855. this.repInit = true;
  856. let delay = 400;
  857. while (this.repDoing) {
  858. this.handleClick(pointX, pointY);
  859. await sleep(delay);
  860. if (delay > 15)
  861. delay *= 0.8;
  862. }
  863. this.repInit = false;
  864. }
  865. }
  866. endClickRepeat() {
  867. this.repDoing = false;
  868. }
  869. onTouchStart(event) {
  870. if (!this.$isMobileDevice)
  871. return;
  872. this.endClickRepeat();
  873. if (event.touches.length == 1) {
  874. const touch = event.touches[0];
  875. const rect = event.target.getBoundingClientRect();
  876. const x = touch.pageX - rect.left;
  877. const y = touch.pageY - rect.top;
  878. const hc = this.handleClick(x, y, new Set(['Menu']));
  879. if (hc) {
  880. if (hc != 'Menu') {
  881. this.repDoing = true;
  882. this.debouncedStartClickRepeat(x, y);
  883. } else {
  884. this.startTouch = {x, y};
  885. }
  886. }
  887. }
  888. }
  889. onTouchMove(event) {
  890. if (this.startTouch) {
  891. event.preventDefault();
  892. }
  893. }
  894. onTouchEnd(event) {
  895. if (!this.$isMobileDevice)
  896. return;
  897. this.endClickRepeat();
  898. if (event.changedTouches.length == 1) {
  899. const touch = event.changedTouches[0];
  900. const rect = event.target.getBoundingClientRect();
  901. const x = touch.pageX - rect.left;
  902. const y = touch.pageY - rect.top;
  903. if (this.startTouch) {
  904. const dy = this.startTouch.y - y;
  905. const dx = this.startTouch.x - x;
  906. const moveDelta = 30;
  907. const touchDelta = 15;
  908. if (dy > 0 && Math.abs(dy) >= moveDelta && Math.abs(dy) > Math.abs(dx)) {
  909. //движение вверх
  910. this.doFullScreenToggle();
  911. } else if (dy < 0 && Math.abs(dy) >= moveDelta && Math.abs(dy) > Math.abs(dx)) {
  912. //движение вниз
  913. this.doScrollingToggle();
  914. } else if (dx > 0 && Math.abs(dx) >= moveDelta && Math.abs(dy) < Math.abs(dx)) {
  915. //движение влево
  916. this.doScrollingSpeedDown();
  917. } else if (dx < 0 && Math.abs(dx) >= moveDelta && Math.abs(dy) < Math.abs(dx)) {
  918. //движение вправо
  919. this.doScrollingSpeedUp();
  920. } else if (Math.abs(dy) < touchDelta && Math.abs(dx) < touchDelta) {
  921. this.doToolBarToggle();
  922. }
  923. this.startTouch = null;
  924. }
  925. }
  926. }
  927. onTouchCancel() {
  928. if (!this.$isMobileDevice)
  929. return;
  930. this.endClickRepeat();
  931. }
  932. onMouseDown(event) {
  933. if (this.$isMobileDevice)
  934. return;
  935. this.endClickRepeat();
  936. if (event.button == 0) {
  937. const hc = this.handleClick(event.offsetX, event.offsetY);
  938. if (hc && hc != 'Menu') {
  939. this.repDoing = true;
  940. this.debouncedStartClickRepeat(event.offsetX, event.offsetY);
  941. }
  942. } else if (event.button == 1) {
  943. this.doScrollingToggle();
  944. } else if (event.button == 2) {
  945. this.doToolBarToggle();
  946. }
  947. }
  948. onMouseUp() {
  949. if (this.$isMobileDevice)
  950. return;
  951. this.endClickRepeat();
  952. }
  953. onMouseWheel(event) {
  954. if (this.$isMobileDevice)
  955. return;
  956. if (event.deltaY > 0) {
  957. this.doDown();
  958. } else if (event.deltaY < 0) {
  959. this.doUp();
  960. }
  961. }
  962. onStatusBarClick() {
  963. const url = this.meta.url;
  964. if (url && url.indexOf('file://') != 0) {
  965. window.open(url, '_blank');
  966. } else {
  967. this.$alert('Оригинал недоступен, т.к. файл книги был загружен с локального диска', '', {type: 'warning'});
  968. }
  969. }
  970. getClickAction(pointX, pointY) {
  971. const w = pointX/this.realWidth*100;
  972. const h = pointY/this.realHeight*100;
  973. let action = '';
  974. loops: {
  975. for (const x in clickMap) {
  976. for (const y in clickMap[x]) {
  977. if (w < x && h < y) {
  978. action = clickMap[x][y];
  979. break loops;
  980. }
  981. }
  982. }
  983. }
  984. return action;
  985. }
  986. handleClick(pointX, pointY, exclude) {
  987. const action = this.getClickAction(pointX, pointY);
  988. if (!exclude || !exclude.has(action)) {
  989. switch (action) {
  990. case 'Down' ://Down
  991. this.doDown();
  992. break;
  993. case 'Up' ://Up
  994. this.doUp();
  995. break;
  996. case 'PgDown' ://PgDown
  997. this.doPageDown();
  998. break;
  999. case 'PgUp' ://PgUp
  1000. this.doPageUp();
  1001. break;
  1002. case 'Menu' :
  1003. this.doToolBarToggle();
  1004. break;
  1005. default :
  1006. // Nothing
  1007. }
  1008. }
  1009. return action;
  1010. }
  1011. }
  1012. //-----------------------------------------------------------------------------
  1013. </script>
  1014. <style scoped>
  1015. .main {
  1016. flex: 1;
  1017. margin: 0;
  1018. padding: 0;
  1019. overflow: hidden;
  1020. position: relative;
  1021. min-width: 200px;
  1022. }
  1023. .layout {
  1024. margin: 0;
  1025. padding: 0;
  1026. position: absolute;
  1027. z-index: 10;
  1028. }
  1029. .over-hidden {
  1030. overflow: hidden;
  1031. }
  1032. .on-top {
  1033. z-index: 100;
  1034. }
  1035. .back {
  1036. z-index: 5;
  1037. }
  1038. .events {
  1039. z-index: 20;
  1040. background-color: rgba(0,0,0,0);
  1041. }
  1042. </style>
  1043. <style>
  1044. .paper1 {
  1045. background: url("images/paper1.jpg") center;
  1046. background-size: cover;
  1047. }
  1048. .paper2 {
  1049. background: url("images/paper2.jpg") center;
  1050. background-size: cover;
  1051. }
  1052. .paper3 {
  1053. background: url("images/paper3.jpg") center;
  1054. background-size: cover;
  1055. }
  1056. .paper4 {
  1057. background: url("images/paper4.jpg") center;
  1058. background-size: cover;
  1059. }
  1060. .paper5 {
  1061. background: url("images/paper5.jpg") center;
  1062. background-size: cover;
  1063. }
  1064. .paper6 {
  1065. background: url("images/paper6.jpg") center;
  1066. background-size: cover;
  1067. }
  1068. .paper7 {
  1069. background: url("images/paper7.jpg") center;
  1070. background-size: cover;
  1071. }
  1072. .paper8 {
  1073. background: url("images/paper8.jpg") center;
  1074. background-size: cover;
  1075. }
  1076. .paper9 {
  1077. background: url("images/paper9.jpg");
  1078. }
  1079. @keyframes page1-animation-thaw {
  1080. 0% { opacity: 0; }
  1081. 100% { opacity: 1; }
  1082. }
  1083. @keyframes page2-animation-thaw {
  1084. 0% { opacity: 1; }
  1085. 100% { opacity: 0; }
  1086. }
  1087. </style>