CopyTextPage.vue 3.2 KB

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