Skip to content

Commit b4a9b7a

Browse files
committed
feat: add type definition for PluginManager
1 parent 593777e commit b4a9b7a

1 file changed

Lines changed: 117 additions & 5 deletions

File tree

src/types.d.ts

Lines changed: 117 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,25 @@ declare module 'obsidian' {
99
interface App {
1010
embedRegistry: EmbedRegistry;
1111
internalPlugins: InternalPluginManager;
12+
plugins: PluginManager;
1213
}
1314

15+
/**
16+
* Bounding box interface.
17+
*/
1418
interface CanvasBBox {
1519
maxX: number;
1620
maxY: number;
1721
minX: number;
1822
minY: number;
1923
}
2024

21-
/** @typeonly */
25+
/**
26+
* Underlying layer of canvas that displays nodes and edges, and
27+
* interacts directly with users.
28+
*
29+
* @typeonly
30+
*/
2231
class CanvasEditor {
2332
canvasControlsEl: HTMLElement;
2433
canvasEl: HTMLElement;
@@ -27,25 +36,66 @@ declare module 'obsidian' {
2736
quickSettingsButton: HTMLElement;
2837
undoBtnEl: HTMLElement;
2938
view: CanvasEditorOwner;
39+
/**
40+
* Wraps `canvasEl`.
41+
*/
3042
wrapperEl: HTMLElement;
3143
zoomToFitQueued: boolean;
3244
constructor(view: CanvasEditorOwner);
45+
/**
46+
* Clear `CanvasEditor` from its content without unregister any of global
47+
* event handlers.
48+
*
49+
* Use `unload()` to completely unload the canvas.
50+
*/
3351
clear(): void;
3452
createPlaceholder(): void;
53+
/**
54+
* Initialize `CanvasEditor`.
55+
*/
3556
load(): void;
57+
/**
58+
* Indicate that the canvas' viewport is changed and update its display
59+
* afterwards.
60+
*/
3661
markViewportChanged(): void;
62+
/**
63+
* Run when the canvas is being resized.
64+
*/
3765
onResize(): void;
66+
/**
67+
* Run when the canvas is being scrolled.
68+
*/
3869
onWheel(evt: WheelEvent): void;
70+
/**
71+
* Enqueue canvas display update.
72+
*/
3973
requestFrame(timestamp?: number): void;
74+
/**
75+
* Serialize `CanvasData` into nodes and edges.
76+
*/
4077
setData(data: CanvasData): void;
78+
/**
79+
* Toggle read-only state.
80+
*/
4181
setReadonly(readonly: boolean): void;
82+
/**
83+
* Clear `CanvasEditor` from its content and unregister global event
84+
* handlers.
85+
*/
4286
unload(): void;
4387
}
4488

4589
interface CanvasEditorOwner {
4690
app: App;
4791
canvas: CanvasEditor;
92+
/**
93+
* Element that contains `CanvasEditor`.
94+
*/
4895
contentEl: HTMLElement;
96+
/**
97+
* Should be a canvas file.
98+
*/
4999
file: TFile | null;
50100
plugin: CanvasPluginInstance;
51101
requestSave(): void;
@@ -80,6 +130,10 @@ declare module 'obsidian' {
80130
}
81131

82132
interface EmbedComponent extends Component {
133+
/**
134+
* Run once before attaching this to the DOM. You should wrap your code
135+
* that loads file content here.
136+
*/
83137
loadFile(): void;
84138
}
85139

@@ -94,19 +148,51 @@ declare module 'obsidian' {
94148
state?: unknown;
95149
}
96150

97-
type EmbedCreator = (context: EmbedContext, file: TFile, subpath?: string) => EmbedComponent;
151+
/**
152+
* Function that returns an `EmbedComponent`.
153+
*/
154+
type EmbedCreator = (
155+
context: EmbedContext,
156+
file: TFile,
157+
subpath?: string
158+
) => EmbedComponent;
98159

99-
/** @typeonly */
160+
/**
161+
* Manages embeds registered under file extensions.
162+
*
163+
* @typeonly
164+
*/
100165
class EmbedRegistry extends Events {
101166
embedByExtension: Record<string, EmbedCreator>;
167+
/**
168+
* Register an `EmbedCreator` under file extension. Use this to customize
169+
* embed for certain file.
170+
*
171+
* @param ext File extension.
172+
* @param creator `EmbedCreator` implementation.
173+
*
174+
* @throws Throws error if another `EmbedCreator` is already registered
175+
* under the same extension.
176+
*/
102177
registerExtension(ext: string, creator: EmbedCreator): void;
103178
unregisterExtension(ext: string): void;
104179
}
105180

106-
/** @typeonly */
181+
/**
182+
* Wraps `InternalPluginInstance` instance.
183+
*
184+
* @typeonly
185+
*/
107186
class InternalPlugin<T extends InternalPluginIDs> extends Component {
187+
/**
188+
* Indicates whether it is enabled.
189+
*/
108190
enabled: boolean;
109191
instance: InternalPluginInstanceMap[T];
192+
/**
193+
* Views that belong to this plugin. Each view creator is mapped onto
194+
* view type.
195+
*/
110196
views: {
111197
[V in InternalPluginViewTypes<T>]: TypedViewCreator<ViewTypeMap[V]>;
112198
};
@@ -115,16 +201,30 @@ declare module 'obsidian' {
115201
type InternalPluginIDs = keyof InternalPluginInstanceMap;
116202

117203
interface InternalPluginInstance {
204+
/**
205+
* Unique id of the plugin.
206+
*/
118207
id: string;
119208
}
120209

121210
interface InternalPluginInstanceMap {
122211
'canvas': CanvasPluginInstance;
123212
}
124213

125-
/** @typeonly */
214+
/**
215+
* Manages the lifecycle of internal plugins (core plugins).
216+
*
217+
* @typeonly
218+
*/
126219
class InternalPluginManager extends Events {
220+
/**
221+
* Get internal plugin by its id, regardless of whether it is enabled or
222+
* not.
223+
*/
127224
getPluginById<T extends InternalPluginIDs>(id: T): InternalPlugin<T>;
225+
/**
226+
* Triggered when an internal plugin has been enabled or disabled.
227+
*/
128228
on(name: 'change', callback: (plugin: InternalPlugin<InternalPluginIDs>) => unknown, ctx?: any): EventRef;
129229
}
130230

@@ -134,6 +234,15 @@ declare module 'obsidian' {
134234
'canvas': 'canvas';
135235
}
136236

237+
/**
238+
* Manages the lifecycle of community plugins.
239+
*
240+
* @typeonly
241+
*/
242+
class PluginManager {
243+
isEnabled(id: string): boolean;
244+
}
245+
137246
type TypedViewCreator<T extends View> = (leaf: WorkspaceLeaf) => T;
138247

139248
interface ViewTypeMap {
@@ -142,6 +251,9 @@ declare module 'obsidian' {
142251
}
143252

144253
interface WorkspaceLeaf {
254+
/**
255+
* Reload contained view.
256+
*/
145257
rebuildView(): Promise<void>;
146258
}
147259
}

0 commit comments

Comments
 (0)