zhongrj
2025-11-24 276323dce9613867abb3f58a4cc2abbfb2fd0dea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import React from 'react';
import '../css/TagsField.scss';
import PropTypes from 'prop-types';
import update from 'immutability-helper';
import { _ } from '../classes/gettext';
import Tags from '../classes/Tags';
 
class TagsField extends React.Component {
  static defaultProps = {
    tags: [],
    onUpdate: () => {}
  };
 
  static propTypes = {
    tags: PropTypes.arrayOf(PropTypes.string),
    onUpdate: PropTypes.func
  };
 
  constructor(props){
    super(props);
 
    this.state = {
      userTags: Tags.userTags(props.tags),
      systemTags: Tags.systemTags(props.tags)
    }
 
    this.dzList = [];
    this.domTags = [];
  }
 
  componentDidUpdate(){
    this.props.onUpdate(Tags.combine(this.state.userTags, this.state.systemTags));
  }
 
  componentWillUnmount(){
    this.restoreDropzones();
  }
 
  disableDropzones(){
    if (this.disabledDz) return;
    let parent = this.domNode.parentElement;
    while(parent){
      if (parent.dropzone){
        parent.dropzone.removeListeners();
        this.dzList.push(parent.dropzone);
      }
      parent = parent.parentElement;
    }
    this.disabledDz = true;
  }
 
  restoreDropzones(){
    if (!this.disabledDz) return;
 
    this.dzList.forEach(dz => {
      dz.restoreListeners();
    });
    this.dzList = [];
    this.disabledDz = false;
  }
 
  handleKeyDown = e => {
    if (e.key === "Tab" || e.key === "Enter" || e.key === "," || e.key === " "){
      e.preventDefault();
      e.stopPropagation();
      this.addTag();
    }else if (e.key === "Backspace" && this.inputText.innerText === ""){
      this.removeTag(this.state.userTags.length - 1);
    }
  }
 
  focus = () => {
    this.inputText.focus();
  }
 
  stop = e => {
    e.stopPropagation();
  }
 
  handleRemoveTag = idx => {
    return e => {
      e.stopPropagation();
      this.removeTag(idx);
    }
  }
 
  removeTag = idx => {
    this.setState(update(this.state, { userTags: { $splice: [[idx, 1]] } }));
  }
 
  addTag = () => {
    let text = this.inputText.innerText;
    if (text !== ""){
      // Do not allow system tags
      if (!text.startsWith(".")){
 
        // Only lower case text allowed
        text = text.toLowerCase();
 
        // Check for dulicates
        if (this.state.userTags.indexOf(text) === -1){
          this.setState(update(this.state, {
            userTags: {$push: [text]}
          }));
        }
      }
      this.inputText.innerText = "";
    }
  }
 
  handleDragStart = tag => {
    return e => {
      this.disableDropzones();
      e.stopPropagation();
      e.dataTransfer.setData("application/tag", tag);
      e.dataTransfer.dropEffect = "move";
    }
  }
 
  handleDrop = e => {
    e.preventDefault();
    const dragTag = e.dataTransfer.getData("application/tag");
    const [moveTag, side] = this.findClosestTag(e.clientX, e.clientY);
 
    const { userTags } = this.state;
    if (moveTag){
      const dragIdx = userTags.indexOf(dragTag);
      const moveIdx = userTags.indexOf(moveTag);
      if (dragIdx !== -1 && moveIdx !== -1){
        if (dragIdx === moveIdx) return;
        else{
          // Put drag tag in front of move tag
          let insertIdx = side === "right" ? moveIdx + 1 : moveIdx;
          userTags.splice(insertIdx, 0, dragTag);
          for (let i = 0; i < userTags.length; i++){
            if (userTags[i] === dragTag && i !== insertIdx){
              userTags.splice(i, 1);
              break;
            }
          }
          this.setState({userTags});
        }
      }
    }
  }
  handleDragOver = e => {
    e.preventDefault();
    e.dataTransfer.dropEffect = "move";
  }
  handleDragEnter = e => {
    e.preventDefault();
  }
  handleDragEnd = () => {
    this.restoreDropzones();
  }
 
  findClosestTag = (clientX, clientY) => {
    let closestTag = null;
    let minDistX = Infinity, minDistY = Infinity;
    let rowTagY = null;
    const { userTags } = this.state;
 
    // Find tags in closest row
    this.domTags.forEach((domTag, i) => {
      const b = domTag.getBoundingClientRect();
      const tagY = b.y + (b.height / 2);
      let dy = clientY - tagY,
          sqDistY = dy*dy;
      
      if (sqDistY < minDistY){
        minDistY = sqDistY;
        rowTagY = tagY;
      }
    });
 
    if (!rowTagY) return [null, ""];
 
    // From row, find closest in X
    this.domTags.forEach((domTag, i) => {
      const b = domTag.getBoundingClientRect();
      const tagY = b.y + (b.height / 2);
      if (Math.abs(tagY - rowTagY) < 0.001){
        const tagX = b.x + b.width;
        let dx = clientX - tagX,
            sqDistX = dx*dx;
        if (sqDistX < minDistX){
          closestTag = userTags[i];
          minDistX = sqDistX;
        }
      }
    });
 
    let side = "right";
    if (closestTag){
      const b = this.domTags[this.state.userTags.indexOf(closestTag)].getBoundingClientRect();
      const centerX = b.x + b.width / 2.0;
      if (clientX < centerX) side = "left";
    }
 
    return [closestTag, side];
  }
 
  render() {
    return (<div
                ref={domNode => this.domNode = domNode}
                spellCheck="false"
                autoComplete="off"
                onClick={this.focus}
                onDrop={this.handleDrop}
                onDragOver={this.handleDragOver}
                onDragEnter={this.handleDragEnter}
                className="form-control tags-field">{this.state.userTags.map((tag, i) => 
                  <div draggable="true" className="tag-badge" key={i} ref={domNode => this.domTags[i] = domNode} 
                      onClick={this.stop} 
                      onDragStart={this.handleDragStart(tag)} 
                      onDragEnd={this.handleDragEnd}>{tag} <a href="javascript:void(0)" onClick={this.handleRemoveTag(i)}>×</a>&nbsp;&nbsp;</div>
                )}
                <div className="inputText" contentEditable="true" ref={(domNode) => this.inputText = domNode}
                     onKeyDown={this.handleKeyDown}
                     onBlur={this.addTag}></div>
        </div>);
  }
}
 
export default TagsField;