TextPage.vue 41 KB

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