mediawiki-extensions-Visual.../modules/ve/actions/ve.FormatAction.js
Trevor Parscal 2419f7638c Death and/or destruction
So. It turns out that the design of SurfaceFragment is a little -
shall we say - wonky.

One of the best things about ve.dm.SurfaceFragment is its magical
ability to retain the intention of its range, even as transactions
are being processed. This ability is granted by each fragment
listening to the surface's change event, and responding by using
translateRange for each transaction that gets processed. Surface
fragments also have these clever methods that allow you to get a
fragment based on another, which makes adjusting the range easy to do
inline without having to manually store multiple fragments or
modifying the original.

This sounded good, and we seemed to all be convinced it was well
designed. But if you add a console.log( 'hello' ); to the first line
of ve.dm.SurfaceFragment.prototype.onTransact, and then start using
the bold tool on various selections of text, you will find that there
may indeed be a flaw. What you will probably realize is that the
number of times that particular line of code is being called is
disturbingly large, and increases each time you do just about anything
in the editor. What's going on? How did we get here? Read on…

It turns out that fragments are immortal. We create them, they listen
to the surface's transact event, we are done with them, but the
surface keeps on emitting events to the now long forgotten about
fragments. They continue to build up over time, never go out of scope,
and bloat the hell out of our program.

The same ended up being true of toolbars - and each time the context
menu fired up a new one the old one was left in limbo, still
responding to events, still taking up memory, but not being visible to
the user.

All of this immortality was causing strange and difficult to track
down problems. This patch fixes this by introducing a destroy method.
This method unbinds events, allowing the object to finally fall out of
scope and die - and more importantly stop receiving notifications of
changes.

This is a hack, but Ed will no doubt get this situation sorted out
properly by making fragments lazy-evaluate their selections by only
storing an identifier of the most recent transaction they were based
on, see bug 47343.

Change-Id: I18bb986001a44732a7871b9d79dc3015eedfb168
2013-04-18 13:56:20 -07:00

81 lines
2.1 KiB
JavaScript

/*!
* VisualEditor FormatAction class.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Format action.
*
* @class
* @extends ve.Action
* @constructor
* @param {ve.Surface} surface Surface to act on
*/
ve.FormatAction = function VeFormatAction( surface ) {
// Parent constructor
ve.Action.call( this, surface );
};
/* Inheritance */
ve.inheritClass( ve.FormatAction, ve.Action );
/* Static Properties */
/**
* List of allowed methods for this action.
*
* @static
* @property
*/
ve.FormatAction.static.methods = ['convert'];
/* Methods */
/**
* Convert the format of content.
*
* Conversion splits and unwraps all lists and replaces content branch nodes.
*
* TODO: Refactor functionality into {ve.dm.SurfaceFragment}.
*
* @param {string} type
* @param {Object} attributes
*/
ve.FormatAction.prototype.convert = function ( type, attributes ) {
var selected, i, length, contentBranch, txs,
surfaceView = this.surface.getView(),
surfaceModel = this.surface.getModel(),
selection = surfaceModel.getSelection(),
fragmentForSelection = surfaceModel.getFragment( selection, true ),
doc = surfaceModel.getDocument(),
fragments = [];
// We can't have headings or pre's in a list, so if we're trying to convert
// things that are in lists to a heading or a pre, split the list
selected = doc.selectNodes( selection, 'leaves' );
for ( i = 0, length = selected.length; i < length; i++ ) {
contentBranch = selected[i].node.isContent() ?
selected[i].node.getParent() :
selected[i].node;
fragments.push( surfaceModel.getFragment( contentBranch.getOuterRange(), true ) );
}
for ( i = 0, length = fragments.length; i < length; i++ ) {
fragments[i].isolateAndUnwrap( type ).destroy();
}
selection = fragmentForSelection.getRange();
fragmentForSelection.destroy();
txs = ve.dm.Transaction.newFromContentBranchConversion( doc, selection, type, attributes );
surfaceModel.change( txs, selection );
surfaceView.showSelection( selection );
};
/* Registration */
ve.actionFactory.register( 'format', ve.FormatAction );