TextPage.vue 38 KB

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