TextPage.vue 36 KB

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