Skip to content

Commit

Permalink
chore: bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
Quorafind committed Jan 18, 2024
1 parent b42259b commit 8eb1212
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 168 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
"支付宝": "https://cdn.jsdelivr.net/gh/Quorafind/.github@main/IMAGE/%E6%94%AF%E4%BB%98%E5%AE%9D%E4%BB%98%E6%AC%BE%E7%A0%81.jpg"
},
"isDesktopOnly": false
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "canvas-mindmap",
"version": "0.1.5",
"version": "0.1.6",
"description": "A plugin to make your canvas work like a mindmap.",
"main": "main.js",
"scripts": {
Expand Down
4 changes: 0 additions & 4 deletions src/canvasMindMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,10 @@ export default class CanvasMindMap extends Plugin {
const canvasView = this.app.workspace.getLeavesOfType("canvas").first()?.view;
// @ts-ignore
const canvas = canvasView?.canvas;
console.log(canvas);
if (!canvasView) return false;

const patchCanvasView = canvas.constructor;

console.log("patchCanvasView", patchCanvasView);

const canvasViewunistaller = around(canvasView.constructor.prototype, {
onOpen: (next) =>
async function () {
Expand Down Expand Up @@ -378,7 +375,6 @@ export default class CanvasMindMap extends Plugin {


const node = await createChildNode(this.canvas);
console.log(this, node);
if (!node) return;

setTimeout(() => {
Expand Down
317 changes: 156 additions & 161 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,193 +2,188 @@ import { Canvas, CanvasEdge, CanvasNode, requireApiVersion, TFile } from "obsidi
import { CanvasData, CanvasEdgeData, CanvasFileData, CanvasNodeData, CanvasTextData } from "obsidian/canvas";

interface edgeT {
fromOrTo: string;
side: string,
node: CanvasNode | CanvasNodeData,
fromOrTo: string;
side: string,
node: CanvasNode | CanvasNodeData,
}

interface TreeNode {
id: string;
children: TreeNode[];
id: string;
children: TreeNode[];
}

export const random = (e: number) => {
let t = [];
for (let n = 0; n < e; n++) {
t.push((16 * Math.random() | 0).toString(16));
}
return t.join("");
let t = [];
for (let n = 0; n < e; n++) {
t.push((16 * Math.random() | 0).toString(16));
}
return t.join("");
};

export const createChildFileNode = (canvas: any, parentNode: any, file: TFile, path: string, y: number) => {
const node = addNode(
canvas, random(16),
{
x: parentNode.x + parentNode.width + 200,
y: y,
width: parentNode.width,
height: parentNode.height * 0.6,

type: 'file',
content: file.path,
subpath: path,
}
);

addEdge(canvas, random(16), {
fromOrTo: "from",
side: "right",
node: parentNode
}, {
fromOrTo: "to",
side: "left",
node: <CanvasNodeData>node
});

canvas.requestSave();

return node;
const node = addNode(
canvas, random(16),
{
x: parentNode.x + parentNode.width + 200,
y: y,
width: parentNode.width,
height: parentNode.height * 0.6,

type: 'file',
content: file.path,
subpath: path,
}
);

addEdge(canvas, random(16), {
fromOrTo: "from",
side: "right",
node: parentNode
}, {
fromOrTo: "to",
side: "left",
node: <CanvasNodeData>node
});

canvas.requestSave();

return node;
};


export const addNode = (canvas: Canvas, id: string, {
x,
y,
width,
height,
type,
content,
subpath,
x,
y,
width,
height,
type,
content,
subpath,
}: {
x: number,
y: number,
width: number,
height: number,
type: 'text' | 'file',
content: string,
subpath?: string,
x: number,
y: number,
width: number,
height: number,
type: 'text' | 'file',
content: string,
subpath?: string,
}) => {
if (!canvas) return;

const data = canvas.getData();
if (!data) return;

const node: Partial<CanvasTextData | CanvasFileData> = {
"id": id,
"x": x,
"y": y,
"width": width,
"height": height,
"type": type,
};

switch (type) {
case 'text':
node.text = content;
break;
case 'file':
node.file = content;
if (subpath) node.subpath = subpath;
break;
}

canvas.importData(<CanvasData>{
"nodes": [
...data.nodes,
node],
"edges": data.edges,
});

canvas.requestFrame();

return node;
if (!canvas) return;

const data = canvas.getData();
if (!data) return;

const node: Partial<CanvasTextData | CanvasFileData> = {
"id": id,
"x": x,
"y": y,
"width": width,
"height": height,
"type": type,
};

switch (type) {
case 'text':
node.text = content;
break;
case 'file':
node.file = content;
if (subpath) node.subpath = subpath;
break;
}

canvas.importData(<CanvasData>{
"nodes": [
...data.nodes,
node],
"edges": data.edges,
});

canvas.requestFrame();

return node;
};

export const addEdge = (canvas: any, edgeID: string, fromEdge: edgeT, toEdge: edgeT) => {
if (!canvas) return;

const data = canvas.getData();
if (!data) return;

canvas.importData({
"edges": [
...data.edges,
{
"id": edgeID,
"fromNode": fromEdge.node.id,
"fromSide": fromEdge.side,
"toNode": toEdge.node.id,
"toSide": toEdge.side
}
],
"nodes": data.nodes,
});

canvas.requestFrame();
if (!canvas) return;

const data = canvas.getData();
if (!data) return;

canvas.importData({
"edges": [
...data.edges,
{
"id": edgeID,
"fromNode": fromEdge.node.id,
"fromSide": fromEdge.side,
"toNode": toEdge.node.id,
"toSide": toEdge.side
}
],
"nodes": data.nodes,
});

canvas.requestFrame();
};

export function buildTrees(canvasData: CanvasData, direction: 'LR' | 'RL' | 'TB' | 'BT'): TreeNode[] {
const trees: TreeNode[] = [];
const nodeMap: Map<string, TreeNode> = new Map();
const edgeMap: Map<string, string[]> = new Map();

canvasData.nodes.forEach(node => {
nodeMap.set(node.id, {
...node,
children: []
});
});

canvasData.edges.forEach(edge => {
if (!edgeMap.has(edge.fromNode)) {
edgeMap.set(edge.fromNode, []);
}
edgeMap.get(edge.fromNode)?.push(edge.toNode);
});

const rootNodes = canvasData.nodes.filter(node =>
!canvasData.edges.some(edge => edge.toNode === node.id)
);

console.log(rootNodes, edgeMap);

rootNodes.forEach(rootNode => {
const tree = buildTree(rootNode.id, edgeMap, nodeMap, direction);
trees.push(tree);
});

return trees;
const trees: TreeNode[] = [];
const nodeMap: Map<string, TreeNode> = new Map();
const edgeMap: Map<string, string[]> = new Map();

canvasData.nodes.forEach(node => {
nodeMap.set(node.id, {
...node,
children: []
});
});

canvasData.edges.forEach(edge => {
if (!edgeMap.has(edge.fromNode)) {
edgeMap.set(edge.fromNode, []);
}
edgeMap.get(edge.fromNode)?.push(edge.toNode);
});

const rootNodes = canvasData.nodes.filter(node =>
!canvasData.edges.some(edge => edge.toNode === node.id)
);

rootNodes.forEach(rootNode => {
const tree = buildTree(rootNode.id, edgeMap, nodeMap, direction);
trees.push(tree);
});

return trees;
}

function buildTree(nodeId: string, edgeMap: Map<string, string[]>, nodeMap: Map<string, TreeNode>, direction: 'LR' | 'RL' | 'TB' | 'BT'): TreeNode {
const node = nodeMap.get(nodeId) as TreeNode;
console.log(nodeId, node, edgeMap.get(nodeId));

edgeMap.get(nodeId)?.forEach(childId => {
if (shouldAddChild(nodeId, childId, direction, nodeMap)) {
node.children.push(buildTree(childId, edgeMap, nodeMap, direction));
}
});
return node;
const node = nodeMap.get(nodeId) as TreeNode;

edgeMap.get(nodeId)?.forEach(childId => {
if (shouldAddChild(nodeId, childId, direction, nodeMap)) {
node.children.push(buildTree(childId, edgeMap, nodeMap, direction));
}
});
return node;
}

function shouldAddChild(parentId: string, childId: string, direction: 'LR' | 'RL' | 'TB' | 'BT', nodeMap: Map<string, TreeNode>): boolean {
const parent = nodeMap.get(parentId) as unknown as CanvasNodeData;
const child = nodeMap.get(childId) as unknown as CanvasNodeData;

console.log(parent, child);

switch (direction) {
case 'LR':
return parent.x < child.x;
case 'RL':
return parent.x > child.x;
case 'TB':
return parent.y < child.y;
case 'BT':
return parent.y > child.y;
default:
return true;
}
const parent = nodeMap.get(parentId) as unknown as CanvasNodeData;
const child = nodeMap.get(childId) as unknown as CanvasNodeData;

switch (direction) {
case 'LR':
return parent.x < child.x;
case 'RL':
return parent.x > child.x;
case 'TB':
return parent.y < child.y;
case 'BT':
return parent.y > child.y;
default:
return true;
}
}

3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"0.1.2": "1.1.0",
"0.1.3": "1.1.0",
"0.1.4": "1.4.0",
"0.1.5": "1.4.0"
"0.1.5": "1.4.0",
"0.1.6": "1.4.0"
}

0 comments on commit 8eb1212

Please sign in to comment.