TextPage.vue 37 KB

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