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