|
15 | 15 | import { JointJSNote } from "./JointJS/JointJSNote"; |
16 | 16 | import { JointJSAssociation } from "./JointJS/JointJSAssociation"; |
17 | 17 | import { JointJSGeneralization } from "./JointJS/JointJSGeneralization"; |
| 18 | + import Selector from "./view/Selector.svelte"; |
18 | 19 |
|
19 | | - let editorMode: EditorMode = $state(EditorMode.Panning); |
20 | | - let selectedCellViews = $state<joint.dia.CellView[]>([]); |
21 | | - const selectionRectangle: joint.shapes.standard.Rectangle = |
22 | | - new joint.shapes.standard.Rectangle(); |
| 20 | + function select(x) {} |
| 21 | + let selectedCellViews: joint.dia.CellView[] = $state([]); |
23 | 22 |
|
24 | | - let copiedViews: joint.dia.CellView[]; |
| 23 | + let editorMode: EditorMode = $state(EditorMode.Panning); |
| 24 | + let copiedViews: joint.dia.CellView[] = []; |
25 | 25 |
|
26 | 26 | let paperEl: HTMLElement; |
27 | 27 |
|
28 | | - export function handleKeydown(e: KeyboardEvent) { |
29 | | - if (e.ctrlKey || e.metaKey) { |
30 | | - if (e.key.toLowerCase() === "s") { |
31 | | - e.preventDefault(); |
| 28 | + $effect(() => { |
| 29 | + paper.setElement(paperEl); |
| 30 | +
|
| 31 | + const diagramJSON = localStorage.getItem("diagram"); |
| 32 | + if (diagramJSON) { |
| 33 | + graph.fromJSON(JSON.parse(diagramJSON)); |
| 34 | + } |
| 35 | +
|
| 36 | + paper.setDimensions(paperEl.clientWidth, paperEl.clientHeight); |
| 37 | + paper.render(); |
| 38 | + }); |
| 39 | +
|
| 40 | + export function handleKeydown(event: KeyboardEvent) { |
| 41 | + if (event.ctrlKey || event.metaKey) { |
| 42 | + if (event.key.toLowerCase() === "s") { |
| 43 | + event.preventDefault(); |
32 | 44 | exportJSON(); |
33 | 45 | } |
34 | 46 |
|
35 | | - if (e.key.toLowerCase() === "e") { |
36 | | - e.preventDefault(); |
| 47 | + if (event.key.toLowerCase() === "e") { |
| 48 | + event.preventDefault(); |
37 | 49 | exportSVG(); |
38 | 50 | } |
39 | 51 |
|
40 | | - if (e.key.toLowerCase() === "i") { |
41 | | - e.preventDefault(); |
| 52 | + if (event.key.toLowerCase() === "i") { |
| 53 | + event.preventDefault(); |
42 | 54 | importJSON(); |
43 | 55 | } |
44 | 56 |
|
45 | | - if (e.key.toLowerCase() === "c") { |
46 | | - copiedViews = selectedCellViews; |
| 57 | + if (event.key.toLowerCase() === "c") { |
| 58 | + // copiedViews = selectedCellViews; |
47 | 59 | } |
48 | 60 |
|
49 | | - if (e.key.toLowerCase() === "v") { |
| 61 | + if (event.key.toLowerCase() === "v") { |
50 | 62 | for (const cellView of copiedViews) { |
51 | 63 | const newModel = cellView.model.clone(); |
52 | 64 | const pos = newModel.position(); |
|
57 | 69 | } |
58 | 70 | } |
59 | 71 |
|
60 | | - if (e.key == "Escape") { |
61 | | - select([]); |
| 72 | + if (event.key == "Escape") { |
| 73 | + // select([]); |
62 | 74 | } |
63 | 75 |
|
64 | 76 | if ( |
65 | | - e.target instanceof HTMLElement && |
66 | | - e.target.tagName.toLowerCase() == "input" |
| 77 | + event.target instanceof HTMLElement && |
| 78 | + event.target.tagName.toLowerCase() == "input" |
67 | 79 | ) { |
68 | 80 | return; |
69 | 81 | } |
70 | 82 |
|
71 | | - if (e.key == "Backspace" || e.key == "Delete") { |
72 | | - for (const cellView of selectedCellViews) { |
73 | | - cellView.model.remove(); |
74 | | - } |
75 | | - selectedCellViews = []; |
| 83 | + if (event.key == "Backspace" || event.key == "Delete") { |
| 84 | + // for (const cellView of selectedCellViews) { |
| 85 | + // cellView.model.remove(); |
| 86 | + // } |
| 87 | + // selectedCellViews = []; |
76 | 88 |
|
77 | 89 | return; |
78 | 90 | } |
79 | 91 |
|
80 | | - if (e.key === "1") { |
| 92 | + if (event.key === "1") { |
81 | 93 | editorMode = EditorMode.Panning; |
82 | 94 | } |
83 | 95 |
|
84 | | - if (e.key === "2") { |
| 96 | + if (event.key === "2") { |
85 | 97 | editorMode = EditorMode.Selection; |
86 | 98 | } |
87 | 99 |
|
88 | | - if (e.key === "3") { |
| 100 | + if (event.key === "3") { |
89 | 101 | editorMode = EditorMode.Class; |
90 | 102 | } |
91 | 103 |
|
92 | | - if (e.key === "4") { |
| 104 | + if (event.key === "4") { |
93 | 105 | editorMode = EditorMode.Association; |
94 | 106 | } |
95 | 107 |
|
96 | | - if (e.key === "5") { |
| 108 | + if (event.key === "5") { |
97 | 109 | editorMode = EditorMode.Generalization; |
98 | 110 | } |
99 | 111 |
|
100 | | - if (e.key === "6") { |
| 112 | + if (event.key === "6") { |
101 | 113 | editorMode = EditorMode.Note; |
102 | 114 | } |
103 | 115 | } |
104 | 116 |
|
105 | | - function select(cellViews: joint.dia.CellView[]) { |
106 | | - for (const cellView of selectedCellViews) { |
107 | | - cellView.hideTools(); |
108 | | - joint.highlighters.stroke.remove(cellView, "highlight-selected"); |
109 | | - } |
110 | | -
|
111 | | - selectedCellViews = cellViews; |
112 | | -
|
113 | | - if (cellViews.length > 0) { |
114 | | - if (cellViews.length == 1) { |
115 | | - cellViews[0].showTools(); |
116 | | - } |
117 | | - for (const cellView of cellViews) { |
118 | | - const isClass = cellView.model instanceof JointJSClass; |
119 | | - joint.highlighters.stroke.add( |
120 | | - cellView, |
121 | | - isClass ? { selector: "body" } : { selector: "line" }, |
122 | | - "highlight-selected", |
123 | | - { |
124 | | - padding: isClass ? 6 : 0, |
125 | | - // layer: isClass && cellViews.length > 1 ? null : "back", // "back" prevents the highlighter to cover the link |
126 | | - attrs: { |
127 | | - stroke: "black", |
128 | | - // stroke: isClass |
129 | | - // ? darkenHSL(cellView.model.attr("body/stroke")) |
130 | | - // : darkenHSL(cellView.model.attr("line/stroke")), |
131 | | - // "stroke-width": isClass ? 3 : 12, |
132 | | - }, |
133 | | - }, |
134 | | - ); |
135 | | - } |
136 | | - } |
137 | | - } |
138 | | -
|
139 | 117 | graph.on("add remove change", function (cell: any) { |
140 | 118 | if ( |
141 | 119 | !( |
|
263 | 241 | }; |
264 | 242 |
|
265 | 243 | paper.on("blank:pointerclick", (event, x, y) => { |
266 | | - event.preventDefault(); |
| 244 | + // event.preventDefault(); |
267 | 245 |
|
268 | 246 | if (editorMode == EditorMode.Class) { |
269 | 247 | const obj = new JointJSClass(); |
270 | 248 | obj.position(x, y); |
271 | 249 | obj.addTo(graph); |
272 | | - select([paper.findViewByModel(obj)]); |
| 250 | + // select([paper.findViewByModel(obj)]); |
273 | 251 | return; |
274 | 252 | } |
275 | 253 |
|
276 | 254 | if (editorMode == EditorMode.Note) { |
277 | 255 | const obj = new JointJSNote(); |
278 | 256 | obj.position(x, y); |
279 | 257 | obj.addTo(graph); |
280 | | - select([paper.findViewByModel(obj)]); |
| 258 | + // select([paper.findViewByModel(obj)]); |
281 | 259 | return; |
282 | 260 | } |
283 | 261 |
|
284 | | - select([]); |
| 262 | + // select([]); |
285 | 263 | }); |
286 | 264 |
|
287 | | - paper.on( |
288 | | - "cell:pointerclick", |
289 | | - function (cellView: joint.dia.CellView, evt: joint.dia.Event) { |
290 | | - evt.stopPropagation(); |
291 | | - select([cellView]); |
292 | | - editorMode = EditorMode.Panning; |
293 | | - }, |
294 | | - ); |
295 | | -
|
296 | | - paper.on({ |
297 | | - "blank:pointerdown": (evt, x, y) => { |
298 | | - evt.data = { x, y, button: evt.button }; |
299 | | -
|
300 | | - if (EditorMode.Selection && evt.data && evt.data.button == 0) { |
301 | | - selectionRectangle.addTo(graph); |
302 | | - selectionRectangle.position(x, y); |
303 | | - selectionRectangle.attr({ |
304 | | - body: { |
305 | | - width: 0, |
306 | | - height: 0, |
307 | | - fill: "rgba(0, 162, 255, 0.1)", |
308 | | - stroke: "#00A2FF", |
309 | | - }, |
310 | | - }); |
311 | | - } |
312 | | - }, |
313 | | - "blank:pointermove cell:pointermove": (evt) => { |
314 | | - const currentPoint = paper.clientToLocalPoint( |
315 | | - evt.clientX ?? 0, |
316 | | - evt.clientY ?? 0, |
317 | | - ); |
318 | | -
|
319 | | - if ( |
320 | | - evt.data && |
321 | | - (editorMode == EditorMode.Panning || evt.data.button === 1) |
322 | | - ) { |
323 | | - const dx = (currentPoint.x ?? 0) - evt.data.x; |
324 | | - const dy = (currentPoint.y ?? 0) - evt.data.y; |
325 | | -
|
326 | | - const translate = paper.translate(); |
327 | | - paper.translate(translate.tx + dx, translate.ty + dy); |
328 | | - } |
329 | | -
|
330 | | - if ( |
331 | | - editorMode == EditorMode.Selection && |
332 | | - evt.data && |
333 | | - evt.data.button == 0 |
334 | | - ) { |
335 | | - const width = Math.max( |
336 | | - currentPoint.x - selectionRectangle.position().x, |
337 | | - 0, |
338 | | - ); |
339 | | - const height = Math.max( |
340 | | - currentPoint.y - selectionRectangle.position().y, |
341 | | - 0, |
342 | | - ); |
343 | | - selectionRectangle.size(width, height); |
344 | | - selectionRectangle.attr({ body: { width, height } }); |
345 | | - } |
346 | | - }, |
347 | | - "blank:pointerup cell:pointerup": (_evt) => { |
348 | | - if ( |
349 | | - editorMode == EditorMode.Selection && |
350 | | - selectionRectangle.graph != null |
351 | | - ) { |
352 | | - select( |
353 | | - paper |
354 | | - .findCellViewsInArea(selectionRectangle.getBBox(), { |
355 | | - strict: true, |
356 | | - }) |
357 | | - .filter( |
358 | | - (cellView) => cellView.model != selectionRectangle, |
359 | | - ), |
360 | | - ); |
361 | | - selectionRectangle.remove(); |
362 | | - } |
363 | | - }, |
364 | | - }); |
365 | | -
|
366 | | - $effect(() => { |
367 | | - paper.setElement(paperEl); |
368 | | - paper.setDimensions(paperEl.clientWidth, paperEl.clientHeight); |
369 | | - paper.render(); |
370 | | -
|
371 | | - const diagramJSON = localStorage.getItem("diagram"); |
372 | | - if (diagramJSON) { |
373 | | - graph.fromJSON(JSON.parse(diagramJSON)); |
374 | | - } |
375 | | - }); |
| 265 | + // paper.on( |
| 266 | + // "cell:pointerclick", |
| 267 | + // function (cellView: joint.dia.CellView, evt: joint.dia.Event) { |
| 268 | + // evt.stopPropagation(); |
| 269 | + // select([cellView]); |
| 270 | + // editorMode = EditorMode.Panning; |
| 271 | + // }, |
| 272 | + // ); |
| 273 | +
|
| 274 | + // paper.on({ |
| 275 | + // "blank:pointerdown": (evt, x, y) => { |
| 276 | + // evt.data = { x, y, button: evt.button }; |
| 277 | + // |
| 278 | + // if (EditorMode.Selection && evt.data && evt.data.button == 0) { |
| 279 | + // selectionRectangle.addTo(graph); |
| 280 | + // selectionRectangle.toFront(); |
| 281 | + // selectionRectangle.position(x, y); |
| 282 | + // selectionRectangle.attr({ |
| 283 | + // body: { |
| 284 | + // width: 0, |
| 285 | + // height: 0, |
| 286 | + // fill: "rgba(0, 162, 255, 0.1)", |
| 287 | + // stroke: "#00A2FF", |
| 288 | + // }, |
| 289 | + // }); |
| 290 | + // } |
| 291 | + // }, |
| 292 | + // "blank:pointermove cell:pointermove": (evt) => { |
| 293 | + // const currentPoint = paper.clientToLocalPoint( |
| 294 | + // evt.clientX ?? 0, |
| 295 | + // evt.clientY ?? 0, |
| 296 | + // ); |
| 297 | + // |
| 298 | + // if ( |
| 299 | + // evt.data && |
| 300 | + // (editorMode == EditorMode.Panning || evt.data.button === 1) |
| 301 | + // ) { |
| 302 | + // const dx = (currentPoint.x ?? 0) - evt.data.x; |
| 303 | + // const dy = (currentPoint.y ?? 0) - evt.data.y; |
| 304 | + // |
| 305 | + // const translate = paper.translate(); |
| 306 | + // paper.translate(translate.tx + dx, translate.ty + dy); |
| 307 | + // } |
| 308 | + // |
| 309 | + // if ( |
| 310 | + // editorMode == EditorMode.Selection && |
| 311 | + // evt.data && |
| 312 | + // evt.data.button == 0 |
| 313 | + // ) { |
| 314 | + // const width = Math.max( |
| 315 | + // currentPoint.x - selectionRectangle.position().x, |
| 316 | + // 0, |
| 317 | + // ); |
| 318 | + // const height = Math.max( |
| 319 | + // currentPoint.y - selectionRectangle.position().y, |
| 320 | + // 0, |
| 321 | + // ); |
| 322 | + // selectionRectangle.size(width, height); |
| 323 | + // selectionRectangle.attr({ body: { width, height } }); |
| 324 | + // } |
| 325 | + // }, |
| 326 | + // "blank:pointerup cell:pointerup": (_evt) => { |
| 327 | + // if ( |
| 328 | + // editorMode == EditorMode.Selection && |
| 329 | + // selectionRectangle.graph != null |
| 330 | + // ) { |
| 331 | + // select( |
| 332 | + // paper |
| 333 | + // .findCellViewsInArea(selectionRectangle.getBBox(), { |
| 334 | + // strict: true, |
| 335 | + // }) |
| 336 | + // .filter( |
| 337 | + // (cellView) => cellView.model != selectionRectangle, |
| 338 | + // ), |
| 339 | + // ); |
| 340 | + // selectionRectangle.remove(); |
| 341 | + // } |
| 342 | + // }, |
| 343 | + // }); |
| 344 | +
|
| 345 | + let counter = 0; |
376 | 346 | </script> |
377 | 347 |
|
378 | 348 | <svelte:window |
379 | 349 | onresize={() => paper.setDimensions(window.innerWidth, window.innerHeight)} |
380 | 350 | onkeydown={handleKeydown} |
| 351 | + onclick={() => { |
| 352 | + counter++; |
| 353 | + console.log("click", counter); |
| 354 | + }} |
381 | 355 | /> |
382 | 356 |
|
| 357 | +<Selector bind:selectedCellViews bind:editorMode /> |
| 358 | + |
383 | 359 | <div class="relative flex flex-col w-full h-screen"> |
384 | 360 | <Toolbar bind:editorMode /> |
385 | 361 |
|
386 | 362 | <div class="flex w-full h-full"> |
387 | 363 | <div class="w-72 h-full bg-white border-r border-gray-300"> |
388 | | - <PropertyInspector cellViews={selectedCellViews} /> |
| 364 | + <!-- <PropertyInspector cellViews={selectedCellViews} /> --> |
389 | 365 | </div> |
390 | 366 | <div id="paper" class="w-full h-full" bind:this={paperEl}></div> |
391 | 367 | </div> |
|
0 commit comments