TextPage.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. window.addEventListener('resize', () => {
  35. this.onResize();
  36. });
  37. }
  38. mounted() {
  39. this.canvas = this.$refs.canvas;
  40. this.context = this.canvas.getContext('2d');
  41. this.context.textAlign = 'left';
  42. this.context.textBaseline = 'bottom';
  43. }
  44. calcDrawProps() {
  45. this.canvas.width = this.$refs.main.clientWidth;
  46. this.canvas.height = this.$refs.main.clientHeight;
  47. this.lineHeight = this.fontSize + this.lineInterval;
  48. this.pageLineCount = Math.floor(this.canvas.height/this.lineHeight);
  49. this.w = this.canvas.width - 2*this.indent;
  50. if (this.parsed) {
  51. this.parsed.p = this.p;
  52. this.parsed.w = this.w;// px, ширина текста
  53. this.parsed.font = this.font;
  54. this.parsed.wordWrap = this.wordWrap;
  55. this.measureText = (text, style) => {// eslint-disable-line no-unused-vars
  56. return this.context.measureText(text).width;
  57. };
  58. this.parsed.measureText = this.measureText;
  59. }
  60. }
  61. showBook() {
  62. this.$refs.main.focus();
  63. this.book = null;
  64. this.meta = null;
  65. this.fb2 = null;
  66. this.parsed = null;
  67. this.linesUp = null;
  68. this.linesDown = null;
  69. //draw props
  70. this.textColor = 'black';
  71. this.backgroundColor = '#478355';
  72. this.fontStyle = '';// 'bold','italic'
  73. this.fontSize = 40;// px
  74. this.fontName = 'arial';
  75. this.lineInterval = 15;// px, межстрочный интервал
  76. this.textAlignJustify = true;// выравнивание по ширине
  77. this.p = 60;// px, отступ параграфа
  78. this.indent = 20;// px, отступ всего текста слева и справа
  79. this.wordWrap = true;
  80. this.calcDrawProps();
  81. this.drawPage();// пока не загрузили, очистим канвас
  82. if (this.lastBook) {
  83. (async() => {
  84. const isParsed = await bookManager.hasBookParsed(this.lastBook);
  85. if (!isParsed) {
  86. return;
  87. }
  88. this.book = await bookManager.getBook(this.lastBook);
  89. this.meta = bookManager.metaOnly(this.book);
  90. this.fb2 = this.meta.fb2;
  91. this.$root.$emit('set-app-title', _.compact([
  92. this.fb2.lastName,
  93. this.fb2.middleName,
  94. this.fb2.firstName,
  95. '-',
  96. this.fb2.bookTitle
  97. ]).join(' '));
  98. const parsed = this.book.parsed;
  99. this.parsed = parsed;
  100. this.calcDrawProps();
  101. this.drawPage();
  102. })();
  103. }
  104. }
  105. onResize() {
  106. this.calcDrawProps();
  107. this.drawPage();
  108. }
  109. get font() {
  110. return `${this.fontStyle} ${this.fontSize}px ${this.fontName}`;
  111. }
  112. drawPage() {
  113. if (!this.lastBook)
  114. return;
  115. //пустой канвас
  116. const canvas = this.canvas;
  117. const context = this.context;
  118. context.fillStyle = this.backgroundColor;
  119. context.fillRect(0, 0, canvas.width, canvas.height);
  120. if (!this.book)
  121. return;
  122. context.font = this.font;
  123. context.fillStyle = this.textColor;
  124. const spaceWidth = this.context.measureText(' ').width;
  125. const lines = this.parsed.getLines(this.bookPos, this.pageLineCount + 1);
  126. let len = lines.length;
  127. len = (len > this.pageLineCount ? len = this.pageLineCount : len);
  128. let y = 0;
  129. for (let i = 0; i < len; i++) {
  130. const line = lines[i];
  131. /* line:
  132. {
  133. begin: Number,
  134. end: Number,
  135. first: Boolean,
  136. last: Boolean,
  137. parts: array of {
  138. style: 'bold'|'italic',
  139. text: String,
  140. }
  141. }*/
  142. let text = '';
  143. for (const part of line.parts) {
  144. text += part.text;
  145. }
  146. let indent = this.indent + (line.first ? this.p : 0);
  147. y += this.lineHeight;
  148. let filled = false;
  149. if (this.textAlignJustify && !line.last) {
  150. const words = text.split(' ');
  151. if (words.length > 1) {
  152. const spaceCount = words.length - 1;
  153. const space = (this.w - line.width + spaceWidth*spaceCount)/spaceCount;
  154. let x = indent;
  155. for (const word of words) {
  156. context.fillText(word, x, y);
  157. x += this.measureText(word) + space;
  158. }
  159. filled = true;
  160. }
  161. }
  162. if (!filled)
  163. context.fillText(text, indent, y);
  164. }
  165. this.linesUp = this.parsed.getLines(this.bookPos, -(this.pageLineCount + 1));
  166. this.linesDown = lines;
  167. }
  168. doDown() {
  169. if (this.linesDown && this.linesDown.length > 1) {
  170. this.bookPos = this.linesDown[1].begin;
  171. }
  172. }
  173. doUp() {
  174. if (this.linesUp && this.linesUp.length > 1) {
  175. this.bookPos = this.linesUp[1].begin;
  176. }
  177. }
  178. doPageDown() {
  179. if (this.linesDown) {
  180. let i = this.pageLineCount;
  181. i--;
  182. if (i >= 0 && this.linesDown.length > i) {
  183. this.bookPos = this.linesDown[i].begin;
  184. }
  185. }
  186. }
  187. doPageUp() {
  188. if (this.linesUp) {
  189. let i = this.pageLineCount;
  190. i--;
  191. i = (i > this.linesUp.length - 1 ? this.linesUp.length - 1 : i);
  192. if (i >= 0 && this.linesUp.length > i) {
  193. this.bookPos = this.linesUp[i].begin;
  194. }
  195. }
  196. }
  197. doHome() {
  198. this.bookPos = 0;
  199. }
  200. doEnd() {
  201. if (this.parsed.para.length) {
  202. const lastPara = this.parsed.para[this.parsed.para.length - 1];
  203. this.bookPos = lastPara.offset + lastPara.length - 1;
  204. }
  205. }
  206. keyHook(event) {
  207. if (event.type == 'keydown') {
  208. switch (event.code) {
  209. case 'ArrowDown':
  210. this.doDown();
  211. break;
  212. case 'ArrowUp':
  213. this.doUp();
  214. break;
  215. case 'PageDown':
  216. case 'ArrowRight':
  217. case 'Enter':
  218. case 'Space':
  219. this.doPageDown();
  220. break;
  221. case 'PageUp':
  222. case 'ArrowLeft':
  223. case 'Backspace':
  224. this.doPageUp();
  225. break;
  226. case 'Home':
  227. this.doHome();
  228. break;
  229. case 'End':
  230. this.doEnd();
  231. break;
  232. }
  233. }
  234. }
  235. }
  236. //-----------------------------------------------------------------------------
  237. </script>
  238. <style scoped>
  239. .main {
  240. flex: 1;
  241. margin: 0;
  242. padding: 0;
  243. overflow: hidden;
  244. }
  245. .canvas {
  246. margin: 0;
  247. padding: 0;
  248. }
  249. </style>