obsidian-typings 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/obsidian-ex.d.ts CHANGED
@@ -1125,7 +1125,7 @@ interface PluginManifest {
1125
1125
  /**
1126
1126
  * Storage location of the plugin relative to the vault root
1127
1127
  */
1128
- dir: string;
1128
+ dir?: string;
1129
1129
  /**
1130
1130
  * URL for funding the author
1131
1131
  */
@@ -1137,7 +1137,7 @@ interface PluginManifest {
1137
1137
  /**
1138
1138
  * Whether the plugin is designed for desktop use only
1139
1139
  */
1140
- isDesktopOnly: boolean;
1140
+ isDesktopOnly?: boolean;
1141
1141
  /**
1142
1142
  * Minimum Obsidian version compatible with the plugin
1143
1143
  */
@@ -2714,7 +2714,7 @@ declare module 'obsidian' {
2714
2714
  /**
2715
2715
  * @internal Add a section to the menu
2716
2716
  */
2717
- addSections(items: string[]): Menu;
2717
+ addSections(items: string[]): this;
2718
2718
  /**
2719
2719
  * @internal Close the currently open submenu
2720
2720
  */
@@ -2777,13 +2777,13 @@ declare module 'obsidian' {
2777
2777
  * @internal Set the parent element of the menu (i.e. for workspace leaf context menu)
2778
2778
  * @param el - Element to set as parent
2779
2779
  */
2780
- setParentElement(el: HTMLElement): Menu;
2780
+ setParentElement(el: HTMLElement): this;
2781
2781
  /**
2782
2782
  * @internal Add a section to the submenu config
2783
2783
  * @param section
2784
2784
  * @param submenu
2785
2785
  */
2786
- setSectionSubmenu(section: string, submenu: {title: string, icon: string}): Menu;
2786
+ setSectionSubmenu(section: string, submenu: {title: string, icon: string}): this;
2787
2787
  /**
2788
2788
  * @internal Sort the items in the menu
2789
2789
  */
@@ -2850,7 +2850,7 @@ declare module 'obsidian' {
2850
2850
  * @internal Calls `setChecked`, prefer usage of that function instead
2851
2851
  * @param active - Whether the menu item should be checked
2852
2852
  */
2853
- setActive(active: boolean): void;
2853
+ setActive(active: boolean): this;
2854
2854
  /**
2855
2855
  * Create a submenu on the menu item
2856
2856
  * @tutorial Creates the foldable menus with more options as seen when you right-click in the editor (e.g. "Insert", "Format", ...)
@@ -2860,7 +2860,7 @@ declare module 'obsidian' {
2860
2860
  * @internal Add warning styling to the menu item
2861
2861
  * @param warning - Whether the menu item should be styled as a warning
2862
2862
  */
2863
- setWarning(warning: boolean): void;
2863
+ setWarning(warning: boolean): this;
2864
2864
  }
2865
2865
 
2866
2866
  interface MetadataCache {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "obsidian-typings",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Extended type definitions for the Obsidian API (https://obsidian.md)",
5
5
  "main": "",
6
6
  "types": "obsidian-ex.d.ts",
package/CHANGELOG.md DELETED
@@ -1,30 +0,0 @@
1
- # Changelog
2
-
3
- ## v1.0.4
4
- - Added missing EventRef types
5
- - **View Registry:** `view-registered`, `view-unregistered`, `extensions-updated`
6
- - **Metadata Cache:** `initialized`, `finished`
7
- - **Workspace:** `hover-link`, `tab-group-menu`, `swipe`, `layout-ready`, `url-menu`, `search:results-menu`, `receive-text-menu`,
8
- `receive-files-menu`, `canvas:selection-menu`, `canvas:node-menu`, `canvas:node-connection-drop-menu`, `canvas:edge-menu`
9
- - **Workspace Leaf:** `history-change`, `leaf-menu`
10
- - Added missing typings for `Menu` and `MenuItem` classes
11
- - Added missing typings for `Tree` and `TreeItem` classes (used in `Outline` and `FileExplorer` plugin views)
12
- - Set vertical height for a split leaf item via `setDimension`
13
- - Bumped `electron-types` version
14
-
15
- ## v1.0.3
16
- - Change `electron-types` types inclusion
17
- - Added note in `README.md` on automatically including new types
18
- - Fix lambda definitions and tsc compilatiion
19
-
20
- ## v1.0.2
21
- - Changed `any` type to `unknown` to enforcing explicit type casting
22
- - Fixed `CONTRIBUTING.md` link in `README.md`
23
- - Added badges to `README.md`
24
-
25
- ## v1.0.1
26
- - Automated npm publishing
27
-
28
- ## v1.0.0 (initial npm release)
29
- - Made package installable via npm
30
- - Clarified README, added contribution guidelines
package/CONTRIBUTING.md DELETED
@@ -1,235 +0,0 @@
1
- # Introduction
2
-
3
- ## General Notes
4
- Feel free to start typing any part of the Obsidian API that is not yet typed, or fixing/adding additional descriptions to existing typings.
5
- If you are unsure about anything, don't hesitate to open an issue.
6
-
7
- ### TSDoc
8
- Please use [TSDoc](https://tsdoc.org/) to document the typings. This will allow the documentation to be automatically generated
9
- within your IDE, and can help with the discoverability of the typings.
10
-
11
-
12
- Example:
13
- ```ts
14
- interface someObject {
15
- /**
16
- * Does something specific
17
- * @param val1 Enables some functionality
18
- * @tutorial Useful for implementing some features for your plugin
19
- * @remark Prefer using `someOtherMethod` instead
20
- */
21
- someMethod(val1: boolean): number;
22
-
23
- /**
24
- * Does something that you're not certain about, and should probably not be used
25
- * @internal
26
- */
27
- someInternalMethod(): void;
28
- }
29
- ```
30
-
31
- > [!NOTE]
32
- > `@internal` --- Method is *very likely* not fully/correctly typed, and/or is not intended to be used by plugins.
33
- >
34
- > `@remark` --- Alternatives that should be used instead, or warnings about the function.
35
- >
36
- > `@tutorial` --- Short description on how the function could be used in your plugin.
37
- >
38
- > `@deprecated` --- Method is deprecated in a particular version (can't be found in `app.js` anymore).
39
-
40
-
41
- # Tutorial
42
-
43
- ## Adding new typings
44
-
45
- ### Finding/Discovering new typings
46
-
47
- The first step to add new typings, is finding the object/interface/module you want to add typings for within the Obsidian app.
48
- One of the easiest way to do this, is to open the developer console (Ctrl+Shift+I), and start searching for the interface you wish to
49
- type from the `app.` object.
50
-
51
- If you want to, for example, add typings for the `InternalPlugins` object, you can type `app.internalPlugins` into the console,
52
- which will produce the following output:
53
-
54
- ![images/prototype-reference.png](images/prototype-reference.png)
55
-
56
- Note how the internalPlugins object contains multiple prototypes: the `InternalPlugins` interface itself, but also
57
- the `Events` class and then the `Object` literal (does not need to be typed). This can be determined this by looking at the other definitions in the (un)official API,
58
- and checking whether the two objects define the same properties.
59
-
60
- Thus, to define `InternalPlugin`, you can start by adding each of the methods and properties to a `InternalPlugins` interface:
61
-
62
- ```ts
63
- interface InternalPlugins extends Events {
64
- // Variables
65
- app: App;
66
- config: Record<unknown, unknown>;
67
- migration: unknown;
68
- plugins: Record<unknown, unknown>;
69
-
70
- // Methods
71
- requestSaveConfig(): unknown;
72
- enable(): unknown;
73
- getEnabledPluginById(var1: unknown): unknown;
74
- getEnabledPlugins(): unknown;
75
- // ...
76
- }
77
- ```
78
- To keep it simple, we first add `unknown` as type to every variable and method -- unless it is obvious what the type should be
79
- (i.e. `app` will always be an instance of `App`).
80
-
81
- Next up, is the most tedious part of adding typings: finding the correct types for each of the methods and variables.
82
-
83
- ### Typing variables
84
-
85
- The easiest way to start, is by tackling the variables first. For example, in the console output, you can see that `config`
86
- is mapping a string to `true`. In this case, it would be safe to assume that the type of `config` is `Record<string, boolean>`.
87
-
88
- ```ts
89
- interface InternalPlugins extends Events {
90
- // ...
91
- config: Record<string, boolean>;
92
- // ...
93
- }
94
- ```
95
-
96
- However, you can take this one step further still by considering the fact that each string is essentialy a different plugin ID.
97
- In this case, we might just define a new type that contains all the plugin IDs, and use this as the key for the `config` object:
98
-
99
- ```ts
100
- type InternalPluginName = 'audio-recorder' | 'backlink' | 'bookmarks' | 'canvas' /*| ... */;
101
- interface InternalPlugins extends Events {
102
- // ...
103
- config: Record<InternalPluginName, boolean>;
104
- // ...
105
- }
106
- ```
107
-
108
- Similarly, you can define the `plugins` variable as `Record<InternalPluginName, unknown>`.
109
-
110
- Further, it is safe to assume that each element in `Plugins` would be some instance of a `InternalPlugin` class, so we
111
- will also need to add a new interface for this:
112
-
113
- ```ts
114
- interface InternalPlugin {
115
- app: App;
116
- commands: unknown;
117
- // etc. (repeat the same procedure)
118
- }
119
-
120
- interface InternalPlugins extends Events {
121
- // ...
122
- plugins: Record<InternalPluginName, InternalPlugin>;
123
- // ...
124
- }
125
- ```
126
-
127
- Make sure to also add brief descriptions (using [TSDoc](https://tsdoc.org/)) to each of the variables. Mark variables that you are not entirely sure about with `@internal`.
128
- Feel free to copy descriptions from previous typings, or from the official API.
129
-
130
-
131
- ### Typing methods
132
-
133
- Typing methods is a bit more difficult, as aside you will need to know what the method does, and what the expected input and output is.
134
-
135
- You could start by typing the methods that do not take any arguments, and have a simple return type. For example, the `requestSaveConfig` method.
136
- From the name, we can already make the assumption that this method will probably _not_ return anything, as it is just telling
137
- the app that a config should be saved, the method will thus _likely_ be of type `void`.
138
-
139
- However, to make sure whether this assumption is correct, you _**should**_ check the source code (see next section).
140
- If possible, you can also run the method in the console, and see what happens (in this case, nothing happens, so it is likely just `void`).
141
-
142
- As before, if you are not certain about the return type or the workings of the function in general,
143
- add a `@internal` tag to the method description, and/or mark the return type as `unknown`.
144
-
145
-
146
- ```ts
147
- interface InternalPlugins extends Events {
148
- // ...
149
- /**
150
- * @internal Request save of plugin configs
151
- */
152
- requestSaveConfig(): unknown;
153
- // ...
154
- }
155
- ```
156
-
157
- Next up, are methods that take arguments. For example, the `getEnabledPluginById` method takes a single argument.
158
- It is very likely that this argument will just be of type `InternalPluginName`, but again, you could easily verify this by
159
- running the method in the console (i.e. `app.internalPlugins.getEnabledPluginById('audio-recorder')` and `app.internalPlugins.getEnabledPluginById('wrong-id')`).
160
-
161
- ```ts
162
- interface InternalPlugins extends Events {
163
- // ...
164
- /**
165
- * Get an internal plugin by ID
166
- * @param id - ID of the plugin to get
167
- * @returns Plugin instance
168
- */
169
- getEnabledPluginById(id: InternalPluginName): InternalPlugin;
170
- // ...
171
- }
172
- ```
173
-
174
-
175
- ## Traversing and analysing the source code
176
-
177
- Finally, this is both the most tedious but also the most important technique: finding the definition of the method in the minified source code.
178
-
179
- Start off by going back to the console, and going into the `Sources` tab. Here you will find the `app.js` file where
180
- all the internal methods are defined.
181
-
182
- 1. Go to the `Sources` tab
183
- 2. Select the `app.js` file
184
- 3. `Pretty format` the code (optional)
185
- 4. Copy the code to your IDE of choice (optional, but recommended)
186
-
187
-
188
- ![images/accessing-main.png](images/accessing-main.png)
189
-
190
- With access to the minified code, you can now start searching through it and find the definition of the method you are trying to type.
191
-
192
- For any method "XYZ", start by just searching for "XYZ". Generally, the method is defined as either:
193
- - `t.XYZ = ...` (for prototype methods)
194
- - `t.prototype.XYZ = ...` (for prototype methods)
195
- - `function XYZ(` (for internal/minified methods)
196
-
197
- At this stage, you might get lucky and get a single definition, or you may get multiple definitions of the method.
198
- In the latter case,
199
- you will need try to look at the context of the code to determine which interface/class the method belongs to.
200
- The main tip for finding the correct definition, is to look at the other methods being defined on the prototype,
201
- and check if these match with the other methods of your object.
202
-
203
- With the correct line found for the definition, you can now start analysing the code to determine the input/output types
204
- and its behaviour.
205
-
206
- For example, the `requestSaveConfig` method (seen earlier) is defined as follows:
207
-
208
- ```js
209
- n.requestSaveConfig = at(n.saveConfig.bind(n), 1e3, !0),
210
- ```
211
-
212
- Here, we find the following three things:
213
- - `at` is a minified function that takes three arguments, and its return value is the return value of the method
214
- - `1e3` is shorthand notation for `1000`
215
- - `!0` is shorthand notation for `true`
216
-
217
- (There are many shorthands and structural changes that you will need to get used to in the minified code)
218
-
219
- Since we don't know what `at` does, we need to start searching through the code again. Make sure to enable caps + whole word search, as `at` is a very common word.
220
- Start by searching for `function at`, or `.prototype.at`. With any luck, this will lead you to the following definition:
221
-
222
- ```js
223
- function at(e, t, n) {
224
- void 0 === t && (t = 0),
225
- void 0 === n && (n = !1);
226
- var i, r = null, o = null, a = null, s = 0, l = function() {
227
- var t = o
228
- , n = a;
229
- /* ... */
230
- }
231
- }
232
- ```
233
-
234
- You may want to pass the code through your favourite flavour of LLM or de-minifier to make the code at least somewhat understandable.
235
- Depending on whether you manage to decipher the code, you can now explicitly define the behaviour and/or types of the method.
Binary file
Binary file