TextPage.vue 36 KB

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