Page MenuHomePhabricator

Cleanup and extend the VE hooks system
Open, Needs TriagePublic

Description

There are numerous tasks that request a new hook from VE, but as T265981#8918502 points out, the current system is a mess created by inconsistently adding new hooks ad-hoc.

After T162337 with have a decent audit of the current hooks at https://www.mediawiki.org/wiki/VisualEditor/Hooks.

We should come up with a new system that is more consistent and extendable, and create new hooks to serve known use cases.

Event Timeline

Our initial proposal is to provide a single hook whenever a target is constructed (either from the ve.init.mw.Target constructor, or ve.init.mw.targetFactory.create), passing the target instance.
Users would then be expected to look at the target object created to see if they want to hook into, e.g. by using target.constructor.static.name (which would be 'article', 'cx', 'discussionTools', 'flow', etc.), and maybe OO.ui.isMobile().
More granular integration would be done via the VE events system, e.g. ve.activationComplete would be replaced by target.on( 'surfaceReady' ).

Change 992800 had a related patch set uploaded (by Esanders; author: Esanders):

[mediawiki/extensions/VisualEditor@master] [WIP] Introduce new init hook (ve.newTarget) to replace others

https://gerrit.wikimedia.org/r/992800

Change #992800 merged by jenkins-bot:

[mediawiki/extensions/VisualEditor@master] Introduce a new init hook (ve.newTarget) to replace all others

https://gerrit.wikimedia.org/r/992800

matmarex subscribed.

I think this will be a welcome addition for gadget developers.

Quiddity subscribed.

For Tech News, does anyone have suggested wording for how to describe this? I'm not sure what aspects to highlight, or if there is (or should be!) any related documentation page(s) to link to, beyond mw:VisualEditor/Hooks.

Timing-wise, I hesitantly suggest placing it in the week after next's Tech News edition (to be sent on 10 Feb), so that once gadget developers learn about it, they can immediately start using it. If there is more urgency to the announcement, let me know so that we can include it in the upcoming edition.

@Quiddity maybe something like:

VisualEditor has introduced a new client-side hook for developers to use when integrating with the VE target lifecycle. This hook should replace the existing lifecycle-related hooks, and be more consistent between different platforms. In addition, the new hook will apply to uses of VE outside of just full article editing, allowing gadgets to interact with the editor in DiscussionTools as well. We intend to deprecate and eventually remove the old lifecycle hooks, so any use cases that this new hook does not cover would be of interest to us.

There's no real urgency to announcing it. The new hook exists and can be used (as of the train getting out on the 6th), but the old hooks are going to be around and not changing for some time.

An example of how to migrate would really help. That's not very clear to me.

@Pppery there's a block of code with comments at the top of https://www.mediawiki.org/wiki/VisualEditor/Hooks that calls out equivalent usages. Would you prefer more of a worked example -- a "here's an existing example usage, and here's how it'd be migrated"?

No, that's fine, I just didn't see that.

Just noting that mw.hook( … ).deprecate() is a thing now, which I assume will be useful here :)