TextPage.vue 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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" @touchmove.stop="onTouchMove" @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>
  27. <div v-show="!clickControl && showStatusBar" class="layout" v-html="statusBarClickable" @mousedown.prevent.stop @touchstart.stop
  28. @click.prevent.stop="onStatusBarClick"></div>
  29. <!-- невидимым делать нельзя, вовремя не подгружаютя шрифты -->
  30. <canvas ref="offscreenCanvas" class="layout" style="width: 0px; height: 0px"></canvas>
  31. </div>
  32. </template>
  33. <script>
  34. //-----------------------------------------------------------------------------
  35. import Vue from 'vue';
  36. import Component from 'vue-class-component';
  37. import {loadCSS} from 'fg-loadcss';
  38. import _ from 'lodash';
  39. import {sleep} from '../../../share/utils';
  40. import bookManager from '../share/bookManager';
  41. import DrawHelper from './DrawHelper';
  42. import rstore from '../../../store/modules/reader';
  43. import {clickMap} from '../share/clickMap';
  44. const minLayoutWidth = 100;
  45. export default @Component({
  46. watch: {
  47. bookPos: function() {
  48. this.$emit('book-pos-changed', {bookPos: this.bookPos, bookPosSeen: this.bookPosSeen});
  49. this.draw();
  50. },
  51. bookPosSeen: function() {
  52. this.$emit('book-pos-changed', {bookPos: this.bookPos, bookPosSeen: this.bookPosSeen});
  53. },
  54. settings: function() {
  55. this.debouncedLoadSettings();
  56. },
  57. toggleLayout: function() {
  58. this.updateLayout();
  59. },
  60. inAnimation: function() {
  61. this.updateLayout();
  62. },
  63. },
  64. })
  65. class TextPage extends Vue {
  66. toggleLayout = false;
  67. showStatusBar = false;
  68. clickControl = true;
  69. background = null;
  70. page1 = null;
  71. page2 = null;
  72. statusBar = null;
  73. statusBarClickable = null;
  74. lastBook = null;
  75. bookPos = 0;
  76. bookPosSeen = null;
  77. fontStyle = null;
  78. fontSize = null;
  79. fontName = null;
  80. fontWeight = null;
  81. inAnimation = false;
  82. meta = null;
  83. created() {
  84. this.drawHelper = new DrawHelper();
  85. this.commit = this.$store.commit;
  86. this.dispatch = this.$store.dispatch;
  87. this.config = this.$store.state.config;
  88. this.reader = this.$store.state.reader;
  89. this.debouncedStartClickRepeat = _.debounce((x, y) => {
  90. this.startClickRepeat(x, y);
  91. }, 800);
  92. this.debouncedPrepareNextPage = _.debounce(() => {
  93. this.prepareNextPage();
  94. }, 100);
  95. this.debouncedDrawStatusBar = _.throttle(() => {
  96. this.drawStatusBar();
  97. }, 60);
  98. this.debouncedLoadSettings = _.debounce(() => {
  99. this.loadSettings();
  100. }, 50);
  101. this.debouncedUpdatePage = _.debounce(async(lines) => {
  102. if (!this.pageChangeAnimation)
  103. this.toggleLayout = !this.toggleLayout;
  104. else {
  105. this.page2 = this.page1;
  106. this.toggleLayout = true;
  107. }
  108. if (this.toggleLayout)
  109. this.page1 = this.drawHelper.drawPage(lines);
  110. else
  111. this.page2 = this.drawHelper.drawPage(lines);
  112. await this.doPageAnimation();
  113. }, 10);
  114. this.$root.$on('resize', () => {this.$nextTick(this.onResize)});
  115. }
  116. mounted() {
  117. this.context = this.$refs.offscreenCanvas.getContext('2d');
  118. }
  119. hex2rgba(hex, alpha = 1) {
  120. const [r, g, b] = hex.match(/\w\w/g).map(x => parseInt(x, 16));
  121. return `rgba(${r},${g},${b},${alpha})`;
  122. }
  123. calcDrawProps() {
  124. //preloaded fonts
  125. this.fontList = [`12px ${this.fontName}`];
  126. //widths
  127. this.realWidth = this.$refs.main.clientWidth;
  128. this.realHeight = this.$refs.main.clientHeight;
  129. this.$refs.layoutEvents.style.width = this.realWidth + 'px';
  130. this.$refs.layoutEvents.style.height = this.realHeight + 'px';
  131. this.w = this.realWidth - 2*this.indentLR;
  132. this.scrollHeight = this.realHeight - (this.showStatusBar ? this.statusBarHeight : 0);
  133. this.h = this.scrollHeight - 2*this.indentTB;
  134. this.lineHeight = this.fontSize + this.lineInterval;
  135. this.pageLineCount = 1 + Math.floor((this.h - this.lineHeight + this.lineInterval/2)/this.lineHeight);
  136. this.$refs.scrollingPage1.style.width = this.w + 'px';
  137. this.$refs.scrollingPage2.style.width = this.w + 'px';
  138. //stuff
  139. this.currentAnimation = '';
  140. this.pageChangeDirectionDown = true;
  141. this.fontShift = this.fontVertShift/100;
  142. this.textShift = this.textVertShift/100 + this.fontShift;
  143. //statusBar
  144. this.$refs.statusBar.style.left = '0px';
  145. this.$refs.statusBar.style.top = (this.statusBarTop ? 1 : this.realHeight - this.statusBarHeight) + 'px';
  146. this.statusBarColor = this.hex2rgba(this.textColor || '#000000', this.statusBarColorAlpha);
  147. //drawHelper
  148. this.drawHelper.realWidth = this.realWidth;
  149. this.drawHelper.realHeight = this.realHeight;
  150. this.drawHelper.lastBook = this.lastBook;
  151. this.drawHelper.book = this.book;
  152. this.drawHelper.parsed = this.parsed;
  153. this.drawHelper.pageLineCount = this.pageLineCount;
  154. this.drawHelper.backgroundColor = this.backgroundColor;
  155. this.drawHelper.statusBarColor = this.statusBarColor;
  156. this.drawHelper.fontStyle = this.fontStyle;
  157. this.drawHelper.fontWeight = this.fontWeight;
  158. this.drawHelper.fontSize = this.fontSize;
  159. this.drawHelper.fontName = this.fontName;
  160. this.drawHelper.fontShift = this.fontShift;
  161. this.drawHelper.textColor = this.textColor;
  162. this.drawHelper.textShift = this.textShift;
  163. this.drawHelper.p = this.p;
  164. this.drawHelper.w = this.w;
  165. this.drawHelper.h = this.h;
  166. this.drawHelper.indentLR = this.indentLR;
  167. this.drawHelper.textAlignJustify = this.textAlignJustify;
  168. this.drawHelper.lineHeight = this.lineHeight;
  169. this.drawHelper.context = this.context;
  170. //statusBar
  171. this.statusBarClickable = this.drawHelper.statusBarClickable(this.statusBarTop, this.statusBarHeight);
  172. //parsed
  173. if (this.parsed) {
  174. this.parsed.p = this.p;
  175. this.parsed.w = this.w;// px, ширина текста
  176. this.parsed.font = this.font;
  177. this.parsed.fontSize = this.fontSize;
  178. this.parsed.wordWrap = this.wordWrap;
  179. this.parsed.cutEmptyParagraphs = this.cutEmptyParagraphs;
  180. this.parsed.addEmptyParagraphs = this.addEmptyParagraphs;
  181. let t = '';
  182. while (this.drawHelper.measureText(t, {}) < this.w) t += 'Щ';
  183. this.parsed.maxWordLength = t.length - 1;
  184. this.parsed.measureText = this.drawHelper.measureText.bind(this.drawHelper);
  185. this.parsed.lineHeight = this.lineHeight;
  186. this.parsed.showImages = this.showImages;
  187. this.parsed.showInlineImagesInCenter = this.showInlineImagesInCenter;
  188. this.parsed.imageHeightLines = this.imageHeightLines;
  189. this.parsed.imageFitWidth = this.imageFitWidth;
  190. this.parsed.compactTextPerc = this.compactTextPerc;
  191. }
  192. //scrolling page
  193. const pageSpace = this.scrollHeight - this.pageLineCount*this.lineHeight;
  194. let y = pageSpace/2;
  195. if (this.showStatusBar)
  196. y += this.statusBarHeight*(this.statusBarTop ? 1 : 0);
  197. let page1 = this.$refs.scrollBox1;
  198. let page2 = this.$refs.scrollBox2;
  199. page1.style.perspective = '3072px';
  200. page2.style.perspective = '3072px';
  201. page1.style.width = this.w + this.indentLR + 'px';
  202. page2.style.width = this.w + this.indentLR + 'px';
  203. page1.style.height = this.scrollHeight - (pageSpace > 0 ? pageSpace : 0) + 'px';
  204. page2.style.height = this.scrollHeight - (pageSpace > 0 ? pageSpace : 0) + 'px';
  205. page1.style.top = y + 'px';
  206. page2.style.top = y + 'px';
  207. page1.style.left = this.indentLR + 'px';
  208. page2.style.left = this.indentLR + 'px';
  209. page1 = this.$refs.scrollingPage1;
  210. page2 = this.$refs.scrollingPage2;
  211. page1.style.width = this.w + this.indentLR + 'px';
  212. page2.style.width = this.w + this.indentLR + 'px';
  213. page1.style.height = this.scrollHeight + this.lineHeight + 'px';
  214. page2.style.height = this.scrollHeight + this.lineHeight + 'px';
  215. }
  216. async checkLoadedFonts() {
  217. let loaded = await Promise.all(this.fontList.map(font => document.fonts.check(font)));
  218. if (loaded.some(r => !r)) {
  219. loaded = await Promise.all(this.fontList.map(font => document.fonts.load(font)));
  220. if (loaded.some(r => !r.length))
  221. throw new Error('some font not loaded');
  222. }
  223. }
  224. async loadFonts() {
  225. this.fontsLoading = true;
  226. let inst = null;
  227. (async() => {
  228. await sleep(500);
  229. if (this.fontsLoading)
  230. inst = this.$notify({
  231. title: '',
  232. dangerouslyUseHTMLString: true,
  233. message: 'Загрузка шрифта &nbsp;<i class="el-icon-loading"></i>',
  234. duration: 0
  235. });
  236. })();
  237. if (!this.fontsLoaded)
  238. this.fontsLoaded = {};
  239. //загрузка дин.шрифта
  240. const loaded = this.fontsLoaded[this.fontCssUrl];
  241. if (this.fontCssUrl && !loaded) {
  242. loadCSS(this.fontCssUrl);
  243. this.fontsLoaded[this.fontCssUrl] = 1;
  244. }
  245. const waitingTime = 10*1000;
  246. const delay = 100;
  247. let i = 0;
  248. //ждем шрифты
  249. while (i < waitingTime/delay) {
  250. i++;
  251. try {
  252. await this.checkLoadedFonts();
  253. i = waitingTime;
  254. } catch (e) {
  255. await sleep(delay);
  256. }
  257. }
  258. if (i !== waitingTime) {
  259. this.$notify.error({
  260. title: 'Ошибка загрузки',
  261. message: 'Некоторые шрифты не удалось загрузить'
  262. });
  263. }
  264. this.fontsLoading = false;
  265. if (inst)
  266. inst.close();
  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((resolve, reject) => { (async() => {
  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. })().catch(reject); });
  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. case 'rotate':
  562. await this.drawHelper.doPageAnimationRotate(page1, page2,
  563. duration, this.pageChangeDirectionDown, transition1Finish, transition2Finish);
  564. break;
  565. case 'flip':
  566. await this.drawHelper.doPageAnimationFlip(page1, page2,
  567. duration, this.pageChangeDirectionDown, transition1Finish, transition2Finish, this.backgroundColor);
  568. break;
  569. }
  570. this.resolveAnimation1Finish = null;
  571. this.resolveAnimation2Finish = null;
  572. this.resolveTransition1Finish = null;
  573. this.resolveTransition2Finish = null;
  574. page1.style.animation = '';
  575. page2.style.animation = '';
  576. page1.style.transition = '';
  577. page1.style.transform = 'none';
  578. page1.offsetHeight;
  579. page2.style.transition = '';
  580. page2.style.transform = 'none';
  581. page2.offsetHeight;
  582. this.currentAnimation = '';
  583. this.pageChangeDirectionDown = false;//true только если PgDown
  584. this.inAnimation = false;
  585. this.stopAnimation = false;
  586. }
  587. }
  588. getLines(bookPos) {
  589. if (!this.parsed || this.pageLineCount < 1)
  590. return {};
  591. return {
  592. linesDown: this.parsed.getLines(bookPos, 2*this.pageLineCount),
  593. linesUp: this.parsed.getLines(bookPos, -2*this.pageLineCount)
  594. };
  595. }
  596. drawStatusBar(message) {
  597. if (this.w < minLayoutWidth) {
  598. this.statusBar = null;
  599. return;
  600. }
  601. if (this.showStatusBar && this.linesDown && this.pageLineCount > 0) {
  602. const lines = this.linesDown;
  603. let i = this.pageLineCount;
  604. if (this.keepLastToFirst)
  605. i--;
  606. i = (i > lines.length - 1 ? lines.length - 1 : i);
  607. if (i >= 0) {
  608. if (!message)
  609. message = this.statusBarMessage;
  610. if (!message)
  611. message = this.title;
  612. this.statusBar = this.drawHelper.drawStatusBar(this.statusBarTop, this.statusBarHeight,
  613. lines[i].end, this.parsed.textLength, message);
  614. this.bookPosSeen = lines[i].end;
  615. }
  616. } else {
  617. this.statusBar = '';
  618. }
  619. }
  620. blinkCachedLoadMessage(state) {
  621. if (state === 'finish') {
  622. this.statusBarMessage = '';
  623. } else if (state) {
  624. this.statusBarMessage = 'Книга загружена из кэша';
  625. } else {
  626. this.statusBarMessage = ' ';
  627. }
  628. this.drawStatusBar();
  629. }
  630. async lazyParsePara() {
  631. if (!this.parsed || this.doingLazyParse)
  632. return;
  633. this.doingLazyParse = true;
  634. let j = 0;
  635. let k = 0;
  636. let prevPerc = 0;
  637. this.stopLazyParse = false;
  638. for (let i = 0; i < this.parsed.para.length; i++) {
  639. j++;
  640. if (j > 1) {
  641. await sleep(1);
  642. j = 0;
  643. }
  644. if (this.stopLazyParse)
  645. break;
  646. this.parsed.parsePara(i);
  647. k++;
  648. if (k > 100) {
  649. let perc = Math.round(i/this.parsed.para.length*100);
  650. if (perc != prevPerc)
  651. this.drawStatusBar(`Обработка текста ${perc}%`);
  652. prevPerc = perc;
  653. k = 0;
  654. }
  655. }
  656. this.drawStatusBar();
  657. this.doingLazyParse = false;
  658. }
  659. async refreshTime() {
  660. if (!this.timeRefreshing) {
  661. this.timeRefreshing = true;
  662. await sleep(60*1000);
  663. if (this.book && this.parsed.textLength) {
  664. this.debouncedDrawStatusBar();
  665. }
  666. this.timeRefreshing = false;
  667. this.refreshTime();
  668. }
  669. }
  670. prepareNextPage() {
  671. // подготовка следующей страницы заранее
  672. if (!this.book || !this.parsed.textLength || !this.linesDown || this.pageLineCount < 1)
  673. return;
  674. let i = this.pageLineCount;
  675. if (this.keepLastToFirst)
  676. i--;
  677. if (i >= 0 && this.linesDown.length > i) {
  678. this.bookPosPrepared = this.linesDown[i].begin;
  679. const lines = this.getLines(this.bookPosPrepared);
  680. this.linesDownNext = lines.linesDown;
  681. this.linesUpNext = lines.linesUp;
  682. if (this.toggleLayout)
  683. this.page2 = this.drawHelper.drawPage(lines.linesDown);//наоборот
  684. else
  685. this.page1 = this.drawHelper.drawPage(lines.linesDown);
  686. this.pagePrepared = true;
  687. }
  688. }
  689. doDown() {
  690. if (this.linesDown && this.linesDown.length > this.pageLineCount && this.pageLineCount > 0) {
  691. this.bookPos = this.linesDown[1].begin;
  692. }
  693. }
  694. doUp() {
  695. if (this.linesUp && this.linesUp.length > 1 && this.pageLineCount > 0) {
  696. this.bookPos = this.linesUp[1].begin;
  697. }
  698. }
  699. doPageDown() {
  700. if (this.linesDown && this.pageLineCount > 0) {
  701. let i = this.pageLineCount;
  702. if (this.keepLastToFirst)
  703. i--;
  704. if (i >= 0 && this.linesDown.length >= 2*i) {
  705. this.currentAnimation = this.pageChangeAnimation;
  706. this.pageChangeDirectionDown = true;
  707. this.bookPos = this.linesDown[i].begin;
  708. } else
  709. this.doEnd();
  710. }
  711. }
  712. doPageUp() {
  713. if (this.linesUp && this.pageLineCount > 0) {
  714. let i = this.pageLineCount;
  715. if (this.keepLastToFirst)
  716. i--;
  717. i = (i > this.linesUp.length - 1 ? this.linesUp.length - 1 : i);
  718. if (i >= 0 && this.linesUp.length > i) {
  719. this.currentAnimation = this.pageChangeAnimation;
  720. this.pageChangeDirectionDown = false;
  721. this.bookPos = this.linesUp[i].begin;
  722. }
  723. }
  724. }
  725. doHome() {
  726. this.currentAnimation = this.pageChangeAnimation;
  727. this.pageChangeDirectionDown = false;
  728. this.bookPos = 0;
  729. }
  730. doEnd(noAni) {
  731. if (this.parsed.para.length && this.pageLineCount > 0) {
  732. let i = this.parsed.para.length - 1;
  733. let lastPos = this.parsed.para[i].offset + this.parsed.para[i].length - 1;
  734. const lines = this.parsed.getLines(lastPos, -this.pageLineCount);
  735. if (lines) {
  736. i = this.pageLineCount - 1;
  737. i = (i > lines.length - 1 ? lines.length - 1 : i);
  738. if (!noAni)
  739. this.currentAnimation = this.pageChangeAnimation;
  740. this.pageChangeDirectionDown = true;
  741. this.bookPos = lines[i].begin;
  742. }
  743. }
  744. }
  745. doToolBarToggle() {
  746. this.$emit('tool-bar-toggle');
  747. }
  748. doScrollingToggle() {
  749. this.$emit('scrolling-toggle');
  750. }
  751. doFullScreenToggle() {
  752. this.$emit('full-screen-toogle');
  753. }
  754. async doFontSizeInc() {
  755. if (!this.settingsChanging) {
  756. this.settingsChanging = true;
  757. const newSize = (this.settings.fontSize + 1 < 100 ? this.settings.fontSize + 1 : 100);
  758. const newSettings = Object.assign({}, this.settings, {fontSize: newSize});
  759. this.commit('reader/setSettings', newSettings);
  760. await sleep(50);
  761. this.settingsChanging = false;
  762. }
  763. }
  764. async doFontSizeDec() {
  765. if (!this.settingsChanging) {
  766. this.settingsChanging = true;
  767. const newSize = (this.settings.fontSize - 1 > 5 ? this.settings.fontSize - 1 : 5);
  768. const newSettings = Object.assign({}, this.settings, {fontSize: newSize});
  769. this.commit('reader/setSettings', newSettings);
  770. await sleep(50);
  771. this.settingsChanging = false;
  772. }
  773. }
  774. async doScrollingSpeedUp() {
  775. if (!this.settingsChanging) {
  776. this.settingsChanging = true;
  777. const newDelay = (this.settings.scrollingDelay - 50 > 1 ? this.settings.scrollingDelay - 50 : 1);
  778. const newSettings = Object.assign({}, this.settings, {scrollingDelay: newDelay});
  779. this.commit('reader/setSettings', newSettings);
  780. await sleep(50);
  781. this.settingsChanging = false;
  782. }
  783. }
  784. async doScrollingSpeedDown() {
  785. if (!this.settingsChanging) {
  786. this.settingsChanging = true;
  787. const newDelay = (this.settings.scrollingDelay + 50 < 10000 ? this.settings.scrollingDelay + 50 : 10000);
  788. const newSettings = Object.assign({}, this.settings, {scrollingDelay: newDelay});
  789. this.commit('reader/setSettings', newSettings);
  790. await sleep(50);
  791. this.settingsChanging = false;
  792. }
  793. }
  794. keyHook(event) {
  795. let result = false;
  796. if (event.type == 'keydown' && !event.ctrlKey && !event.altKey) {
  797. result = true;
  798. switch (event.code) {
  799. case 'ArrowDown':
  800. if (event.shiftKey)
  801. this.doScrollingSpeedUp();
  802. else
  803. this.doDown();
  804. break;
  805. case 'ArrowUp':
  806. if (event.shiftKey)
  807. this.doScrollingSpeedDown();
  808. else
  809. this.doUp();
  810. break;
  811. case 'PageDown':
  812. case 'ArrowRight':
  813. this.doPageDown();
  814. break;
  815. case 'Space':
  816. if (event.shiftKey)
  817. this.doPageUp();
  818. else
  819. this.doPageDown();
  820. break;
  821. case 'PageUp':
  822. case 'ArrowLeft':
  823. case 'Backspace':
  824. this.doPageUp();
  825. break;
  826. case 'Home':
  827. this.doHome();
  828. break;
  829. case 'End':
  830. this.doEnd();
  831. break;
  832. case 'KeyA':
  833. if (event.shiftKey)
  834. this.doFontSizeDec();
  835. else
  836. this.doFontSizeInc();
  837. break;
  838. case 'Enter':
  839. case 'Backquote'://`
  840. case 'KeyF':
  841. this.doFullScreenToggle();
  842. break;
  843. case 'Tab':
  844. case 'KeyQ':
  845. this.doToolBarToggle();
  846. event.preventDefault();
  847. event.stopPropagation();
  848. break;
  849. default:
  850. result = false;
  851. break;
  852. }
  853. }
  854. return result;
  855. }
  856. async startClickRepeat(pointX, pointY) {
  857. this.repX = pointX;
  858. this.repY = pointY;
  859. if (!this.repInit && this.repDoing) {
  860. this.repInit = true;
  861. let delay = 400;
  862. while (this.repDoing) {
  863. this.handleClick(pointX, pointY);
  864. await sleep(delay);
  865. if (delay > 15)
  866. delay *= 0.8;
  867. }
  868. this.repInit = false;
  869. }
  870. }
  871. endClickRepeat() {
  872. this.repDoing = false;
  873. }
  874. onTouchStart(event) {
  875. if (!this.$isMobileDevice)
  876. return;
  877. this.endClickRepeat();
  878. if (event.touches.length == 1) {
  879. const touch = event.touches[0];
  880. const rect = event.target.getBoundingClientRect();
  881. const x = touch.pageX - rect.left;
  882. const y = touch.pageY - rect.top;
  883. const hc = this.handleClick(x, y, new Set(['Menu']));
  884. if (hc) {
  885. if (hc != 'Menu') {
  886. this.repDoing = true;
  887. this.debouncedStartClickRepeat(x, y);
  888. } else {
  889. this.startTouch = {x, y};
  890. }
  891. }
  892. }
  893. }
  894. onTouchMove(event) {
  895. if (this.startTouch) {
  896. event.preventDefault();
  897. }
  898. }
  899. onTouchEnd(event) {
  900. if (!this.$isMobileDevice)
  901. return;
  902. this.endClickRepeat();
  903. if (event.changedTouches.length == 1) {
  904. const touch = event.changedTouches[0];
  905. const rect = event.target.getBoundingClientRect();
  906. const x = touch.pageX - rect.left;
  907. const y = touch.pageY - rect.top;
  908. if (this.startTouch) {
  909. const dy = this.startTouch.y - y;
  910. const dx = this.startTouch.x - x;
  911. const moveDelta = 30;
  912. const touchDelta = 15;
  913. if (dy > 0 && Math.abs(dy) >= moveDelta && Math.abs(dy) > Math.abs(dx)) {
  914. //движение вверх
  915. this.doFullScreenToggle();
  916. } else if (dy < 0 && Math.abs(dy) >= moveDelta && Math.abs(dy) > Math.abs(dx)) {
  917. //движение вниз
  918. this.doScrollingToggle();
  919. } else if (dx > 0 && Math.abs(dx) >= moveDelta && Math.abs(dy) < Math.abs(dx)) {
  920. //движение влево
  921. this.doScrollingSpeedDown();
  922. } else if (dx < 0 && Math.abs(dx) >= moveDelta && Math.abs(dy) < Math.abs(dx)) {
  923. //движение вправо
  924. this.doScrollingSpeedUp();
  925. } else if (Math.abs(dy) < touchDelta && Math.abs(dx) < touchDelta) {
  926. this.doToolBarToggle();
  927. }
  928. this.startTouch = null;
  929. }
  930. }
  931. }
  932. onTouchCancel() {
  933. if (!this.$isMobileDevice)
  934. return;
  935. this.endClickRepeat();
  936. }
  937. onMouseDown(event) {
  938. if (this.$isMobileDevice)
  939. return;
  940. this.endClickRepeat();
  941. if (event.button == 0) {
  942. const hc = this.handleClick(event.offsetX, event.offsetY);
  943. if (hc && hc != 'Menu') {
  944. this.repDoing = true;
  945. this.debouncedStartClickRepeat(event.offsetX, event.offsetY);
  946. }
  947. } else if (event.button == 1) {
  948. this.doScrollingToggle();
  949. } else if (event.button == 2) {
  950. this.doToolBarToggle();
  951. }
  952. }
  953. onMouseUp() {
  954. if (this.$isMobileDevice)
  955. return;
  956. this.endClickRepeat();
  957. }
  958. onMouseWheel(event) {
  959. if (this.$isMobileDevice)
  960. return;
  961. if (event.deltaY > 0) {
  962. this.doDown();
  963. } else if (event.deltaY < 0) {
  964. this.doUp();
  965. }
  966. }
  967. onStatusBarClick() {
  968. const url = this.meta.url;
  969. if (url && url.indexOf('file://') != 0) {
  970. window.open(url, '_blank');
  971. } else {
  972. this.$alert('Оригинал недоступен, т.к. файл книги был загружен с локального диска', '', {type: 'warning'});
  973. }
  974. }
  975. getClickAction(pointX, pointY) {
  976. const w = pointX/this.realWidth*100;
  977. const h = pointY/this.realHeight*100;
  978. let action = '';
  979. loops: {
  980. for (const x in clickMap) {
  981. for (const y in clickMap[x]) {
  982. if (w < x && h < y) {
  983. action = clickMap[x][y];
  984. break loops;
  985. }
  986. }
  987. }
  988. }
  989. return action;
  990. }
  991. handleClick(pointX, pointY, exclude) {
  992. const action = this.getClickAction(pointX, pointY);
  993. if (!exclude || !exclude.has(action)) {
  994. switch (action) {
  995. case 'Down' ://Down
  996. this.doDown();
  997. break;
  998. case 'Up' ://Up
  999. this.doUp();
  1000. break;
  1001. case 'PgDown' ://PgDown
  1002. this.doPageDown();
  1003. break;
  1004. case 'PgUp' ://PgUp
  1005. this.doPageUp();
  1006. break;
  1007. case 'Menu' :
  1008. this.doToolBarToggle();
  1009. break;
  1010. default :
  1011. // Nothing
  1012. }
  1013. }
  1014. return action;
  1015. }
  1016. }
  1017. //-----------------------------------------------------------------------------
  1018. </script>
  1019. <style scoped>
  1020. .main {
  1021. flex: 1;
  1022. margin: 0;
  1023. padding: 0;
  1024. overflow: hidden;
  1025. position: relative;
  1026. min-width: 200px;
  1027. }
  1028. .layout {
  1029. margin: 0;
  1030. padding: 0;
  1031. position: absolute;
  1032. z-index: 10;
  1033. }
  1034. .over-hidden {
  1035. overflow: hidden;
  1036. }
  1037. .on-top {
  1038. z-index: 100;
  1039. }
  1040. .back {
  1041. z-index: 5;
  1042. }
  1043. .events {
  1044. z-index: 20;
  1045. background-color: rgba(0,0,0,0);
  1046. }
  1047. </style>
  1048. <style>
  1049. .paper1 {
  1050. background: url("images/paper1.jpg") center;
  1051. background-size: cover;
  1052. }
  1053. .paper2 {
  1054. background: url("images/paper2.jpg") center;
  1055. background-size: cover;
  1056. }
  1057. .paper3 {
  1058. background: url("images/paper3.jpg") center;
  1059. background-size: cover;
  1060. }
  1061. .paper4 {
  1062. background: url("images/paper4.jpg") center;
  1063. background-size: cover;
  1064. }
  1065. .paper5 {
  1066. background: url("images/paper5.jpg") center;
  1067. background-size: cover;
  1068. }
  1069. .paper6 {
  1070. background: url("images/paper6.jpg") center;
  1071. background-size: cover;
  1072. }
  1073. .paper7 {
  1074. background: url("images/paper7.jpg") center;
  1075. background-size: cover;
  1076. }
  1077. .paper8 {
  1078. background: url("images/paper8.jpg") center;
  1079. background-size: cover;
  1080. }
  1081. .paper9 {
  1082. background: url("images/paper9.jpg");
  1083. }
  1084. @keyframes page1-animation-thaw {
  1085. 0% { opacity: 0; }
  1086. 100% { opacity: 1; }
  1087. }
  1088. @keyframes page2-animation-thaw {
  1089. 0% { opacity: 1; }
  1090. 100% { opacity: 0; }
  1091. }
  1092. </style>