TextPage.vue 40 KB

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