You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
4.2 KiB

  1. <template lang="pug">
  2. div(v-on:click="setSelectedRange")
  3. span(
  4. v-for="r in chunks"
  5. v-bind:class="{ tag: id2label[r.label].text_color }"
  6. v-bind:style="{ \
  7. color: id2label[r.label].text_color, \
  8. backgroundColor: id2label[r.label].background_color \
  9. }"
  10. ) {{ text.slice(r.start_offset, r.end_offset) }}
  11. button.delete.is-small(v-if="id2label[r.label].text_color", v-on:click="removeLabel(r)")
  12. </template>
  13. <script>
  14. export default {
  15. props: {
  16. labels: {
  17. type: Array, // [{id: Integer, color: String, text: String}]
  18. default: () => [],
  19. },
  20. text: {
  21. type: String,
  22. default: '',
  23. },
  24. entityPositions: {
  25. type: Array, // [{'startOffset': 10, 'endOffset': 15, 'label_id': 1}]
  26. default: () => [],
  27. },
  28. },
  29. data: () => ({
  30. startOffset: 0,
  31. endOffset: 0,
  32. }),
  33. computed: {
  34. sortedEntityPositions() {
  35. /* eslint-disable vue/no-side-effects-in-computed-properties */
  36. this.entityPositions = this.entityPositions.sort((a, b) => a.start_offset - b.start_offset);
  37. return this.entityPositions;
  38. /* eslint-enable vue/no-side-effects-in-computed-properties */
  39. },
  40. chunks() {
  41. const res = [];
  42. let left = 0;
  43. for (let i = 0; i < this.sortedEntityPositions.length; i++) {
  44. const e = this.sortedEntityPositions[i];
  45. const l = this.makeLabel(left, e.start_offset);
  46. res.push(l);
  47. res.push(e);
  48. left = e.end_offset;
  49. }
  50. const l = this.makeLabel(left, this.text.length);
  51. res.push(l);
  52. return res;
  53. },
  54. id2label() {
  55. const id2label = {};
  56. // default value;
  57. id2label[-1] = {
  58. text_color: '',
  59. background_color: '',
  60. };
  61. for (let i = 0; i < this.labels.length; i++) {
  62. const label = this.labels[i];
  63. id2label[label.id] = label;
  64. }
  65. return id2label;
  66. },
  67. },
  68. watch: {
  69. entityPositions() {
  70. this.resetRange();
  71. },
  72. },
  73. methods: {
  74. setSelectedRange() {
  75. let start;
  76. let end;
  77. if (window.getSelection) {
  78. const range = window.getSelection().getRangeAt(0);
  79. const preSelectionRange = range.cloneRange();
  80. preSelectionRange.selectNodeContents(this.$el);
  81. preSelectionRange.setEnd(range.startContainer, range.startOffset);
  82. start = preSelectionRange.toString().length;
  83. end = start + range.toString().length;
  84. } else if (document.selection && document.selection.type !== 'Control') {
  85. const selectedTextRange = document.selection.createRange();
  86. const preSelectionTextRange = document.body.createTextRange();
  87. preSelectionTextRange.moveToElementText(this.$el);
  88. preSelectionTextRange.setEndPoint('EndToStart', selectedTextRange);
  89. start = preSelectionTextRange.text.length;
  90. end = start + selectedTextRange.text.length;
  91. }
  92. this.startOffset = start;
  93. this.endOffset = end;
  94. console.log(start, end); // eslint-disable-line no-console
  95. },
  96. validRange() {
  97. if (this.startOffset === this.endOffset) {
  98. return false;
  99. }
  100. if (this.startOffset > this.text.length || this.endOffset > this.text.length) {
  101. return false;
  102. }
  103. if (this.startOffset < 0 || this.endOffset < 0) {
  104. return false;
  105. }
  106. for (let i = 0; i < this.entityPositions.length; i++) {
  107. const e = this.entityPositions[i];
  108. if ((e.start_offset <= this.startOffset) && (this.startOffset <= e.end_offset)) {
  109. return false;
  110. }
  111. if ((e.start_offset <= this.endOffset) && (this.endOffset <= e.end_offset)) {
  112. return false;
  113. }
  114. }
  115. return true;
  116. },
  117. resetRange() {
  118. this.startOffset = 0;
  119. this.endOffset = 0;
  120. },
  121. addLabel(labelId) {
  122. if (this.validRange()) {
  123. const label = {
  124. start_offset: this.startOffset,
  125. end_offset: this.endOffset,
  126. label: labelId,
  127. };
  128. this.$emit('add-label', label);
  129. }
  130. },
  131. removeLabel(index) {
  132. this.$emit('remove-label', index);
  133. },
  134. makeLabel(startOffset, endOffset) {
  135. const label = {
  136. id: 0,
  137. label: -1,
  138. start_offset: startOffset,
  139. end_offset: endOffset,
  140. };
  141. return label;
  142. },
  143. },
  144. };
  145. </script>