TextPage.vue 28 KB

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