TextPage.vue 37 KB

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