TextPage.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. this.updateCanvasSize();
  81. const parsed = this.book.parsed;
  82. parsed.p = 30;// px, отступ параграфа
  83. parsed.w = this.canvas.width;// px, ширина страницы
  84. parsed.measureText = (text, style) => {// eslint-disable-line no-unused-vars
  85. return this.context.measureText(text).width;
  86. };
  87. this.parsed = parsed;
  88. this.drawPage();
  89. })();
  90. }
  91. }
  92. drawPage() {
  93. if (!this.lastBook)
  94. return;
  95. //пустой канвас
  96. const canvas = this.canvas;
  97. const context = this.context;
  98. context.font = `${this.fontStyle} ${this.fontSize}px ${this.fontName}`;
  99. context.fillStyle = this.backgroundColor;
  100. context.fillRect(0, 0, canvas.width, canvas.height);
  101. if (!this.book)
  102. return;
  103. context.fillStyle = this.textColor;
  104. const lines = this.parsed.getLines(this.bookPos, this.pageLineCount + 1);
  105. let len = lines.length;
  106. len = (len > this.pageLineCount ? len = this.pageLineCount : len);
  107. let y = 0;
  108. for (let i = 0; i < len; i++) {
  109. const line = lines[i];
  110. /* line:
  111. {
  112. begin: Number,
  113. end: Number,
  114. para: Boolean,
  115. parts: array of {
  116. style: 'bold'|'italic',
  117. text: String,
  118. }
  119. }*/
  120. let text = '';
  121. for (const part of line.parts) {
  122. text += part.text;
  123. }
  124. y += this.lineHeight;
  125. context.fillText(text, 0, y);
  126. }
  127. this.linesUp = this.parsed.getLines(this.bookPos, -(this.pageLineCount + 1));
  128. this.linesDown = lines;
  129. }
  130. doPageDown() {
  131. if (this.linesDown) {
  132. let i = this.pageLineCount;
  133. i--;
  134. if (i >= 0 && this.linesDown.length > i) {
  135. this.bookPos = this.linesDown[i].begin;
  136. }
  137. }
  138. }
  139. doPageUp() {
  140. if (this.linesUp) {
  141. let i = this.pageLineCount;
  142. i--;
  143. i = (i > this.linesUp.length - 1 ? this.linesUp.length - 1 : i);
  144. if (i >= 0 && this.linesUp.length > i) {
  145. this.bookPos = this.linesUp[i].begin;
  146. }
  147. }
  148. }
  149. doHome() {
  150. this.bookPos = 0;
  151. }
  152. doEnd() {
  153. if (this.parsed.para.length) {
  154. const lastPara = this.parsed.para[this.parsed.para.length - 1];
  155. this.bookPos = lastPara.offset + lastPara.length - 1;
  156. }
  157. }
  158. keyHook(event) {
  159. if (event.type == 'keydown') {
  160. switch (event.key) {
  161. case 'PageDown':
  162. this.doPageDown();
  163. break;
  164. case 'PageUp':
  165. this.doPageUp();
  166. break;
  167. case 'Home':
  168. this.doHome();
  169. break;
  170. case 'End':
  171. this.doEnd();
  172. break;
  173. }
  174. }
  175. }
  176. }
  177. //-----------------------------------------------------------------------------
  178. </script>
  179. <style scoped>
  180. .main {
  181. flex: 1;
  182. display: flex;
  183. flex-direction: column;
  184. }
  185. .canvas {
  186. }
  187. pre {
  188. margin: 0;
  189. padding: 0;
  190. }
  191. p {
  192. margin: 0;
  193. padding: 0;
  194. }
  195. </style>