From 8eb1212bbe2ec80329b4fb27cce0ea517ae8edef Mon Sep 17 00:00:00 2001 From: Quorafind Date: Thu, 18 Jan 2024 22:39:08 +0800 Subject: [PATCH] chore: bump version --- manifest.json | 2 +- package.json | 2 +- src/canvasMindMap.ts | 4 - src/utils.ts | 317 +++++++++++++++++++++---------------------- versions.json | 3 +- 5 files changed, 160 insertions(+), 168 deletions(-) diff --git a/manifest.json b/manifest.json index bab5ce8..348a750 100644 --- a/manifest.json +++ b/manifest.json @@ -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 -} +} \ No newline at end of file diff --git a/package.json b/package.json index 159ef2f..a4a596f 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/canvasMindMap.ts b/src/canvasMindMap.ts index 8988750..e02c0cd 100644 --- a/src/canvasMindMap.ts +++ b/src/canvasMindMap.ts @@ -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 () { @@ -378,7 +375,6 @@ export default class CanvasMindMap extends Plugin { const node = await createChildNode(this.canvas); - console.log(this, node); if (!node) return; setTimeout(() => { diff --git a/src/utils.ts b/src/utils.ts index 31bd9fd..bb6db04 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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: 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: 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 = { - "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({ - "nodes": [ - ...data.nodes, - node], - "edges": data.edges, - }); - - canvas.requestFrame(); - - return node; + if (!canvas) return; + + const data = canvas.getData(); + if (!data) return; + + const node: Partial = { + "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({ + "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 = new Map(); - const edgeMap: Map = 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 = new Map(); + const edgeMap: Map = 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, nodeMap: Map, 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): 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; + } } diff --git a/versions.json b/versions.json index dd0b588..aad7be5 100644 --- a/versions.json +++ b/versions.json @@ -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" } \ No newline at end of file