TextPage.vue 37 KB

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