TextPage.vue 37 KB

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