TextPage.vue 26 KB

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