TextPage.vue 39 KB

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