-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
sourceViewObserver.tsx
197 lines (175 loc) · 8.06 KB
/
sourceViewObserver.tsx
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import {
MarkdownView,
TFile,
parseLinktext
} from 'obsidian';
import AnnotatorPlugin from 'main';
export default class SourceViewObserver {
private plugin: AnnotatorPlugin;
private tmpLinkInfos: {linkText: string; count: number}[] = [];
private tmpTargetIndex = -1;
private targetClassNameSet: Set<string> = new Set();
private observer: MutationObserver;
constructor(plugin: AnnotatorPlugin) {
this.plugin = plugin;
this.initClassNameSet();
this.initObserver();
}
initClassNameSet() {
const prefixObservedClassName = [
'cm-highlight ', 'cm-em ',
'cm-header cm-header-1 ', 'cm-header cm-header-2 ', 'cm-header cm-header-3 ',
'cm-header cm-header-4 ', 'cm-header cm-header-5 ', 'cm-header cm-header-6 '
];
const suffixObservedClassName = [
'',
' cm-list-1', ' cm-list-2', ' cm-list-3',
' cm-quote cm-quote-1', ' cm-strong'
];
const baseTargetClassName = 'cm-hmd-internal-link cm-link-alias';
for (const className of prefixObservedClassName) {
this.targetClassNameSet.add(className + baseTargetClassName);
}
for (const className of suffixObservedClassName) {
this.targetClassNameSet.add(baseTargetClassName + className);
}
}
initObserver() {
const activeLeaf = this.plugin.app.workspace.getActiveViewOfType(MarkdownView);
if (!activeLeaf) return;
const that = this;
const observedClassName = 'cm-hmd-internal-link cm-link-has-alias';
const targetClassName = 'cm-hmd-internal-link cm-link-alias';
const filePath = this.plugin.app.workspace.getActiveFile().path;
const observeCallback = function(mutations: MutationRecord[]) {
that.plugin.log('-----------------------------');
for (const mutation of mutations) {
if (mutation.type == 'childList') {
for (const addedNode of mutation.addedNodes) {
const addedElement = addedNode as Element;
if (addedElement.className && addedElement.className.indexOf(observedClassName) >= 0) {
that.linkOnFocus(addedElement);
}
}
for (const removedNode of mutation.removedNodes) {
const removedElement = removedNode as Element;
if (removedElement.className && removedElement.className.indexOf(observedClassName) >= 0) {
const removedLinkText = removedElement.textContent;
that.plugin.log('remove');
that.linkOnBlur(mutation.target as Element, removedLinkText, filePath, targetClassName);
}
}
} else if (mutation.type == 'attributes') {
if (mutation.oldValue && mutation.oldValue.indexOf(observedClassName) >= 0) {
that.plugin.log('attri');
that.linkOnBlur(mutation.target.parentNode as Element, '', filePath, targetClassName);
}
}
}
}
this.observer = new MutationObserver(observeCallback);
}
getObserver(): MutationObserver {
return this.observer;
}
setObserver(observer: MutationObserver) {
this.observer = observer;
}
watch() {
const activeLeaf = this.plugin.app.workspace.getActiveViewOfType(MarkdownView);
if (!activeLeaf) return;
const regex2Find = /(?<=[\[]{2})[^\]]*(?=[\]]{2})/gm;
const filePath = this.plugin.app.workspace.getActiveFile().path;
const view = activeLeaf.leaf.view.containerEl;
const editor = activeLeaf.editor;
const oldText = editor.getValue();
const linkHref = oldText.match(regex2Find);
if(!linkHref || linkHref.length == 0) return;
const tempSourceLinks = view.getElementsByClassName('cm-hmd-internal-link cm-link-alias') as HTMLCollectionOf<HTMLAnchorElement>;
const length = tempSourceLinks.length;
const sourceLinks = [];
if (length > 1) {
let prev = tempSourceLinks[0];
for (let i = 1; i < length; i++) {
if (tempSourceLinks[i - 1].nextElementSibling != tempSourceLinks[i]) {
sourceLinks.push(prev);
prev = tempSourceLinks[i];
if (i == length - 1) {
sourceLinks.push(tempSourceLinks[i]);
}
} else if (i == length - 1) {
sourceLinks.push(prev);
}
}
} else {
sourceLinks.push(tempSourceLinks[0]);
}
const observeConfig = {
attributeFilter: ['class'],
attributes: true,
characterData: true,
characterDataOldValue: true,
childList: true,
attributeOldValue: true,
subtree: true
};
for (let i = 0; i < sourceLinks.length; i++) {
const tempLink = linkHref[i];
if (typeof tempLink != 'string') continue;
const parsedLink = parseLinktext(tempLink.split('|')[0]);
const annotationid = parsedLink.subpath.startsWith('#^') ? parsedLink.subpath.substring(2) : null;
const file: TFile | null = this.plugin.app.metadataCache.getFirstLinkpathDest(parsedLink.path, filePath);
if (this.plugin.isAnnotationFile(file)) {
this.addClickListener(sourceLinks[i], annotationid, file, false);
this.observer.observe(sourceLinks[i].parentNode, observeConfig);
}
}
}
addClickListener(element: HTMLAnchorElement, annotationid: string, file: TFile, isReadingView: boolean) {
const childs = element.children;
if (isReadingView || childs && childs.length == 1) {
element.addEventListener('click', ev => {
this.plugin.log(annotationid);
ev.preventDefault();
ev.stopPropagation();
ev.stopImmediatePropagation();
const inNewPane = ev.metaKey || ev.ctrlKey || ev.button == 1;
this.plugin.openAnnotationTarget(file, inNewPane, annotationid);
});
}
}
linkOnFocus(element: Element) {
let count = 0;
const linkText = element.textContent;
while(element.className && element.className.indexOf('cm-formatting-link cm-formatting-link-end') == -1) {
if (this.targetClassNameSet.has(element.className)) count++;
element = element.nextElementSibling;
}
this.tmpLinkInfos.push({linkText: linkText, count: count});
}
linkOnBlur(node: Element, rawLinkText: string, filePath: string, className: string) {
const linkIndex = this.tmpTargetIndex + 1;
if (this.tmpLinkInfos.length == 0) return;
const linkInfo = this.tmpLinkInfos[0];
const link = parseLinktext(rawLinkText == '' ? linkInfo.linkText : rawLinkText);
const annotationid = link.subpath.startsWith('#^') ? link.subpath.substring(2) : null;
const file: TFile | null = this.plugin.app.metadataCache.getFirstLinkpathDest(link.path, filePath);
const targets = node.getElementsByClassName(className) as HTMLCollectionOf<HTMLAnchorElement>;
const uniqueTarget = targets[linkIndex];
this.plugin.log(
'linkText: ' + link.path +
' tarIndex: ' + this.tmpTargetIndex +
' linkIndex: ' + linkIndex +
' size: ' + this.tmpLinkInfos.length
);
this.plugin.log(uniqueTarget);
this.tmpTargetIndex += linkInfo.count;
this.addClickListener(uniqueTarget, annotationid, file, false);
this.tmpLinkInfos.splice(0, 1);
if (this.tmpLinkInfos.length == 0) this.resetTmpLinkInfo();
}
resetTmpLinkInfo() {
this.tmpTargetIndex = -1;
this.tmpLinkInfos.length = 0;
}
}