TextPage.vue 38 KB

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