Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/ve.ui.Tool.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface Tool classes.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * UserInterface tool.
9 *
10 * @class
11 * @abstract
12 * @extends OO.ui.Tool
13 *
14 * @constructor
15 * @param {OO.ui.ToolGroup} toolGroup
16 * @param {Object} [config] Configuration options
17 */
18 ve.ui.Tool = function VeUiTool() {
19 // Parent constructor
20 ve.ui.Tool.super.apply( this, arguments );
21
22 // Disable initially
23 this.setDisabled( true );
24 };
25
26 /* Inheritance */
27
28 OO.inheritClass( ve.ui.Tool, OO.ui.Tool );
29
30 /* Static Properties */
31
32 /**
33 * Command to execute when tool is selected.
34 *
35 * @static
36 * @property {string|null}
37 * @inheritable
38 */
39 ve.ui.Tool.static.commandName = null;
40
41 /**
42 * Deactivate tool after it's been selected.
43 *
44 * Use this for tools which don't display as active when relevant content is selected, such as
45 * insertion-only tools.
46 *
47 * @static
48 * @property {boolean}
49 * @inheritable
50 */
51 ve.ui.Tool.static.deactivateOnSelect = true;
52
53 /**
54 * If this tool is used to create a ve.ui.ToolContextItem, should that item be embeddable
55 *
56 * @static
57 * @property {boolean}
58 * @inheritable
59 */
60 ve.ui.Tool.static.makesEmbeddableContextItem = true;
61
62 /**
63 * Icon to use when this tool is shown in a non-toolbar context
64 *
65 * @static
66 * @property {string|null}
67 * @inheritable
68 */
69 ve.ui.Tool.static.fallbackIcon = null;
70
71 /**
72 * Get the symbolic command name for this tool.
73 *
74 * @static
75 * @return {string|null}
76 */
77 ve.ui.Tool.static.getCommandName = function () {
78 return this.commandName;
79 };
80
81 /**
82 * Get the command for this tool in a given surface context
83 *
84 * @static
85 * @param {ve.ui.Surface} surface
86 * @return {ve.ui.Command|null|undefined} Undefined means command not found, null means no command set
87 */
88 ve.ui.Tool.static.getCommand = function ( surface ) {
89 const commandName = this.getCommandName();
90 if ( commandName === null ) {
91 return null;
92 }
93 return surface.commandRegistry.lookup( commandName );
94 };
95
96 /* Methods */
97
98 /**
99 * Handle the toolbar state being updated.
100 *
101 * @param {ve.dm.SurfaceFragment|null} fragment Surface fragment
102 * @param {Object|null} direction Context direction with 'inline' & 'block' properties
103 */
104 ve.ui.Tool.prototype.onUpdateState = function ( fragment ) {
105 const command = this.getCommand();
106 if ( command !== null ) {
107 this.setDisabled(
108 !command || !fragment || !command.isExecutable( fragment ) ||
109 // Show command as disabled if a selection is required and the surface
110 // is read-only. Don't do this in Command as some are actually allowed
111 // to run, e.g. selectAll
112 // TODO: Add an allowedInReadOnly flag to some commands
113 ( command.supportedSelections && fragment.getSurface() && fragment.getSurface().isReadOnly() )
114 );
115 }
116 };
117
118 /**
119 * @inheritdoc
120 */
121 ve.ui.Tool.prototype.onSelect = function () {
122 const command = this.getCommand(),
123 surface = this.toolbar.getSurface();
124
125 let contextClosePromise;
126 if ( command instanceof ve.ui.Command ) {
127 if ( surface.context.inspector ) {
128 contextClosePromise = surface.context.inspector.close().closed;
129 } else {
130 contextClosePromise = ve.createDeferred().resolve().promise();
131 }
132 }
133 if ( this.constructor.static.deactivateOnSelect ) {
134 // It's fine to call setActive here before the promise resolves; it
135 // just disables the button, stopping double-clicks and making it feel more responsive
136 // if the promise is slow.
137 this.setActive( false );
138 }
139 if ( contextClosePromise ) {
140 // N.B. If contextClosePromise is already resolved, then the handler is called
141 // before the call to .done returns
142 contextClosePromise.done( () => {
143 if ( !command.execute( surface, undefined, 'tool' ) ) {
144 // If the command fails, ensure the tool is not active
145 this.setActive( false );
146 }
147 } );
148 }
149 };
150
151 /**
152 * Get the command for this tool.
153 *
154 * @return {ve.ui.Command|null|undefined} Undefined means command not found, null means no command set
155 */
156 ve.ui.Tool.prototype.getCommand = function () {
157 return this.constructor.static.getCommand( this.toolbar.getSurface() );
158 };