generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
164 lines (134 loc) · 4.37 KB
/
main.ts
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
import { ItemView, App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
export default class OptimizeCanvasConnectionsPlugin extends Plugin {
async onload() {
this.addCommand({
id: 'optimize-preserve-axes-selection',
name: 'Optimize selection (preserve axes)',
checkCallback: (checking: boolean) => {
const canvasView = app.workspace.getActiveViewOfType(ItemView);
if (canvasView?.getViewType() == "canvas") {
if (!checking) {
this.optimize('preserve-axes');
}
return true;
}
return false;
}
});
this.addCommand({
id: 'optimize-shortest-path-selection',
name: 'Optimize selection (shortest path)',
checkCallback: (checking: boolean) => {
const canvasView = app.workspace.getActiveViewOfType(ItemView);
if (canvasView?.getViewType() == "canvas") {
if (!checking) {
this.optimize('shortest-path');
}
return true;
}
return false;
}
});
}
onunload() {
}
async optimize(option: string) {
const canvasView = app.workspace.getActiveViewOfType(ItemView);
// @ts-ignore
const canvas = canvasView?.canvas;
// Read current selection
const currentSelection = canvas?.selection;
let selectedIDs = new Array();
// @ts-ignore
currentSelection.forEach(function(selection) {
selectedIDs.push(selection.id);
})
let applyToAll = false;
if (selectedIDs.length == 0) {
applyToAll = true;
}
// Go through every edge
for (let [edgeKey, edge] of canvas['edges']) {
// Find from and to nodes
let fromNode = edge['from']['node'];
let toNode = edge['to']['node'];
// Calculate the many possibilities of distance
let fromPossibilities = [edge['from']['side']]; // default = no change
if (applyToAll || selectedIDs.includes(fromNode['id'])){
switch(option) {
case "shortest-path":
fromPossibilities = ['top', 'bottom', 'left', 'right'];
break
case "preserve-axes":
switch(edge['from']['side']) {
case 'top':
case 'bottom':
fromPossibilities = ['top', 'bottom'];
break;
case 'left':
case 'right':
fromPossibilities = ['left', 'right'];
break;
}
}
}
let toPossibilities = [edge['to']['side']]; // default = no change
if (applyToAll || selectedIDs.includes(toNode['id'])){
switch(option) {
case "shortest-path":
toPossibilities = ['top', 'bottom', 'left', 'right'];
break
case "preserve-axes":
switch(edge['to']['side']) {
case 'top':
case 'bottom':
toPossibilities = ['top', 'bottom'];
break;
case 'left':
case 'right':
toPossibilities = ['left', 'right'];
break;
}
}
}
let distances = []
for (const fromSide of fromPossibilities) {
let fromPoint = {'x': 0, 'y': 0};
if (fromSide == 'top') {
fromPoint = {'x': fromNode['x'] + fromNode['width']/2, 'y': fromNode['y']};
} else if (fromSide == 'bottom') {
fromPoint = {'x': fromNode['x'] + fromNode['width']/2, 'y': fromNode['y'] + fromNode['height']};
} else if (fromSide == 'left') {
fromPoint = {'x': fromNode['x'], 'y': fromNode['y'] + fromNode['height']/2};
} else if (fromSide == 'right') {
fromPoint = {'x': fromNode['x'] + fromNode['width'], 'y': fromNode['y'] + fromNode['height']/2};
}
for (const toSide of toPossibilities) {
let toPoint = {'x': 0, 'y': 0};
if (toSide == 'top') {
toPoint = {'x': toNode['x'] + toNode['width']/2, 'y': toNode['y']};
} else if (toSide == 'bottom') {
toPoint = {'x': toNode['x'] + toNode['width']/2, 'y': toNode['y'] + toNode['height']};
} else if (toSide == 'left') {
toPoint = {'x': toNode['x'], 'y': toNode['y'] + toNode['height']/2};
} else if (toSide == 'right') {
toPoint = {'x': toNode['x'] + toNode['width'], 'y': toNode['y'] + toNode['height']/2};
}
distances.push({
'fromSide': fromSide,
'toSide': toSide,
'distance': (toPoint.x - fromPoint.x) ** 2 + (toPoint.y - fromPoint.y) ** 2});
}
}
// Find the best one
distances = distances.sort(function(a, b) {
return a.distance - b.distance;
});
// Assign it
edge['from']['side'] = distances[0]['fromSide'];
edge['to']['side'] = distances[0]['toSide'];
edge.render();
}
canvas.requestSave();
}
}