TextPage.vue 27 KB

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