-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
287 lines (277 loc) · 9.15 KB
/
index.html
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html" lang="zh">
<head>
<title>UIMarker</title>
<meta charset="utf-8">
</head>
<body onload="onBodyLoad()">
<div id="memo">
<span>UI Marker: support cmd+x(cut)、cmd+c(copy)、cmd+v(paste)、cmd+backspace(delete), Don't use 'nodes' and 'image' as label name.</span>
</div>
<div id="ctrl">
<div style="align-items: center; display: flex">
<label for="jsonData">
<textarea
id="jsonData"
rows="10"
cols="70"
placeholder="JsonData"
></textarea>
</label>
<button class="btn" data-clipboard-target="#jsonData" style="margin-left: 20px">CopyData</button>
</div>
<div style="margin-bottom: 10px">
<button onclick="importData()">ImportData</button>
<button onclick="saveAllPageData()">SaveData</button>
<button onclick="addNode()">AddPosition</button>
<label>
Switch Page
<select id="pageSelector" onchange="onPageChange()" style="width: 200px">
<option value="PageA">PageA</option>
<option value="PageB">PageB</option>
</select>
<span id="imageSize">ImageSize: width * height</span>
</label>
</div>
</div>
<div id="container" style="border: 1px solid"></div>
</body>
<script src="https://cdn.jsdelivr.net/gh/yihuineng/ui-marker@master/lib/x6.js"></script>
<script src="https://cdn.jsdelivr.net/gh/yihuineng/ui-marker@master/lib/clipboard.min.js"></script>
<script>
let randomStr = `${Math.random()}`;
let graph;
let jsonData = {
"PageA": {
"image": "https://cdn.jsdelivr.net/gh/yihuineng/images@master/uimarker/page1.jpg",
"btn1": "100,200",
"btn2": "200,200"
},
"PageB": {
"image": "https://cdn.jsdelivr.net/gh/yihuineng/images@master/uimarker/page2.jpg",
"btn3": "100,200",
"btn4": "300,200"
}
};
let currentPage = 'PageA';
const getImgSize = async (url) => {
return new Promise(resolve => {
// 获取图片宽高
const img = new Image();
img.src = url;
img.onload = () => {
resolve({ width: img.width / 2, height: img.height / 2 });
};
});
};
const newNode = (label = 'new-label', x = 50, y = 50) => {
return {
shape: 'polygon',
x,
y,
width: 80,
height: 40,
label,
attrs: {
body: {
fill: 'red',
stroke: 'red',
points: '0,0 5,0 0,0 0,5',
},
},
};
};
const recreateGraph = async (opts) => {
const {
backgroundImage = 'https://cdn.jsdelivr.net/gh/yihuineng/images@master/uimarker/page1.jpg',
nodes = [],
} = opts;
const imgSize = await getImgSize(backgroundImage);
const width = opts.width || imgSize.width;
const height = opts.height || imgSize.height;
console.log(width, height);
document.getElementById("imageSize").innerText = `ImageSize: ${width} * ${height}`;
if (graph) {
graph.dispose();
graph = null;
}
graph = new X6.Graph({
container: document.getElementById('container'),
clipboard: true,
keyboard: {
enabled: true,
format(key) {
return key
.replace(/\s/g, '')
.replace('cmd', 'command');
},
},
selecting: {
enabled: true,
showNodeSelectionBox: true,
},
width,
height,
background: {
image: backgroundImage,
opacity: 0.5,
size: {
width,
height,
},
},
grid: {
size: 5,
visible: false,
},
});
graph.bindKey('cmd+c', () => {
const cells = graph.getSelectedCells()
if (cells.length) {
graph.copy(cells);
}
return false;
});
graph.bindKey('cmd+v', () => {
if (!graph.isClipboardEmpty()) {
const cells = graph.paste({ offset: 32 });
graph.cleanSelection();
graph.select(cells);
}
return false;
});
graph.bindKey('cmd+x', () => {
const cells = graph.getSelectedCells()
if (cells.length) {
graph.cut(cells);
}
return false;
});
graph.bindKey('cmd+backspace', () => {
const cells = graph.getSelectedCells()
if (cells.length) {
cells.forEach(cell => {
cell.remove();
});
}
return false;
});
graph.on('cell:dblclick', ({ cell, e }) => {
const isNode = cell.isNode()
const name = cell.isNode() ? 'node-editor' : 'edge-editor'
cell.removeTool(name)
cell.addTools({
name,
args: {
event: e,
attrs: {
backgroundColor: isNode ? '#EFF4FF' : '#FFF',
},
},
})
});
graph.fromJSON({ nodes });
};
const importData = async () => {
// fresh image cache
randomStr = `${Math.random()}`;
const jsonStr = document.getElementById("jsonData").value.trim();
jsonData = eval("(" + jsonStr + ")");
const pageKeys = Object.keys(jsonData);
// labels -> nodes
for (const pageKey in jsonData) {
jsonData[pageKey].nodes = getNodesByLabels(jsonData[pageKey]);
}
currentPage = pageKeys[0];
await recreateGraph({
backgroundImage: jsonData[currentPage].image,
nodes: jsonData[currentPage].nodes,
});
// update select
const select = document.getElementById("pageSelector");
select.options.length = 0;
pageKeys.forEach(k => {
select.options.add(new Option(k, k));
});
select[0].selected = true;
};
const addNode = () => {
graph.addNode(newNode());
};
const saveAllPageData = () => {
saveCurrentPage();
const resultJson = { ...jsonData };
for (const key in resultJson) {
delete resultJson[key].nodes;
}
console.log('resultJson', resultJson);
document.getElementById("jsonData").value = JSON.stringify(resultJson, null, 2);
};
// save current page data: nodes labels
const saveCurrentPage = () => {
if (!graph) {
return;
}
const { cells = [] } = graph.toJSON();
const nodes = [];
const labels = {};
cells.forEach(cell => {
const label = cell.attrs.text.text.trim();
labels[label] = `${cell.position.x},${cell.position.y}`;
nodes.push(newNode(label, cell.position.x, cell.position.y));
});
// 更新nodes和labels
jsonData[currentPage] = {
image: jsonData[currentPage].image,
nodes,
...labels
};
};
const onPageChange = async () => {
saveCurrentPage();
const select = document.getElementById("pageSelector");
const idx = select.selectedIndex;
const selPage = select.options[idx].value;
console.log('onPageChange', selPage);
await recreateGraph({
backgroundImage: jsonData[selPage].image + "?ran=" + randomStr,
nodes: jsonData[selPage].nodes,
});
currentPage = selPage;
};
const getNodesByLabels = (labels) => {
const nodes = [];
for (const labelKey in labels) {
if (labelKey !== 'image' && labelKey !== 'nodes') {
const labelPos = labels[labelKey].split(',');
nodes.push(newNode(labelKey, Number.parseInt(labelPos[0]), Number.parseInt(labelPos[1])));
}
}
return nodes;
};
const onBodyLoad = () => {
console.log('onload');
const clipboard = new ClipboardJS('.btn');
clipboard.on('success', () => {
alert('copy success!');
});
// 尝试通过url传参初始化
const urlParams = new URLSearchParams(window.location.search);
const jsonText = urlParams.get('jsonText');
if (jsonText) {
document.getElementById("jsonData").value = jsonText;
importData();
return;
}
// 默认数据初始化
const defPageKeys = Object.keys(jsonData);
for (const key in jsonData) {
jsonData[key].nodes = getNodesByLabels(jsonData[key]);
}
recreateGraph({
backgroundImage: jsonData[defPageKeys[0]].image,
nodes: jsonData[defPageKeys[0]].nodes,
});
};
</script>
</html>