PasteTextPage.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <Window @close="close">
  3. <template #header>
  4. <span style="position: relative; top: -3px">
  5. Вставьте текст и нажмите
  6. <span class="clickable text-primary" style="font-size: 150%; position: relative; top: 1px" @click="loadBuffer">загрузить</span>
  7. или F2
  8. </span>
  9. </template>
  10. <q-input v-model="bookTitle" class="q-px-sm" dense borderless placeholder="Введите название текста" />
  11. <hr />
  12. <textarea ref="textArea" class="text" @paste="calcTitle"></textarea>
  13. </Window>
  14. </template>
  15. <script>
  16. //-----------------------------------------------------------------------------
  17. import vueComponent from '../../../vueComponent.js';
  18. import Window from '../../../share/Window.vue';
  19. import _ from 'lodash';
  20. import * as utils from '../../../../share/utils';
  21. const componentOptions = {
  22. components: {
  23. Window,
  24. },
  25. };
  26. class PasteTextPage {
  27. _options = componentOptions;
  28. bookTitle = '';
  29. created() {
  30. }
  31. mounted() {
  32. this.$refs.textArea.focus();
  33. }
  34. getNonEmptyLine3words(text, count) {
  35. let result = '';
  36. const lines = text.split("\n");
  37. let i = 0;
  38. while (i < lines.length) {
  39. if (lines[i].trim() != '') {
  40. count--;
  41. if (count <= 0) {
  42. result = lines[i];
  43. break;
  44. }
  45. }
  46. i++;
  47. }
  48. result = result.trim().split(' ');
  49. return result.slice(0, 3).join(' ');
  50. }
  51. calcTitle(event) {
  52. if (this.bookTitle == '') {
  53. this.bookTitle = `Из буфера обмена ${utils.dateFormat(new Date())}`;
  54. if (event) {
  55. let text = event.clipboardData.getData('text');
  56. this.bookTitle += ': ' + _.compact([
  57. this.getNonEmptyLine3words(text, 1),
  58. this.getNonEmptyLine3words(text, 2)
  59. ]).join(' - ');
  60. }
  61. }
  62. }
  63. loadBuffer() {
  64. this.calcTitle();
  65. this.$emit('load-buffer', {buffer: `<buffer><fb2-title>${utils.escapeXml(this.bookTitle)}</fb2-title>${utils.escapeXml(this.$refs.textArea.value)}</buffer>`});
  66. this.close();
  67. }
  68. close() {
  69. this.$emit('paste-text-toggle');
  70. }
  71. keyHook(event) {
  72. if (event.type == 'keydown') {
  73. switch (event.key) {
  74. case 'F2':
  75. this.loadBuffer();
  76. break;
  77. case 'Escape':
  78. this.close();
  79. break;
  80. }
  81. }
  82. return true;
  83. }
  84. }
  85. export default vueComponent(PasteTextPage);
  86. //-----------------------------------------------------------------------------
  87. </script>
  88. <style scoped>
  89. .text {
  90. flex: 1;
  91. overflow-wrap: anywhere;
  92. overflow-y: auto;
  93. padding: 0 10px 0 10px;
  94. position: relative;
  95. font-size: 120%;
  96. min-width: 400px;
  97. }
  98. .text:focus {
  99. outline: none;
  100. }
  101. hr {
  102. margin: 0;
  103. padding: 0;
  104. }
  105. .clickable {
  106. color: blue;
  107. cursor: pointer;
  108. }
  109. </style>