TextPage.vue 34 KB

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