PasteTextPage.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 class="q-px-sm" dense borderless v-model="bookTitle" placeholder="Введите название текста"/>
  11. <hr/>
  12. <textarea ref="textArea" class="text" @paste="calcTitle"></textarea>
  13. </Window>
  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 _ from 'lodash';
  21. import * as utils from '../../../../share/utils';
  22. export default @Component({
  23. components: {
  24. Window,
  25. },
  26. })
  27. class PasteTextPage extends Vue {
  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><cut-title>${utils.escapeXml(this.bookTitle)}</cut-title>${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.code) {
  70. case 'F2':
  71. this.loadBuffer();
  72. break;
  73. case 'Escape':
  74. this.close();
  75. break;
  76. }
  77. }
  78. return true;
  79. }
  80. }
  81. //-----------------------------------------------------------------------------
  82. </script>
  83. <style scoped>
  84. .text {
  85. flex: 1;
  86. overflow-wrap: anywhere;
  87. overflow-y: auto;
  88. padding: 0 10px 0 10px;
  89. position: relative;
  90. font-size: 120%;
  91. min-width: 400px;
  92. }
  93. .text:focus {
  94. outline: none;
  95. }
  96. hr {
  97. margin: 0;
  98. padding: 0;
  99. }
  100. .clickable {
  101. color: blue;
  102. cursor: pointer;
  103. }
  104. </style>