Skip to content

Commit e8ba10f

Browse files
committed
refactor -- add editable tags
1 parent 8cc5b57 commit e8ba10f

File tree

8 files changed

+209
-48
lines changed

8 files changed

+209
-48
lines changed

css/style2.css

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ svg text {
1818

1919
#header {
2020
position:fixed;
21+
z-index:100;
2122
background:black;
2223
width:100%;
2324
height:40px;
@@ -89,19 +90,20 @@ svg text {
8990
cursor: pointer;
9091
}
9192

92-
.word .word-cluster,
93+
.word-cluster text,
9394
.word .word-tag {
9495
font-size: 12px;
9596
cursor: text;
9697
}
9798

98-
.word .word-cluster {
99+
.word-cluster text {
99100
fill:#a94442;
100101
}
101102
.word .word-tag {
102103
fill:#333;
103104
}
104-
.editing .word-tag {
105+
.editing .word-tag,
106+
.editing text {
105107
fill: #fff;
106108
}
107109
.editing rect {

js2/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const Main = (function() {
3232
lm = new LabelManager(svg);
3333

3434
// load and render initial dataset by default
35-
changeDataset();
35+
changeDataset(4);
3636

3737
// svg event listeners
3838
svg.on('row-resize', function(e) {

js2/managers/labelmanager.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const LabelManager = (function() {
77
// special keys
88
const Key = {
99
delete: 8,
10+
tab: 9,
1011
enter: 13,
1112
escape: 27,
1213
left: 37,
@@ -33,7 +34,7 @@ const LabelManager = (function() {
3334

3435
function stopEditing() {
3536
if (activeObject && activeObject.isEditing) {
36-
if (!string) { updateString(originalString); }
37+
// if (!string) { updateString(originalString); }
3738
activeObject.stopEditing();
3839
activeObject = null;
3940
originalString = string = null;
@@ -53,6 +54,8 @@ const LabelManager = (function() {
5354
if (string === null) { string = originalString; }
5455
updateString(string.slice(0, -1));
5556
break;
57+
case Key.tab:
58+
break;
5659
case Key.enter:
5760
stopEditing();
5861
break;
@@ -72,15 +75,19 @@ const LabelManager = (function() {
7275
}
7376
})
7477
document.addEventListener('keypress', function(e) {
78+
// console.log(String.fromCharCode(e.which), e.which);
7579
if (activeObject && activeObject.isEditing) {
76-
if (string === null) { string = ''; }
77-
updateString(string + String.fromCharCode(e.which));
80+
if (e.which === 32) {
81+
e.preventDefault();
82+
}
83+
if (!e.ctrlKey && !e.metaKey && !e.altKey) {
84+
if (string === null) { string = ''; }
85+
updateString(string + String.fromCharCode(e.which));
86+
}
7887
}
7988
});
8089

81-
document.addEventListener('mousedown', function(e) {
82-
stopEditing();
83-
});
90+
document.addEventListener('mousedown', stopEditing);
8491

8592
return LabelManager;
8693
})();

js2/managers/rowmanager.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const RowManager = (function() {
4646

4747
addWordToRow(word, row, i, ignorePosition) {
4848
if (isNaN(i)) { i = row.words.length; }
49+
4950
let overflow = row.addWord(word, i, ignorePosition);
5051
while (overflow < row.words.length) {
5152
this.moveWordDownARow(row.idx);
@@ -153,7 +154,9 @@ const RowManager = (function() {
153154

154155
moveWordUpARow(index) {
155156
if (!_rows[index - 1]) { return; }
156-
this.addWordToRow(_rows[index].removeFirstWord(), _rows[index - 1], undefined, true);
157+
let removedWord = _rows[index].removeFirstWord();
158+
this.addWordToRow(removedWord, _rows[index - 1], undefined, true);
159+
removedWord.redrawClusters();
157160
}
158161

159162
moveWordDownARow(index) {

js2/row.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,5 @@ class Row {
134134
}
135135

136136
get ry2() { return this.ry + this.rh; }
137-
get minHeight() { return 50; }
137+
get minHeight() { return Math.max(30, this.wordGroup.bbox().height); }
138138
}

js2/tooltip.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const Tooltip = (function() {
22
let div = {};
3+
let _svg = {};
34
let activeObject = null;
45
let yOffset = 0;
56

@@ -9,6 +10,8 @@ const Tooltip = (function() {
910
yOffset = svg.node.getBoundingClientRect().top;
1011
this.clear = clear;
1112

13+
_svg = svg;
14+
1215
// listeners to open tooltip
1316
svg.on('tag-right-click', openTooltip);
1417
svg.on('word-right-click', openTooltip);
@@ -22,7 +25,7 @@ const Tooltip = (function() {
2225
let html = '';
2326
if (activeObject instanceof Word) {
2427
if (activeObject.tag) {
25-
html += '<p id="menu--edit-tag">Edit tag</p><hr>';
28+
html += '<p id="menu--edit-tag">Edit tag</p><p id="menu--remove-tag">Remove tag</p><hr>';
2629
}
2730
else {
2831
html += '<p id="menu--add-tag">Add tag</p><hr>';
@@ -63,11 +66,19 @@ const Tooltip = (function() {
6366
switch (e.target.id) {
6467
// tag management events
6568
case 'menu--remove-tag':
66-
activeObject.remove();
69+
if (activeObject instanceof Word && activeObject.tag) {
70+
activeObject.tag.remove();
71+
}
72+
else {
73+
activeObject.remove();
74+
}
6775
break;
6876
case 'menu--add-tag':
77+
let tag = activeObject.setTag('?');
78+
_svg.fire('tag-edit', { object: tag });
6979
break;
7080
case 'menu--edit-tag':
81+
_svg.fire('tag-edit', { object: activeObject.tag });
7182
break;
7283
default: ;
7384
}

js2/word.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Word {
3131
else {
3232
this.tag = tag;
3333
}
34+
return this.tag;
3435
}
3536
init(svg) {
3637
this.mainSVG = svg;
@@ -47,7 +48,7 @@ class Word {
4748

4849
// draw cluster info
4950
this.clusters.forEach((cluster) => {
50-
cluster.draw(svg);
51+
cluster.init(this);
5152
});
5253

5354
// translate over by half (since the text is centered)
@@ -82,9 +83,18 @@ class Word {
8283
}
8384
}
8485

86+
redrawClusters() {
87+
this.clusters.forEach(cluster => {
88+
if (cluster.endpoints.indexOf(this) > -1) {
89+
cluster.draw();
90+
}
91+
});
92+
}
93+
8594
move(x) {
8695
this.x = x;
8796
this.svg.transform({x: this.boxWidth / 2 + this.x});
97+
this.redrawClusters();
8898
}
8999

90100
dx(x) {

0 commit comments

Comments
 (0)