TextPage.vue 28 KB

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