CopyTextPage.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. this.text = 'Загрузка';
  36. await this.$nextTick();
  37. const paraIndex = parsed.findParaIndex(bookPos);
  38. this.initStep = true;
  39. this.stopInit = false;
  40. let nextPerc = 0;
  41. let text = '';
  42. let cut = '';
  43. let from = 0;
  44. let to = parsed.para.length;
  45. if (!copyFullText) {
  46. from = paraIndex - 100;
  47. from = (from < 0 ? 0 : from);
  48. to = paraIndex + 100;
  49. to = (to > parsed.para.length ? parsed.para.length : to);
  50. cut = '<p>..... Текст вырезан. Если хотите скопировать больше, поставьте в настройках галочку "Загружать весь текст"';
  51. }
  52. if (from > 0)
  53. text += cut;
  54. for (let i = from; i < to; i++) {
  55. const p = parsed.para[i];
  56. const parts = parsed.splitToStyle(p.text);
  57. if (this.stopInit)
  58. return;
  59. text += `<p id="p${i}" class="copyPara">`;
  60. for (const part of parts)
  61. text += part.text;
  62. const perc = Math.round(i/parsed.para.length*100);
  63. if (perc > nextPerc) {
  64. this.initPercentage = perc;
  65. await sleep(1);
  66. nextPerc = perc + 10;
  67. }
  68. }
  69. if (to < parsed.para.length)
  70. text += cut;
  71. this.text = text;
  72. this.initStep = false;
  73. await this.$nextTick();
  74. this.$refs.text.focus();
  75. const p = document.getElementById('p' + paraIndex);
  76. if (p) {
  77. this.$refs.text.scrollTop = p.offsetTop;
  78. }
  79. }
  80. close() {
  81. this.stopInit = true;
  82. this.$emit('copy-text-toggle');
  83. }
  84. keyHook(event) {
  85. if (event.type == 'keydown' && (event.code == 'Escape')) {
  86. this.close();
  87. }
  88. return true;
  89. }
  90. }
  91. //-----------------------------------------------------------------------------
  92. </script>
  93. <style scoped>
  94. .main {
  95. position: absolute;
  96. width: 100%;
  97. height: 100%;
  98. z-index: 40;
  99. display: flex;
  100. flex-direction: column;
  101. justify-content: center;
  102. align-items: center;
  103. }
  104. .mainWindow {
  105. width: 100%;
  106. height: 100%;
  107. display: flex;
  108. }
  109. .text {
  110. flex: 1;
  111. overflow-wrap: anywhere;
  112. overflow-y: auto;
  113. padding: 0 10px 0 10px;
  114. position: relative;
  115. font-size: 120%;
  116. }
  117. .text:focus {
  118. outline: none;
  119. }
  120. </style>
  121. <style>
  122. .copyPara {
  123. margin: 0;
  124. padding: 0;
  125. text-indent: 30px;
  126. }
  127. </style>