TextPage.vue 28 KB

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