TextPage.vue 35 KB

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