blob: 56021e7f01c4cad5bab5da0685f6c4a8042afc88 (
plain)
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
|
/* eslint-env browser */
export const info = {
id: `draggedLinkInfo`,
state: {
latestDraggedLink: null,
observedLinks: new WeakSet(),
},
};
export function getPageReferences() {
// First start handling all the links that currently exist.
for (const a of document.getElementsByTagName('a')) {
observeLink(a);
addDragListener(a);
}
// Then add a mutation observer to track new links.
const observer = new MutationObserver(records => {
for (const record of records) {
for (const node of record.addedNodes) {
if (node.nodeName !== 'A') continue;
observeLink(node);
}
}
});
observer.observe(document.body, {
subtree: true,
childList: true,
});
}
export function getLatestDraggedLink() {
const {state} = info;
if (state.latestDraggedLink) {
return state.latestDraggedLink.deref() ?? null;
} else {
return null;
}
}
function observeLink(link) {
const {state} = info;
if (state.observedLinks.has(link)) return;
state.observedLinks.add(link);
addDragListener(link);
}
function addDragListener(link) {
const {state} = info;
link.addEventListener('dragstart', _domEvent => {
state.latestDraggedLink = new WeakRef(link);
});
}
|