PasteTextPage.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template>
  2. <div ref="main" class="main" @click="close">
  3. <div class="mainWindow" @click.stop>
  4. <Window @close="close">
  5. <template slot="header">
  6. Вставьте текст и нажмите
  7. <el-button size="mini" style="font-size: 120%; color: blue" @click="loadBuffer">Загрузить</el-button>
  8. или F2
  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. </div>
  17. </div>
  18. </template>
  19. <script>
  20. //-----------------------------------------------------------------------------
  21. import Vue from 'vue';
  22. import Component from 'vue-class-component';
  23. import Window from '../../../share/Window.vue';
  24. import _ from 'lodash';
  25. export default @Component({
  26. components: {
  27. Window,
  28. },
  29. })
  30. class PasteTextPage extends Vue {
  31. bookTitle = '';
  32. created() {
  33. }
  34. mounted() {
  35. this.$refs.textArea.focus();
  36. }
  37. getNonEmptyLine(text, count) {
  38. let result = '';
  39. const lines = text.split("\n");
  40. let i = 0;
  41. while (i < lines.length) {
  42. if (lines[i].trim() != '') {
  43. count--;
  44. if (count <= 0) {
  45. result = lines[i];
  46. break;
  47. }
  48. }
  49. i++;
  50. }
  51. return result;
  52. }
  53. calcTitle(event) {
  54. if (this.bookTitle == '') {
  55. let text = event.clipboardData.getData('text');
  56. this.bookTitle = _.compact([
  57. this.getNonEmptyLine(text, 1),
  58. this.getNonEmptyLine(text, 2)
  59. ]).join(' - ');
  60. }
  61. }
  62. loadBuffer() {
  63. this.$emit('load-buffer', {buffer: `<title>${this.bookTitle}</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. .main {
  87. position: absolute;
  88. width: 100%;
  89. height: 100%;
  90. z-index: 40;
  91. display: flex;
  92. flex-direction: column;
  93. justify-content: center;
  94. align-items: center;
  95. }
  96. .mainWindow {
  97. width: 100%;
  98. height: 100%;
  99. display: flex;
  100. }
  101. .text {
  102. flex: 1;
  103. overflow-wrap: anywhere;
  104. overflow-y: auto;
  105. padding: 0 10px 0 10px;
  106. position: relative;
  107. font-size: 120%;
  108. }
  109. .text:focus {
  110. outline: none;
  111. }
  112. hr {
  113. margin: 0;
  114. padding: 0;
  115. }
  116. </style>