TextPage.vue 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. <template>
  2. <div ref="main" class="main">
  3. <div class="layout back" @wheel.prevent.stop="onMouseWheel">
  4. <div v-html="background"></div>
  5. <!-- img -->
  6. </div>
  7. <div ref="scrollBox1" class="layout" 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.fontSize = this.fontSize;
  182. this.parsed.wordWrap = this.wordWrap;
  183. this.parsed.cutEmptyParagraphs = this.cutEmptyParagraphs;
  184. this.parsed.addEmptyParagraphs = this.addEmptyParagraphs;
  185. let t = '';
  186. while (this.drawHelper.measureText(t, {}) < this.w) t += 'Щ';
  187. this.parsed.maxWordLength = t.length - 1;
  188. this.parsed.measureText = this.drawHelper.measureText.bind(this.drawHelper);
  189. this.parsed.lineHeight = this.lineHeight;
  190. this.parsed.showImages = this.showImages;
  191. this.parsed.showInlineImagesInCenter = this.showInlineImagesInCenter;
  192. this.parsed.imageHeightLines = this.imageHeightLines;
  193. }
  194. //statusBar
  195. this.$refs.statusBar.style.left = '0px';
  196. this.$refs.statusBar.style.top = (this.statusBarTop ? 1 : this.realHeight - this.statusBarHeight) + 'px';
  197. this.statusBarColor = this.hex2rgba(this.textColor || '#000000', this.statusBarColorAlpha);
  198. this.statusBarClickable = this.drawHelper.statusBarClickable(this.statusBarTop, this.statusBarHeight);
  199. //scrolling page
  200. const pageDelta = this.h - (this.pageLineCount*this.lineHeight - this.lineInterval);
  201. let y = this.indentTB + pageDelta/2;
  202. if (this.showStatusBar)
  203. y += this.statusBarHeight*(this.statusBarTop ? 1 : 0);
  204. const page1 = this.$refs.scrollBox1;
  205. const page2 = this.$refs.scrollBox2;
  206. page1.style.width = this.w + 'px';
  207. page2.style.width = this.w + 'px';
  208. page1.style.height = (this.h - pageDelta) + 'px';
  209. page2.style.height = (this.h - pageDelta) + 'px';
  210. page1.style.top = y + 'px';
  211. page2.style.top = y + 'px';
  212. page1.style.left = this.indentLR + 'px';
  213. page2.style.left = this.indentLR + 'px';
  214. }
  215. async checkLoadedFonts() {
  216. let loaded = await Promise.all(this.fontList.map(font => document.fonts.check(font)));
  217. if (loaded.some(r => !r)) {
  218. loaded = await Promise.all(this.fontList.map(font => document.fonts.load(font)));
  219. if (loaded.some(r => !r.length))
  220. throw new Error('some font not loaded');
  221. }
  222. }
  223. async loadFonts() {
  224. this.fontsLoading = true;
  225. if (!this.fontsLoaded)
  226. this.fontsLoaded = {};
  227. //загрузка дин.шрифта
  228. const loaded = this.fontsLoaded[this.fontCssUrl];
  229. if (this.fontCssUrl && !loaded) {
  230. loadCSS(this.fontCssUrl);
  231. this.fontsLoaded[this.fontCssUrl] = 1;
  232. }
  233. const waitingTime = 10*1000;
  234. const delay = 100;
  235. let i = 0;
  236. //ждем шрифты
  237. while (i < waitingTime/delay) {
  238. i++;
  239. try {
  240. await this.checkLoadedFonts();
  241. i = waitingTime;
  242. } catch (e) {
  243. await sleep(delay);
  244. }
  245. }
  246. if (i !== waitingTime) {
  247. this.$notify.error({
  248. title: 'Ошибка загрузки',
  249. message: 'Некоторые шрифты не удалось загрузить'
  250. });
  251. }
  252. this.fontsLoading = false;
  253. }
  254. getSettings() {
  255. const settings = this.settings;
  256. for (let prop in rstore.settingDefaults) {
  257. this[prop] = settings[prop];
  258. }
  259. const wf = this.webFontName;
  260. const i = _.findIndex(rstore.webFonts, ['name', wf]);
  261. if (wf && i >= 0) {
  262. this.fontName = wf;
  263. this.fontCssUrl = rstore.webFonts[i].css;
  264. this.fontVertShift = settings.fontShifts[wf] || 0;
  265. }
  266. }
  267. async calcPropsAndLoadFonts(omitLoadFonts) {
  268. this.calcDrawProps();
  269. this.setBackground();
  270. if (!omitLoadFonts)
  271. await this.loadFonts();
  272. this.draw();
  273. // шрифты хрен знает когда подгружаются в div, поэтому
  274. const parsed = this.parsed;
  275. await sleep(5000);
  276. if (this.parsed === parsed) {
  277. parsed.force = true;
  278. this.draw();
  279. parsed.force = false;
  280. }
  281. }
  282. loadSettings() {
  283. (async() => {
  284. let fontName = this.fontName;
  285. this.getSettings();
  286. await this.calcPropsAndLoadFonts(fontName == this.fontName);
  287. })();
  288. }
  289. showBook() {
  290. this.$refs.main.focus();
  291. this.toggleLayout = false;
  292. this.updateLayout();
  293. this.book = null;
  294. this.meta = null;
  295. this.fb2 = null;
  296. this.parsed = null;
  297. this.linesUp = null;
  298. this.linesDown = null;
  299. this.statusBarMessage = '';
  300. this.getSettings();
  301. this.calcDrawProps();
  302. this.draw();// пока не загрузили, очистим канвас
  303. if (this.lastBook) {
  304. (async() => {
  305. //подождем ленивый парсинг
  306. this.stopLazyParse = true;
  307. while (this.doingLazyParse) await sleep(10);
  308. const isParsed = await bookManager.hasBookParsed(this.lastBook);
  309. if (!isParsed) {
  310. return;
  311. }
  312. this.book = await bookManager.getBook(this.lastBook);
  313. this.meta = bookManager.metaOnly(this.book);
  314. this.fb2 = this.meta.fb2;
  315. const authorName = _.compact([
  316. this.fb2.lastName,
  317. this.fb2.firstName,
  318. this.fb2.middleName
  319. ]).join(' ');
  320. this.title = _.compact([
  321. authorName,
  322. this.fb2.bookTitle
  323. ]).join(' - ');
  324. this.$root.$emit('set-app-title', this.title);
  325. this.parsed = this.book.parsed;
  326. this.page1 = null;
  327. this.page2 = null;
  328. this.statusBar = null;
  329. await this.stopTextScrolling();
  330. this.calcPropsAndLoadFonts();
  331. this.refreshTime();
  332. if (this.lazyParseEnabled)
  333. this.lazyParsePara();
  334. })();
  335. }
  336. }
  337. updateLayout() {
  338. if (this.inAnimation) {
  339. this.$refs.scrollBox1.style.visibility = 'visible';
  340. this.$refs.scrollBox2.style.visibility = 'visible';
  341. } else if (this.toggleLayout) {
  342. this.$refs.scrollBox1.style.visibility = 'visible';
  343. this.$refs.scrollBox2.style.visibility = 'hidden';
  344. } else {
  345. this.$refs.scrollBox1.style.visibility = 'hidden';
  346. this.$refs.scrollBox2.style.visibility = 'visible';
  347. }
  348. }
  349. setBackground() {
  350. this.background = `<div class="layout ${this.wallpaper}" style="width: ${this.realWidth}px; height: ${this.realHeight}px;` +
  351. ` background-color: ${this.backgroundColor}"></div>`;
  352. }
  353. async onResize() {
  354. /*this.page1 = null;
  355. this.page2 = null;
  356. this.statusBar = null;*/
  357. this.calcDrawProps();
  358. this.setBackground();
  359. this.draw();
  360. }
  361. get settings() {
  362. return this.$store.state.reader.settings;
  363. }
  364. get font() {
  365. return `${this.fontStyle} ${this.fontWeight} ${this.fontSize}px ${this.fontName}`;
  366. }
  367. onPage1TransitionEnd() {
  368. if (this.resolveTransition1Finish)
  369. this.resolveTransition1Finish();
  370. }
  371. onPage2TransitionEnd() {
  372. if (this.resolveTransition2Finish)
  373. this.resolveTransition2Finish();
  374. }
  375. startSearch(needle) {
  376. this.drawHelper.needle = needle;
  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. case 'KeyQ':
  810. this.doToolBarToggle();
  811. event.preventDefault();
  812. event.stopPropagation();
  813. break;
  814. default:
  815. result = false;
  816. break;
  817. }
  818. }
  819. return result;
  820. }
  821. async startClickRepeat(pointX, pointY) {
  822. this.repX = pointX;
  823. this.repY = pointY;
  824. if (!this.repInit && this.repDoing) {
  825. this.repInit = true;
  826. let delay = 400;
  827. while (this.repDoing) {
  828. this.handleClick(pointX, pointY);
  829. await sleep(delay);
  830. if (delay > 15)
  831. delay *= 0.8;
  832. }
  833. this.repInit = false;
  834. }
  835. }
  836. endClickRepeat() {
  837. this.repDoing = false;
  838. }
  839. onTouchStart(event) {
  840. if (!this.mobile)
  841. return;
  842. this.endClickRepeat();
  843. if (event.touches.length == 1) {
  844. const touch = event.touches[0];
  845. const rect = event.target.getBoundingClientRect();
  846. const x = touch.pageX - rect.left;
  847. const y = touch.pageY - rect.top;
  848. if (this.handleClick(x, y)) {
  849. this.repDoing = true;
  850. this.debouncedStartClickRepeat(x, y);
  851. }
  852. }
  853. }
  854. onTouchEnd() {
  855. if (!this.mobile)
  856. return;
  857. this.endClickRepeat();
  858. }
  859. onTouchCancel() {
  860. if (!this.mobile)
  861. return;
  862. this.endClickRepeat();
  863. }
  864. onMouseDown(event) {
  865. if (this.mobile)
  866. return;
  867. this.endClickRepeat();
  868. if (event.button == 0) {
  869. if (this.handleClick(event.offsetX, event.offsetY)) {
  870. this.repDoing = true;
  871. this.debouncedStartClickRepeat(event.offsetX, event.offsetY);
  872. }
  873. } else if (event.button == 1) {
  874. this.$emit('scrolling-toggle');
  875. } else if (event.button == 2) {
  876. this.doToolBarToggle();
  877. }
  878. }
  879. onMouseUp() {
  880. if (this.mobile)
  881. return;
  882. this.endClickRepeat();
  883. }
  884. onMouseWheel(event) {
  885. if (this.mobile)
  886. return;
  887. if (event.deltaY > 0) {
  888. this.doDown();
  889. } else if (event.deltaY < 0) {
  890. this.doUp();
  891. }
  892. }
  893. onStatusBarClick() {
  894. const url = this.meta.url;
  895. if (url && url.indexOf('file://') != 0) {
  896. window.open(url, '_blank');
  897. } else {
  898. this.$alert('Оригинал недоступен, т.к. файл книги был загружен с локального диска', '', {type: 'warning'});
  899. }
  900. }
  901. handleClick(pointX, pointY) {
  902. const w = pointX/this.realWidth*100;
  903. const h = pointY/this.realHeight*100;
  904. let action = '';
  905. loops: {
  906. for (const x in clickMap) {
  907. for (const y in clickMap[x]) {
  908. if (w < x && h < y) {
  909. action = clickMap[x][y];
  910. break loops;
  911. }
  912. }
  913. }
  914. }
  915. switch (action) {
  916. case 'Down' ://Down
  917. this.doDown();
  918. break;
  919. case 'Up' ://Up
  920. this.doUp();
  921. break;
  922. case 'PgDown' ://PgDown
  923. this.doPageDown();
  924. break;
  925. case 'PgUp' ://PgUp
  926. this.doPageUp();
  927. break;
  928. case 'Menu' :
  929. this.doToolBarToggle();
  930. break;
  931. default :
  932. // Nothing
  933. }
  934. return (action && action != 'Menu');
  935. }
  936. }
  937. //-----------------------------------------------------------------------------
  938. </script>
  939. <style scoped>
  940. .main {
  941. flex: 1;
  942. margin: 0;
  943. padding: 0;
  944. overflow: hidden;
  945. position: relative;
  946. min-width: 200px;
  947. }
  948. .layout {
  949. margin: 0;
  950. padding: 0;
  951. position: absolute;
  952. z-index: 10;
  953. }
  954. .back {
  955. z-index: 5;
  956. }
  957. .events {
  958. z-index: 20;
  959. background-color: rgba(0,0,0,0);
  960. }
  961. </style>
  962. <style>
  963. .paper1 {
  964. background: url("images/paper1.jpg") center;
  965. background-size: cover;
  966. }
  967. .paper2 {
  968. background: url("images/paper2.jpg") center;
  969. background-size: cover;
  970. }
  971. .paper3 {
  972. background: url("images/paper3.jpg") center;
  973. background-size: cover;
  974. }
  975. .paper4 {
  976. background: url("images/paper4.jpg") center;
  977. background-size: cover;
  978. }
  979. .paper5 {
  980. background: url("images/paper5.jpg") center;
  981. background-size: cover;
  982. }
  983. .paper6 {
  984. background: url("images/paper6.jpg") center;
  985. background-size: cover;
  986. }
  987. .paper7 {
  988. background: url("images/paper7.jpg") center;
  989. background-size: cover;
  990. }
  991. .paper8 {
  992. background: url("images/paper8.jpg") center;
  993. background-size: cover;
  994. }
  995. .paper9 {
  996. background: url("images/paper9.jpg");
  997. }
  998. @keyframes page1-animation-thaw {
  999. 0% { opacity: 0; }
  1000. 100% { opacity: 1; }
  1001. }
  1002. @keyframes page2-animation-thaw {
  1003. 0% { opacity: 1; }
  1004. 100% { opacity: 0; }
  1005. }
  1006. </style>