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