TextPage.vue 37 KB

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