TextPage.vue 37 KB

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