TextPage.vue 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. <template>
  2. <div ref="main" class="main">
  3. <div class="layout back">
  4. <div v-html="background"></div>
  5. <!-- img -->
  6. </div>
  7. <div v-show="toggleLayout" class="layout">
  8. <div ref="scrollingPage" class="layout" @transitionend="onScrollingTransitionEnd">
  9. <div v-html="page1"></div>
  10. </div>
  11. </div>
  12. <div v-show="!toggleLayout" class="layout">
  13. <div v-html="page2"></div>
  14. </div>
  15. <div v-show="showStatusBar" ref="statusBar" class="layout">
  16. <div v-html="statusBar"></div>
  17. </div>
  18. <div ref="layoutEvents" class="layout events" @mousedown.prevent.stop="onMouseDown" @mouseup.prevent.stop="onMouseUp"
  19. @wheel.prevent.stop="onMouseWheel"
  20. @touchstart.stop="onTouchStart" @touchend.stop="onTouchEnd" @touchcancel.prevent.stop="onTouchCancel"
  21. oncontextmenu="return false;">
  22. <div v-show="showStatusBar" v-html="statusBarClickable" @mousedown.prevent.stop @touchstart.stop
  23. @click.prevent.stop="onStatusBarClick"></div>
  24. <div v-show="fontsLoading" ref="fontsLoading"></div>
  25. </div>
  26. <!-- невидимым делать нельзя, вовремя не подгружаютя шрифты -->
  27. <canvas ref="offscreenCanvas" class="layout" style="width: 0px; height: 0px"></canvas>
  28. </div>
  29. </template>
  30. <script>
  31. //-----------------------------------------------------------------------------
  32. import Vue from 'vue';
  33. import Component from 'vue-class-component';
  34. import {loadCSS} from 'fg-loadcss';
  35. import _ from 'lodash';
  36. import {sleep} from '../../../share/utils';
  37. import bookManager from '../share/bookManager';
  38. import DrawHelper from './DrawHelper';
  39. import rstore from '../../../store/modules/reader';
  40. const minLayoutWidth = 100;
  41. export default @Component({
  42. watch: {
  43. bookPos: function(newValue) {
  44. this.debouncedEmitPosChange(newValue);
  45. this.draw();
  46. },
  47. settings: function() {
  48. this.debouncedLoadSettings();
  49. },
  50. },
  51. })
  52. class TextPage extends Vue {
  53. toggleLayout = false;
  54. showStatusBar = false;
  55. background = null;
  56. page1 = null;
  57. page2 = null;
  58. statusBar = null;
  59. statusBarClickable = null;
  60. fontsLoading = null;
  61. lastBook = null;
  62. bookPos = 0;
  63. fontStyle = null;
  64. fontSize = null;
  65. fontName = null;
  66. meta = null;
  67. created() {
  68. this.drawHelper = new DrawHelper();
  69. this.commit = this.$store.commit;
  70. this.dispatch = this.$store.dispatch;
  71. this.config = this.$store.state.config;
  72. this.reader = this.$store.state.reader;
  73. this.debouncedEmitPosChange = _.debounce((newValue) => {
  74. this.$emit('book-pos-changed', {bookPos: newValue, bookPosSeen: this.bookPosSeen});
  75. }, 1000);
  76. this.debouncedStartClickRepeat = _.debounce((x, y) => {
  77. this.startClickRepeat(x, y);
  78. }, 800);
  79. this.debouncedPrepareNextPage = _.debounce(() => {
  80. this.prepareNextPage();
  81. }, 100);
  82. this.debouncedDrawStatusBar = _.throttle(() => {
  83. this.drawStatusBar();
  84. }, 60);
  85. this.debouncedLoadSettings = _.debounce(() => {
  86. this.loadSettings();
  87. }, 50);
  88. this.debouncedUpdatePage = _.debounce((lines) => {
  89. this.toggleLayout = !this.toggleLayout;
  90. if (this.toggleLayout)
  91. this.page1 = this.drawPage(lines);
  92. else
  93. this.page2 = this.drawPage(lines);
  94. this.doPageTransition();
  95. }, 10);
  96. this.$root.$on('resize', () => {this.$nextTick(this.onResize)});
  97. this.mobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent);
  98. /*
  99. const settings = Object.assign({}, this.settings);
  100. let updated = false;
  101. for (let prop in rstore.settingDefaults) {
  102. if (!settings.hasOwnProperty(prop)) {
  103. settings[prop] = rstore.settingDefaults[prop];
  104. updated = true;
  105. }
  106. }
  107. if (updated)
  108. this.commit('reader/setSettings', settings);
  109. */
  110. }
  111. mounted() {
  112. this.context = this.$refs.offscreenCanvas.getContext('2d');
  113. }
  114. hex2rgba(hex, alpha = 1) {
  115. const [r, g, b] = hex.match(/\w\w/g).map(x => parseInt(x, 16));
  116. return `rgba(${r},${g},${b},${alpha})`;
  117. }
  118. calcDrawProps() {
  119. //preloaded fonts
  120. this.fontList = [`12px ${this.fontName}`];
  121. //widths
  122. this.realWidth = this.$refs.main.clientWidth;
  123. this.realHeight = this.$refs.main.clientHeight;
  124. this.$refs.layoutEvents.style.width = this.realWidth + 'px';
  125. this.$refs.layoutEvents.style.height = this.realHeight + 'px';
  126. this.w = this.realWidth - 2*this.indentLR;
  127. this.h = this.realHeight - (this.showStatusBar ? this.statusBarHeight : 0) - 2*this.indentTB;
  128. this.lineHeight = this.fontSize + this.lineInterval;
  129. this.pageLineCount = 1 + Math.floor((this.h - this.fontSize)/this.lineHeight);
  130. if (this.parsed) {
  131. this.parsed.p = this.p;
  132. this.parsed.w = this.w;// px, ширина текста
  133. this.parsed.font = this.font;
  134. this.parsed.wordWrap = this.wordWrap;
  135. let t = '';
  136. while (this.measureText(t, {}) < this.w) t += 'Щ';
  137. this.parsed.maxWordLength = t.length - 1;
  138. this.parsed.measureText = this.measureText;
  139. }
  140. //сообщение "Загрузка шрифтов..."
  141. const flText = 'Загрузка шрифта...';
  142. this.$refs.fontsLoading.innerHTML = flText;
  143. const fontsLoadingStyle = this.$refs.fontsLoading.style;
  144. fontsLoadingStyle.position = 'absolute';
  145. fontsLoadingStyle.fontSize = this.fontSize + 'px';
  146. fontsLoadingStyle.top = (this.realHeight/2 - 2*this.fontSize) + 'px';
  147. fontsLoadingStyle.left = (this.realWidth - this.measureText(flText, {}))/2 + 'px';
  148. //stuff
  149. this.statusBarColor = this.hex2rgba(this.textColor || '#000000', this.statusBarColorAlpha);
  150. this.currentTransition = '';
  151. this.pageChangeDirectionDown = true;
  152. this.fontShift = this.fontVertShift/100;
  153. this.textShift = this.textVertShift/100 + this.fontShift;
  154. //drawHelper
  155. this.drawHelper.realWidth = this.realWidth;
  156. this.drawHelper.realHeight = this.realHeight;
  157. this.drawHelper.backgroundColor = this.backgroundColor;
  158. this.drawHelper.statusBarColor = this.statusBarColor;
  159. this.drawHelper.fontName = this.fontName;
  160. this.drawHelper.fontShift = this.fontShift;
  161. this.drawHelper.measureText = this.measureText;
  162. this.drawHelper.measureTextFont = this.measureTextFont;
  163. this.$refs.statusBar.style.left = '0px';
  164. this.$refs.statusBar.style.top = (this.statusBarTop ? 1 : this.realHeight - this.statusBarHeight) + 'px';
  165. this.statusBarClickable = this.drawHelper.statusBarClickable(this.statusBarTop, this.statusBarHeight);
  166. }
  167. measureText(text, style) {// eslint-disable-line no-unused-vars
  168. this.context.font = this.fontByStyle(style);
  169. return this.context.measureText(text).width;
  170. }
  171. measureTextFont(text, font) {// eslint-disable-line no-unused-vars
  172. this.context.font = font;
  173. return this.context.measureText(text).width;
  174. }
  175. async checkLoadedFonts() {
  176. let loaded = await Promise.all(this.fontList.map(font => document.fonts.check(font)));
  177. if (loaded.some(r => !r)) {
  178. loaded = await Promise.all(this.fontList.map(font => document.fonts.load(font)));
  179. if (loaded.some(r => !r.length))
  180. throw new Error('some font not loaded');
  181. }
  182. }
  183. async loadFonts() {
  184. this.fontsLoading = true;
  185. if (!this.fontsLoaded)
  186. this.fontsLoaded = {};
  187. //загрузка дин.шрифта
  188. const loaded = this.fontsLoaded[this.fontCssUrl];
  189. if (this.fontCssUrl && !loaded) {
  190. loadCSS(this.fontCssUrl);
  191. this.fontsLoaded[this.fontCssUrl] = 1;
  192. }
  193. const waitingTime = 10*1000;
  194. const delay = 100;
  195. let i = 0;
  196. //ждем шрифты
  197. while (i < waitingTime/delay) {
  198. i++;
  199. try {
  200. await this.checkLoadedFonts();
  201. i = waitingTime;
  202. } catch (e) {
  203. await sleep(delay);
  204. }
  205. }
  206. if (i !== waitingTime) {
  207. this.$notify.error({
  208. title: 'Ошибка загрузки',
  209. message: 'Некоторые шрифты не удалось загрузить'
  210. });
  211. }
  212. this.fontsLoading = false;
  213. }
  214. getSettings() {
  215. const settings = this.settings;
  216. for (let prop in rstore.settingDefaults) {
  217. this[prop] = settings[prop];
  218. }
  219. const wf = this.webFontName;
  220. const i = _.findIndex(rstore.webFonts, ['name', wf]);
  221. if (wf && i >= 0) {
  222. this.fontName = wf;
  223. this.fontCssUrl = rstore.webFonts[i].css;
  224. this.fontVertShift = settings.fontShifts[wf] || 0;
  225. }
  226. }
  227. async calcPropsAndLoadFonts(omitLoadFonts) {
  228. this.calcDrawProps();
  229. this.setBackground();
  230. if (!omitLoadFonts)
  231. await this.loadFonts();
  232. this.draw();
  233. // шрифты хрен знает когда подгружаются, поэтому
  234. const parsed = this.parsed;
  235. if (!parsed.force) {
  236. let i = 0;
  237. parsed.force = true;
  238. while (i < 10) {
  239. await sleep(1000);
  240. if (this.parsed != parsed)
  241. break;
  242. this.draw();
  243. i++;
  244. }
  245. parsed.force = false;
  246. }
  247. }
  248. loadSettings() {
  249. (async() => {
  250. let fontName = this.fontName;
  251. this.getSettings();
  252. await this.calcPropsAndLoadFonts(fontName == this.fontName);
  253. })();
  254. }
  255. showBook() {
  256. this.$refs.main.focus();
  257. this.toggleLayout = false;
  258. this.book = null;
  259. this.meta = null;
  260. this.fb2 = null;
  261. this.parsed = null;
  262. this.linesUp = null;
  263. this.linesDown = null;
  264. this.getSettings();
  265. this.calcDrawProps();
  266. this.draw();// пока не загрузили, очистим канвас
  267. if (this.lastBook) {
  268. (async() => {
  269. //подождем ленивый парсинг
  270. this.stopLazyParse = true;
  271. while (this.doingLazyParse) await sleep(10);
  272. const isParsed = await bookManager.hasBookParsed(this.lastBook);
  273. if (!isParsed) {
  274. return;
  275. }
  276. this.book = await bookManager.getBook(this.lastBook);
  277. this.meta = bookManager.metaOnly(this.book);
  278. this.fb2 = this.meta.fb2;
  279. const authorName = _.compact([
  280. this.fb2.lastName,
  281. this.fb2.firstName,
  282. this.fb2.middleName
  283. ]).join(' ');
  284. this.title = _.compact([
  285. authorName,
  286. this.fb2.bookTitle
  287. ]).join(' - ');
  288. this.$root.$emit('set-app-title', this.title);
  289. this.parsed = this.book.parsed;
  290. this.page1 = null;
  291. this.page2 = null;
  292. this.statusBar = null;
  293. await this.stopTextScrolling();
  294. this.calcPropsAndLoadFonts();
  295. this.refreshTime();
  296. if (this.lazyParseEnabled)
  297. this.lazyParsePara();
  298. })();
  299. }
  300. }
  301. setBackground() {
  302. this.background = `<div class="layout" style="width: ${this.realWidth}px; height: ${this.realHeight}px;` +
  303. ` background-color: ${this.backgroundColor}"></div>`;
  304. }
  305. async onResize() {
  306. this.page1 = null;
  307. this.page2 = null;
  308. this.statusBar = null;
  309. this.calcDrawProps();
  310. this.setBackground();
  311. this.draw();
  312. }
  313. get settings() {
  314. return this.$store.state.reader.settings;
  315. }
  316. get font() {
  317. return `${this.fontStyle} ${this.fontWeight} ${this.fontSize}px ${this.fontName}`;
  318. }
  319. fontByStyle(style) {
  320. return `${style.italic ? 'italic' : this.fontStyle} ${style.bold ? 'bold' : this.fontWeight} ${this.fontSize}px ${this.fontName}`;
  321. }
  322. onScrollingTransitionEnd() {
  323. if (this.resolveTransitionFinish)
  324. this.resolveTransitionFinish();
  325. }
  326. async startTextScrolling() {
  327. if (this.doingScrolling || !this.book || !this.parsed.textLength || !this.linesDown || this.pageLineCount < 1 ||
  328. this.linesDown.length <= this.pageLineCount) {
  329. this.$emit('stop-scrolling');
  330. return;
  331. }
  332. this.stopScrolling = false;
  333. this.doingScrolling = true;
  334. const transitionFinish = (timeout) => {
  335. return new Promise(async(resolve) => {
  336. this.resolveTransitionFinish = resolve;
  337. let wait = timeout/100;
  338. while (wait > 0 && !this.stopScrolling) {
  339. wait--;
  340. await sleep(100);
  341. }
  342. resolve();
  343. });
  344. };
  345. if (!this.toggleLayout)
  346. this.page1 = this.page2;
  347. this.toggleLayout = true;
  348. await sleep(50);
  349. const page = this.$refs.scrollingPage;
  350. let i = 0;
  351. while (!this.stopScrolling) {
  352. page.style.transition = `${this.scrollingDelay}ms ${this.scrollingType}`;
  353. page.style.transform = `translateY(-${this.lineHeight}px)`;
  354. if (i > 0) {
  355. this.doDown();
  356. if (this.linesDown.length <= this.pageLineCount + 1) {
  357. this.stopScrolling = true;
  358. }
  359. }
  360. await transitionFinish(this.scrollingDelay + 201);
  361. page.style.transition = '';
  362. page.style.transform = 'none';
  363. page.offsetHeight;
  364. i++;
  365. }
  366. this.resolveTransitionFinish = null;
  367. this.doingScrolling = false;
  368. this.$emit('stop-scrolling');
  369. }
  370. async stopTextScrolling() {
  371. this.stopScrolling = true;
  372. const page = this.$refs.scrollingPage;
  373. page.style.transition = '';
  374. page.style.transform = 'none';
  375. page.offsetHeight;
  376. while (this.doingScrolling) await sleep(10);
  377. }
  378. draw() {
  379. if (this.doingScrolling) {
  380. const lines = this.getLines(this.bookPos);
  381. this.linesDown = lines.linesDown;
  382. this.linesUp = lines.linesUp;
  383. this.page1 = this.drawPage(lines.linesDown);
  384. this.debouncedDrawStatusBar();
  385. return;
  386. }
  387. if (this.w < minLayoutWidth) {
  388. this.page1 = null;
  389. this.page2 = null;
  390. this.statusBar = null;
  391. return;
  392. }
  393. if (this.book && this.bookPos > 0 && this.bookPos >= this.parsed.textLength) {
  394. this.doEnd();
  395. return;
  396. }
  397. if (this.pageChangeDirectionDown && this.pagePrepared && this.bookPos == this.bookPosPrepared) {
  398. this.toggleLayout = !this.toggleLayout;
  399. this.linesDown = this.linesDownNext;
  400. this.linesUp = this.linesUpNext;
  401. this.doPageTransition();
  402. } else {
  403. const lines = this.getLines(this.bookPos);
  404. this.linesDown = lines.linesDown;
  405. this.linesUp = lines.linesUp;
  406. /*if (this.toggleLayout)
  407. this.page1 = this.drawPage(lines.linesDown);
  408. else
  409. this.page2 = this.drawPage(lines.linesDown);*/
  410. this.debouncedUpdatePage(lines.linesDown);
  411. }
  412. this.pagePrepared = false;
  413. this.debouncedPrepareNextPage();
  414. this.debouncedDrawStatusBar();
  415. if (this.book && this.linesDown && this.linesDown.length < this.pageLineCount)
  416. this.doEnd();
  417. }
  418. doPageTransition() {
  419. if (this.currentTransition) {
  420. //this.currentTransition
  421. //this.pageChangeTransitionSpeed
  422. //this.pageChangeDirectionDown
  423. //curr to next transition
  424. //пока заглушка
  425. }
  426. this.currentTransition = '';
  427. this.pageChangeDirectionDown = false;//true только если PgDown
  428. }
  429. getLines(bookPos) {
  430. if (!this.parsed || this.pageLineCount < 1)
  431. return {};
  432. return {
  433. linesDown: this.parsed.getLines(bookPos, 2*this.pageLineCount),
  434. linesUp: this.parsed.getLines(bookPos, -2*this.pageLineCount)
  435. };
  436. }
  437. drawPage(lines) {
  438. if (!this.lastBook || this.pageLineCount < 1)
  439. return '';
  440. let out = `<div class="layout" style="width: ${this.realWidth}px; height: ${this.realHeight}px;` +
  441. ` color: ${this.textColor}">`;
  442. if (!this.book || !lines || !this.parsed.textLength) {
  443. out += '</div>';
  444. return out;
  445. }
  446. const spaceWidth = this.measureText(' ', {});
  447. let y = this.indentTB + (this.h - this.pageLineCount*this.lineHeight + this.lineInterval)/2 + this.fontSize*this.textShift;
  448. if (this.showStatusBar)
  449. y += this.statusBarHeight*(this.statusBarTop ? 1 : 0);
  450. let len = lines.length;
  451. len = (len > this.pageLineCount + 1 ? this.pageLineCount + 1 : len);
  452. for (let i = 0; i < len; i++) {
  453. const line = lines[i];
  454. /* line:
  455. {
  456. begin: Number,
  457. end: Number,
  458. first: Boolean,
  459. last: Boolean,
  460. parts: array of {
  461. style: {bold: Boolean, italic: Boolean, center: Boolean}
  462. text: String,
  463. }
  464. }*/
  465. let indent = this.indentLR + (line.first ? this.p : 0);
  466. let lineText = '';
  467. let center = false;
  468. let centerStyle = {};
  469. for (const part of line.parts) {
  470. lineText += part.text;
  471. center = center || part.style.center;
  472. if (part.style.center)
  473. centerStyle = part.style;
  474. }
  475. let filled = false;
  476. // если выравнивание по ширине включено
  477. if (this.textAlignJustify && !line.last && !center) {
  478. const words = lineText.split(' ');
  479. if (words.length > 1) {
  480. const spaceCount = words.length - 1;
  481. const space = (this.w - line.width + spaceWidth*spaceCount)/spaceCount;
  482. let x = indent;
  483. for (const part of line.parts) {
  484. const font = this.fontByStyle(part.style);
  485. let partWords = part.text.split(' ');
  486. for (let i = 0; i < partWords.length; i++) {
  487. let word = partWords[i];
  488. out += this.drawHelper.fillText(word, x, y, font);
  489. x += this.measureText(word, part.style) + (i < partWords.length - 1 ? space : 0);
  490. }
  491. }
  492. filled = true;
  493. }
  494. }
  495. // просто выводим текст
  496. if (!filled) {
  497. let x = indent;
  498. x = (center ? this.indentLR + (this.w - this.measureText(lineText, centerStyle))/2 : x);
  499. for (const part of line.parts) {
  500. let text = part.text;
  501. const font = this.fontByStyle(part.style);
  502. out += this.drawHelper.fillText(text, x, y, font);
  503. x += this.measureText(text, part.style);
  504. }
  505. }
  506. y += this.lineHeight;
  507. }
  508. out += '</div>';
  509. return out;
  510. }
  511. drawStatusBar(message) {
  512. if (this.w < minLayoutWidth) {
  513. this.statusBar = null;
  514. return;
  515. }
  516. if (this.showStatusBar && this.linesDown && this.pageLineCount > 0) {
  517. const lines = this.linesDown;
  518. let i = this.pageLineCount;
  519. if (this.keepLastToFirst)
  520. i--;
  521. i = (i > lines.length - 1 ? lines.length - 1 : i);
  522. if (i >= 0) {
  523. if (!message)
  524. message = this.statusBarMessage;
  525. if (!message)
  526. message = this.title;
  527. this.statusBar = this.drawHelper.drawStatusBar(this.statusBarTop, this.statusBarHeight,
  528. lines[i].end, this.parsed.textLength, message);
  529. this.bookPosSeen = lines[i].end;
  530. }
  531. } else {
  532. this.statusBar = '';
  533. }
  534. }
  535. blinkCachedLoadMessage(state) {
  536. if (state === 'finish') {
  537. this.statusBarMessage = '';
  538. } else if (state) {
  539. this.statusBarMessage = 'Книга загружена из кеша';
  540. } else {
  541. this.statusBarMessage = ' ';
  542. }
  543. this.drawStatusBar();
  544. }
  545. async lazyParsePara() {
  546. if (!this.parsed || this.doingLazyParse)
  547. return;
  548. this.doingLazyParse = true;
  549. let j = 0;
  550. let k = 0;
  551. let prevPerc = 0;
  552. this.stopLazyParse = false;
  553. for (let i = 0; i < this.parsed.para.length; i++) {
  554. j++;
  555. if (j > 1) {
  556. await sleep(1);
  557. j = 0;
  558. }
  559. if (this.stopLazyParse)
  560. break;
  561. this.parsed.parsePara(i);
  562. k++;
  563. if (k > 100) {
  564. let perc = Math.round(i/this.parsed.para.length*100);
  565. if (perc != prevPerc)
  566. this.drawStatusBar(`Обработка текста ${perc}%`);
  567. prevPerc = perc;
  568. k = 0;
  569. }
  570. }
  571. this.drawStatusBar();
  572. this.doingLazyParse = false;
  573. }
  574. async refreshTime() {
  575. if (!this.timeRefreshing) {
  576. this.timeRefreshing = true;
  577. await sleep(60*1000);
  578. if (this.book && this.parsed.textLength) {
  579. this.debouncedDrawStatusBar();
  580. }
  581. this.timeRefreshing = false;
  582. this.refreshTime();
  583. }
  584. }
  585. prepareNextPage() {
  586. // подготовка следующей страницы заранее
  587. if (!this.book || !this.parsed.textLength || !this.linesDown || this.pageLineCount < 1)
  588. return;
  589. let i = this.pageLineCount;
  590. if (this.keepLastToFirst)
  591. i--;
  592. if (i >= 0 && this.linesDown.length > i) {
  593. this.bookPosPrepared = this.linesDown[i].begin;
  594. const lines = this.getLines(this.bookPosPrepared);
  595. this.linesDownNext = lines.linesDown;
  596. this.linesUpNext = lines.linesUp;
  597. if (this.toggleLayout)
  598. this.page2 = this.drawPage(lines.linesDown);//наоборот
  599. else
  600. this.page1 = this.drawPage(lines.linesDown);
  601. this.pagePrepared = true;
  602. }
  603. }
  604. doDown() {
  605. if (this.linesDown && this.linesDown.length > this.pageLineCount && this.pageLineCount > 0) {
  606. this.bookPos = this.linesDown[1].begin;
  607. }
  608. }
  609. doUp() {
  610. if (this.linesUp && this.linesUp.length > 1 && this.pageLineCount > 0) {
  611. this.bookPos = this.linesUp[1].begin;
  612. }
  613. }
  614. doPageDown() {
  615. if (this.linesDown && this.pageLineCount > 0) {
  616. let i = this.pageLineCount;
  617. if (this.keepLastToFirst)
  618. i--;
  619. if (i >= 0 && this.linesDown.length >= 2*i) {
  620. this.currentTransition = this.pageChangeTransition;
  621. this.pageChangeDirectionDown = true;
  622. this.bookPos = this.linesDown[i].begin;
  623. } else
  624. this.doEnd();
  625. }
  626. }
  627. doPageUp() {
  628. if (this.linesUp && this.pageLineCount > 0) {
  629. let i = this.pageLineCount;
  630. if (this.keepLastToFirst)
  631. i--;
  632. i = (i > this.linesUp.length - 1 ? this.linesUp.length - 1 : i);
  633. if (i >= 0 && this.linesUp.length > i) {
  634. this.currentTransition = this.pageChangeTransition;
  635. this.pageChangeDirectionDown = false;
  636. this.bookPos = this.linesUp[i].begin;
  637. }
  638. }
  639. }
  640. doHome() {
  641. this.bookPos = 0;
  642. }
  643. doEnd() {
  644. if (this.parsed.para.length && this.pageLineCount > 0) {
  645. let i = this.parsed.para.length - 1;
  646. let lastPos = this.parsed.para[i].offset + this.parsed.para[i].length - 1;
  647. const lines = this.parsed.getLines(lastPos, -this.pageLineCount);
  648. if (lines) {
  649. i = this.pageLineCount - 1;
  650. i = (i > lines.length - 1 ? lines.length - 1 : i);
  651. this.bookPos = lines[i].begin;
  652. }
  653. }
  654. }
  655. doToolBarToggle() {
  656. this.$emit('tool-bar-toggle');
  657. }
  658. keyHook(event) {
  659. //console.log(event.code);
  660. if (event.type == 'keydown') {
  661. switch (event.code) {
  662. case 'ArrowDown':
  663. this.doDown();
  664. break;
  665. case 'ArrowUp':
  666. this.doUp();
  667. break;
  668. case 'PageDown':
  669. case 'ArrowRight':
  670. case 'Space':
  671. this.doPageDown();
  672. break;
  673. case 'PageUp':
  674. case 'ArrowLeft':
  675. case 'Backspace':
  676. this.doPageUp();
  677. break;
  678. case 'Home':
  679. this.doHome();
  680. break;
  681. case 'End':
  682. this.doEnd();
  683. break;
  684. case 'Enter':
  685. case 'Backquote'://`
  686. case 'KeyF':
  687. this.$emit('full-screen-toogle');
  688. break;
  689. case 'Tab':
  690. this.doToolBarToggle();
  691. event.preventDefault();
  692. event.stopPropagation();
  693. break;
  694. }
  695. }
  696. }
  697. async startClickRepeat(pointX, pointY) {
  698. this.repX = pointX;
  699. this.repY = pointY;
  700. if (!this.repInit && this.repDoing) {
  701. this.repInit = true;
  702. let delay = 400;
  703. while (this.repDoing) {
  704. this.handleClick(pointX, pointY);
  705. await sleep(delay);
  706. if (delay > 15)
  707. delay *= 0.8;
  708. }
  709. this.repInit = false;
  710. }
  711. }
  712. endClickRepeat() {
  713. this.repDoing = false;
  714. }
  715. onTouchStart(event) {
  716. if (!this.mobile)
  717. return;
  718. this.endClickRepeat();
  719. if (event.touches.length == 1) {
  720. const touch = event.touches[0];
  721. const rect = event.target.getBoundingClientRect();
  722. const x = touch.pageX - rect.left;
  723. const y = touch.pageY - rect.top;
  724. if (this.handleClick(x, y)) {
  725. this.repDoing = true;
  726. this.debouncedStartClickRepeat(x, y);
  727. }
  728. }
  729. }
  730. onTouchEnd() {
  731. if (!this.mobile)
  732. return;
  733. this.endClickRepeat();
  734. }
  735. onTouchCancel() {
  736. if (!this.mobile)
  737. return;
  738. this.endClickRepeat();
  739. }
  740. onMouseDown(event) {
  741. if (this.mobile)
  742. return;
  743. this.endClickRepeat();
  744. if (event.button == 0) {
  745. if (this.handleClick(event.offsetX, event.offsetY)) {
  746. this.repDoing = true;
  747. this.debouncedStartClickRepeat(event.offsetX, event.offsetY);
  748. }
  749. } else if (event.button == 2) {
  750. this.doToolBarToggle();
  751. }
  752. }
  753. onMouseUp() {
  754. if (this.mobile)
  755. return;
  756. this.endClickRepeat();
  757. }
  758. onMouseWheel(event) {
  759. if (this.mobile)
  760. return;
  761. if (event.deltaY > 0) {
  762. this.doDown();
  763. } else if (event.deltaY < 0) {
  764. this.doUp();
  765. }
  766. }
  767. onStatusBarClick() {
  768. window.open(this.meta.url, '_blank');
  769. }
  770. handleClick(pointX, pointY) {
  771. const mouseLegend = {
  772. 40: {30: 'PgUp', 100: 'PgDown'},
  773. 60: {40: 'Up', 60: 'Menu', 100: 'Down'},
  774. 100: {30: 'PgUp', 100: 'PgDown'}
  775. };
  776. const w = pointX/this.realWidth*100;
  777. const h = pointY/this.realHeight*100;
  778. let action = '';
  779. loops: {
  780. for (const x in mouseLegend) {
  781. for (const y in mouseLegend[x]) {
  782. if (w < x && h < y) {
  783. action = mouseLegend[x][y];
  784. break loops;
  785. }
  786. }
  787. }
  788. }
  789. switch (action) {
  790. case 'Down' ://Down
  791. this.doDown();
  792. break;
  793. case 'Up' ://Up
  794. this.doUp();
  795. break;
  796. case 'PgDown' ://PgDown
  797. this.doPageDown();
  798. break;
  799. case 'PgUp' ://PgUp
  800. this.doPageUp();
  801. break;
  802. case 'Menu' :
  803. this.doToolBarToggle();
  804. break;
  805. default :
  806. // Nothing
  807. }
  808. return (action && action != 'Menu');
  809. }
  810. }
  811. //-----------------------------------------------------------------------------
  812. </script>
  813. <style scoped>
  814. .main {
  815. flex: 1;
  816. margin: 0;
  817. padding: 0;
  818. overflow: hidden;
  819. position: relative;
  820. min-width: 200px;
  821. }
  822. .layout {
  823. margin: 0;
  824. padding: 0;
  825. position: absolute;
  826. z-index: 10;
  827. }
  828. .back {
  829. z-index: 5;
  830. }
  831. .events {
  832. z-index: 20;
  833. background-color: rgba(0,0,0,0);
  834. }
  835. </style>