Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/actions/ve.ui.FormatAction.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface FormatAction class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Format action.
9 *
10 * @class
11 * @extends ve.ui.Action
12 *
13 * @constructor
14 * @param {ve.ui.Surface} surface Surface to act on
15 * @param {string} [source]
16 */
17 ve.ui.FormatAction = function VeUiFormatAction() {
18 // Parent constructor
19 ve.ui.FormatAction.super.apply( this, arguments );
20 };
21
22 /* Inheritance */
23
24 OO.inheritClass( ve.ui.FormatAction, ve.ui.Action );
25
26 /* Static Properties */
27
28 ve.ui.FormatAction.static.name = 'format';
29
30 ve.ui.FormatAction.static.methods = [ 'convert' ];
31
32 /* Methods */
33
34 /**
35 * Convert the format of content.
36 *
37 * Conversion splits and unwraps all lists and replaces content branch nodes.
38 *
39 * TODO: Refactor functionality into {ve.dm.SurfaceFragment}.
40 *
41 * @param {string} type
42 * @param {Object} attributes
43 * @return {boolean} Action was executed
44 */
45 ve.ui.FormatAction.prototype.convert = function ( type, attributes ) {
46 const surfaceModel = this.surface.getModel(),
47 fragment = surfaceModel.getFragment(),
48 fragmentSelection = fragment.getSelection();
49
50 if ( !( fragmentSelection instanceof ve.dm.LinearSelection ) ) {
51 return false;
52 }
53
54 const fragments = [];
55
56 // We can't have headings or pre's in a list, so if we're trying to convert
57 // things that are in lists to a heading or a pre, split the list
58 fragment.getSelectedLeafNodes().forEach( ( node ) => {
59 const contentBranch = node.isContent() ? node.getParent() : node;
60
61 fragments.push( surfaceModel.getLinearFragment( contentBranch.getOuterRange(), true ) );
62 } );
63
64 fragments.forEach( ( f ) => {
65 f.isolateAndUnwrap( type );
66 } );
67
68 fragment.convertNodes( type, attributes );
69 if ( fragmentSelection.isCollapsed() ) {
70 // Converting an empty node needs a small selection fixup afterwards,
71 // otherwise the selection will be displayed outside the new empty
72 // node. This causes issues with the display of the current format in
73 // the toolbar, and with hitting enter if no content is entered. Don't
74 // always reapply the selection, because the automatic behavior is
75 // better if isolateAndUnwrap has actually acted. (T151594)
76 surfaceModel.setSelection( fragmentSelection );
77 }
78 this.surface.getView().focus();
79
80 ve.track( 'activity.format', { action: type + ( attributes && attributes.level ? ( '-' + attributes.level ) : '' ) } );
81
82 return true;
83 };
84
85 /* Registration */
86
87 ve.ui.actionFactory.register( ve.ui.FormatAction );