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