TextPage.vue 32 KB

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