Skip to content

Commit

Permalink
fix(view:codeblock:mermaid): Use unique node list for adding click ha…
Browse files Browse the repository at this point in the history
…ndlers
  • Loading branch information
SkepticMystic committed Apr 6, 2024
1 parent ce82bb7 commit d0ad017
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/utils/mermaid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,25 @@ const from_edges = (

lines.push("");

// NOTE: This is _pretty_ inefficient, but necessary.
// If we just take all unique target_ids, we miss source nodes that don't have any incoming edges.
const nodes = remove_duplicates_by(
edges.flatMap((e) => [
{ id: e.source_id, attr: e.source_attr },
{ id: e.target_id, attr: e.target_attr },
]),
(n) => n.id,
);

// const active_file = get(active_file_store);
// if (active_file && nodes.find((n) => n.id === active_file.path)) {
// lines.push(
// `\tclass ${encodeURIComponent(active_file.path)} BC-active-note`,
// );
// }

switch (config?.click?.method) {
case "class": {
// NOTE: This is _pretty_ inefficient, but necessary.
// If we just take all unique target_ids, we miss source nodes that don't have any incoming edges.
const nodes = remove_duplicates_by(
edges.flatMap((e) => [
{ id: e.source_id, attr: e.source_attr },
{ id: e.target_id, attr: e.target_attr },
]),
(n) => n.id,
);

if (nodes.length) {
lines.push(
`\tclass ${nodes.map((n) => encodeURIComponent(n.id))} internal-link`,
Expand All @@ -84,19 +91,19 @@ const from_edges = (
}

case "href": {
edges.forEach(({ target_id }) => {
nodes.forEach((node) => {
lines.push(
`\tclick ${target_id} "${(<Extract<(typeof config)["click"], { method: "href" }>>config.click)?.getter(target_id)}"`,
`\tclick ${encodeURIComponent(node.id)} "${(<Extract<(typeof config)["click"], { method: "href" }>>config.click)?.getter(node.id)}"`,
);
});

break;
}

case "callback": {
edges.forEach(({ target_id }) => {
nodes.forEach((node) => {
lines.push(
`\tclick ${target_id} "${(<Extract<(typeof config)["click"], { method: "callback" }>>config.click)?.callback_name}"`,
`\tclick ${encodeURIComponent(node.id)} "${(<Extract<(typeof config)["click"], { method: "callback" }>>config.click)?.callback_name}"`,
);
});

Expand Down

0 comments on commit d0ad017

Please sign in to comment.