TextPage.vue 38 KB

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