CopyTextPage.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template>
  2. <div ref="main" class="main" @click="close">
  3. <div class="mainWindow" @click.stop>
  4. <Window @close="close">
  5. <template slot="header">
  6. Скопировать текст
  7. </template>
  8. <div ref="text" class="text" tabindex="-1">
  9. <div v-html="text"></div>
  10. </div>
  11. </Window>
  12. </div>
  13. </div>
  14. </template>
  15. <script>
  16. //-----------------------------------------------------------------------------
  17. import Vue from 'vue';
  18. import Component from 'vue-class-component';
  19. import Window from '../../share/Window.vue';
  20. import {sleep} from '../../../share/utils';
  21. export default @Component({
  22. components: {
  23. Window,
  24. },
  25. })
  26. class CopyTextPage extends Vue {
  27. text = null;
  28. initStep = null;
  29. initPercentage = 0;
  30. created() {
  31. this.commit = this.$store.commit;
  32. this.reader = this.$store.state.reader;
  33. }
  34. async init(bookPos, parsed, copyFullText) {
  35. if (parsed && this.parsed != parsed) {
  36. this.text = 'Загрузка';
  37. await this.$nextTick();
  38. const paraIndex = parsed.findParaIndex(bookPos);
  39. this.initStep = true;
  40. this.stopInit = false;
  41. let nextPerc = 0;
  42. let text = '';
  43. let cut = '';
  44. let from = 0;
  45. let to = parsed.para.length;
  46. if (!copyFullText) {
  47. from = paraIndex - 100;
  48. from = (from < 0 ? 0 : from);
  49. to = paraIndex + 100;
  50. to = (to > parsed.para.length ? parsed.para.length : to);
  51. cut = '<p>..... Текст вырезан. Если хотите скопировать больше, поставьте в настройках галочку "Загружать весь текст"';
  52. }
  53. if (from > 0)
  54. text += cut;
  55. for (let i = from; i < to; i++) {
  56. const p = parsed.para[i];
  57. const parts = parsed.splitToStyle(p.text);
  58. if (this.stopInit)
  59. return;
  60. text += `<p id="p${i}" class="copyPara">`;
  61. for (const part of parts)
  62. text += part.text;
  63. const perc = Math.round(i/parsed.para.length*100);
  64. if (perc > nextPerc) {
  65. this.initPercentage = perc;
  66. await sleep(1);
  67. nextPerc = perc + 10;
  68. }
  69. }
  70. if (to < parsed.para.length)
  71. text += cut;
  72. this.text = text;
  73. this.initStep = false;
  74. this.parsed = parsed;
  75. await this.$nextTick();
  76. this.$refs.text.focus();
  77. const p = document.getElementById('p' + paraIndex);
  78. if (p) {
  79. this.$refs.text.scrollTop = p.offsetTop;
  80. }
  81. }
  82. }
  83. close() {
  84. this.stopInit = true;
  85. this.$emit('copy-text-toggle');
  86. }
  87. keyHook(event) {
  88. if (event.type == 'keydown' && (event.code == 'Escape')) {
  89. this.close();
  90. }
  91. return true;
  92. }
  93. }
  94. //-----------------------------------------------------------------------------
  95. </script>
  96. <style scoped>
  97. .main {
  98. position: absolute;
  99. width: 100%;
  100. height: 100%;
  101. z-index: 40;
  102. display: flex;
  103. flex-direction: column;
  104. justify-content: center;
  105. align-items: center;
  106. }
  107. .mainWindow {
  108. width: 100%;
  109. height: 100%;
  110. display: flex;
  111. }
  112. .text {
  113. flex: 1;
  114. overflow-wrap: anywhere;
  115. overflow-y: auto;
  116. margin: 0 10px 0 10px;
  117. position: relative;
  118. font-size: 120%;
  119. }
  120. .text:focus {
  121. outline: none;
  122. }
  123. </style>
  124. <style>
  125. .copyPara {
  126. margin: 0;
  127. padding: 0;
  128. text-indent: 30px;
  129. }
  130. </style>