TextPage.vue 37 KB

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