Skip to content

Commit 773af71

Browse files
authored
Merge pull request #33 from Derugon/main
Add MediaWiki core hooks
2 parents b973105 + 27f0cea commit 773af71

File tree

5 files changed

+453
-187
lines changed

5 files changed

+453
-187
lines changed

README.md

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
[![NPM version](https://img.shields.io/npm/v/types-mediawiki.svg)](https://www.npmjs.com/package/types-mediawiki)
22
![Linter](https://github.com/wikimedia-gadgets/types-mediawiki/workflows/Lint/badge.svg)
3+
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
34

45
# types-mediawiki
56

67
TypeScript definitions for MediaWiki JS interface.
78

8-
This package covers the functions and classes in the `mw` global object, as well a few jQuery plugins used in MediaWiki core. All commonly used parts of the interface are covered but as far as complete coverage is concerned, this is a work in progress.
9+
This package covers the functions and classes in the `mw` global object, as well some jQuery plugins used in MediaWiki core. All commonly used parts of the interface are covered.
10+
11+
[`@types/jquery`](https://www.npmjs.com/package/@types/jquery) and [`@types/oojs-ui`](https://www.npmjs.com/package/@types/oojs-ui) from [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped) are included as dependencies, so you don't need to install them separately.
912

1013
[![Download stats](https://nodei.co/npm/types-mediawiki.png?downloads=true&downloadRank=true)](https://nodei.co/npm/types-mediawiki/)
1114

@@ -25,7 +28,7 @@ Edit your project's `tsconfig.json` file so that it includes
2528
]
2629
```
2730

28-
You should be all set! `mw` will be available in the global scope. There is no need to put any import statements in the TypeScript source files. This package includes [@types/jquery](https://www.npmjs.com/package/@types/jquery) as a dependency, so you don't need to install that separately.
31+
You should be all set! `mw` will be available in the global scope. There is no need to put any import statements in the TypeScript source files.
2932

3033
**If you find any errors or have suggestions for more specific typings, please open a PR or file an issue.**
3134

@@ -56,11 +59,3 @@ import type { ApiEditPageParams, ApiParseParams } from "types-mediawiki/api_para
5659

5760
Since it is just a type import, it doesn't generate any JavaScript. Hence, such imports can also be used in non-modular applications.
5861

59-
## Types for OOjs & OOUI
60-
TypeScript definitions of OOjs and OOUI is available on [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped) and npm as [`@types/oojs`](https://www.npmjs.com/package/@types/oojs) and [`@types/oojs-ui`](https://www.npmjs.com/package/@types/oojs-ui) packages.
61-
62-
## TODO
63-
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
64-
65-
- Add doc comments for `mw.Title`, `mw.Uri`, `mw.storage`, `mw.language` and `mw.loader`.
66-
- Add types for more jQuery plugins.

mw/hook.d.ts

+248-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { User } from "./user";
2+
13
/**
2-
* Registry and firing of events.
4+
* An instance of a hook, created via {@link mw.hook mw.hook method}.
35
*
46
* MediaWiki has various interface components that are extended, enhanced
57
* or manipulated in some other way by extensions, gadgets and even
@@ -43,40 +45,279 @@
4345
*/
4446
interface Hook<T extends any[] = any[]> {
4547
/**
46-
* Register a hook handler
48+
* Register a hook handler.
4749
*
4850
* @param {...Function} handler Function to bind.
4951
* @chainable
5052
*/
5153
add(...handler: Array<(...data: T) => any>): this;
5254

5355
/**
54-
* Run a hook.
56+
* Call hook handlers with data.
5557
*
5658
* @param {*} data
5759
* @chainable
5860
*/
5961
fire(...data: T): this;
6062

6163
/**
62-
* Unregister a hook handler
64+
* Unregister a hook handler.
6365
*
6466
* @param {...Function} handler Function to unbind.
6567
* @chainable
6668
*/
6769
remove(...handler: Array<(...data: T) => any>): this;
6870
}
6971

72+
interface PostEditData {
73+
/**
74+
* Message that listeners should use when displaying notifications.
75+
* String for plain text, use array or jQuery object to pass actual nodes.
76+
*/
77+
message?: string | JQuery | HTMLElement[];
78+
/**
79+
* User that made the edit.
80+
*/
81+
user?: string | User;
82+
/**
83+
* Whether a temporary user account was created.
84+
*/
85+
tempUserCreated?: boolean;
86+
}
87+
88+
interface SearchIndex {
89+
[k: string]: SearchIndexEntry[];
90+
}
91+
92+
interface SearchIndexEntry {
93+
$highlight: JQuery;
94+
$field: JQuery;
95+
$wrapper: JQuery;
96+
$tabPanel: JQuery;
97+
}
98+
99+
interface EditRecovery {
100+
fieldChangeHandler(): void;
101+
}
102+
70103
declare global {
71104
namespace mw {
72105
/**
73106
* Create an instance of mw.hook.
74107
*
75-
* @method hook
76-
* @member mw
77108
* @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.hook
78109
*/
79-
function hook<T extends any[] = any[]>(event: string): Hook<T>;
110+
function hook(
111+
event: "apisandbox.formatRequest"
112+
): Hook<
113+
[
114+
items: OO.ui.MenuOptionWidget[],
115+
displayParams: object,
116+
rawParams: object,
117+
method: string,
118+
ajaxOptions: JQuery.AjaxSettings
119+
]
120+
>;
121+
122+
/**
123+
* Create an instance of mw.hook, fired after EditRecovery has loaded any recovery data, added event handlers, etc.
124+
*
125+
* @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.hook
126+
*/
127+
function hook(event: "editRecovery.loadEnd"): Hook<[editRecovery: EditRecovery]>;
128+
129+
/**
130+
* Create an instance of mw.hook.
131+
*
132+
* @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.hook
133+
*/
134+
function hook(event: "htmlform.enhance"): Hook<[$root: JQuery]>;
135+
136+
/**
137+
* Create an instance of mw.hook, fired after an edit was successfully saved.
138+
*
139+
* Does not fire for null edits.
140+
*
141+
* Code that fires the postEdit hook should first set `wgRevisionId` and `wgCurRevisionId` to the revision associated with the edit that triggered the postEdit hook, then fire the postEdit hook, e.g.:
142+
*
143+
* ```
144+
* mw.config.set( {
145+
* wgCurRevisionId: data.newrevid,
146+
* wgRevisionId: data.newrevid
147+
* } );
148+
* // Now fire the hook.
149+
* mw.hook( 'postEdit' ).fire();
150+
* ```
151+
*
152+
* @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.hook
153+
*/
154+
function hook(event: "postEdit"): Hook<[data?: PostEditData]>;
155+
156+
/**
157+
* Create an instance of mw.hook, fired after the listener for #postEdit removes the notification.
158+
*
159+
* @deprecated
160+
* @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.hook
161+
*/
162+
function hook(event: "postEdit.afterRemoval"): Hook<[]>;
163+
164+
/**
165+
* Create an instance of mw.hook.
166+
*
167+
* @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.hook
168+
*/
169+
function hook(event: "prefs.search.buildIndex"): Hook<[index: SearchIndex]>;
170+
171+
/**
172+
* Create an instance of mw.hook, fired when a trusted UI element to perform a logout has been activated.
173+
*
174+
* This will end the user session, and either redirect to the given URL on success, or queue an error message via mw.notification.
175+
*
176+
* @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.hook
177+
*/
178+
function hook(event: "skin.logout"): Hook<[href: string]>;
179+
180+
/**
181+
* Create an instance of mw.hook, fired when initialization of the filtering interface for changes list is complete.
182+
*
183+
* @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.hook
184+
*/
185+
function hook(event: "structuredChangeFilters.ui.initialized"): Hook<[]>;
186+
187+
/**
188+
* Create an instance of mw.hook, fired when a portlet is successfully created.
189+
*
190+
* Example usage:
191+
*
192+
* ```
193+
* mw.hook( 'util.addPortlet' ).add( ( p ) => {
194+
* p.style.border = 'solid 1px black';
195+
* } );
196+
* ```
197+
*
198+
* @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.hook
199+
*/
200+
function hook(
201+
event: "util.addPortlet"
202+
): Hook<[portlet: HTMLElement, before: string | undefined]>;
203+
204+
/**
205+
* Create an instance of mw.hook, fired when a portlet link is successfully created.
206+
*
207+
* Example usage:
208+
*
209+
* ```
210+
* mw.hook( 'util.addPortletLink' ).add( ( link ) => {
211+
* const span = $( '<span class="icon">' );
212+
* link.appendChild( span );
213+
* } );
214+
* ```
215+
*
216+
* @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.hook
217+
*/
218+
function hook(event: "util.addPortletLink"): Hook<[item: HTMLLIElement, data: object]>;
219+
220+
/**
221+
* Create an instance of mw.hook, fired when categories are being added to the DOM.
222+
*
223+
* It is encouraged to fire it before the main DOM is changed (when $content is still detached). However, this order is not defined either way, so you should only rely on $content itself.
224+
*
225+
* This includes the ready event on a page load (including post-edit loads) and when content has been previewed with LivePreview.
226+
*
227+
* @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.hook
228+
*/
229+
function hook(event: "wikipage.categories"): Hook<[$content: JQuery]>;
230+
231+
/**
232+
* Create an instance of mw.hook, fired after collapsible content has been initialized.
233+
*
234+
* This gives an option to modify the collapsible behavior.
235+
*
236+
* @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.hook
237+
*/
238+
function hook(event: "wikipage.collapsibleContent"): Hook<[$collapsible: JQuery]>;
239+
240+
/**
241+
* Create an instance of mw.hook, fired when wiki content has been added to the DOM.
242+
*
243+
* This should only be fired after $content has been attached.
244+
*
245+
* This includes the ready event on a page load (including post-edit loads) and when content has been previewed with LivePreview.
246+
*
247+
* @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.hook
248+
*/
249+
function hook(event: "wikipage.content"): Hook<[$content: JQuery]>;
250+
251+
/**
252+
* Create an instance of mw.hook, fired when a diff is added to a page or dynamically displayed to the user.
253+
*
254+
* Similar to the wikipage.content hook, `$diff` may still be detached when the hook is fired.
255+
*
256+
* @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.hook
257+
*/
258+
function hook(event: "wikipage.diff"): Hook<[$table: JQuery<HTMLTableElement>]>;
259+
260+
/**
261+
* Create an instance of mw.hook.
262+
*
263+
* @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.hook
264+
*/
265+
function hook(
266+
event: "wikipage.diff.diffTypeSwitch"
267+
): Hook<[inlineToggleSwitch: OO.ui.ToggleSwitchWidget]>;
268+
269+
/**
270+
* Create an instance of mw.hook.
271+
*
272+
* @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.hook
273+
*/
274+
function hook(event: "wikipage.diff.wikitextBodyUpdate"): Hook<[$wikitextDiffBody: JQuery]>;
275+
276+
/**
277+
* Create an instance of mw.hook, fired when the editform is added to the edit page.
278+
*
279+
* Similar to the wikipage.content hoo $editForm can still be detached when this hook is fired.
280+
*
281+
* @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.hook
282+
*/
283+
function hook(event: "wikipage.editform"): Hook<[$editForm: JQuery]>;
284+
285+
/**
286+
* Create an instance of mw.hook, fired when a page's {@link https://www.mediawiki.org/wiki/Special:MyLanguage/Help:Page_status_indicators status indicators} are being added to the DOM or updated.
287+
*
288+
* @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.hook
289+
*/
290+
function hook(event: "wikipage.indicators"): Hook<[$indicators: JQuery]>;
291+
292+
/**
293+
* Create an instance of mw.hook, fired when dynamic changes have been made to the table of contents.
294+
*
295+
* @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.hook
296+
*/
297+
function hook(event: "wikipage.tableOfContents"): Hook<[sections: any[]]>;
298+
299+
/**
300+
* Create an instance of mw.hook, fired when the page watch status has changed.
301+
*
302+
* Example usage:
303+
* ```
304+
* mw.hook( 'wikipage.watchlistChange' ).add( ( isWatched, expiry, expirySelected ) => {
305+
* // Do things
306+
* } );
307+
* ```
308+
*
309+
* @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.hook
310+
*/
311+
function hook(
312+
event: "wikipage.watchlistChange"
313+
): Hook<[isWatched: boolean, expiry: string, expirySelected: string]>;
314+
315+
/**
316+
* Create an instance of mw.hook.
317+
*
318+
* @see https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.hook
319+
*/
320+
function hook<T extends any[] = any[]>(name: string): Hook<T>;
80321
}
81322
}
82323

0 commit comments

Comments
 (0)