PasteTextPage.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <Window @close="close">
  3. <template slot="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. let text = event.clipboardData.getData('text');
  54. this.bookTitle = `Из буфера обмена ${utils.formatDate(new Date(), 'noDate')}: ` + _.compact([
  55. this.getNonEmptyLine3words(text, 1),
  56. this.getNonEmptyLine3words(text, 2)
  57. ]).join(' - ');
  58. }
  59. }
  60. loadBuffer() {
  61. this.$emit('load-buffer', {buffer: `<buffer><fb2-title>${utils.escapeXml(this.bookTitle)}</fb2-title>${utils.escapeXml(this.$refs.textArea.value)}</buffer>`});
  62. this.close();
  63. }
  64. close() {
  65. this.$emit('paste-text-toggle');
  66. }
  67. keyHook(event) {
  68. if (event.type == 'keydown') {
  69. switch (event.key) {
  70. case 'F2':
  71. this.loadBuffer();
  72. break;
  73. case 'Escape':
  74. this.close();
  75. break;
  76. }
  77. }
  78. return true;
  79. }
  80. }
  81. export default vueComponent(PasteTextPage);
  82. //-----------------------------------------------------------------------------
  83. </script>
  84. <style scoped>
  85. .text {
  86. flex: 1;
  87. overflow-wrap: anywhere;
  88. overflow-y: auto;
  89. padding: 0 10px 0 10px;
  90. position: relative;
  91. font-size: 120%;
  92. min-width: 400px;
  93. }
  94. .text:focus {
  95. outline: none;
  96. }
  97. hr {
  98. margin: 0;
  99. padding: 0;
  100. }
  101. .clickable {
  102. color: blue;
  103. cursor: pointer;
  104. }
  105. </style>