generated from RyotaUshio/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathindex.ts
355 lines (301 loc) · 14.8 KB
/
index.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import { App, Component, TFile, parseLinktext } from 'obsidian';
import { PDFDocumentProxy } from 'pdfjs-dist';
import PDFPlus from 'main';
import { ColorPalette } from 'color-palette';
import { copyLinkAPI } from './copy-link';
import { HighlightAPI } from './highlights';
import { WorkspaceAPI } from './workspace-api';
import { encodeLinktext, parsePDFSubpath } from 'utils';
import { AnnotationElement, DestArray, EventBus, ObsidianViewer, PDFPageView, PDFViewExtraState, PDFViewerChild, RawPDFViewer } from 'typings';
export class PDFPlusAPI {
app: App;
plugin: PDFPlus
/** Sub-modules */
copyLink: copyLinkAPI;
highlight: HighlightAPI;
workspace: WorkspaceAPI;
constructor(plugin: PDFPlus) {
this.app = plugin.app;
this.plugin = plugin;
this.copyLink = new copyLinkAPI(plugin);
this.highlight = new HighlightAPI(plugin);
this.workspace = new WorkspaceAPI(plugin);
}
/**
* @param component A component such that the callback is unregistered when the component is unloaded, or `null` if the callback should be called only once.
*/
registerPDFEvent(name: string, eventBus: EventBus, component: Component | null, cb: (data: any) => any) {
const listener = async (data: any) => {
cb(data);
if (!component) eventBus.off(name, listener);
};
component?.register(() => eventBus.off(name, listener));
eventBus.on(name, listener);
}
/**
* Register a callback executed when the text layer for a page gets rendered.
* Note that PDF rendering is "lazy"; the text layer for a page is not rendered until the page is scrolled into view.
*
* @param component A component such that the callback is unregistered when the component is unloaded, or `null` if the callback should be called only once.
*/
onTextLayerReady(viewer: ObsidianViewer, component: Component | null, cb: (pageView: PDFPageView, pageNumber: number, newlyRendered: boolean) => any) {
viewer.pdfViewer?._pages
.forEach((pageView, pageIndex) => {
if (pageView.textLayer) {
cb(pageView, pageIndex + 1, false); // page number is 1-based
}
});
this.registerPDFEvent('textlayerrendered', viewer.eventBus, component, (data: { source: PDFPageView, pageNumber: number }) => {
cb(data.source, data.pageNumber, true);
});
}
/**
* Register a callback executed when the annotation layer for a page gets rendered.
*
* @param component A component such that the callback is unregistered when the component is unloaded, or `null` if the callback should be called only once.
*/
onAnnotationLayerReady(viewer: ObsidianViewer, component: Component | null, cb: (pageView: PDFPageView, pageNumber: number, newlyRendered: boolean) => any) {
viewer.pdfViewer?._pages
.forEach((pageView, pageIndex) => {
if (pageView.annotationLayer) {
cb(pageView, pageIndex + 1, false); // page number is 1-based
}
});
this.registerPDFEvent('annotationlayerrendered', viewer.eventBus, component, (data: { source: PDFPageView, pageNumber: number }) => {
cb(data.source, data.pageNumber, true);
});
}
applyPDFViewStateToViewer(pdfViewer: RawPDFViewer, state: PDFViewExtraState) {
const applyState = () => {
if (typeof state.left === 'number' && typeof state.top === 'number' && typeof state.zoom === 'number') {
pdfViewer.scrollPageIntoView({ pageNumber: state.page, destArray: [state.page, { name: 'XYZ' }, state.left, state.top, state.zoom] });
} else {
pdfViewer.currentPageNumber = state.page;
}
};
if (pdfViewer.pagesCount) { // pages are already loaded
applyState();
} else { // pages are not loaded yet (this is the case typically when opening a different file)
this.registerPDFEvent('pagesloaded', pdfViewer.eventBus, null, () => applyState());
}
}
getPageElAssociatedWithNode(node: Node) {
const el = node.instanceOf(HTMLElement) ? node : node.parentElement;
if (!el) return null;
const pageEl = el.closest('.page');
if (!pageEl || !(pageEl.instanceOf(HTMLElement))) return null;
return pageEl;
}
getPageElFromSelection(selection: Selection) {
const range = selection.rangeCount > 0 ? selection.getRangeAt(0) : null;
return range ? this.getPageElAssociatedWithNode(range.startContainer) : null;
}
getPageElFromEvent(event: MouseEvent) {
return event.target instanceof Node
? this.getPageElAssociatedWithNode(event.target)
: null;
}
getPageNumberFromEvent(event: MouseEvent): number | null {
const pageEl = this.getPageElFromEvent(event);
const pageNumber = pageEl?.dataset.pageNumber;
if (pageNumber === undefined) return null;
return +pageNumber;
}
getToolbarAssociatedWithNode(node: Node) {
const el = node.instanceOf(HTMLElement) ? node : node.parentElement;
if (!el) return null;
const containerEl = el.closest('.pdf-container');
const toolbarEl = containerEl?.previousElementSibling;
if (toolbarEl && toolbarEl.hasClass('pdf-toolbar')) {
return toolbarEl;
}
return null;
}
getToolbarAssociatedWithSelection() {
const selection = activeWindow.getSelection();
if (selection && selection.rangeCount > 0) {
const range = selection.getRangeAt(0);
return this.getToolbarAssociatedWithNode(range.startContainer);
}
return null;
}
getColorPaletteAssociatedWithNode(node: Node) {
const toolbarEl = this.getToolbarAssociatedWithNode(node);
if (!toolbarEl) return null;
const paletteEl = toolbarEl.querySelector<HTMLElement>('.' + ColorPalette.CLS)
if (!paletteEl) return null;
return ColorPalette.elInstanceMap.get(paletteEl) ?? null;
}
getColorPaletteAssociatedWithSelection() {
const selection = activeWindow.getSelection();
if (selection && selection.rangeCount > 0) {
const range = selection.getRangeAt(0);
return this.getColorPaletteAssociatedWithNode(range.startContainer);
}
return null;
}
getColorPaletteFromChild(child: PDFViewerChild): ColorPalette | null {
const viewrEl = child.pdfViewer.dom?.viewerEl;
if (viewrEl) return this.getColorPaletteAssociatedWithNode(viewrEl);
return null;
}
getColorPaletteContainedIn(el: HTMLElement) {
for (const [paletteEl, palette] of ColorPalette.elInstanceMap) {
if (el.contains(paletteEl)) return palette;
}
return null;
}
getPDFViewerChildAssociatedWithNode(node: Node) {
for (const [viewerEl, child] of this.plugin.pdfViwerChildren) {
if (viewerEl.contains(node)) return child;
}
}
/**
* Convert a destination name (see the PDF spec (PDF 32000-1:2008), 12.3.2.3 "Named Destinations")
* into a subpath of the form `#page=<pageNumber>&offset=<left>,<top>,<zoom>`.
*
* For how Obsidian handles the "offset" parameter, see the PDFViewerChild.prototype.applySubpath method
* in Obsidian's app.js.
*
* The rule is:
* - `offset` is a comma-separated list of three (or two) numbers, representing the "left", "top", and "zoom" parameters.
* - If "left" is omitted, then only the "top" parameter is used and the destination is treated as "[page /FitBH top]".
* - What is "FitBH"? Well, Table 151 in the PDF spec says:
* > "Display the page designated by page, with the vertical coordinate top positioned at the top edge of
* the window and the contents of the page magnified just enough to fit the entire width of its bounding box
* within the window.
* > A null value for top specifies that the current value of that parameter shall be retained unchanged."
* - Otherwise, the destination is treated as "[page /XYZ left top zoom]".
* - According to the PDF spec, "XYZ" means:
* > "Display the page designated by page, with the coordinates (left, top) positioned at the upper-left corner of
* the window and the contents of the page magnified by the factor zoom.
* > A null value for any of the parameters left, top, or zoom specifies that the current value of that parameter
* shall be retained unchanged. A zoom value of 0 has the same meaning as a null value."
*/
async destIdToSubpath(destId: string, doc: PDFDocumentProxy) {
const dest = await doc.getDestination(destId);
if (!dest) return null;
const page = await doc.getPageIndex(dest[0]);
// @ts-ignore
return this.destArrayToSubpath([page, ...dest.slice(1)]);
}
/**
* page: a 0-based page number
* destType.name: Obsidian only supports "XYZ" and "FitBH"
*/
destArrayToSubpath(destArray: DestArray) {
const pageNumber = destArray[0];
let top = '';
let left = '';
let zoom = '';
if (destArray[1].name === 'XYZ') {
left = '' + destArray[2];
top = '' + destArray[3];
// Obsidian recognizes the `offset` parameter as "FitHB" if the third parameter is omitted.
// from the PDF spec: "A zoom value of 0 has the same meaning as a null value."
zoom = '' + (destArray[4] ?? 0);
} else if (destArray[1].name === 'FitBH') {
top = '' + destArray[2];
}
const subpath = `#page=${pageNumber + 1}&offset=${left},${top},${zoom}`;
return subpath;
}
getAnnotationInfoFromAnnotationElement(annot: AnnotationElement) {
return {
page: annot.parent.page.pageNumber,
id: annot.data.id,
}
}
getAnnotationInfoFromPopupEl(popupEl: HTMLElement) {
if (!popupEl.matches('.popupWrapper[data-annotation-id]')) return null;
const pageEl = popupEl.closest<HTMLElement>('div.page');
if (!pageEl || pageEl.dataset.pageNumber === undefined) return null;
const page = +pageEl.dataset.pageNumber;
const id = popupEl.dataset.annotationId;
if (id === undefined) return null;
return { page, id };
}
registerGlobalDomEvent<K extends keyof DocumentEventMap>(component: Component, type: K, callback: (this: HTMLElement, ev: DocumentEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void {
component.registerDomEvent(document, type, callback, options);
this.app.workspace.onLayoutReady(() => {
// For the currently opened windows
const windows = new Set<Window>();
this.app.workspace.iterateAllLeaves((leaf) => {
const win = leaf.getContainer().win;
if (win !== window) windows.add(win);
});
windows.forEach((window) => {
component.registerDomEvent(window.document, type, callback, options);
});
// For windows opened in the future
component.registerEvent(this.app.workspace.on('window-open', (win, window) => {
component.registerDomEvent(window.document, type, callback, options);
}));
});
}
/**
* The same as app.fileManager.generateMarkdownLink(), but respects the "alias" parameter for non-markdown files as well.
* See https://github.com/obsidianmd/obsidian-api/issues/154
*/
generateMarkdownLink(file: TFile, sourcePath: string, subpath?: string, alias?: string) {
const app = this.app;
const useMarkdownLinks = app.vault.getConfig('useMarkdownLinks');
const useWikilinks = !useMarkdownLinks;
const linkpath = app.metadataCache.fileToLinktext(file, sourcePath, useWikilinks);
let linktext = linkpath + (subpath || '');
if (file.path === sourcePath && subpath) linktext = subpath;
let nonEmbedLink;
if (useMarkdownLinks) {
nonEmbedLink = '['.concat(alias || file.basename, '](').concat(encodeLinktext(linktext), ')');
} else {
if (alias && alias.toLowerCase() === linktext.toLowerCase()) {
linktext = alias;
alias = undefined;
}
nonEmbedLink = alias
? '[['.concat(linktext, '|').concat(alias, ']]')
: '[['.concat(linktext, ']]');
}
return 'md' !== file.extension ? '!' + nonEmbedLink : nonEmbedLink;
}
isBacklinked(file: TFile, subpathParams?: { page: number, selection?: [number, number, number, number], annotation?: string }): boolean {
// validate parameters
if (subpathParams) {
const { page, selection, annotation } = subpathParams;
if (isNaN(page) || page < 1) throw new Error('Invalid page number');
if (selection && (selection.length !== 4 || selection.some((pos) => isNaN(pos)))) throw new Error('Invalid selection');
if (selection && typeof annotation === 'string') throw new Error('Selection and annotation cannot be used together');
}
// query type
const isFileQuery = !subpathParams;
const isPageQuery = subpathParams && !subpathParams.selection && !subpathParams.annotation;
const isSelectionQuery = subpathParams && !!(subpathParams.selection);
const isAnnotationQuery = typeof subpathParams?.annotation === 'string';
const backlinkDict = this.app.metadataCache.getBacklinksForFile(file);
if (isFileQuery) return backlinkDict.count() > 0;
for (const sourcePath of backlinkDict.keys()) {
const backlinks = backlinkDict.get(sourcePath);
if (!backlinks) continue;
for (const backlink of backlinks) {
const { subpath } = parseLinktext(backlink.link);
const result = parsePDFSubpath(subpath);
if (!result) continue;
if (isPageQuery && result.page === subpathParams.page) return true;
if (isSelectionQuery
&& 'beginIndex' in result
&& result.page === subpathParams.page
&& result.beginIndex === subpathParams.selection![0]
&& result.beginOffset === subpathParams.selection![1]
&& result.endIndex === subpathParams.selection![2]
&& result.endOffset === subpathParams.selection![3]
) return true;
if (isAnnotationQuery
&& 'annotation' in result
&& result.page === subpathParams.page
&& result.annotation === subpathParams.annotation
) return true;
}
}
return false;
}
}