TextPage.vue 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. <template>
  2. <div ref="main" class="main">
  3. <div class="layout back">
  4. <div v-html="background"></div>
  5. <!-- img -->
  6. </div>
  7. <div v-show="toggleLayout" ref="scrollBox1" class="layout" style="overflow: hidden">
  8. <div ref="scrollingPage" class="layout" @transitionend="onScrollingTransitionEnd">
  9. <div v-html="page1"></div>
  10. </div>
  11. </div>
  12. <div v-show="!toggleLayout" ref="scrollBox2" class="layout" style="overflow: hidden">
  13. <div v-html="page2"></div>
  14. </div>
  15. <div v-show="showStatusBar" ref="statusBar" class="layout">
  16. <div v-html="statusBar"></div>
  17. </div>
  18. <div ref="layoutEvents" class="layout events" @mousedown.prevent.stop="onMouseDown" @mouseup.prevent.stop="onMouseUp"
  19. @wheel.prevent.stop="onMouseWheel"
  20. @touchstart.stop="onTouchStart" @touchend.stop="onTouchEnd" @touchcancel.prevent.stop="onTouchCancel"
  21. oncontextmenu="return false;">
  22. <div v-show="showStatusBar" v-html="statusBarClickable" @mousedown.prevent.stop @touchstart.stop
  23. @click.prevent.stop="onStatusBarClick"></div>
  24. <div v-show="fontsLoading" ref="fontsLoading"></div>
  25. </div>
  26. <!-- невидимым делать нельзя, вовремя не подгружаютя шрифты -->
  27. <canvas ref="offscreenCanvas" class="layout" style="width: 0px; height: 0px"></canvas>
  28. </div>
  29. </template>
  30. <script>
  31. //-----------------------------------------------------------------------------
  32. import Vue from 'vue';
  33. import Component from 'vue-class-component';
  34. import {loadCSS} from 'fg-loadcss';
  35. import _ from 'lodash';
  36. import {sleep} from '../../../share/utils';
  37. import bookManager from '../share/bookManager';
  38. import DrawHelper from './DrawHelper';
  39. import rstore from '../../../store/modules/reader';
  40. const minLayoutWidth = 100;
  41. export default @Component({
  42. watch: {
  43. bookPos: function(newValue) {
  44. this.debouncedEmitPosChange(newValue);
  45. this.draw();
  46. },
  47. settings: function() {
  48. this.debouncedLoadSettings();
  49. },
  50. },
  51. })
  52. class TextPage extends Vue {
  53. toggleLayout = false;
  54. showStatusBar = false;
  55. background = null;
  56. page1 = null;
  57. page2 = null;
  58. statusBar = null;
  59. statusBarClickable = null;
  60. fontsLoading = null;
  61. lastBook = null;
  62. bookPos = 0;
  63. fontStyle = null;
  64. fontSize = null;
  65. fontName = null;
  66. meta = null;
  67. created() {
  68. this.drawHelper = new DrawHelper();
  69. this.commit = this.$store.commit;
  70. this.dispatch = this.$store.dispatch;
  71. this.config = this.$store.state.config;
  72. this.reader = this.$store.state.reader;
  73. this.debouncedEmitPosChange = _.debounce((newValue) => {
  74. this.$emit('book-pos-changed', {bookPos: newValue, bookPosSeen: this.bookPosSeen});
  75. }, 1000);
  76. this.debouncedStartClickRepeat = _.debounce((x, y) => {
  77. this.startClickRepeat(x, y);
  78. }, 800);
  79. this.debouncedPrepareNextPage = _.debounce(() => {
  80. this.prepareNextPage();
  81. }, 100);
  82. this.debouncedDrawStatusBar = _.throttle(() => {
  83. this.drawStatusBar();
  84. }, 60);
  85. this.debouncedLoadSettings = _.debounce(() => {
  86. this.loadSettings();
  87. }, 50);
  88. this.debouncedUpdatePage = _.debounce((lines) => {
  89. this.toggleLayout = !this.toggleLayout;
  90. if (this.toggleLayout)
  91. this.page1 = this.drawPage(lines);
  92. else
  93. this.page2 = this.drawPage(lines);
  94. this.doPageTransition();
  95. }, 10);
  96. this.$root.$on('resize', () => {this.$nextTick(this.onResize)});
  97. this.mobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent);
  98. /*
  99. const settings = Object.assign({}, this.settings);
  100. let updated = false;
  101. for (let prop in rstore.settingDefaults) {
  102. if (!settings.hasOwnProperty(prop)) {
  103. settings[prop] = rstore.settingDefaults[prop];
  104. updated = true;
  105. }
  106. }
  107. if (updated)
  108. this.commit('reader/setSettings', settings);
  109. */
  110. }
  111. mounted() {
  112. this.context = this.$refs.offscreenCanvas.getContext('2d');
  113. }
  114. hex2rgba(hex, alpha = 1) {
  115. const [r, g, b] = hex.match(/\w\w/g).map(x => parseInt(x, 16));
  116. return `rgba(${r},${g},${b},${alpha})`;
  117. }
  118. calcDrawProps() {
  119. //preloaded fonts
  120. this.fontList = [`12px ${this.fontName}`];
  121. //widths
  122. this.realWidth = this.$refs.main.clientWidth;
  123. this.realHeight = this.$refs.main.clientHeight;
  124. this.$refs.layoutEvents.style.width = this.realWidth + 'px';
  125. this.$refs.layoutEvents.style.height = this.realHeight + 'px';
  126. this.w = this.realWidth - 2*this.indentLR;
  127. this.h = this.realHeight - (this.showStatusBar ? this.statusBarHeight : 0) - 2*this.indentTB;
  128. this.lineHeight = this.fontSize + this.lineInterval;
  129. this.pageLineCount = 1 + Math.floor((this.h - this.fontSize)/this.lineHeight);
  130. if (this.parsed) {
  131. this.parsed.p = this.p;
  132. this.parsed.w = this.w;// px, ширина текста
  133. this.parsed.font = this.font;
  134. this.parsed.wordWrap = this.wordWrap;
  135. let t = '';
  136. while (this.measureText(t, {}) < this.w) t += 'Щ';
  137. this.parsed.maxWordLength = t.length - 1;
  138. this.parsed.measureText = this.measureText;
  139. }
  140. //сообщение "Загрузка шрифтов..."
  141. const flText = 'Загрузка шрифта...';
  142. this.$refs.fontsLoading.innerHTML = flText;
  143. const fontsLoadingStyle = this.$refs.fontsLoading.style;
  144. fontsLoadingStyle.position = 'absolute';
  145. fontsLoadingStyle.fontSize = this.fontSize + 'px';
  146. fontsLoadingStyle.top = (this.realHeight/2 - 2*this.fontSize) + 'px';
  147. fontsLoadingStyle.left = (this.realWidth - this.measureText(flText, {}))/2 + 'px';
  148. //stuff
  149. this.statusBarColor = this.hex2rgba(this.textColor || '#000000', this.statusBarColorAlpha);
  150. this.currentTransition = '';
  151. this.pageChangeDirectionDown = true;
  152. this.fontShift = this.fontVertShift/100;
  153. this.textShift = this.textVertShift/100 + this.fontShift;
  154. //drawHelper
  155. this.drawHelper.realWidth = this.realWidth;
  156. this.drawHelper.realHeight = this.realHeight;
  157. this.drawHelper.backgroundColor = this.backgroundColor;
  158. this.drawHelper.statusBarColor = this.statusBarColor;
  159. this.drawHelper.fontName = this.fontName;
  160. this.drawHelper.fontShift = this.fontShift;
  161. this.drawHelper.measureText = this.measureText;
  162. this.drawHelper.measureTextFont = this.measureTextFont;
  163. this.$refs.statusBar.style.left = '0px';
  164. this.$refs.statusBar.style.top = (this.statusBarTop ? 1 : this.realHeight - this.statusBarHeight) + 'px';
  165. this.statusBarClickable = this.drawHelper.statusBarClickable(this.statusBarTop, this.statusBarHeight);
  166. //scrolling page
  167. const pageDelta = this.h - (this.pageLineCount*this.lineHeight - this.lineInterval);
  168. let y = this.indentTB + pageDelta/2;
  169. if (this.showStatusBar)
  170. y += this.statusBarHeight*(this.statusBarTop ? 1 : 0);
  171. const page1 = this.$refs.scrollBox1;
  172. const page2 = this.$refs.scrollBox2;
  173. page1.style.width = this.w + 'px';
  174. page2.style.width = this.w + 'px';
  175. page1.style.height = (this.h - pageDelta) + 'px';
  176. page2.style.height = (this.h - pageDelta) + 'px';
  177. page1.style.top = y + 'px';
  178. page2.style.top = y + 'px';
  179. page1.style.left = this.indentLR + 'px';
  180. page2.style.left = this.indentLR + 'px';
  181. }
  182. measureText(text, style) {// eslint-disable-line no-unused-vars
  183. this.context.font = this.fontByStyle(style);
  184. return this.context.measureText(text).width;
  185. }
  186. measureTextFont(text, font) {// eslint-disable-line no-unused-vars
  187. this.context.font = font;
  188. return this.context.measureText(text).width;
  189. }
  190. async checkLoadedFonts() {
  191. let loaded = await Promise.all(this.fontList.map(font => document.fonts.check(font)));
  192. if (loaded.some(r => !r)) {
  193. loaded = await Promise.all(this.fontList.map(font => document.fonts.load(font)));
  194. if (loaded.some(r => !r.length))
  195. throw new Error('some font not loaded');
  196. }
  197. }
  198. async loadFonts() {
  199. this.fontsLoading = true;
  200. if (!this.fontsLoaded)
  201. this.fontsLoaded = {};
  202. //загрузка дин.шрифта
  203. const loaded = this.fontsLoaded[this.fontCssUrl];
  204. if (this.fontCssUrl && !loaded) {
  205. loadCSS(this.fontCssUrl);
  206. this.fontsLoaded[this.fontCssUrl] = 1;
  207. }
  208. const waitingTime = 10*1000;
  209. const delay = 100;
  210. let i = 0;
  211. //ждем шрифты
  212. while (i < waitingTime/delay) {
  213. i++;
  214. try {
  215. await this.checkLoadedFonts();
  216. i = waitingTime;
  217. } catch (e) {
  218. await sleep(delay);
  219. }
  220. }
  221. if (i !== waitingTime) {
  222. this.$notify.error({
  223. title: 'Ошибка загрузки',
  224. message: 'Некоторые шрифты не удалось загрузить'
  225. });
  226. }
  227. this.fontsLoading = false;
  228. }
  229. getSettings() {
  230. const settings = this.settings;
  231. for (let prop in rstore.settingDefaults) {
  232. this[prop] = settings[prop];
  233. }
  234. const wf = this.webFontName;
  235. const i = _.findIndex(rstore.webFonts, ['name', wf]);
  236. if (wf && i >= 0) {
  237. this.fontName = wf;
  238. this.fontCssUrl = rstore.webFonts[i].css;
  239. this.fontVertShift = settings.fontShifts[wf] || 0;
  240. }
  241. }
  242. async calcPropsAndLoadFonts(omitLoadFonts) {
  243. this.calcDrawProps();
  244. this.setBackground();
  245. if (!omitLoadFonts)
  246. await this.loadFonts();
  247. this.draw();
  248. // шрифты хрен знает когда подгружаются, поэтому
  249. const parsed = this.parsed;
  250. if (!parsed.force) {
  251. let i = 0;
  252. parsed.force = true;
  253. while (i < 10) {
  254. await sleep(1000);
  255. if (this.parsed != parsed)
  256. break;
  257. this.draw();
  258. i++;
  259. }
  260. parsed.force = false;
  261. }
  262. }
  263. loadSettings() {
  264. (async() => {
  265. let fontName = this.fontName;
  266. this.getSettings();
  267. await this.calcPropsAndLoadFonts(fontName == this.fontName);
  268. })();
  269. }
  270. showBook() {
  271. this.$refs.main.focus();
  272. this.toggleLayout = false;
  273. this.book = null;
  274. this.meta = null;
  275. this.fb2 = null;
  276. this.parsed = null;
  277. this.linesUp = null;
  278. this.linesDown = null;
  279. this.getSettings();
  280. this.calcDrawProps();
  281. this.draw();// пока не загрузили, очистим канвас
  282. if (this.lastBook) {
  283. (async() => {
  284. //подождем ленивый парсинг
  285. this.stopLazyParse = true;
  286. while (this.doingLazyParse) await sleep(10);
  287. const isParsed = await bookManager.hasBookParsed(this.lastBook);
  288. if (!isParsed) {
  289. return;
  290. }
  291. this.book = await bookManager.getBook(this.lastBook);
  292. this.meta = bookManager.metaOnly(this.book);
  293. this.fb2 = this.meta.fb2;
  294. const authorName = _.compact([
  295. this.fb2.lastName,
  296. this.fb2.firstName,
  297. this.fb2.middleName
  298. ]).join(' ');
  299. this.title = _.compact([
  300. authorName,
  301. this.fb2.bookTitle
  302. ]).join(' - ');
  303. this.$root.$emit('set-app-title', this.title);
  304. this.parsed = this.book.parsed;
  305. this.page1 = null;
  306. this.page2 = null;
  307. this.statusBar = null;
  308. await this.stopTextScrolling();
  309. this.calcPropsAndLoadFonts();
  310. this.refreshTime();
  311. if (this.lazyParseEnabled)
  312. this.lazyParsePara();
  313. })();
  314. }
  315. }
  316. setBackground() {
  317. this.background = `<div class="layout" style="width: ${this.realWidth}px; height: ${this.realHeight}px;` +
  318. ` background-color: ${this.backgroundColor}"></div>`;
  319. }
  320. async onResize() {
  321. this.page1 = null;
  322. this.page2 = null;
  323. this.statusBar = null;
  324. this.calcDrawProps();
  325. this.setBackground();
  326. this.draw();
  327. }
  328. get settings() {
  329. return this.$store.state.reader.settings;
  330. }
  331. get font() {
  332. return `${this.fontStyle} ${this.fontWeight} ${this.fontSize}px ${this.fontName}`;
  333. }
  334. fontByStyle(style) {
  335. return `${style.italic ? 'italic' : this.fontStyle} ${style.bold ? 'bold' : this.fontWeight} ${this.fontSize}px ${this.fontName}`;
  336. }
  337. onScrollingTransitionEnd() {
  338. if (this.resolveTransitionFinish)
  339. this.resolveTransitionFinish();
  340. }
  341. async startTextScrolling() {
  342. if (this.doingScrolling || !this.book || !this.parsed.textLength || !this.linesDown || this.pageLineCount < 1 ||
  343. this.linesDown.length <= this.pageLineCount) {
  344. this.$emit('stop-scrolling');
  345. return;
  346. }
  347. this.stopScrolling = false;
  348. this.doingScrolling = true;
  349. const transitionFinish = (timeout) => {
  350. return new Promise(async(resolve) => {
  351. this.resolveTransitionFinish = resolve;
  352. let wait = timeout/100;
  353. while (wait > 0 && !this.stopScrolling) {
  354. wait--;
  355. await sleep(100);
  356. }
  357. resolve();
  358. });
  359. };
  360. if (!this.toggleLayout)
  361. this.page1 = this.page2;
  362. this.toggleLayout = true;
  363. await this.$nextTick();
  364. await sleep(50);
  365. this.cachedPos = -1;
  366. const page = this.$refs.scrollingPage;
  367. let i = 0;
  368. while (!this.stopScrolling) {
  369. page.style.transition = `${this.scrollingDelay}ms ${this.scrollingType}`;
  370. page.style.transform = `translateY(-${this.lineHeight}px)`;
  371. if (i > 0) {
  372. this.doDown();
  373. if (this.linesDown.length <= this.pageLineCount + 1) {
  374. this.stopScrolling = true;
  375. }
  376. }
  377. await transitionFinish(this.scrollingDelay + 201);
  378. page.style.transition = '';
  379. page.style.transform = 'none';
  380. page.offsetHeight;
  381. i++;
  382. }
  383. this.resolveTransitionFinish = null;
  384. this.doingScrolling = false;
  385. this.$emit('stop-scrolling');
  386. }
  387. async stopTextScrolling() {
  388. this.stopScrolling = true;
  389. const page = this.$refs.scrollingPage;
  390. page.style.transition = '';
  391. page.style.transform = 'none';
  392. page.offsetHeight;
  393. while (this.doingScrolling) await sleep(10);
  394. }
  395. draw() {
  396. if (this.doingScrolling) {
  397. if (this.cachedPos == this.bookPos) {
  398. this.linesDown = this.linesCached.linesDown;
  399. this.linesUp = this.linesCached.linesUp;
  400. this.page1 = this.pageCached;
  401. } else {
  402. const lines = this.getLines(this.bookPos);
  403. this.linesDown = lines.linesDown;
  404. this.linesUp = lines.linesUp;
  405. this.page1 = this.drawPage(lines.linesDown);
  406. }
  407. //caching next
  408. if (this.cachedPageTimer)
  409. clearTimeout(this.cachedPageTimer);
  410. this.cachedPageTimer = setTimeout(() => {
  411. if (this.linesDown && this.linesDown.length > this.pageLineCount && this.pageLineCount > 0) {
  412. this.cachedPos = this.linesDown[1].begin;
  413. this.linesCached = this.getLines(this.cachedPos);
  414. this.pageCached = this.drawPage(this.linesCached.linesDown);
  415. }
  416. this.cachedPageTimer = null;
  417. }, 20);
  418. this.debouncedDrawStatusBar();
  419. return;
  420. }
  421. if (this.w < minLayoutWidth) {
  422. this.page1 = null;
  423. this.page2 = null;
  424. this.statusBar = null;
  425. return;
  426. }
  427. if (this.book && this.bookPos > 0 && this.bookPos >= this.parsed.textLength) {
  428. this.doEnd();
  429. return;
  430. }
  431. if (this.pageChangeDirectionDown && this.pagePrepared && this.bookPos == this.bookPosPrepared) {
  432. this.toggleLayout = !this.toggleLayout;
  433. this.linesDown = this.linesDownNext;
  434. this.linesUp = this.linesUpNext;
  435. this.doPageTransition();
  436. } else {
  437. const lines = this.getLines(this.bookPos);
  438. this.linesDown = lines.linesDown;
  439. this.linesUp = lines.linesUp;
  440. /*if (this.toggleLayout)
  441. this.page1 = this.drawPage(lines.linesDown);
  442. else
  443. this.page2 = this.drawPage(lines.linesDown);*/
  444. this.debouncedUpdatePage(lines.linesDown);
  445. }
  446. this.pagePrepared = false;
  447. this.debouncedPrepareNextPage();
  448. this.debouncedDrawStatusBar();
  449. if (this.book && this.linesDown && this.linesDown.length < this.pageLineCount)
  450. this.doEnd();
  451. }
  452. doPageTransition() {
  453. if (this.currentTransition) {
  454. //this.currentTransition
  455. //this.pageChangeTransitionSpeed
  456. //this.pageChangeDirectionDown
  457. //curr to next transition
  458. //пока заглушка
  459. }
  460. this.currentTransition = '';
  461. this.pageChangeDirectionDown = false;//true только если PgDown
  462. }
  463. getLines(bookPos) {
  464. if (!this.parsed || this.pageLineCount < 1)
  465. return {};
  466. return {
  467. linesDown: this.parsed.getLines(bookPos, 2*this.pageLineCount),
  468. linesUp: this.parsed.getLines(bookPos, -2*this.pageLineCount)
  469. };
  470. }
  471. drawPage(lines) {
  472. if (!this.lastBook || this.pageLineCount < 1 || !this.book || !lines || !this.parsed.textLength)
  473. return '';
  474. const spaceWidth = this.measureText(' ', {});
  475. let out = `<div class="layout" style="width: ${this.realWidth}px; height: ${this.realHeight}px;` +
  476. ` color: ${this.textColor}">`;
  477. let len = lines.length;
  478. len = (len > this.pageLineCount + 1 ? this.pageLineCount + 1 : len);
  479. let y = this.fontSize*this.textShift;
  480. for (let i = 0; i < len; i++) {
  481. const line = lines[i];
  482. /* line:
  483. {
  484. begin: Number,
  485. end: Number,
  486. first: Boolean,
  487. last: Boolean,
  488. parts: array of {
  489. style: {bold: Boolean, italic: Boolean, center: Boolean}
  490. text: String,
  491. }
  492. }*/
  493. let indent = line.first ? this.p : 0;
  494. let lineText = '';
  495. let center = false;
  496. let centerStyle = {};
  497. for (const part of line.parts) {
  498. lineText += part.text;
  499. center = center || part.style.center;
  500. if (part.style.center)
  501. centerStyle = part.style;
  502. }
  503. let filled = false;
  504. // если выравнивание по ширине включено
  505. if (this.textAlignJustify && !line.last && !center) {
  506. const words = lineText.split(' ');
  507. if (words.length > 1) {
  508. const spaceCount = words.length - 1;
  509. const space = (this.w - line.width + spaceWidth*spaceCount)/spaceCount;
  510. let x = indent;
  511. for (const part of line.parts) {
  512. const font = this.fontByStyle(part.style);
  513. let partWords = part.text.split(' ');
  514. for (let i = 0; i < partWords.length; i++) {
  515. let word = partWords[i];
  516. out += this.drawHelper.fillText(word, x, y, font);
  517. x += this.measureText(word, part.style) + (i < partWords.length - 1 ? space : 0);
  518. }
  519. }
  520. filled = true;
  521. }
  522. }
  523. // просто выводим текст
  524. if (!filled) {
  525. let x = indent;
  526. x = (center ? (this.w - this.measureText(lineText, centerStyle))/2 : x);
  527. for (const part of line.parts) {
  528. let text = part.text;
  529. const font = this.fontByStyle(part.style);
  530. out += this.drawHelper.fillText(text, x, y, font);
  531. x += this.measureText(text, part.style);
  532. }
  533. }
  534. y += this.lineHeight;
  535. }
  536. out += '</div>';
  537. return out;
  538. }
  539. drawStatusBar(message) {
  540. if (this.w < minLayoutWidth) {
  541. this.statusBar = null;
  542. return;
  543. }
  544. if (this.showStatusBar && this.linesDown && this.pageLineCount > 0) {
  545. const lines = this.linesDown;
  546. let i = this.pageLineCount;
  547. if (this.keepLastToFirst)
  548. i--;
  549. i = (i > lines.length - 1 ? lines.length - 1 : i);
  550. if (i >= 0) {
  551. if (!message)
  552. message = this.statusBarMessage;
  553. if (!message)
  554. message = this.title;
  555. this.statusBar = this.drawHelper.drawStatusBar(this.statusBarTop, this.statusBarHeight,
  556. lines[i].end, this.parsed.textLength, message);
  557. this.bookPosSeen = lines[i].end;
  558. }
  559. } else {
  560. this.statusBar = '';
  561. }
  562. }
  563. blinkCachedLoadMessage(state) {
  564. if (state === 'finish') {
  565. this.statusBarMessage = '';
  566. } else if (state) {
  567. this.statusBarMessage = 'Книга загружена из кеша';
  568. } else {
  569. this.statusBarMessage = ' ';
  570. }
  571. this.drawStatusBar();
  572. }
  573. async lazyParsePara() {
  574. if (!this.parsed || this.doingLazyParse)
  575. return;
  576. this.doingLazyParse = true;
  577. let j = 0;
  578. let k = 0;
  579. let prevPerc = 0;
  580. this.stopLazyParse = false;
  581. for (let i = 0; i < this.parsed.para.length; i++) {
  582. j++;
  583. if (j > 1) {
  584. await sleep(1);
  585. j = 0;
  586. }
  587. if (this.stopLazyParse)
  588. break;
  589. this.parsed.parsePara(i);
  590. k++;
  591. if (k > 100) {
  592. let perc = Math.round(i/this.parsed.para.length*100);
  593. if (perc != prevPerc)
  594. this.drawStatusBar(`Обработка текста ${perc}%`);
  595. prevPerc = perc;
  596. k = 0;
  597. }
  598. }
  599. this.drawStatusBar();
  600. this.doingLazyParse = false;
  601. }
  602. async refreshTime() {
  603. if (!this.timeRefreshing) {
  604. this.timeRefreshing = true;
  605. await sleep(60*1000);
  606. if (this.book && this.parsed.textLength) {
  607. this.debouncedDrawStatusBar();
  608. }
  609. this.timeRefreshing = false;
  610. this.refreshTime();
  611. }
  612. }
  613. prepareNextPage() {
  614. // подготовка следующей страницы заранее
  615. if (!this.book || !this.parsed.textLength || !this.linesDown || this.pageLineCount < 1)
  616. return;
  617. let i = this.pageLineCount;
  618. if (this.keepLastToFirst)
  619. i--;
  620. if (i >= 0 && this.linesDown.length > i) {
  621. this.bookPosPrepared = this.linesDown[i].begin;
  622. const lines = this.getLines(this.bookPosPrepared);
  623. this.linesDownNext = lines.linesDown;
  624. this.linesUpNext = lines.linesUp;
  625. if (this.toggleLayout)
  626. this.page2 = this.drawPage(lines.linesDown);//наоборот
  627. else
  628. this.page1 = this.drawPage(lines.linesDown);
  629. this.pagePrepared = true;
  630. }
  631. }
  632. doDown() {
  633. if (this.linesDown && this.linesDown.length > this.pageLineCount && this.pageLineCount > 0) {
  634. this.bookPos = this.linesDown[1].begin;
  635. }
  636. }
  637. doUp() {
  638. if (this.linesUp && this.linesUp.length > 1 && this.pageLineCount > 0) {
  639. this.bookPos = this.linesUp[1].begin;
  640. }
  641. }
  642. doPageDown() {
  643. if (this.linesDown && this.pageLineCount > 0) {
  644. let i = this.pageLineCount;
  645. if (this.keepLastToFirst)
  646. i--;
  647. if (i >= 0 && this.linesDown.length >= 2*i) {
  648. this.currentTransition = this.pageChangeTransition;
  649. this.pageChangeDirectionDown = true;
  650. this.bookPos = this.linesDown[i].begin;
  651. } else
  652. this.doEnd();
  653. }
  654. }
  655. doPageUp() {
  656. if (this.linesUp && this.pageLineCount > 0) {
  657. let i = this.pageLineCount;
  658. if (this.keepLastToFirst)
  659. i--;
  660. i = (i > this.linesUp.length - 1 ? this.linesUp.length - 1 : i);
  661. if (i >= 0 && this.linesUp.length > i) {
  662. this.currentTransition = this.pageChangeTransition;
  663. this.pageChangeDirectionDown = false;
  664. this.bookPos = this.linesUp[i].begin;
  665. }
  666. }
  667. }
  668. doHome() {
  669. this.bookPos = 0;
  670. }
  671. doEnd() {
  672. if (this.parsed.para.length && this.pageLineCount > 0) {
  673. let i = this.parsed.para.length - 1;
  674. let lastPos = this.parsed.para[i].offset + this.parsed.para[i].length - 1;
  675. const lines = this.parsed.getLines(lastPos, -this.pageLineCount);
  676. if (lines) {
  677. i = this.pageLineCount - 1;
  678. i = (i > lines.length - 1 ? lines.length - 1 : i);
  679. this.bookPos = lines[i].begin;
  680. }
  681. }
  682. }
  683. doToolBarToggle() {
  684. this.$emit('tool-bar-toggle');
  685. }
  686. keyHook(event) {
  687. //console.log(event.code);
  688. if (event.type == 'keydown') {
  689. switch (event.code) {
  690. case 'ArrowDown':
  691. this.doDown();
  692. break;
  693. case 'ArrowUp':
  694. this.doUp();
  695. break;
  696. case 'PageDown':
  697. case 'ArrowRight':
  698. case 'Space':
  699. this.doPageDown();
  700. break;
  701. case 'PageUp':
  702. case 'ArrowLeft':
  703. case 'Backspace':
  704. this.doPageUp();
  705. break;
  706. case 'Home':
  707. this.doHome();
  708. break;
  709. case 'End':
  710. this.doEnd();
  711. break;
  712. case 'Enter':
  713. case 'Backquote'://`
  714. case 'KeyF':
  715. this.$emit('full-screen-toogle');
  716. break;
  717. case 'Tab':
  718. this.doToolBarToggle();
  719. event.preventDefault();
  720. event.stopPropagation();
  721. break;
  722. }
  723. }
  724. }
  725. async startClickRepeat(pointX, pointY) {
  726. this.repX = pointX;
  727. this.repY = pointY;
  728. if (!this.repInit && this.repDoing) {
  729. this.repInit = true;
  730. let delay = 400;
  731. while (this.repDoing) {
  732. this.handleClick(pointX, pointY);
  733. await sleep(delay);
  734. if (delay > 15)
  735. delay *= 0.8;
  736. }
  737. this.repInit = false;
  738. }
  739. }
  740. endClickRepeat() {
  741. this.repDoing = false;
  742. }
  743. onTouchStart(event) {
  744. if (!this.mobile)
  745. return;
  746. this.endClickRepeat();
  747. if (event.touches.length == 1) {
  748. const touch = event.touches[0];
  749. const rect = event.target.getBoundingClientRect();
  750. const x = touch.pageX - rect.left;
  751. const y = touch.pageY - rect.top;
  752. if (this.handleClick(x, y)) {
  753. this.repDoing = true;
  754. this.debouncedStartClickRepeat(x, y);
  755. }
  756. }
  757. }
  758. onTouchEnd() {
  759. if (!this.mobile)
  760. return;
  761. this.endClickRepeat();
  762. }
  763. onTouchCancel() {
  764. if (!this.mobile)
  765. return;
  766. this.endClickRepeat();
  767. }
  768. onMouseDown(event) {
  769. if (this.mobile)
  770. return;
  771. this.endClickRepeat();
  772. if (event.button == 0) {
  773. if (this.handleClick(event.offsetX, event.offsetY)) {
  774. this.repDoing = true;
  775. this.debouncedStartClickRepeat(event.offsetX, event.offsetY);
  776. }
  777. } else if (event.button == 1) {
  778. this.$emit('scrolling-toggle');
  779. } else if (event.button == 2) {
  780. this.doToolBarToggle();
  781. }
  782. }
  783. onMouseUp() {
  784. if (this.mobile)
  785. return;
  786. this.endClickRepeat();
  787. }
  788. onMouseWheel(event) {
  789. if (this.mobile)
  790. return;
  791. if (event.deltaY > 0) {
  792. this.doDown();
  793. } else if (event.deltaY < 0) {
  794. this.doUp();
  795. }
  796. }
  797. onStatusBarClick() {
  798. window.open(this.meta.url, '_blank');
  799. }
  800. handleClick(pointX, pointY) {
  801. const mouseLegend = {
  802. 40: {30: 'PgUp', 100: 'PgDown'},
  803. 60: {40: 'Up', 60: 'Menu', 100: 'Down'},
  804. 100: {30: 'PgUp', 100: 'PgDown'}
  805. };
  806. const w = pointX/this.realWidth*100;
  807. const h = pointY/this.realHeight*100;
  808. let action = '';
  809. loops: {
  810. for (const x in mouseLegend) {
  811. for (const y in mouseLegend[x]) {
  812. if (w < x && h < y) {
  813. action = mouseLegend[x][y];
  814. break loops;
  815. }
  816. }
  817. }
  818. }
  819. switch (action) {
  820. case 'Down' ://Down
  821. this.doDown();
  822. break;
  823. case 'Up' ://Up
  824. this.doUp();
  825. break;
  826. case 'PgDown' ://PgDown
  827. this.doPageDown();
  828. break;
  829. case 'PgUp' ://PgUp
  830. this.doPageUp();
  831. break;
  832. case 'Menu' :
  833. this.doToolBarToggle();
  834. break;
  835. default :
  836. // Nothing
  837. }
  838. return (action && action != 'Menu');
  839. }
  840. }
  841. //-----------------------------------------------------------------------------
  842. </script>
  843. <style scoped>
  844. .main {
  845. flex: 1;
  846. margin: 0;
  847. padding: 0;
  848. overflow: hidden;
  849. position: relative;
  850. min-width: 200px;
  851. }
  852. .layout {
  853. margin: 0;
  854. padding: 0;
  855. position: absolute;
  856. z-index: 10;
  857. }
  858. .back {
  859. z-index: 5;
  860. }
  861. .events {
  862. z-index: 20;
  863. background-color: rgba(0,0,0,0);
  864. }
  865. </style>