PasteTextPage.vue 2.9 KB

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