TextPage.vue 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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. const lines = this.getLines(this.bookPos);
  303. this.linesDown = lines.linesDown;
  304. this.linesUp = lines.linesUp;
  305. if (this.toggleLayout)
  306. this.page1 = this.drawPage(lines.linesDown);
  307. else
  308. this.page2 = this.drawPage(lines.linesDown);
  309. }
  310. if (this.currentTransition) {
  311. //this.currentTransition
  312. //this.pageChangeTransitionSpeed
  313. //this.pageChangeDirectionDown
  314. //curr to next transition
  315. //пока заглушка
  316. }
  317. this.currentTransition = '';
  318. this.pageChangeDirectionDown = false;//true только если PgDown
  319. this.pagePrepared = false;
  320. this.debouncedPrepareNextPage();
  321. this.debouncedDrawStatusBar();
  322. if (this.book && this.linesDown && this.linesDown.length < this.pageLineCount)
  323. this.doEnd();
  324. }
  325. getLines(bookPos) {
  326. if (!this.parsed || this.pageLineCount < 1)
  327. return {};
  328. return {
  329. linesDown: this.parsed.getLines(bookPos, 2*this.pageLineCount),
  330. linesUp: this.parsed.getLines(bookPos, -2*this.pageLineCount)
  331. };
  332. }
  333. drawPage(lines) {
  334. if (!this.lastBook || this.pageLineCount < 1)
  335. return '';
  336. let out = `<div class="layout" style="width: ${this.realWidth}px; height: ${this.realHeight}px;` +
  337. ` color: ${this.textColor}; background-color: ${this.backgroundColor}">`;
  338. if (!this.book || !lines || !this.parsed.textLength) {
  339. out += '</div>';
  340. return out;
  341. }
  342. const spaceWidth = this.measureText(' ', {});
  343. let y = this.indentTB - this.lineInterval/2 + (this.h - this.pageLineCount*this.lineHeight)/2 + this.fontSize*this.fontShift;
  344. if (this.showStatusBar)
  345. y += this.statusBarHeight*(this.statusBarTop ? 1 : 0);
  346. let len = lines.length;
  347. len = (len > this.pageLineCount ? len = this.pageLineCount : len);
  348. for (let i = 0; i < len; i++) {
  349. const line = lines[i];
  350. /* line:
  351. {
  352. begin: Number,
  353. end: Number,
  354. first: Boolean,
  355. last: Boolean,
  356. parts: array of {
  357. style: {bold: Boolean, italic: Boolean, center: Boolean}
  358. text: String,
  359. }
  360. }*/
  361. let indent = this.indentLR + (line.first ? this.p : 0);
  362. let lineText = '';
  363. let center = false;
  364. let centerStyle = {};
  365. for (const part of line.parts) {
  366. lineText += part.text;
  367. center = center || part.style.center;
  368. if (part.style.center)
  369. centerStyle = part.style.center;
  370. }
  371. let filled = false;
  372. // если выравнивание по ширине включено
  373. if (this.textAlignJustify && !line.last && !center) {
  374. const words = lineText.split(' ');
  375. if (words.length > 1) {
  376. const spaceCount = words.length - 1;
  377. const space = (this.w - line.width + spaceWidth*spaceCount)/spaceCount;
  378. let x = indent;
  379. for (const part of line.parts) {
  380. const font = this.fontByStyle(part.style);
  381. let partWords = part.text.split(' ');
  382. for (let i = 0; i < partWords.length; i++) {
  383. let word = partWords[i];
  384. out += this.drawHelper.fillText(word, x, y, font);
  385. x += this.measureText(word, part.style) + (i < partWords.length - 1 ? space : 0);
  386. }
  387. }
  388. filled = true;
  389. }
  390. }
  391. // просто выводим текст
  392. if (!filled) {
  393. let x = indent;
  394. x = (center ? this.indent + (this.w - this.measureText(lineText, centerStyle))/2 : x);
  395. for (const part of line.parts) {
  396. let text = part.text;
  397. const font = this.fontByStyle(part.style);
  398. out += this.drawHelper.fillText(text, x, y, font);
  399. x += this.measureText(text, part.style);
  400. }
  401. }
  402. y += this.lineHeight;
  403. }
  404. out += '</div>';
  405. return out;
  406. }
  407. drawStatusBar() {
  408. if (this.w < minLayoutWidth) {
  409. this.statusBar = null;
  410. return;
  411. }
  412. if (this.showStatusBar && this.linesDown && this.pageLineCount > 0) {
  413. const lines = this.linesDown;
  414. let i = this.pageLineCount;
  415. if (this.keepLastToFirst)
  416. i--;
  417. i = (i > lines.length - 1 ? lines.length - 1 : i);
  418. if (i >= 0) {
  419. let message = this.statusBarMessage;
  420. if (!message)
  421. message = this.title;
  422. this.statusBar = this.drawHelper.drawStatusBar(this.statusBarTop, this.statusBarHeight,
  423. lines[i].end, this.parsed.textLength, message);
  424. this.bookPosSeen = lines[i].end;
  425. }
  426. } else {
  427. this.statusBar = '';
  428. }
  429. }
  430. blinkCachedLoadMessage(state) {
  431. if (state === 'finish') {
  432. this.statusBarMessage = '';
  433. } else if (state) {
  434. this.statusBarMessage = 'Книга загружена из кеша';
  435. } else {
  436. this.statusBarMessage = ' ';
  437. }
  438. this.drawStatusBar();
  439. }
  440. async refreshTime() {
  441. if (!this.timeRefreshing) {
  442. this.timeRefreshing = true;
  443. await sleep(60*1000);
  444. if (this.book && this.parsed.textLength) {
  445. this.debouncedDrawStatusBar();
  446. }
  447. this.timeRefreshing = false;
  448. this.refreshTime();
  449. }
  450. }
  451. prepareNextPage() {
  452. // подготовка следующей страницы заранее
  453. if (!this.book || !this.parsed.textLength || !this.linesDown || this.pageLineCount < 1)
  454. return;
  455. if (!this.preparing) {
  456. this.preparing = true;
  457. (async() => {
  458. await sleep(100);
  459. if (this.cancelPrepare) {
  460. this.preparing = false;
  461. return;
  462. }
  463. let i = this.pageLineCount;
  464. if (this.keepLastToFirst)
  465. i--;
  466. if (i >= 0 && this.linesDown.length > i) {
  467. this.bookPosPrepared = this.linesDown[i].begin;
  468. const lines = this.getLines(this.bookPosPrepared);
  469. this.linesDownNext = lines.linesDown;
  470. this.linesUpNext = lines.linesUp;
  471. if (this.toggleLayout)
  472. this.page2 = this.drawPage(lines.linesDown);//наоборот
  473. else
  474. this.page1 = this.drawPage(lines.linesDown);
  475. this.pagePrepared = true;
  476. }
  477. this.preparing = false;
  478. })();
  479. }
  480. }
  481. doDown() {
  482. if (this.linesDown && this.linesDown.length > this.pageLineCount && this.pageLineCount > 0) {
  483. this.bookPos = this.linesDown[1].begin;
  484. }
  485. }
  486. doUp() {
  487. if (this.linesUp && this.linesUp.length > 1 && this.pageLineCount > 0) {
  488. this.bookPos = this.linesUp[1].begin;
  489. }
  490. }
  491. doPageDown() {
  492. if (this.linesDown && this.pageLineCount > 0) {
  493. let i = this.pageLineCount;
  494. if (this.keepLastToFirst)
  495. i--;
  496. if (i >= 0 && this.linesDown.length >= 2*i) {
  497. this.currentTransition = this.pageChangeTransition;
  498. this.pageChangeDirectionDown = true;
  499. this.bookPos = this.linesDown[i].begin;
  500. } else
  501. this.doEnd();
  502. }
  503. }
  504. doPageUp() {
  505. if (this.linesUp && this.pageLineCount > 0) {
  506. let i = this.pageLineCount;
  507. if (this.keepLastToFirst)
  508. i--;
  509. i = (i > this.linesUp.length - 1 ? this.linesUp.length - 1 : i);
  510. if (i >= 0 && this.linesUp.length > i) {
  511. this.currentTransition = this.pageChangeTransition;
  512. this.pageChangeDirectionDown = false;
  513. this.bookPos = this.linesUp[i].begin;
  514. }
  515. }
  516. }
  517. doHome() {
  518. this.bookPos = 0;
  519. }
  520. doEnd() {
  521. if (this.parsed.para.length && this.pageLineCount > 0) {
  522. let i = this.parsed.para.length - 1;
  523. let lastPos = this.parsed.para[i].offset + this.parsed.para[i].length - 1;
  524. const lines = this.parsed.getLines(lastPos, -this.pageLineCount);
  525. if (lines) {
  526. i = this.pageLineCount - 1;
  527. i = (i > lines.length - 1 ? lines.length - 1 : i);
  528. this.bookPos = lines[i].begin;
  529. }
  530. }
  531. }
  532. doToolBarToggle() {
  533. this.$emit('tool-bar-toggle');
  534. }
  535. keyHook(event) {
  536. //console.log(event.code);
  537. if (event.type == 'keydown') {
  538. switch (event.code) {
  539. case 'ArrowDown':
  540. this.doDown();
  541. break;
  542. case 'ArrowUp':
  543. this.doUp();
  544. break;
  545. case 'PageDown':
  546. case 'ArrowRight':
  547. case 'Space':
  548. this.doPageDown();
  549. break;
  550. case 'PageUp':
  551. case 'ArrowLeft':
  552. case 'Backspace':
  553. this.doPageUp();
  554. break;
  555. case 'Home':
  556. this.doHome();
  557. break;
  558. case 'End':
  559. this.doEnd();
  560. break;
  561. case 'Enter':
  562. case 'Backquote'://`
  563. case 'KeyF':
  564. this.$emit('full-screen-toogle');
  565. break;
  566. case 'Tab':
  567. this.doToolBarToggle();
  568. event.preventDefault();
  569. event.stopPropagation();
  570. break;
  571. }
  572. }
  573. }
  574. async startClickRepeat(pointX, pointY) {
  575. this.repX = pointX;
  576. this.repY = pointY;
  577. if (!this.repInit && this.repDoing) {
  578. this.repInit = true;
  579. let delay = 400;
  580. while (this.repDoing) {
  581. this.handleClick(pointX, pointY);
  582. await sleep(delay);
  583. if (delay > 15)
  584. delay *= 0.8;
  585. }
  586. this.repInit = false;
  587. }
  588. }
  589. endClickRepeat() {
  590. this.repDoing = false;
  591. }
  592. onTouchStart(event) {
  593. if (!this.mobile)
  594. return;
  595. this.endClickRepeat();
  596. if (event.touches.length == 1) {
  597. const touch = event.touches[0];
  598. const rect = event.target.getBoundingClientRect();
  599. const x = touch.pageX - rect.left;
  600. const y = touch.pageY - rect.top;
  601. if (this.handleClick(x, y)) {
  602. this.repDoing = true;
  603. this.debouncedStartClickRepeat(x, y);
  604. }
  605. }
  606. }
  607. onTouchEnd() {
  608. if (!this.mobile)
  609. return;
  610. this.endClickRepeat();
  611. }
  612. onTouchCancel() {
  613. if (!this.mobile)
  614. return;
  615. this.endClickRepeat();
  616. }
  617. onMouseDown(event) {
  618. if (this.mobile)
  619. return;
  620. this.endClickRepeat();
  621. if (event.button == 0) {
  622. if (this.handleClick(event.offsetX, event.offsetY)) {
  623. this.repDoing = true;
  624. this.debouncedStartClickRepeat(event.offsetX, event.offsetY);
  625. }
  626. } else if (event.button == 2) {
  627. this.doToolBarToggle();
  628. }
  629. }
  630. onMouseUp() {
  631. if (this.mobile)
  632. return;
  633. this.endClickRepeat();
  634. }
  635. onMouseWheel(event) {
  636. if (this.mobile)
  637. return;
  638. if (event.deltaY > 0) {
  639. this.doDown();
  640. } else if (event.deltaY < 0) {
  641. this.doUp();
  642. }
  643. }
  644. onStatusBarClick() {
  645. window.open(this.meta.url, '_blank');
  646. }
  647. handleClick(pointX, pointY) {
  648. const mouseLegend = {
  649. 40: {30: 'PgUp', 100: 'PgDown'},
  650. 60: {40: 'Up', 60: 'Menu', 100: 'Down'},
  651. 100: {30: 'PgUp', 100: 'PgDown'}
  652. };
  653. const w = pointX/this.realWidth*100;
  654. const h = pointY/this.realHeight*100;
  655. let action = '';
  656. loops: {
  657. for (const x in mouseLegend) {
  658. for (const y in mouseLegend[x]) {
  659. if (w < x && h < y) {
  660. action = mouseLegend[x][y];
  661. break loops;
  662. }
  663. }
  664. }
  665. }
  666. switch (action) {
  667. case 'Down' ://Down
  668. this.doDown();
  669. break;
  670. case 'Up' ://Up
  671. this.doUp();
  672. break;
  673. case 'PgDown' ://PgDown
  674. this.doPageDown();
  675. break;
  676. case 'PgUp' ://PgUp
  677. this.doPageUp();
  678. break;
  679. case 'Menu' :
  680. this.doToolBarToggle();
  681. break;
  682. default :
  683. // Nothing
  684. }
  685. return (action && action != 'Menu');
  686. }
  687. }
  688. //-----------------------------------------------------------------------------
  689. </script>
  690. <style scoped>
  691. .main {
  692. flex: 1;
  693. margin: 0;
  694. padding: 0;
  695. overflow: hidden;
  696. position: relative;
  697. min-width: 200px;
  698. }
  699. .layout {
  700. margin: 0;
  701. padding: 0;
  702. position: absolute;
  703. z-index: 10;
  704. }
  705. .events {
  706. z-index: 20;
  707. background-color: rgba(0,0,0,0);
  708. }
  709. </style>