TextPage.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <template>
  2. <div ref="main" class="main">
  3. <canvas ref="canvas" class="canvas"></canvas>
  4. </div>
  5. </template>
  6. <script>
  7. //-----------------------------------------------------------------------------
  8. import Vue from 'vue';
  9. import Component from 'vue-class-component';
  10. import _ from 'lodash';
  11. import bookManager from '../share/bookManager';
  12. export default @Component({
  13. watch: {
  14. bookPos: function(newValue) {
  15. this.debouncedEmitPosChange(newValue);
  16. this.drawPage();
  17. },
  18. },
  19. })
  20. class TextPage extends Vue {
  21. lastBook = null;
  22. bookPos = 0;
  23. //убрать
  24. meta = null;
  25. items = null;
  26. created() {
  27. this.commit = this.$store.commit;
  28. this.dispatch = this.$store.dispatch;
  29. this.config = this.$store.state.config;
  30. this.reader = this.$store.state.reader;
  31. this.debouncedEmitPosChange = _.debounce((newValue) => {
  32. this.$emit('book-pos-changed', {bookPos: newValue});
  33. }, 100);
  34. }
  35. mounted() {
  36. this.canvas = this.$refs.canvas;
  37. this.context = this.canvas.getContext('2d');
  38. this.context.textAlign = 'left';
  39. }
  40. updateCanvasSize() {
  41. this.canvas.width = this.$refs.main.clientWidth;
  42. this.canvas.height = this.$refs.main.clientHeight;
  43. this.lineHeight = this.fontSize + this.lineInterval;
  44. this.pageLineCount = Math.floor(this.canvas.height/this.lineHeight);
  45. }
  46. showBook() {
  47. this.$refs.main.focus();
  48. this.book = null;
  49. this.meta = null;
  50. this.fb2 = null;
  51. this.parsed = null;
  52. this.linesUp = null;
  53. this.linesDown = null;
  54. this.pageLineCount = 0;
  55. //canvas props
  56. this.textColor = 'black';
  57. this.backgroundColor = '#478355';
  58. this.fontStyle = ''; //'bold','italic'
  59. this.fontSize = 20; //px
  60. this.fontName = 'arial';
  61. this.lineInterval = 5; //px
  62. this.updateCanvasSize();
  63. this.drawPage();//пока не загрузили, очистим канвас
  64. if (this.lastBook) {
  65. (async() => {
  66. const isParsed = await bookManager.hasBookParsed(this.lastBook);
  67. if (!isParsed) {
  68. return;
  69. }
  70. this.book = await bookManager.getBook(this.lastBook);
  71. this.meta = bookManager.metaOnly(this.book);
  72. this.fb2 = this.meta.fb2;
  73. this.$root.$emit('set-app-title', _.compact([
  74. this.fb2.lastName,
  75. this.fb2.middleName,
  76. this.fb2.firstName,
  77. '-',
  78. this.fb2.bookTitle
  79. ]).join(' '));
  80. const parsed = this.book.parsed;
  81. parsed.p = 30;//px, отступ параграфа
  82. parsed.w = 300;//px, ширина страницы
  83. parsed.measureText = (text, style) => {// eslint-disable-line no-unused-vars
  84. return this.context.measureText(text).width;
  85. };
  86. this.parsed = parsed;
  87. this.drawPage();
  88. })();
  89. }
  90. }
  91. drawPage() {
  92. if (!this.lastBook)
  93. return;
  94. //пустой канвас
  95. const canvas = this.canvas;
  96. const context = this.context;
  97. context.font = `${this.fontStyle} ${this.fontSize}px ${this.fontName}`;
  98. context.fillStyle = this.backgroundColor;
  99. context.fillRect(0, 0, canvas.width, canvas.height);
  100. if (!this.book)
  101. return;
  102. context.fillStyle = this.textColor;
  103. const lines = this.parsed.getLines(this.bookPos, this.pageLineCount + 1);
  104. let len = lines.length;
  105. len = (len > this.pageLineCount ? len = this.pageLineCount : len);
  106. let y = 0;
  107. for (let i = 0; i < len; i++) {
  108. const line = lines[i];
  109. /* line:
  110. {
  111. begin: Number,
  112. end: Number,
  113. parts: array of {
  114. style: 'bold'|'italic',
  115. text: String,
  116. }
  117. }*/
  118. let text = '';
  119. for (const part of line.parts) {
  120. text += part.text;
  121. }
  122. y += this.lineHeight;
  123. context.fillText(text, 0, y);
  124. }
  125. this.linesUp = this.parsed.getLines(this.bookPos, -(this.pageLineCount + 1));
  126. this.linesDown = lines;
  127. }
  128. doPageDown() {
  129. if (this.linesDown) {
  130. let i = this.pageLineCount;
  131. i--;
  132. if (i >= 0 && this.linesDown.length > i) {
  133. this.bookPos = this.linesDown[i].begin;
  134. }
  135. }
  136. }
  137. doPageUp() {
  138. if (this.linesUp) {
  139. let i = this.pageLineCount;
  140. i--;
  141. i = (i > this.linesUp.length - 1 ? this.linesUp.length - 1 : i);
  142. if (i >= 0 && this.linesUp.length > i) {
  143. this.bookPos = this.linesUp[i].begin;
  144. }
  145. }
  146. }
  147. doHome() {
  148. this.bookPos = 0;
  149. }
  150. doEnd() {
  151. if (this.parsed.para.length) {
  152. const lastPara = this.parsed.para[this.parsed.para.length - 1];
  153. this.bookPos = lastPara.offset + lastPara.length - 1;
  154. }
  155. }
  156. keyHook(event) {
  157. if (event.type == 'keydown') {
  158. switch (event.key) {
  159. case 'PageDown':
  160. this.doPageDown();
  161. break;
  162. case 'PageUp':
  163. this.doPageUp();
  164. break;
  165. case 'Home':
  166. this.doHome();
  167. break;
  168. case 'End':
  169. this.doEnd();
  170. break;
  171. }
  172. }
  173. }
  174. }
  175. //-----------------------------------------------------------------------------
  176. </script>
  177. <style scoped>
  178. .main {
  179. flex: 1;
  180. display: flex;
  181. flex-direction: column;
  182. }
  183. .canvas {
  184. }
  185. pre {
  186. margin: 0;
  187. padding: 0;
  188. }
  189. p {
  190. margin: 0;
  191. padding: 0;
  192. }
  193. </style>