-
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
-
288
-
289
-
290
-
291
-
292
-
293
-
294
-
295
-
296
-
297
-
298
-
299
-
300
-
301
-
302
-
303
-
304
-
305
-
306
-
307
-
308
-
309
-
310
-
311
-
312
-
313
-
314
-
315
-
316
-
317
-
318
-
319
-
320
-
321
-
322
-
323
-
324
-
325
-
326
-
327
-
328
-
329
-
330
-
331
-
332
-
333
-
334
-
335
-
336
-
337
-
338
-
339
-
340
-
341
-
342
-
343
-
344
-
345
-
346
-
347
-
348
-
349
-
350
-
351
-
352
-
353
-
354
-
355
-
356
-
357
-
358
-
359
-
360
-
361
-
362
-
363
-
364
-
365
-
366
-
367
-
368
-
369
-
370
-
371
-
372
-
373
-
374
-
375
-
376
-
377
-
378
-
379
-
380
-
381
-
382
-
383
-
384
-
385
-
386
-
387
-
388
-
389
-
390
-
391
-
392
-
393
-
394
-
395
-
396
-
397
-
398
-
399
-
400
-
401
-
402
-
403
-
404
-
405
-
406
-
407
-
408
-
409
-
410
-
411
-
412
-
413
-
414
-
415
-
416
-
417
-
418
-
419
-
420
-
421
-
422
-
423
-
424
-
425
-
426
-
427
-
428
-
429
-
430
-
431
-
432
-
433
-
434
-
435
-
436
-
437
-
438
-
439
-
440
-
441
-
442
-
443
-
444
-
445
-
446
-
447
-
448
-
449
-
450
-
451
-
452
-
453
-
454
-
455
-
456
-
457
-
458
-
459
-
460
-
461
-
462
-
463
-
464
-
465
-
466
-
467
-
468
-
469
-
470
-
471
-
472
-
473
-
474
-
475
-
476
-
477
-
478
-
479
-
480
-
481
-
482
-
483
-
484
-
485
-
486
-
487
-
488
-
489
-
490
-
491
-
492
-
493
-
494
-
495
-
496
-
497
-
498
-
499
-
500
-
501
-
502
-
503
-
504
-
505
-
506
-
507
-
508
-
509
-
510
-
511
-
512
-
513
-
514
-
515
-
516
-
517
-
518
-
519
-
520
-
521
-
522
-
523
-
524
-
525
-
526
-
527
-
528
-
529
-
530
-
531
-
532
-
533
-
534
-
535
-
536
-
537
-
538
-
539
-
540
-
541
-
542
-
543
-
544
-
545
-
546
-
547
-
548
-
549
-
550
-
551
-
552
-
553
-
554
-
555
-
556
-
557
-
558
-
559
-
560
-
561
-
562
-
563
-
564
-
565
-
566
-
567
-
568
-
569
-
570
-
571
-
572
-
573
-
574
-
575
-
576
-
577
-
578
-
579
-
580
-
581
-
582
-
583
-
584
-
585
-
586
-
587
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
//
// SPDX-License-Identifier: Apache-2.0
/** @jsx s */
import { extname } from "../../../deps/deno.land/std/path/mod.ts";
import type * as Hast from "../../../deps/esm.sh/hast/types.ts";
import { h, s } from "../../../deps/esm.sh/hastscript/mod.ts";
import { logger } from "../../../logger.ts";
import type {
CanvasColor,
Edge,
FileNode,
GroupNode,
JSONCanvas,
LinkNode,
Node,
NodeSide,
TextNode,
} from "../../../content_parser/json_canvas/types.ts";
import { buildClasses, css, cx } from "../css.ts";
import {
getBoundingBox,
getClosestSides,
getConnectionPoint,
getDirV,
move,
type Point2D,
type Vec2D,
vecMul,
} from "./layout.ts";
const c = buildClasses("jc", [
"embed",
"canvas",
"nodeContainer",
"nodeBg",
"vectorCanvas",
"groupNode",
"iframe",
"edgeLabel",
]);
export const jsonCanvasStyles = css`
.${c.canvas} {
position: relative;
}
.${c.vectorCanvas} {
position: absolute;
inset: 0;
pointer-events: none;
}
.${c.nodeContainer} {
position: absolute;
display: flex;
justify-content: stretch;
align-items: stretch;
border-style: solid;
border-width: var(--canvas-node-stroke-width);
border-radius: var(--canvas-node-radius, 3px);
box-shadow: 0 0 2px hsl(0deg 0% 0% / 0.2);
overflow: visible;
}
.${c.nodeBg} {
position: absolute;
inset: 0;
pointer-events: none;
opacity: var(--canvas-node-bg-opacity);
}
.${c.embed} {
width: 100%;
height: 100%;
padding: 4px 8px;
overflow: auto;
}
.${c.embed} > p {
margin-block-start: calc(var(--baseline) * 0.5rem);
}
.${c.embed} > :first-child {
margin-block-start: 0;
}
.${c.groupNode} {
position: absolute;
top: -6px;
left: 0;
padding: 2px 0.5em;
border-radius: 3px;
transform: translateY(-100%);
}
.${c.iframe} {
width: 100%;
height: 100%;
border: none;
}
.${c.edgeLabel} {
position: absolute;
display: inline-block;
padding: 2px 0.5em;
font-size: 0.9rem;
background-color: var(--color-bg);
border-radius: 2px;
color: var(--color-fg);
transform: translate(-50%, -50%);
}
`;
function canvasColorToCssColor(color: CanvasColor): string {
switch (color) {
case "1":
return "var(--canvas-color-red)";
case "2":
return "var(--canvas-color-orange)";
case "3":
return "var(--canvas-color-yellow)";
case "4":
return "var(--canvas-color-green)";
case "5":
return "var(--canvas-color-cyan)";
case "6":
return "var(--canvas-color-purple)";
default:
return color;
}
}
interface StyleConstructor {
[key: string]: string | number | undefined | null;
}
function constructStyle(c: StyleConstructor): string {
return Object.entries(c).map(([key, value]) => {
if (!value) {
return null;
}
return key + ":" + (typeof value === "number" ? value + "px" : value);
}).filter((s): s is string => !!s).join(";");
}
interface TextNodeProps {
node: TextNode<Hast.Nodes>;
}
function textNode({ node }: TextNodeProps) {
return <div class={c.embed}>{node.text}</div>;
}
interface LinkNodeProps {
node: LinkNode;
}
function linkNode({ node }: LinkNodeProps) {
return (
<iframe
class={c.iframe}
src={node.url}
width={node.width}
height={node.height}
/>
);
}
interface FileNodeProps {
node: FileNode;
}
function fileNode({ node }: FileNodeProps) {
const ext = extname(node.file);
switch (ext) {
case ".jpg":
case ".jpeg":
case ".avif":
case ".bmp":
case ".png":
case ".svg":
case ".webp": {
return (
<img
style="max-width:100%;max-height:100%;object-fit:contain;"
src={node.file}
/>
);
}
case ".mkv":
case ".mov":
case ".mp4":
case ".ogv":
case ".webm": {
return (
<video
style="max-width:100%;max-height:100%;object-fit:contain;"
src={node.file}
/>
);
}
case ".flac":
case ".m4a":
case ".mp3":
case ".ogg":
case ".wav":
case ".3gp": {
return (
<div style="width:100%;height:100%;display:grid;place-items:center;">
<audio src={node.file} />
</div>
);
}
default: {
return (
<iframe
class={c.iframe}
src={node.file}
sandbox="allow-scripts"
loading="lazy"
width={node.width}
height={node.height}
/>
);
}
}
}
interface GroupNodeProps {
node: GroupNode;
}
function groupNode({ node }: GroupNodeProps) {
if (!node.label) {
return null;
}
const color = node.color
? canvasColorToCssColor(node.color)
: "var(--canvas-color-fallback)";
return h("span", {
className: c.groupNode,
style: constructStyle({
"background-color": color,
// TODO: Make fg color configurable
color: "var(--color-bg)",
}),
}, [
node.label,
]);
}
interface NodeRendererProps {
node: Node<Hast.Nodes>;
}
function nodeRenderer({ node }: NodeRendererProps) {
switch (node.type) {
case "text":
return textNode({ node });
case "link":
return linkNode({ node });
case "file":
return fileNode({ node });
case "group":
return groupNode({ node });
}
}
interface EdgeArrowProps {
target: Point2D;
pointTo: NodeSide;
size?: number;
fill?: string;
stroke?: string;
}
function edgeArrow({ target, pointTo, size = 15, ...rest }: EdgeArrowProps) {
const start = `M${target[0]},${target[1]}`;
const width = size * 1.2;
let v1: Vec2D;
let v2: Vec2D;
switch (pointTo) {
case "top":
v1 = [-width * 0.5, -size];
v2 = [width, 0];
break;
case "right":
v1 = [size, -width * 0.5];
v2 = [0, width];
break;
case "bottom":
v1 = [width * 0.5, size];
v2 = [-width, 0];
break;
case "left":
v1 = [-size, -width * 0.5];
v2 = [0, width];
break;
}
return (
<path
{...rest}
d={[start, `l${v1[0]},${v1[1]}`, `l${v2[0]},${v2[1]}`, start, "Z"].join(
" ",
)}
/>
);
}
interface ComputedEdge {
edge: Edge;
fromSide: NodeSide;
toSide: NodeSide;
fromStart: Point2D;
toStart: Point2D;
fromPoint: Point2D;
toPoint: Point2D;
controlPoints: readonly [Point2D, Point2D];
}
function computeEdge(
edge: Edge,
nodes: Map<string, Node<unknown>>,
arrowSize: number,
): ComputedEdge | null {
const fromNode = nodes.get(edge.fromNode);
if (!fromNode) {
logger().warn(
"Malformed JSONCanvas: " +
`Edge(id=${edge.id}) points to non-existing fromNode(id=${edge.fromNode})`,
{
edge,
},
);
return null;
}
const toNode = nodes.get(edge.toNode);
if (!toNode) {
logger().warn(
"Malformed JSONCanvas: " +
`Edge(id=${edge.id}) points to non-existing toNode(id=${edge.toNode})`,
{
edge,
},
);
return null;
}
const [fromSide, toSide] = getClosestSides(
fromNode,
edge.fromSide,
toNode,
edge.toSide,
);
const fromOffsetUnitVec = getDirV(fromSide);
const toOffsetUnitVec = getDirV(toSide);
// Not defined in spec or documents, but Obsidian Canvas uses
// different defaults. wtf
const fromEnd = edge.fromEnd ?? "none";
const toEnd = edge.toEnd ?? "arrow";
const fromPoint = getConnectionPoint(fromNode, fromSide);
const toPoint = getConnectionPoint(toNode, toSide);
// Subtract by 1 otherwise tiny gap appears.
const fromStart = fromEnd === "arrow"
? move(
fromPoint,
vecMul(fromOffsetUnitVec, [arrowSize - 1, arrowSize - 1]),
)
: fromPoint;
const toStart = toEnd === "arrow"
? move(
toPoint,
vecMul(toOffsetUnitVec, [arrowSize - 1, arrowSize - 1]),
)
: toPoint;
const center: Point2D = [
(toStart[0] + fromStart[0]) / 2,
(toStart[1] + fromStart[1]) / 2,
];
// Bezier control points.
// TODO: Improve Bezier control points: Most of curves looks nearly perfect,
// but Obsidian seems to employ special handling when a connector
// overlaps with a node.
const p1: Point2D = move(
fromStart,
vecMul(fromOffsetUnitVec, [
Math.abs(center[0] - fromStart[0]),
Math.abs(center[1] - fromStart[1]),
]),
);
const p2: Point2D = move(
toStart,
vecMul(toOffsetUnitVec, [
Math.abs(toStart[0] - center[0]),
Math.abs(toStart[1] - center[1]),
]),
);
return {
edge,
fromSide,
toSide,
fromStart,
toStart,
fromPoint,
toPoint,
controlPoints: [p1, p2],
};
}
export interface JSONCanvasProps {
className?: string;
data: JSONCanvas<Hast.Nodes>;
arrowSize?: number;
}
export function jsonCanvas(
{ className, data, arrowSize = 20 }: JSONCanvasProps,
) {
const boundingBox = getBoundingBox(data);
const viewBox = [
boundingBox.x,
boundingBox.y,
boundingBox.width,
boundingBox.height,
].map((n) => n.toFixed(0)).join(" ");
// Convert JSONCanvas coordinates to HTML(CSS)'s one
const x = (v: number) => v - boundingBox.x;
const y = (v: number) => v - boundingBox.y;
/**
* Edges refer nodes by ID. This map helps and optimizes its retrieving operation.
* Without using `Map`, lookup takes `O(N)`.
*/
const nodes = new Map<string, Node<unknown>>(
data.nodes?.map((node) => [node.id, node]),
);
const computedEdges = data.edges?.map((edge) =>
computeEdge(edge, nodes, arrowSize)
).filter((edge): edge is ComputedEdge => !!edge);
return h("div", {
className: cx(className, c.canvas),
style: {
width: boundingBox.width + "px",
height: boundingBox.height + "px",
},
}, [
data.nodes?.map((node) => {
const color = node.color
? canvasColorToCssColor(node.color)
: "var(--canvas-color-fallback)";
return h("div", {
className: c.nodeContainer,
style: {
"border-color": color,
left: x(node.x) + "px",
top: y(node.y) + "px",
width: node.width + "px",
height: node.height + "px",
},
}, [
h("div", {
className: c.nodeBg,
style: {
"background-color": color,
},
}, []),
nodeRenderer({ node }),
]);
}),
<svg
class={c.vectorCanvas}
xmlns="http://www.w3.org/2000/svg"
viewbox={viewBox}
style="width: 100%; height: 100%;"
>
{computedEdges?.map(
(
{
edge,
fromPoint,
fromSide,
fromStart,
toStart,
toPoint,
toSide,
controlPoints: [p1, p2],
},
) => {
const color = edge.color
? canvasColorToCssColor(edge.color)
: "var(--canvas-color-fallback)";
const d = [
`M ${fromStart[0]},${fromStart[1]}`,
`C ${p1[0]},${p1[1]} ${p2[0]},${p2[1]} ${toStart[0]},${toStart[1]}`,
].join(" ");
return (
<g>
<path
d={d}
stroke={color}
stroke-width="var(--canvas-edge-stroke-width)"
fill="none"
/>
{edge.fromEnd === "arrow"
? (
edgeArrow({
target: fromPoint,
pointTo: fromSide,
fill: color,
size: arrowSize,
})
)
: null}
{edge.toEnd !== "none"
? (
edgeArrow({
target: toPoint,
pointTo: toSide,
fill: color,
size: arrowSize,
})
)
: null}
</g>
);
},
)}
</svg>,
computedEdges?.map(({ edge, controlPoints: [p1, p2] }) => {
if (!edge.label) {
return null;
}
return h("span", {
className: c.edgeLabel,
style: {
left: x((p1[0] + p2[0]) * 0.5) + "px",
top: y((p1[1] + p2[1]) * 0.5) + "px",
},
}, [
edge.label,
]);
}),
]);
}