TextPage.vue 41 KB

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