-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwikilinkDragHandler.ts
More file actions
170 lines (149 loc) · 4.93 KB
/
wikilinkDragHandler.ts
File metadata and controls
170 lines (149 loc) · 4.93 KB
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
import {
type PluginValue,
ViewPlugin,
type ViewUpdate,
WidgetType,
Decoration,
type DecorationSet,
EditorView,
} from "@codemirror/view";
import { TFile } from "obsidian";
import type DiscourseGraphPlugin from "~/index";
const buildObsidianUrl = (vaultName: string, filePath: string): string => {
return `obsidian://open?vault=${encodeURIComponent(vaultName)}&file=${encodeURIComponent(filePath)}`;
};
const resolveFileFromLinkText = (
linkText: string,
plugin: DiscourseGraphPlugin,
): TFile | null => {
const activeFile = plugin.app.workspace.getActiveFile();
if (!activeFile) return null;
const resolved = plugin.app.metadataCache.getFirstLinkpathDest(
linkText,
activeFile.path,
);
return resolved instanceof TFile ? resolved : null;
};
const setDragData = (
e: DragEvent,
file: TFile,
plugin: DiscourseGraphPlugin,
): void => {
const vaultName = plugin.app.vault.getName();
const url = buildObsidianUrl(vaultName, file.path);
e.dataTransfer?.setData("text/uri-list", url);
e.dataTransfer?.setData("text/plain", url);
};
// --- Live Preview ---
/**
* Extract the file path from a link match.
* Handles wikilinks (`[[path]]`, `[[path|alias]]`) and
* markdown links (`[text](path.md)`), decoding URL-encoded paths.
*/
const extractLinkPath = (match: string): string => {
// Wikilink: [[path]] or [[path|alias]]
if (match.startsWith("[[")) {
const inner = match.slice(2, -2);
const pipeIndex = inner.indexOf("|");
return pipeIndex >= 0 ? inner.slice(0, pipeIndex) : inner;
}
// Markdown link: [text](path)
const parenOpen = match.lastIndexOf("(");
const rawPath = match.slice(parenOpen + 1, -1);
try {
return decodeURIComponent(rawPath);
} catch (error) {
return rawPath;
}
};
/**
* Widget that renders a small drag handle next to an internal link.
* CM6 widgets get `ignoreEvent() → true` by default, which means
* the editor completely ignores mouse events on them — native drag works.
*/
class WikilinkDragHandleWidget extends WidgetType {
constructor(
private linkPath: string,
private plugin: DiscourseGraphPlugin,
) {
super();
}
eq(other: WikilinkDragHandleWidget): boolean {
return this.linkPath === other.linkPath;
}
toDOM(): HTMLElement {
const handle = document.createElement("span");
handle.className =
"inline-block cursor-grab opacity-30 text-[10px] text-[var(--text-muted)] align-middle ml-0.5 transition-opacity duration-150 ease-in-out select-none";
handle.draggable = true;
handle.setAttribute("aria-label", "Drag to canvas");
handle.textContent = "⠿";
handle.addEventListener("mouseenter", () => {
handle.style.opacity = "1";
});
handle.addEventListener("mouseleave", () => {
handle.style.opacity = "";
});
handle.addEventListener("dragstart", (e) => {
const file = resolveFileFromLinkText(this.linkPath, this.plugin);
if (!file) {
e.preventDefault();
return;
}
setDragData(e, file, this.plugin);
});
return handle;
}
}
// Matches wikilinks [[...]] and markdown links [text](path.md).
// Embed exclusion (![[...]] and ) is handled in the loop.
const INTERNAL_LINK_RE = /\[\[([^\]]+)\]\]|\[([^\]]+)\]\(([^)]+\.md)\)/g;
const buildWidgetDecorations = (
view: EditorView,
plugin: DiscourseGraphPlugin,
): DecorationSet => {
const widgets = [];
for (const { from, to } of view.visibleRanges) {
const text = view.state.doc.sliceString(from, to);
let match: RegExpExecArray | null;
INTERNAL_LINK_RE.lastIndex = 0;
while ((match = INTERNAL_LINK_RE.exec(text)) !== null) {
let isEmbed = false;
if (match.index > 0) isEmbed = text[match.index - 1] === "!";
isEmbed = view.state.doc.sliceString(from - 1, from) === "!";
if (isEmbed) continue;
const matchEnd = from + match.index + match[0].length;
const linkPath = extractLinkPath(match[0]);
const widget = new WikilinkDragHandleWidget(linkPath, plugin);
widgets.push(Decoration.widget({ widget, side: 1 }).range(matchEnd));
}
}
// Decorations must be sorted by position
widgets.sort((a, b) => a.from - b.from);
return Decoration.set(widgets);
};
/**
* CM6 ViewPlugin that adds a draggable grip icon after each internal link
* in Live Preview. Matches both wikilinks (`[[...]]`) and markdown links
* (`[text](path.md)`), inserting a widget decoration after each match.
*/
export const createWikilinkDragExtension = (
plugin: DiscourseGraphPlugin,
): ViewPlugin<PluginValue> => {
return ViewPlugin.fromClass(
class {
decorations: DecorationSet;
constructor(view: EditorView) {
this.decorations = buildWidgetDecorations(view, plugin);
}
update(update: ViewUpdate): void {
if (update.docChanged || update.viewportChanged) {
this.decorations = buildWidgetDecorations(update.view, plugin);
}
}
},
{
decorations: (v) => v.decorations,
},
);
};