TextPage.vue 26 KB

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