TextPage.vue 39 KB

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