TextPage.vue 26 KB

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