@@ -6,26 +6,73 @@ Vue.use(require('vue-shortkey'), {
66} ) ;
77
88Vue . component ( 'annotator' , {
9- template : '<div @click="setSelectedRange">'
10- + ' <span v-for="r in chunks"'
11- + ' v-bind:class="{tag: id2label[r.label].text_color}"'
12- + ' v-bind:style="{ color: id2label[r.label].text_color, backgroundColor: id2label[r.label].background_color }"'
13- + ' >{{ text.slice(r.start_offset, r.end_offset) }}<button class="delete is-small"'
14- + ' v-if="id2label[r.label].text_color"'
15- + ' @click="removeLabel(r)"></button></span>'
16- + ' </div>' ,
179 props : {
18- labels : Array , // [{id: Integer, color: String, text: String}]
19- text : String ,
20- entityPositions : Array , // [{'startOffset': 10, 'endOffset': 15, 'label_id': 1}]
10+ labels : {
11+ type : Array , // [{id: Integer, color: String, text: String}]
12+ default : ( ) => [ ] ,
13+ } ,
14+ text : {
15+ type : String ,
16+ default : '' ,
17+ } ,
18+ entityPositions : {
19+ type : Array , // [{'startOffset': 10, 'endOffset': 15, 'label_id': 1}]
20+ default : ( ) => [ ] ,
21+ } ,
2122 } ,
23+
2224 data ( ) {
2325 return {
2426 startOffset : 0 ,
2527 endOffset : 0 ,
2628 } ;
2729 } ,
2830
31+ computed : {
32+ sortedEntityPositions ( ) {
33+ /* eslint-disable vue/no-side-effects-in-computed-properties */
34+ this . entityPositions = this . entityPositions . sort ( ( a , b ) => a . start_offset - b . start_offset ) ;
35+ return this . entityPositions ;
36+ /* eslint-enable vue/no-side-effects-in-computed-properties */
37+ } ,
38+
39+ chunks ( ) {
40+ const res = [ ] ;
41+ let left = 0 ;
42+ for ( let i = 0 ; i < this . sortedEntityPositions . length ; i ++ ) {
43+ const e = this . sortedEntityPositions [ i ] ;
44+ const l = this . makeLabel ( left , e . start_offset ) ;
45+ res . push ( l ) ;
46+ res . push ( e ) ;
47+ left = e . end_offset ;
48+ }
49+ const l = this . makeLabel ( left , this . text . length ) ;
50+ res . push ( l ) ;
51+
52+ return res ;
53+ } ,
54+
55+ id2label ( ) {
56+ const id2label = { } ;
57+ // default value;
58+ id2label [ - 1 ] = {
59+ text_color : '' ,
60+ background_color : '' ,
61+ } ;
62+ for ( let i = 0 ; i < this . labels . length ; i ++ ) {
63+ const label = this . labels [ i ] ;
64+ id2label [ label . id ] = label ;
65+ }
66+ return id2label ;
67+ } ,
68+ } ,
69+
70+ watch : {
71+ entityPositions ( ) {
72+ this . resetRange ( ) ;
73+ } ,
74+ } ,
75+
2976 methods : {
3077 setSelectedRange ( ) {
3178 let start ;
@@ -103,48 +150,14 @@ Vue.component('annotator', {
103150 } ,
104151 } ,
105152
106- watch : {
107- entityPositions ( ) {
108- this . resetRange ( ) ;
109- } ,
110- } ,
111-
112- computed : {
113- sortedEntityPositions ( ) {
114- this . entityPositions = this . entityPositions . sort ( ( a , b ) => a . start_offset - b . start_offset ) ;
115- return this . entityPositions ;
116- } ,
117-
118- chunks ( ) {
119- const res = [ ] ;
120- let left = 0 ;
121- for ( let i = 0 ; i < this . sortedEntityPositions . length ; i ++ ) {
122- const e = this . sortedEntityPositions [ i ] ;
123- const l = this . makeLabel ( left , e . start_offset ) ;
124- res . push ( l ) ;
125- res . push ( e ) ;
126- left = e . end_offset ;
127- }
128- const l = this . makeLabel ( left , this . text . length ) ;
129- res . push ( l ) ;
130-
131- return res ;
132- } ,
133-
134- id2label ( ) {
135- const id2label = { } ;
136- // default value;
137- id2label [ - 1 ] = {
138- text_color : '' ,
139- background_color : '' ,
140- } ;
141- for ( let i = 0 ; i < this . labels . length ; i ++ ) {
142- const label = this . labels [ i ] ;
143- id2label [ label . id ] = label ;
144- }
145- return id2label ;
146- } ,
147- } ,
153+ template : '<div @click="setSelectedRange">'
154+ + ' <span v-for="r in chunks"'
155+ + ' v-bind:class="{tag: id2label[r.label].text_color}"'
156+ + ' v-bind:style="{ color: id2label[r.label].text_color, backgroundColor: id2label[r.label].background_color }"'
157+ + ' >{{ text.slice(r.start_offset, r.end_offset) }}<button class="delete is-small"'
158+ + ' v-if="id2label[r.label].text_color"'
159+ + ' @click="removeLabel(r)"></button></span>'
160+ + ' </div>' ,
148161} ) ;
149162
150163const vm = new Vue ( { // eslint-disable-line no-unused-vars
0 commit comments