TextPage.vue 37 KB

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