TextPage.vue 39 KB

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