TextPage.vue 36 KB

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