TextPage.vue 39 KB

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