TextPage.vue 26 KB

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