TextPage.vue 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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. await this.stopTextScrolling();
  310. this.calcDrawProps();
  311. this.setBackground();
  312. this.draw();
  313. }
  314. get settings() {
  315. return this.$store.state.reader.settings;
  316. }
  317. get font() {
  318. return `${this.fontStyle} ${this.fontWeight} ${this.fontSize}px ${this.fontName}`;
  319. }
  320. fontByStyle(style) {
  321. return `${style.italic ? 'italic' : this.fontStyle} ${style.bold ? 'bold' : this.fontWeight} ${this.fontSize}px ${this.fontName}`;
  322. }
  323. onScrollingTransitionEnd() {
  324. if (this.resolveTransitionFinish)
  325. this.resolveTransitionFinish();
  326. }
  327. async startTextScrolling() {
  328. if (this.doingScrolling || !this.book || !this.parsed.textLength || !this.linesDown || this.pageLineCount < 1 ||
  329. this.linesDown.length <= this.pageLineCount) {
  330. this.$emit('scrolling-toggle');
  331. return;
  332. }
  333. this.stopScrolling = false;
  334. this.doingScrolling = true;
  335. const transitionFinish = (timeout) => {
  336. return new Promise(async(resolve) => {
  337. this.resolveTransitionFinish = resolve;
  338. let steps = timeout/100;
  339. while (steps > 0 && !this.stopScrolling) {
  340. steps--;
  341. await sleep(100);
  342. }
  343. resolve();
  344. });
  345. };
  346. if (!this.toggleLayout)
  347. this.page1 = this.page2;
  348. this.toggleLayout = true;
  349. await sleep(50);
  350. const page = this.$refs.scrollingPage;
  351. let i = 0;
  352. while (!this.stopScrolling) {
  353. page.style.transition = 'all 2s linear 0s';
  354. page.style.transform = `translateY(-${this.lineHeight}px)`;
  355. if (i > 0) {
  356. this.doDown();
  357. if (this.linesDown.length <= this.pageLineCount + 1) {
  358. this.$emit('scrolling-toggle');
  359. this.stopScrolling = true;
  360. }
  361. }
  362. await transitionFinish(2500);
  363. page.style.transition = '';
  364. page.style.transform = 'none';
  365. page.offsetHeight;
  366. i++;
  367. }
  368. page.style.transition = '';
  369. page.style.transform = 'none';
  370. this.resolveTransitionFinish = null;
  371. this.doingScrolling = false;
  372. }
  373. async stopTextScrolling() {
  374. this.stopScrolling = true;
  375. const page = this.$refs.scrollingPage;
  376. page.style.transition = '';
  377. page.style.transform = 'none';
  378. page.offsetHeight;
  379. while (this.doingScrolling) await sleep(10);
  380. }
  381. draw() {
  382. if (this.doingScrolling) {
  383. const lines = this.getLines(this.bookPos);
  384. this.linesDown = lines.linesDown;
  385. this.linesUp = lines.linesUp;
  386. this.page1 = this.drawPage(lines.linesDown);
  387. this.debouncedDrawStatusBar();
  388. return;
  389. }
  390. if (this.w < minLayoutWidth) {
  391. this.page1 = null;
  392. this.page2 = null;
  393. this.statusBar = null;
  394. return;
  395. }
  396. if (this.book && this.bookPos > 0 && this.bookPos >= this.parsed.textLength) {
  397. this.doEnd();
  398. return;
  399. }
  400. if (this.pageChangeDirectionDown && this.pagePrepared && this.bookPos == this.bookPosPrepared) {
  401. this.toggleLayout = !this.toggleLayout;
  402. this.linesDown = this.linesDownNext;
  403. this.linesUp = this.linesUpNext;
  404. this.doPageTransition();
  405. } else {
  406. const lines = this.getLines(this.bookPos);
  407. this.linesDown = lines.linesDown;
  408. this.linesUp = lines.linesUp;
  409. /*if (this.toggleLayout)
  410. this.page1 = this.drawPage(lines.linesDown);
  411. else
  412. this.page2 = this.drawPage(lines.linesDown);*/
  413. this.debouncedUpdatePage(lines.linesDown);
  414. }
  415. this.pagePrepared = false;
  416. this.debouncedPrepareNextPage();
  417. this.debouncedDrawStatusBar();
  418. if (this.book && this.linesDown && this.linesDown.length < this.pageLineCount)
  419. this.doEnd();
  420. }
  421. doPageTransition() {
  422. if (this.currentTransition) {
  423. //this.currentTransition
  424. //this.pageChangeTransitionSpeed
  425. //this.pageChangeDirectionDown
  426. //curr to next transition
  427. //пока заглушка
  428. }
  429. this.currentTransition = '';
  430. this.pageChangeDirectionDown = false;//true только если PgDown
  431. }
  432. getLines(bookPos) {
  433. if (!this.parsed || this.pageLineCount < 1)
  434. return {};
  435. return {
  436. linesDown: this.parsed.getLines(bookPos, 2*this.pageLineCount),
  437. linesUp: this.parsed.getLines(bookPos, -2*this.pageLineCount)
  438. };
  439. }
  440. drawPage(lines) {
  441. if (!this.lastBook || this.pageLineCount < 1)
  442. return '';
  443. let out = `<div class="layout" style="width: ${this.realWidth}px; height: ${this.realHeight}px;` +
  444. ` color: ${this.textColor}">`;
  445. if (!this.book || !lines || !this.parsed.textLength) {
  446. out += '</div>';
  447. return out;
  448. }
  449. const spaceWidth = this.measureText(' ', {});
  450. let y = this.indentTB + (this.h - this.pageLineCount*this.lineHeight + this.lineInterval)/2 + this.fontSize*this.textShift;
  451. if (this.showStatusBar)
  452. y += this.statusBarHeight*(this.statusBarTop ? 1 : 0);
  453. let len = lines.length;
  454. len = (len > this.pageLineCount + 1 ? this.pageLineCount + 1 : len);
  455. for (let i = 0; i < len; i++) {
  456. const line = lines[i];
  457. /* line:
  458. {
  459. begin: Number,
  460. end: Number,
  461. first: Boolean,
  462. last: Boolean,
  463. parts: array of {
  464. style: {bold: Boolean, italic: Boolean, center: Boolean}
  465. text: String,
  466. }
  467. }*/
  468. let indent = this.indentLR + (line.first ? this.p : 0);
  469. let lineText = '';
  470. let center = false;
  471. let centerStyle = {};
  472. for (const part of line.parts) {
  473. lineText += part.text;
  474. center = center || part.style.center;
  475. if (part.style.center)
  476. centerStyle = part.style;
  477. }
  478. let filled = false;
  479. // если выравнивание по ширине включено
  480. if (this.textAlignJustify && !line.last && !center) {
  481. const words = lineText.split(' ');
  482. if (words.length > 1) {
  483. const spaceCount = words.length - 1;
  484. const space = (this.w - line.width + spaceWidth*spaceCount)/spaceCount;
  485. let x = indent;
  486. for (const part of line.parts) {
  487. const font = this.fontByStyle(part.style);
  488. let partWords = part.text.split(' ');
  489. for (let i = 0; i < partWords.length; i++) {
  490. let word = partWords[i];
  491. out += this.drawHelper.fillText(word, x, y, font);
  492. x += this.measureText(word, part.style) + (i < partWords.length - 1 ? space : 0);
  493. }
  494. }
  495. filled = true;
  496. }
  497. }
  498. // просто выводим текст
  499. if (!filled) {
  500. let x = indent;
  501. x = (center ? this.indentLR + (this.w - this.measureText(lineText, centerStyle))/2 : x);
  502. for (const part of line.parts) {
  503. let text = part.text;
  504. const font = this.fontByStyle(part.style);
  505. out += this.drawHelper.fillText(text, x, y, font);
  506. x += this.measureText(text, part.style);
  507. }
  508. }
  509. y += this.lineHeight;
  510. }
  511. out += '</div>';
  512. return out;
  513. }
  514. drawStatusBar(message) {
  515. if (this.w < minLayoutWidth) {
  516. this.statusBar = null;
  517. return;
  518. }
  519. if (this.showStatusBar && this.linesDown && this.pageLineCount > 0) {
  520. const lines = this.linesDown;
  521. let i = this.pageLineCount;
  522. if (this.keepLastToFirst)
  523. i--;
  524. i = (i > lines.length - 1 ? lines.length - 1 : i);
  525. if (i >= 0) {
  526. if (!message)
  527. message = this.statusBarMessage;
  528. if (!message)
  529. message = this.title;
  530. this.statusBar = this.drawHelper.drawStatusBar(this.statusBarTop, this.statusBarHeight,
  531. lines[i].end, this.parsed.textLength, message);
  532. this.bookPosSeen = lines[i].end;
  533. }
  534. } else {
  535. this.statusBar = '';
  536. }
  537. }
  538. blinkCachedLoadMessage(state) {
  539. if (state === 'finish') {
  540. this.statusBarMessage = '';
  541. } else if (state) {
  542. this.statusBarMessage = 'Книга загружена из кеша';
  543. } else {
  544. this.statusBarMessage = ' ';
  545. }
  546. this.drawStatusBar();
  547. }
  548. async lazyParsePara() {
  549. if (!this.parsed || this.doingLazyParse)
  550. return;
  551. this.doingLazyParse = true;
  552. let j = 0;
  553. let k = 0;
  554. let prevPerc = 0;
  555. this.stopLazyParse = false;
  556. for (let i = 0; i < this.parsed.para.length; i++) {
  557. j++;
  558. if (j > 1) {
  559. await sleep(1);
  560. j = 0;
  561. }
  562. if (this.stopLazyParse)
  563. break;
  564. this.parsed.parsePara(i);
  565. k++;
  566. if (k > 100) {
  567. let perc = Math.round(i/this.parsed.para.length*100);
  568. if (perc != prevPerc)
  569. this.drawStatusBar(`Обработка текста ${perc}%`);
  570. prevPerc = perc;
  571. k = 0;
  572. }
  573. }
  574. this.drawStatusBar();
  575. this.doingLazyParse = false;
  576. }
  577. async refreshTime() {
  578. if (!this.timeRefreshing) {
  579. this.timeRefreshing = true;
  580. await sleep(60*1000);
  581. if (this.book && this.parsed.textLength) {
  582. this.debouncedDrawStatusBar();
  583. }
  584. this.timeRefreshing = false;
  585. this.refreshTime();
  586. }
  587. }
  588. prepareNextPage() {
  589. // подготовка следующей страницы заранее
  590. if (!this.book || !this.parsed.textLength || !this.linesDown || this.pageLineCount < 1)
  591. return;
  592. let i = this.pageLineCount;
  593. if (this.keepLastToFirst)
  594. i--;
  595. if (i >= 0 && this.linesDown.length > i) {
  596. this.bookPosPrepared = this.linesDown[i].begin;
  597. const lines = this.getLines(this.bookPosPrepared);
  598. this.linesDownNext = lines.linesDown;
  599. this.linesUpNext = lines.linesUp;
  600. if (this.toggleLayout)
  601. this.page2 = this.drawPage(lines.linesDown);//наоборот
  602. else
  603. this.page1 = this.drawPage(lines.linesDown);
  604. this.pagePrepared = true;
  605. }
  606. }
  607. doDown() {
  608. if (this.linesDown && this.linesDown.length > this.pageLineCount && this.pageLineCount > 0) {
  609. this.bookPos = this.linesDown[1].begin;
  610. }
  611. }
  612. doUp() {
  613. if (this.linesUp && this.linesUp.length > 1 && this.pageLineCount > 0) {
  614. this.bookPos = this.linesUp[1].begin;
  615. }
  616. }
  617. doPageDown() {
  618. if (this.linesDown && this.pageLineCount > 0) {
  619. let i = this.pageLineCount;
  620. if (this.keepLastToFirst)
  621. i--;
  622. if (i >= 0 && this.linesDown.length >= 2*i) {
  623. this.currentTransition = this.pageChangeTransition;
  624. this.pageChangeDirectionDown = true;
  625. this.bookPos = this.linesDown[i].begin;
  626. } else
  627. this.doEnd();
  628. }
  629. }
  630. doPageUp() {
  631. if (this.linesUp && this.pageLineCount > 0) {
  632. let i = this.pageLineCount;
  633. if (this.keepLastToFirst)
  634. i--;
  635. i = (i > this.linesUp.length - 1 ? this.linesUp.length - 1 : i);
  636. if (i >= 0 && this.linesUp.length > i) {
  637. this.currentTransition = this.pageChangeTransition;
  638. this.pageChangeDirectionDown = false;
  639. this.bookPos = this.linesUp[i].begin;
  640. }
  641. }
  642. }
  643. doHome() {
  644. this.bookPos = 0;
  645. }
  646. doEnd() {
  647. if (this.parsed.para.length && this.pageLineCount > 0) {
  648. let i = this.parsed.para.length - 1;
  649. let lastPos = this.parsed.para[i].offset + this.parsed.para[i].length - 1;
  650. const lines = this.parsed.getLines(lastPos, -this.pageLineCount);
  651. if (lines) {
  652. i = this.pageLineCount - 1;
  653. i = (i > lines.length - 1 ? lines.length - 1 : i);
  654. this.bookPos = lines[i].begin;
  655. }
  656. }
  657. }
  658. doToolBarToggle() {
  659. this.$emit('tool-bar-toggle');
  660. }
  661. keyHook(event) {
  662. //console.log(event.code);
  663. if (event.type == 'keydown') {
  664. switch (event.code) {
  665. case 'ArrowDown':
  666. this.doDown();
  667. break;
  668. case 'ArrowUp':
  669. this.doUp();
  670. break;
  671. case 'PageDown':
  672. case 'ArrowRight':
  673. case 'Space':
  674. this.doPageDown();
  675. break;
  676. case 'PageUp':
  677. case 'ArrowLeft':
  678. case 'Backspace':
  679. this.doPageUp();
  680. break;
  681. case 'Home':
  682. this.doHome();
  683. break;
  684. case 'End':
  685. this.doEnd();
  686. break;
  687. case 'Enter':
  688. case 'Backquote'://`
  689. case 'KeyF':
  690. this.$emit('full-screen-toogle');
  691. break;
  692. case 'Tab':
  693. this.doToolBarToggle();
  694. event.preventDefault();
  695. event.stopPropagation();
  696. break;
  697. }
  698. }
  699. }
  700. async startClickRepeat(pointX, pointY) {
  701. this.repX = pointX;
  702. this.repY = pointY;
  703. if (!this.repInit && this.repDoing) {
  704. this.repInit = true;
  705. let delay = 400;
  706. while (this.repDoing) {
  707. this.handleClick(pointX, pointY);
  708. await sleep(delay);
  709. if (delay > 15)
  710. delay *= 0.8;
  711. }
  712. this.repInit = false;
  713. }
  714. }
  715. endClickRepeat() {
  716. this.repDoing = false;
  717. }
  718. onTouchStart(event) {
  719. if (!this.mobile)
  720. return;
  721. this.endClickRepeat();
  722. if (event.touches.length == 1) {
  723. const touch = event.touches[0];
  724. const rect = event.target.getBoundingClientRect();
  725. const x = touch.pageX - rect.left;
  726. const y = touch.pageY - rect.top;
  727. if (this.handleClick(x, y)) {
  728. this.repDoing = true;
  729. this.debouncedStartClickRepeat(x, y);
  730. }
  731. }
  732. }
  733. onTouchEnd() {
  734. if (!this.mobile)
  735. return;
  736. this.endClickRepeat();
  737. }
  738. onTouchCancel() {
  739. if (!this.mobile)
  740. return;
  741. this.endClickRepeat();
  742. }
  743. onMouseDown(event) {
  744. if (this.mobile)
  745. return;
  746. this.endClickRepeat();
  747. if (event.button == 0) {
  748. if (this.handleClick(event.offsetX, event.offsetY)) {
  749. this.repDoing = true;
  750. this.debouncedStartClickRepeat(event.offsetX, event.offsetY);
  751. }
  752. } else if (event.button == 2) {
  753. this.doToolBarToggle();
  754. }
  755. }
  756. onMouseUp() {
  757. if (this.mobile)
  758. return;
  759. this.endClickRepeat();
  760. }
  761. onMouseWheel(event) {
  762. if (this.mobile)
  763. return;
  764. if (event.deltaY > 0) {
  765. this.doDown();
  766. } else if (event.deltaY < 0) {
  767. this.doUp();
  768. }
  769. }
  770. onStatusBarClick() {
  771. window.open(this.meta.url, '_blank');
  772. }
  773. handleClick(pointX, pointY) {
  774. const mouseLegend = {
  775. 40: {30: 'PgUp', 100: 'PgDown'},
  776. 60: {40: 'Up', 60: 'Menu', 100: 'Down'},
  777. 100: {30: 'PgUp', 100: 'PgDown'}
  778. };
  779. const w = pointX/this.realWidth*100;
  780. const h = pointY/this.realHeight*100;
  781. let action = '';
  782. loops: {
  783. for (const x in mouseLegend) {
  784. for (const y in mouseLegend[x]) {
  785. if (w < x && h < y) {
  786. action = mouseLegend[x][y];
  787. break loops;
  788. }
  789. }
  790. }
  791. }
  792. switch (action) {
  793. case 'Down' ://Down
  794. this.doDown();
  795. break;
  796. case 'Up' ://Up
  797. this.doUp();
  798. break;
  799. case 'PgDown' ://PgDown
  800. this.doPageDown();
  801. break;
  802. case 'PgUp' ://PgUp
  803. this.doPageUp();
  804. break;
  805. case 'Menu' :
  806. this.doToolBarToggle();
  807. break;
  808. default :
  809. // Nothing
  810. }
  811. return (action && action != 'Menu');
  812. }
  813. }
  814. //-----------------------------------------------------------------------------
  815. </script>
  816. <style scoped>
  817. .main {
  818. flex: 1;
  819. margin: 0;
  820. padding: 0;
  821. overflow: hidden;
  822. position: relative;
  823. min-width: 200px;
  824. }
  825. .layout {
  826. margin: 0;
  827. padding: 0;
  828. position: absolute;
  829. z-index: 10;
  830. }
  831. .back {
  832. z-index: 5;
  833. }
  834. .events {
  835. z-index: 20;
  836. background-color: rgba(0,0,0,0);
  837. }
  838. </style>