TextPage.vue 3.4 KB

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