TextPage.vue 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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 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 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.$emit('book-pos-changed', {bookPos: newValue, bookPosSeen: this.bookPosSeen});
  45. this.draw();
  46. },
  47. settings: function() {
  48. this.debouncedLoadSettings();
  49. },
  50. toggleLayout: function() {
  51. this.updateLayout();
  52. },
  53. },
  54. })
  55. class TextPage extends Vue {
  56. toggleLayout = false;
  57. showStatusBar = false;
  58. background = null;
  59. page1 = null;
  60. page2 = null;
  61. statusBar = null;
  62. statusBarClickable = null;
  63. fontsLoading = null;
  64. lastBook = null;
  65. bookPos = 0;
  66. fontStyle = null;
  67. fontSize = null;
  68. fontName = null;
  69. meta = null;
  70. created() {
  71. this.drawHelper = new DrawHelper();
  72. this.commit = this.$store.commit;
  73. this.dispatch = this.$store.dispatch;
  74. this.config = this.$store.state.config;
  75. this.reader = this.$store.state.reader;
  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.updateLayout();
  274. this.book = null;
  275. this.meta = null;
  276. this.fb2 = null;
  277. this.parsed = null;
  278. this.linesUp = null;
  279. this.linesDown = null;
  280. this.searching = false;
  281. this.statusBarMessage = '';
  282. this.getSettings();
  283. this.calcDrawProps();
  284. this.draw();// пока не загрузили, очистим канвас
  285. if (this.lastBook) {
  286. (async() => {
  287. //подождем ленивый парсинг
  288. this.stopLazyParse = true;
  289. while (this.doingLazyParse) await sleep(10);
  290. const isParsed = await bookManager.hasBookParsed(this.lastBook);
  291. if (!isParsed) {
  292. return;
  293. }
  294. this.book = await bookManager.getBook(this.lastBook);
  295. this.meta = bookManager.metaOnly(this.book);
  296. this.fb2 = this.meta.fb2;
  297. const authorName = _.compact([
  298. this.fb2.lastName,
  299. this.fb2.firstName,
  300. this.fb2.middleName
  301. ]).join(' ');
  302. this.title = _.compact([
  303. authorName,
  304. this.fb2.bookTitle
  305. ]).join(' - ');
  306. this.$root.$emit('set-app-title', this.title);
  307. this.parsed = this.book.parsed;
  308. this.page1 = null;
  309. this.page2 = null;
  310. this.statusBar = null;
  311. await this.stopTextScrolling();
  312. this.calcPropsAndLoadFonts();
  313. this.refreshTime();
  314. if (this.lazyParseEnabled)
  315. this.lazyParsePara();
  316. })();
  317. }
  318. }
  319. updateLayout() {
  320. if (this.toggleLayout) {
  321. this.$refs.scrollBox1.style.visibility = 'visible';
  322. this.$refs.scrollBox2.style.visibility = 'hidden';
  323. } else {
  324. this.$refs.scrollBox1.style.visibility = 'hidden';
  325. this.$refs.scrollBox2.style.visibility = 'visible';
  326. }
  327. }
  328. setBackground() {
  329. this.background = `<div class="layout ${this.wallpaper}" style="width: ${this.realWidth}px; height: ${this.realHeight}px;` +
  330. ` background-color: ${this.backgroundColor}"></div>`;
  331. }
  332. async onResize() {
  333. /*this.page1 = null;
  334. this.page2 = null;
  335. this.statusBar = null;*/
  336. this.calcDrawProps();
  337. this.setBackground();
  338. this.draw();
  339. }
  340. get settings() {
  341. return this.$store.state.reader.settings;
  342. }
  343. get font() {
  344. return `${this.fontStyle} ${this.fontWeight} ${this.fontSize}px ${this.fontName}`;
  345. }
  346. fontByStyle(style) {
  347. return `${style.italic ? 'italic' : this.fontStyle} ${style.bold ? 'bold' : this.fontWeight} ${this.fontSize}px ${this.fontName}`;
  348. }
  349. onScrollingTransitionEnd() {
  350. if (this.resolveTransitionFinish)
  351. this.resolveTransitionFinish();
  352. }
  353. startSearch(needle) {
  354. this.needle = '';
  355. const words = needle.split(' ');
  356. for (const word of words) {
  357. if (word != '') {
  358. this.needle = word;
  359. break;
  360. }
  361. }
  362. this.searching = true;
  363. this.draw();
  364. }
  365. stopSearch() {
  366. this.searching = false;
  367. this.draw();
  368. }
  369. async startTextScrolling() {
  370. if (this.doingScrolling || !this.book || !this.parsed.textLength || !this.linesDown || this.pageLineCount < 1 ||
  371. this.linesDown.length <= this.pageLineCount) {
  372. this.$emit('stop-scrolling');
  373. return;
  374. }
  375. this.stopScrolling = false;
  376. this.doingScrolling = true;
  377. const transitionFinish = (timeout) => {
  378. return new Promise(async(resolve) => {
  379. this.resolveTransitionFinish = resolve;
  380. let wait = timeout/100;
  381. while (wait > 0 && !this.stopScrolling) {
  382. wait--;
  383. await sleep(100);
  384. }
  385. resolve();
  386. });
  387. };
  388. if (!this.toggleLayout)
  389. this.page1 = this.page2;
  390. this.toggleLayout = true;
  391. await this.$nextTick();
  392. await sleep(50);
  393. this.cachedPos = -1;
  394. const page = this.$refs.scrollingPage;
  395. let i = 0;
  396. while (!this.stopScrolling) {
  397. page.style.transition = `${this.scrollingDelay}ms ${this.scrollingType}`;
  398. page.style.transform = `translateY(-${this.lineHeight}px)`;
  399. if (i > 0) {
  400. this.doDown();
  401. if (this.linesDown.length <= this.pageLineCount + 1) {
  402. this.stopScrolling = true;
  403. }
  404. }
  405. await transitionFinish(this.scrollingDelay + 201);
  406. page.style.transition = '';
  407. page.style.transform = 'none';
  408. page.offsetHeight;
  409. i++;
  410. }
  411. this.resolveTransitionFinish = null;
  412. this.doingScrolling = false;
  413. this.$emit('stop-scrolling');
  414. }
  415. async stopTextScrolling() {
  416. this.stopScrolling = true;
  417. const page = this.$refs.scrollingPage;
  418. page.style.transition = '';
  419. page.style.transform = 'none';
  420. page.offsetHeight;
  421. while (this.doingScrolling) await sleep(10);
  422. }
  423. draw() {
  424. if (this.doingScrolling) {
  425. if (this.cachedPos == this.bookPos) {
  426. this.linesDown = this.linesCached.linesDown;
  427. this.linesUp = this.linesCached.linesUp;
  428. this.page1 = this.pageCached;
  429. } else {
  430. const lines = this.getLines(this.bookPos);
  431. this.linesDown = lines.linesDown;
  432. this.linesUp = lines.linesUp;
  433. this.page1 = this.drawPage(lines.linesDown);
  434. }
  435. //caching next
  436. if (this.cachedPageTimer)
  437. clearTimeout(this.cachedPageTimer);
  438. this.cachedPageTimer = setTimeout(() => {
  439. if (this.linesDown && this.linesDown.length > this.pageLineCount && this.pageLineCount > 0) {
  440. this.cachedPos = this.linesDown[1].begin;
  441. this.linesCached = this.getLines(this.cachedPos);
  442. this.pageCached = this.drawPage(this.linesCached.linesDown);
  443. }
  444. this.cachedPageTimer = null;
  445. }, 20);
  446. this.debouncedDrawStatusBar();
  447. return;
  448. }
  449. if (this.w < minLayoutWidth) {
  450. this.page1 = null;
  451. this.page2 = null;
  452. this.statusBar = null;
  453. return;
  454. }
  455. if (this.book && this.bookPos > 0 && this.bookPos >= this.parsed.textLength) {
  456. this.doEnd();
  457. return;
  458. }
  459. if (this.pageChangeDirectionDown && this.pagePrepared && this.bookPos == this.bookPosPrepared) {
  460. this.toggleLayout = !this.toggleLayout;
  461. this.linesDown = this.linesDownNext;
  462. this.linesUp = this.linesUpNext;
  463. this.doPageTransition();
  464. } else {
  465. const lines = this.getLines(this.bookPos);
  466. this.linesDown = lines.linesDown;
  467. this.linesUp = lines.linesUp;
  468. /*if (this.toggleLayout)
  469. this.page1 = this.drawPage(lines.linesDown);
  470. else
  471. this.page2 = this.drawPage(lines.linesDown);*/
  472. this.debouncedUpdatePage(lines.linesDown);
  473. }
  474. this.pagePrepared = false;
  475. this.debouncedPrepareNextPage();
  476. this.debouncedDrawStatusBar();
  477. if (this.book && this.linesDown && this.linesDown.length < this.pageLineCount)
  478. this.doEnd();
  479. }
  480. doPageTransition() {
  481. if (this.currentTransition) {
  482. //this.currentTransition
  483. //this.pageChangeTransitionSpeed
  484. //this.pageChangeDirectionDown
  485. //curr to next transition
  486. //пока заглушка
  487. }
  488. this.currentTransition = '';
  489. this.pageChangeDirectionDown = false;//true только если PgDown
  490. }
  491. getLines(bookPos) {
  492. if (!this.parsed || this.pageLineCount < 1)
  493. return {};
  494. return {
  495. linesDown: this.parsed.getLines(bookPos, 2*this.pageLineCount),
  496. linesUp: this.parsed.getLines(bookPos, -2*this.pageLineCount)
  497. };
  498. }
  499. drawPage(lines) {
  500. if (!this.lastBook || this.pageLineCount < 1 || !this.book || !lines || !this.parsed.textLength)
  501. return '';
  502. const spaceWidth = this.measureText(' ', {});
  503. let out = `<div class="layout" style="width: ${this.realWidth}px; height: ${this.realHeight}px;` +
  504. ` color: ${this.textColor}">`;
  505. let len = lines.length;
  506. len = (len > this.pageLineCount + 1 ? this.pageLineCount + 1 : len);
  507. let y = this.fontSize*this.textShift;
  508. for (let i = 0; i < len; i++) {
  509. const line = lines[i];
  510. /* line:
  511. {
  512. begin: Number,
  513. end: Number,
  514. first: Boolean,
  515. last: Boolean,
  516. parts: array of {
  517. style: {bold: Boolean, italic: Boolean, center: Boolean}
  518. text: String,
  519. }
  520. }*/
  521. let indent = line.first ? this.p : 0;
  522. let lineText = '';
  523. let center = false;
  524. let centerStyle = {};
  525. for (const part of line.parts) {
  526. lineText += part.text;
  527. center = center || part.style.center;
  528. if (part.style.center)
  529. centerStyle = part.style;
  530. }
  531. let filled = false;
  532. // если выравнивание по ширине включено
  533. if (this.textAlignJustify && !line.last && !center) {
  534. const words = lineText.split(' ');
  535. if (words.length > 1) {
  536. const spaceCount = words.length - 1;
  537. const space = (this.w - line.width + spaceWidth*spaceCount)/spaceCount;
  538. let x = indent;
  539. for (const part of line.parts) {
  540. const font = this.fontByStyle(part.style);
  541. let partWords = part.text.split(' ');
  542. for (let j = 0; j < partWords.length; j++) {
  543. let f = font;
  544. let style = part.style;
  545. let word = partWords[j];
  546. if (i == 0 && this.searching && word.toLowerCase().indexOf(this.needle) >= 0) {
  547. style = Object.assign({}, part.style, {bold: true});
  548. f = this.fontByStyle(style);
  549. }
  550. out += this.drawHelper.fillText(word, x, y, f);
  551. x += this.measureText(word, style) + (j < partWords.length - 1 ? space : 0);
  552. }
  553. }
  554. filled = true;
  555. }
  556. }
  557. // просто выводим текст
  558. if (!filled) {
  559. let x = indent;
  560. x = (center ? (this.w - this.measureText(lineText, centerStyle))/2 : x);
  561. for (const part of line.parts) {
  562. let font = this.fontByStyle(part.style);
  563. if (i == 0 && this.searching) {//для поиска, разбивка по словам
  564. let partWords = part.text.split(' ');
  565. for (let j = 0; j < partWords.length; j++) {
  566. let f = font;
  567. let style = part.style;
  568. let word = partWords[j];
  569. if (word.toLowerCase().indexOf(this.needle) >= 0) {
  570. style = Object.assign({}, part.style, {bold: true});
  571. f = this.fontByStyle(style);
  572. }
  573. out += this.drawHelper.fillText(word, x, y, f);
  574. x += this.measureText(word, style) + (j < partWords.length - 1 ? spaceWidth : 0);
  575. }
  576. } else {
  577. out += this.drawHelper.fillText(part.text, x, y, font);
  578. x += this.measureText(part.text, part.style);
  579. }
  580. }
  581. }
  582. y += this.lineHeight;
  583. }
  584. out += '</div>';
  585. return out;
  586. }
  587. drawStatusBar(message) {
  588. if (this.w < minLayoutWidth) {
  589. this.statusBar = null;
  590. return;
  591. }
  592. if (this.showStatusBar && this.linesDown && this.pageLineCount > 0) {
  593. const lines = this.linesDown;
  594. let i = this.pageLineCount;
  595. if (this.keepLastToFirst)
  596. i--;
  597. i = (i > lines.length - 1 ? lines.length - 1 : i);
  598. if (i >= 0) {
  599. if (!message)
  600. message = this.statusBarMessage;
  601. if (!message)
  602. message = this.title;
  603. this.statusBar = this.drawHelper.drawStatusBar(this.statusBarTop, this.statusBarHeight,
  604. lines[i].end, this.parsed.textLength, message);
  605. this.bookPosSeen = lines[i].end;
  606. }
  607. } else {
  608. this.statusBar = '';
  609. }
  610. }
  611. blinkCachedLoadMessage(state) {
  612. if (state === 'finish') {
  613. this.statusBarMessage = '';
  614. } else if (state) {
  615. this.statusBarMessage = 'Книга загружена из кеша';
  616. } else {
  617. this.statusBarMessage = ' ';
  618. }
  619. this.drawStatusBar();
  620. }
  621. async lazyParsePara() {
  622. if (!this.parsed || this.doingLazyParse)
  623. return;
  624. this.doingLazyParse = true;
  625. let j = 0;
  626. let k = 0;
  627. let prevPerc = 0;
  628. this.stopLazyParse = false;
  629. for (let i = 0; i < this.parsed.para.length; i++) {
  630. j++;
  631. if (j > 1) {
  632. await sleep(1);
  633. j = 0;
  634. }
  635. if (this.stopLazyParse)
  636. break;
  637. this.parsed.parsePara(i);
  638. k++;
  639. if (k > 100) {
  640. let perc = Math.round(i/this.parsed.para.length*100);
  641. if (perc != prevPerc)
  642. this.drawStatusBar(`Обработка текста ${perc}%`);
  643. prevPerc = perc;
  644. k = 0;
  645. }
  646. }
  647. this.drawStatusBar();
  648. this.doingLazyParse = false;
  649. }
  650. async refreshTime() {
  651. if (!this.timeRefreshing) {
  652. this.timeRefreshing = true;
  653. await sleep(60*1000);
  654. if (this.book && this.parsed.textLength) {
  655. this.debouncedDrawStatusBar();
  656. }
  657. this.timeRefreshing = false;
  658. this.refreshTime();
  659. }
  660. }
  661. prepareNextPage() {
  662. // подготовка следующей страницы заранее
  663. if (!this.book || !this.parsed.textLength || !this.linesDown || this.pageLineCount < 1)
  664. return;
  665. let i = this.pageLineCount;
  666. if (this.keepLastToFirst)
  667. i--;
  668. if (i >= 0 && this.linesDown.length > i) {
  669. this.bookPosPrepared = this.linesDown[i].begin;
  670. const lines = this.getLines(this.bookPosPrepared);
  671. this.linesDownNext = lines.linesDown;
  672. this.linesUpNext = lines.linesUp;
  673. if (this.toggleLayout)
  674. this.page2 = this.drawPage(lines.linesDown);//наоборот
  675. else
  676. this.page1 = this.drawPage(lines.linesDown);
  677. this.pagePrepared = true;
  678. }
  679. }
  680. doDown() {
  681. if (this.linesDown && this.linesDown.length > this.pageLineCount && this.pageLineCount > 0) {
  682. this.bookPos = this.linesDown[1].begin;
  683. }
  684. }
  685. doUp() {
  686. if (this.linesUp && this.linesUp.length > 1 && this.pageLineCount > 0) {
  687. this.bookPos = this.linesUp[1].begin;
  688. }
  689. }
  690. doPageDown() {
  691. if (this.linesDown && this.pageLineCount > 0) {
  692. let i = this.pageLineCount;
  693. if (this.keepLastToFirst)
  694. i--;
  695. if (i >= 0 && this.linesDown.length >= 2*i) {
  696. this.currentTransition = this.pageChangeTransition;
  697. this.pageChangeDirectionDown = true;
  698. this.bookPos = this.linesDown[i].begin;
  699. } else
  700. this.doEnd();
  701. }
  702. }
  703. doPageUp() {
  704. if (this.linesUp && this.pageLineCount > 0) {
  705. let i = this.pageLineCount;
  706. if (this.keepLastToFirst)
  707. i--;
  708. i = (i > this.linesUp.length - 1 ? this.linesUp.length - 1 : i);
  709. if (i >= 0 && this.linesUp.length > i) {
  710. this.currentTransition = this.pageChangeTransition;
  711. this.pageChangeDirectionDown = false;
  712. this.bookPos = this.linesUp[i].begin;
  713. }
  714. }
  715. }
  716. doHome() {
  717. this.bookPos = 0;
  718. }
  719. doEnd() {
  720. if (this.parsed.para.length && this.pageLineCount > 0) {
  721. let i = this.parsed.para.length - 1;
  722. let lastPos = this.parsed.para[i].offset + this.parsed.para[i].length - 1;
  723. const lines = this.parsed.getLines(lastPos, -this.pageLineCount);
  724. if (lines) {
  725. i = this.pageLineCount - 1;
  726. i = (i > lines.length - 1 ? lines.length - 1 : i);
  727. this.bookPos = lines[i].begin;
  728. }
  729. }
  730. }
  731. doToolBarToggle() {
  732. this.$emit('tool-bar-toggle');
  733. }
  734. async doFontSizeInc() {
  735. if (!this.settingsChanging) {
  736. this.settingsChanging = true;
  737. const newSize = (this.settings.fontSize + 1 < 100 ? this.settings.fontSize + 1 : 100);
  738. const newSettings = Object.assign({}, this.settings, {fontSize: newSize});
  739. this.commit('reader/setSettings', newSettings);
  740. await sleep(50);
  741. this.settingsChanging = false;
  742. }
  743. }
  744. async doFontSizeDec() {
  745. if (!this.settingsChanging) {
  746. this.settingsChanging = true;
  747. const newSize = (this.settings.fontSize - 1 > 5 ? this.settings.fontSize - 1 : 5);
  748. const newSettings = Object.assign({}, this.settings, {fontSize: newSize});
  749. this.commit('reader/setSettings', newSettings);
  750. await sleep(50);
  751. this.settingsChanging = false;
  752. }
  753. }
  754. async doScrollingSpeedUp() {
  755. if (!this.settingsChanging) {
  756. this.settingsChanging = true;
  757. const newDelay = (this.settings.scrollingDelay - 50 > 1 ? this.settings.scrollingDelay - 50 : 1);
  758. const newSettings = Object.assign({}, this.settings, {scrollingDelay: newDelay});
  759. this.commit('reader/setSettings', newSettings);
  760. await sleep(50);
  761. this.settingsChanging = false;
  762. }
  763. }
  764. async doScrollingSpeedDown() {
  765. if (!this.settingsChanging) {
  766. this.settingsChanging = true;
  767. const newDelay = (this.settings.scrollingDelay + 50 < 10000 ? this.settings.scrollingDelay + 50 : 10000);
  768. const newSettings = Object.assign({}, this.settings, {scrollingDelay: newDelay});
  769. this.commit('reader/setSettings', newSettings);
  770. await sleep(50);
  771. this.settingsChanging = false;
  772. }
  773. }
  774. keyHook(event) {
  775. let result = false;
  776. if (event.type == 'keydown' && !event.ctrlKey && !event.altKey) {
  777. result = true;
  778. switch (event.code) {
  779. case 'ArrowDown':
  780. if (event.shiftKey)
  781. this.doScrollingSpeedUp();
  782. else
  783. this.doDown();
  784. break;
  785. case 'ArrowUp':
  786. if (event.shiftKey)
  787. this.doScrollingSpeedDown();
  788. else
  789. this.doUp();
  790. break;
  791. case 'PageDown':
  792. case 'ArrowRight':
  793. this.doPageDown();
  794. break;
  795. case 'Space':
  796. if (event.shiftKey)
  797. this.doPageUp();
  798. else
  799. this.doPageDown();
  800. break;
  801. case 'PageUp':
  802. case 'ArrowLeft':
  803. case 'Backspace':
  804. this.doPageUp();
  805. break;
  806. case 'Home':
  807. this.doHome();
  808. break;
  809. case 'End':
  810. this.doEnd();
  811. break;
  812. case 'KeyA':
  813. if (event.shiftKey)
  814. this.doFontSizeDec();
  815. else
  816. this.doFontSizeInc();
  817. break;
  818. case 'Enter':
  819. case 'Backquote'://`
  820. case 'KeyF':
  821. this.$emit('full-screen-toogle');
  822. break;
  823. case 'Tab':
  824. this.doToolBarToggle();
  825. event.preventDefault();
  826. event.stopPropagation();
  827. break;
  828. default:
  829. result = false;
  830. break;
  831. }
  832. }
  833. return result;
  834. }
  835. async startClickRepeat(pointX, pointY) {
  836. this.repX = pointX;
  837. this.repY = pointY;
  838. if (!this.repInit && this.repDoing) {
  839. this.repInit = true;
  840. let delay = 400;
  841. while (this.repDoing) {
  842. this.handleClick(pointX, pointY);
  843. await sleep(delay);
  844. if (delay > 15)
  845. delay *= 0.8;
  846. }
  847. this.repInit = false;
  848. }
  849. }
  850. endClickRepeat() {
  851. this.repDoing = false;
  852. }
  853. onTouchStart(event) {
  854. if (!this.mobile)
  855. return;
  856. this.endClickRepeat();
  857. if (event.touches.length == 1) {
  858. const touch = event.touches[0];
  859. const rect = event.target.getBoundingClientRect();
  860. const x = touch.pageX - rect.left;
  861. const y = touch.pageY - rect.top;
  862. if (this.handleClick(x, y)) {
  863. this.repDoing = true;
  864. this.debouncedStartClickRepeat(x, y);
  865. }
  866. }
  867. }
  868. onTouchEnd() {
  869. if (!this.mobile)
  870. return;
  871. this.endClickRepeat();
  872. }
  873. onTouchCancel() {
  874. if (!this.mobile)
  875. return;
  876. this.endClickRepeat();
  877. }
  878. onMouseDown(event) {
  879. if (this.mobile)
  880. return;
  881. this.endClickRepeat();
  882. if (event.button == 0) {
  883. if (this.handleClick(event.offsetX, event.offsetY)) {
  884. this.repDoing = true;
  885. this.debouncedStartClickRepeat(event.offsetX, event.offsetY);
  886. }
  887. } else if (event.button == 1) {
  888. this.$emit('scrolling-toggle');
  889. } else if (event.button == 2) {
  890. this.doToolBarToggle();
  891. }
  892. }
  893. onMouseUp() {
  894. if (this.mobile)
  895. return;
  896. this.endClickRepeat();
  897. }
  898. onMouseWheel(event) {
  899. if (this.mobile)
  900. return;
  901. if (event.deltaY > 0) {
  902. this.doDown();
  903. } else if (event.deltaY < 0) {
  904. this.doUp();
  905. }
  906. }
  907. onStatusBarClick() {
  908. window.open(this.meta.url, '_blank');
  909. }
  910. handleClick(pointX, pointY) {
  911. const mouseLegend = {
  912. 40: {30: 'PgUp', 100: 'PgDown'},
  913. 60: {40: 'Up', 60: 'Menu', 100: 'Down'},
  914. 100: {30: 'PgUp', 100: 'PgDown'}
  915. };
  916. const w = pointX/this.realWidth*100;
  917. const h = pointY/this.realHeight*100;
  918. let action = '';
  919. loops: {
  920. for (const x in mouseLegend) {
  921. for (const y in mouseLegend[x]) {
  922. if (w < x && h < y) {
  923. action = mouseLegend[x][y];
  924. break loops;
  925. }
  926. }
  927. }
  928. }
  929. switch (action) {
  930. case 'Down' ://Down
  931. this.doDown();
  932. break;
  933. case 'Up' ://Up
  934. this.doUp();
  935. break;
  936. case 'PgDown' ://PgDown
  937. this.doPageDown();
  938. break;
  939. case 'PgUp' ://PgUp
  940. this.doPageUp();
  941. break;
  942. case 'Menu' :
  943. this.doToolBarToggle();
  944. break;
  945. default :
  946. // Nothing
  947. }
  948. return (action && action != 'Menu');
  949. }
  950. }
  951. //-----------------------------------------------------------------------------
  952. </script>
  953. <style scoped>
  954. .main {
  955. flex: 1;
  956. margin: 0;
  957. padding: 0;
  958. overflow: hidden;
  959. position: relative;
  960. min-width: 200px;
  961. }
  962. .layout {
  963. margin: 0;
  964. padding: 0;
  965. position: absolute;
  966. z-index: 10;
  967. }
  968. .back {
  969. z-index: 5;
  970. }
  971. .events {
  972. z-index: 20;
  973. background-color: rgba(0,0,0,0);
  974. }
  975. </style>
  976. <style>
  977. .paper1 {
  978. background: url("images/paper1.jpg") center;
  979. background-size: cover;
  980. }
  981. .paper2 {
  982. background: url("images/paper2.jpg") center;
  983. background-size: cover;
  984. }
  985. .paper3 {
  986. background: url("images/paper3.jpg") center;
  987. background-size: cover;
  988. }
  989. .paper4 {
  990. background: url("images/paper4.jpg") center;
  991. background-size: cover;
  992. }
  993. .paper5 {
  994. background: url("images/paper5.jpg") center;
  995. background-size: cover;
  996. }
  997. .paper6 {
  998. background: url("images/paper6.jpg") center;
  999. background-size: cover;
  1000. }
  1001. .paper7 {
  1002. background: url("images/paper7.jpg") center;
  1003. background-size: cover;
  1004. }
  1005. .paper8 {
  1006. background: url("images/paper8.jpg") center;
  1007. background-size: cover;
  1008. }
  1009. .paper9 {
  1010. background: url("images/paper9.jpg");
  1011. }
  1012. </style>