TextPage.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <div ref="main" class="main">
  3. <p v-for="item in items" :key="item.id">
  4. {{ item.text }}
  5. </p>
  6. </div>
  7. </template>
  8. <script>
  9. //-----------------------------------------------------------------------------
  10. import Vue from 'vue';
  11. import Component from 'vue-class-component';
  12. import _ from 'lodash';
  13. import bookManager from '../share/bookManager';
  14. export default @Component({
  15. watch: {
  16. bookPos: function(newValue) {
  17. this.$emit('book-pos-changed', {bookPos: newValue});
  18. this.drawPage();
  19. },
  20. },
  21. })
  22. class TextPage extends Vue {
  23. lastBook = null;
  24. bookPos = 0;
  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. }
  32. showBook() {
  33. this.$refs.main.focus();
  34. this.book = null;
  35. this.meta = null;
  36. this.fb2 = null;
  37. this.parsed = null;
  38. this.drawPage();//пока не загрузили, очистим канвас
  39. if (this.lastBook) {
  40. (async() => {
  41. const isParsed = await bookManager.hasBookParsed(this.lastBook);
  42. if (!isParsed) {
  43. return;
  44. }
  45. this.book = await bookManager.getBook(this.lastBook);
  46. this.meta = bookManager.metaOnly(this.book);
  47. this.fb2 = this.meta.fb2;
  48. this.$root.$emit('set-app-title', _.compact([
  49. this.fb2.lastName,
  50. this.fb2.middleName,
  51. this.fb2.firstName,
  52. '-',
  53. this.fb2.bookTitle
  54. ]).join(' '));
  55. const parsed = this.book.parsed;
  56. parsed.p = 30;//px, отступ параграфа
  57. parsed.w = 300;//px, ширина страницы
  58. parsed.measureText = (text, style) => {
  59. if (style == 'bold')
  60. return text.length*12;
  61. else
  62. return text.length*10;
  63. };
  64. this.parsed = parsed;
  65. this.drawPage();
  66. })();
  67. }
  68. }
  69. drawPage() {
  70. if (!this.lastBook)
  71. return;
  72. //пустой канвас
  73. this.items = [];
  74. if (!this.book)
  75. return;
  76. const lines = this.parsed.getLines(this.bookPos, 30);
  77. let newItems = [];
  78. for (const line of lines) {
  79. /* line:
  80. {
  81. begin: Number,
  82. end: Number,
  83. parts: array of {
  84. style: 'bold'|'italic',
  85. text: String,
  86. }
  87. }*/
  88. const item = {text: '', id: line.begin};
  89. for (const part of line.parts) {
  90. item.text += part.text;
  91. }
  92. newItems.push(item);
  93. }
  94. this.items = newItems;
  95. }
  96. keyHook(event) {
  97. }
  98. }
  99. //-----------------------------------------------------------------------------
  100. </script>
  101. <style scoped>
  102. .main {
  103. flex: 1;
  104. display: flex;
  105. flex-direction: column;
  106. }
  107. p {
  108. margin: 0;
  109. padding: 0;
  110. }
  111. </style>