VisualEditor/Gadgets

Getting around
First of all you need to access the code with one (or more) of the following options:
 * Download the code from git:
 * (for more information see: mw:Extension:VisualEditor)

You may use add ?debug=1 to the url to get non compressed JS - this is a bad idea for VE since loading may take several minutes
 * Open an article, and edit it with visual editor. Open Web Developer tools (F12 in most browsers), and go to the Sources or Debugger tab. You should look at "load.php....ext.visualEditor...". Since MediaWiki ResourceLoader gives you the compressed JS you should use a tool to "Pretty print" or "Beautify" the code (such options exist in Chrome and Firefox).
 * If you don't see the JS it may come from local cache - run in console localStorage.clear and reload the page

Debug diving
The Hitchhiker’s Guide to the VisualEditor gadgets has a few things to say on the subject: Good debugger is about the most massively useful thing a gadget writer can have. Since there is no good documentation yet for VisualEditor, and may never be, to understand how this black magic works you will have to dive into debugging. Don't worry - it's simple.

Assume we want to understand how transclusion of templates work inside VE:
 * To access VE object:  for the active VisualEditor surface on the page.
 * Since we know (as users) that there is a dialog to insert templates, we should find this dialog in the code (search with ctrl+F). You should find a function called "ve.ui.MWTransclusionDialog.prototype.onAddTemplateButtonClick" - place a breakpoint there
 * As user, open the dialog, select a template, fill some parameters, and then add the template to the page - you will hit the break point.
 * Inspecting the transclusion object we can see what it looks like under the hood:


 * Now that we know how template code is represented - we can go on with the debugger, step by step and see how it is added to the document:

Now that we understand how templates are represented, let's take a look on an example of UI class - toolbar. There is a configuration object for the main toolbar:

Where * includes all the registered tools. (What are those tools? you can take a look/debug the function OO.ui.ToolGroup.prototype.populate to understand how it works - it loads all the registered commands, removed already used, remove the exclude and order using demote/promote).

Deployment
The above describes how to code a script for VE. Once your script is ready, you would like to publish it so other editors can use it. As other scripts, this can be done in one of the following options:
 * Gadget - turn it as a gadget that user can select it in their preferences (requires sysop rights). This is preferred method since it allows users to to install the gadget without code editing.
 * User script - users can install your script by adding code snippet to Special:Mypage/common.js

Gadget - Registering VE plugin
Registering your script in MediaWiki:Gadgets-definition allows other editors to turn your use using Special:Preferences (in Gadgets tab). The following section describes shortly how to do it, and details the specific requirements for a VE gadget. A more detailed explanation for general gadget can be found in Extension:Gadgets.

Since VE is heavy module, you must not create a gadget that dependent on VE internals. Instead you should create 2 gadgets:
 * A real gadget - that may be dependent on VE internals, but users shouldn't be able to turn it on (use hidden). The gadget code could be derived from below.
 * Create a "gadget loader" - a small gadget that tells VE to load your real gadget once VE is activated by the user

For example:

User script
If you don't have admin rights, or your script hasn't been tested enough or the target audience for your script is small, you can let users install the script by editing their personal JS (Special:Mypage/common.js). Afterwards you can publish it as a user script in pages such as en:Wikipedia:User scripts.

Here we use client side API for ResourceLoader to do the same as the above section, e.g. to add our script to be loaded when user open VE. Example for a URL for user script:
 * //meta.wikimedia.org/w/index.php?title=User:Jimbo_Wales/MyGadget.js&action=raw&ctype=text/javascript
 * Assuming the gadget code is located in meta:User:Jimbo_Wales/MyGadget.js, and contains code such as derived code from below.

Real examples for gadgets/scripts that interact with VE
Here are some real world gadgets/scripts for VE. You can use them as is or use them as example for building your own gadgets!
 * en:MediaWiki:Gadget-defaultsummaries.js – Adds two new dropdown boxes below the edit summary box with some useful default summaries.
 * he:MediaWiki:Gadget-VeDirectionMarkTool.js, he:MediaWiki:Gadget-VeExtendedBar.js – Adds a button for inserting a common template (this is also an example of how to use addPlugin).
 * en:User:Eran/refToolbarVe.js, en:User:Eran/refToolbarVeLoader.js – Adds a button for cite dialog, in which user can select a cite template to be included in article.
 * en:User:ערן/veReplace.js – Search and replace. Demonstration of how to interact with the underlying model and create dialogs.
 * meta:User:Matma Rex/visualeditor-signature.js – A tool to insert a signature ( ~ ) that is correctly rendered and formatted in the editor.
 * de:Benutzer:Schnark/js/veAutocorrect.js – User script that implements an autocorrect feature, mostly for typographical replacements.
 * de:Benutzer:Schnark/js/veSummary.js — "Very hackish" user script for providing access to previous edit summaries.
 * zh:MediaWiki:Gadget-EditorAPIs.js - A library that presents a common interface for wikitext editing. Useful for 2017 source editor.