TextPage.vue 37 KB

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