Esanders has uploaded a new change for review.
https://gerrit.wikimedia.org/r/274395
Change subject: Pass command and sequence registries to the surface
......................................................................
Pass command and sequence registries to the surface
This allows the surface too lookup command without having
to reach up to the target global (ve.init.target). It allows
surfaces to potentially use different registries.
Change-Id: Id975ea9da43654cd9991a302b49a9fb32782078e
---
M src/ce/ve.ce.FocusableNode.js
M src/ce/ve.ce.Surface.js
M src/init/ve.init.Target.js
M src/ui/contextitems/ve.ui.ToolContextItem.js
M src/ui/ve.ui.ContextItem.js
M src/ui/ve.ui.Sequence.js
M src/ui/ve.ui.Surface.js
M src/ui/ve.ui.Tool.js
M src/ui/ve.ui.Toolbar.js
M src/ui/widgets/ve.ui.ContextOptionWidget.js
M src/ui/widgets/ve.ui.TargetWidget.js
M src/ve.TriggerListener.js
12 files changed, 72 insertions(+), 42 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor
refs/changes/95/274395/1
diff --git a/src/ce/ve.ce.FocusableNode.js b/src/ce/ve.ce.FocusableNode.js
index fb131d4..b989aaa 100644
--- a/src/ce/ve.ce.FocusableNode.js
+++ b/src/ce/ve.ce.FocusableNode.js
@@ -279,13 +279,14 @@
* @method
*/
ve.ce.FocusableNode.prototype.executeCommand = function () {
- var command;
+ var command, surface;
if ( !this.model.isInspectable() ) {
return false;
}
- command = ve.init.target.commandRegistry.getCommandForNode( this );
+ surface = this.focusableSurface.getSurface();
+ command = surface.commandRegistry.getCommandForNode( this );
if ( command ) {
- command.execute( this.focusableSurface.getSurface() );
+ command.execute( surface );
}
};
diff --git a/src/ce/ve.ce.Surface.js b/src/ce/ve.ce.Surface.js
index 38083ee..1968909 100644
--- a/src/ce/ve.ce.Surface.js
+++ b/src/ce/ve.ce.Surface.js
@@ -2721,7 +2721,7 @@
return;
}
- sequences = ve.init.target.sequenceRegistry.findMatching(
model.getDocument().data, selection.getModel().getRanges()[ 0 ].end );
+ sequences = this.getSurface().sequenceRegistry.findMatching(
model.getDocument().data, selection.getModel().getRanges()[ 0 ].end );
// sequences.length will likely be 0 or 1 so don't cache
for ( i = 0; i < sequences.length; i++ ) {
diff --git a/src/init/ve.init.Target.js b/src/init/ve.init.Target.js
index 9c6ab8b..8eac960 100644
--- a/src/init/ve.init.Target.js
+++ b/src/init/ve.init.Target.js
@@ -40,8 +40,8 @@
this.commandRegistry = config.commandRegistry || ve.ui.commandRegistry;
this.sequenceRegistry = config.sequenceRegistry ||
ve.ui.sequenceRegistry;
this.dataTransferHandlerFactory = config.dataTransferHandlerFactory ||
ve.ui.dataTransferHandlerFactory;
- this.documentTriggerListener = new ve.TriggerListener(
this.constructor.static.documentCommands );
- this.targetTriggerListener = new ve.TriggerListener(
this.constructor.static.targetCommands );
+ this.documentTriggerListener = new ve.TriggerListener(
this.constructor.static.documentCommands, this.commandRegistry );
+ this.targetTriggerListener = new ve.TriggerListener(
this.constructor.static.targetCommands, this.commandRegistry );
this.$scrollContainer = this.getScrollContainer();
this.toolbarScrollOffset = 0;
@@ -297,6 +297,8 @@
ve.init.Target.prototype.getSurfaceConfig = function ( config ) {
return ve.extendObject( {
$scrollContainer: this.$scrollContainer,
+ commandRegistry: this.commandRegistry,
+ sequenceRegistry: this.sequenceRegistry,
includeCommands: this.constructor.static.includeCommands,
excludeCommands: OO.simpleArrayUnion(
this.constructor.static.excludeCommands,
diff --git a/src/ui/contextitems/ve.ui.ToolContextItem.js
b/src/ui/contextitems/ve.ui.ToolContextItem.js
index 1f5b2ca..9f957d9 100644
--- a/src/ui/contextitems/ve.ui.ToolContextItem.js
+++ b/src/ui/contextitems/ve.ui.ToolContextItem.js
@@ -38,7 +38,7 @@
* @inheritdoc
*/
ve.ui.ToolContextItem.prototype.getCommand = function () {
- return ve.init.target.commandRegistry.lookup(
this.tool.static.commandName );
+ return this.tool.static.getCommand( this.context.getSurface() );
};
/**
diff --git a/src/ui/ve.ui.ContextItem.js b/src/ui/ve.ui.ContextItem.js
index aa9e889..8b6491d 100644
--- a/src/ui/ve.ui.ContextItem.js
+++ b/src/ui/ve.ui.ContextItem.js
@@ -106,7 +106,7 @@
* @return {ve.ui.Command} Command
*/
ve.ui.ContextItem.prototype.getCommand = function () {
- return ve.init.target.commandRegistry.lookup(
this.constructor.static.commandName );
+ return this.context.getSurface().commandRegistry.lookup(
this.constructor.static.commandName );
};
/**
diff --git a/src/ui/ve.ui.Sequence.js b/src/ui/ve.ui.Sequence.js
index f6802ed..e43d77d 100644
--- a/src/ui/ve.ui.Sequence.js
+++ b/src/ui/ve.ui.Sequence.js
@@ -73,7 +73,7 @@
return false;
}
- command = ve.init.target.commandRegistry.lookup( this.getCommandName()
);
+ command = surface.commandRegistry.lookup( this.getCommandName() );
if ( !command ) {
return false;
diff --git a/src/ui/ve.ui.Surface.js b/src/ui/ve.ui.Surface.js
index b92ab98..bddfefb 100644
--- a/src/ui/ve.ui.Surface.js
+++ b/src/ui/ve.ui.Surface.js
@@ -15,6 +15,8 @@
* @param {HTMLDocument|Array|ve.dm.LinearData|ve.dm.Document} dataOrDoc
Document data to edit
* @param {Object} [config] Configuration options
* @cfg {jQuery} [$scrollContainer] The scroll container of the surface
+ * @cfg {ve.ui.CommandRegistry} [commandRegistry] Command registry to use
+ * @cfg {ve.ui.SequenceRegistry} [sequenceRegistry] Sequence registry to use
* @cfg {string[]|null} [includeCommands] List of commands to include, null
for all registered commands
* @cfg {string[]} [excludeCommands] List of commands to exclude
* @cfg {Object} [importRules] Import rules
@@ -39,10 +41,12 @@
this.$controls = $( '<div>' );
this.$menus = $( '<div>' );
this.$placeholder = $( '<div>' ).addClass( 've-ui-surface-placeholder'
);
+ this.commandRegistry = config.commandRegistry ||
ve.init.target.commandRegistry;
+ this.sequenceRegistry = config.sequenceRegistry ||
ve.init.target.sequenceRegistry;
this.commands = OO.simpleArrayDifference(
- config.includeCommands || Object.keys(
ve.init.target.commandRegistry.registry ), config.excludeCommands || []
+ config.includeCommands || Object.keys(
this.commandRegistry.registry ), config.excludeCommands || []
);
- this.triggerListener = new ve.TriggerListener( this.commands );
+ this.triggerListener = new ve.TriggerListener( this.commands,
this.commandRegistry );
if ( dataOrDoc instanceof ve.dm.Document ) {
// ve.dm.Document
documentModel = dataOrDoc;
@@ -544,6 +548,20 @@
};
/**
+ * Execute a command by name
+ *
+ * @param {string} commandName Command name
+ * @return {boolean} The command was executed
+ */
+ve.ui.Surface.prototype.executeCommand = function ( commandName ) {
+ var command = this.commandRegistry.lookup( commandName );
+ if ( command ) {
+ return command.execute( this );
+ }
+ return false;
+};
+
+/**
* Set the current height of the toolbar.
*
* Used for scroll-into-view calculations.
diff --git a/src/ui/ve.ui.Tool.js b/src/ui/ve.ui.Tool.js
index 4a53861..93aeeb3 100644
--- a/src/ui/ve.ui.Tool.js
+++ b/src/ui/ve.ui.Tool.js
@@ -68,6 +68,20 @@
return this.commandName;
};
+/**
+ * Get the command for this tool in a given surface context
+ *
+ * @param {ve.ui.Surface} surface Surface
+ * @return {ve.ui.Command|null|undefined} Undefined means command not found,
null means no command set
+ */
+ve.ui.Tool.static.getCommand = function ( surface ) {
+ var commandName = this.getCommandName();
+ if ( commandName === null ) {
+ return null;
+ }
+ return surface.commandRegistry.lookup( commandName );
+};
+
/* Methods */
/**
@@ -113,8 +127,5 @@
* @return {ve.ui.Command|null|undefined} Undefined means command not found,
null means no command set
*/
ve.ui.Tool.prototype.getCommand = function () {
- if ( this.constructor.static.commandName === null ) {
- return null;
- }
- return ve.init.target.commandRegistry.lookup(
this.constructor.static.commandName );
+ return this.constructor.static.getCommand( this.toolbar.getSurface() );
};
diff --git a/src/ui/ve.ui.Toolbar.js b/src/ui/ve.ui.Toolbar.js
index 8fe3b6b..6934236 100644
--- a/src/ui/ve.ui.Toolbar.js
+++ b/src/ui/ve.ui.Toolbar.js
@@ -247,7 +247,7 @@
* @return {string[]} Command names
*/
ve.ui.Toolbar.prototype.getCommands = function () {
- return this.getSurface().triggerListener.getCommands();
+ return this.getSurface().getCommands();
};
/**
diff --git a/src/ui/widgets/ve.ui.ContextOptionWidget.js
b/src/ui/widgets/ve.ui.ContextOptionWidget.js
index 7f9129c..eef1174 100644
--- a/src/ui/widgets/ve.ui.ContextOptionWidget.js
+++ b/src/ui/widgets/ve.ui.ContextOptionWidget.js
@@ -67,5 +67,5 @@
* @return {ve.ui.Command} Command
*/
ve.ui.ContextOptionWidget.prototype.getCommand = function () {
- return ve.init.target.commandRegistry.lookup(
this.tool.static.commandName );
+ return this.tool.static.getCommand( this.context.getSurface() );
};
diff --git a/src/ui/widgets/ve.ui.TargetWidget.js
b/src/ui/widgets/ve.ui.TargetWidget.js
index f24eb19..d92871b 100644
--- a/src/ui/widgets/ve.ui.TargetWidget.js
+++ b/src/ui/widgets/ve.ui.TargetWidget.js
@@ -28,12 +28,20 @@
OO.ui.Widget.call( this, config );
// Properties
+ this.commandRegistry = config.commandRegistry ||
ve.init.target.commandRegistry;
+ this.sequenceRegistry = config.sequenceRegistry ||
ve.init.target.sequenceRegistry;
+ this.dataTransferHandlerFactory = config.dataTransferHandlerFactory ||
ve.init.target.dataTransferHandlerFactory;
+ // TODO: Override document/targetTriggerListener
+
this.surface = ve.init.target.createSurface( doc, {
+ commandRegistry: this.commandRegistry,
+ sequenceRegistry: this.sequenceRegistry,
includeCommands: config.includeCommands,
excludeCommands: config.excludeCommands,
importRules: config.importRules,
inDialog: config.inDialog
} );
+ // TODO: Use a TargetToolbar when trigger listeners are set here
this.toolbar = new ve.ui.Toolbar();
// Initialization
diff --git a/src/ve.TriggerListener.js b/src/ve.TriggerListener.js
index 9b1ca14..60a1451 100644
--- a/src/ve.TriggerListener.js
+++ b/src/ve.TriggerListener.js
@@ -11,14 +11,26 @@
*
* @constructor
* @param {string[]} commands Commands to listen to triggers for
+ * @param {ve.ui.CommandRegistry} commandRegistry Command registry to get
commands from
*/
-ve.TriggerListener = function VeTriggerListener( commands ) {
+ve.TriggerListener = function VeTriggerListener( commands, commandRegistry ) {
+ var i, j, command, triggers;
+
// Properties
- this.commands = [];
+ this.commands = commands;
this.commandsByTrigger = {};
this.triggers = {};
- this.setupCommands( commands );
+ for ( i = this.commands.length - 1; i >= 0; i-- ) {
+ command = this.commands[ i ];
+ triggers = ve.ui.triggerRegistry.lookup( command );
+ if ( triggers ) {
+ for ( j = triggers.length - 1; j >= 0; j-- ) {
+ this.commandsByTrigger[ triggers[ j
].toString() ] = commandRegistry.lookup( command );
+ }
+ this.triggers[ command ] = triggers;
+ }
+ }
};
/* Inheritance */
@@ -26,28 +38,6 @@
OO.initClass( ve.TriggerListener );
/* Methods */
-
-/**
- * Setup commands
- *
- * @param {string[]} commands Commands to listen to triggers for
- */
-ve.TriggerListener.prototype.setupCommands = function ( commands ) {
- var i, j, command, triggers;
- this.commands = commands;
- if ( commands.length ) {
- for ( i = this.commands.length - 1; i >= 0; i-- ) {
- command = this.commands[ i ];
- triggers = ve.ui.triggerRegistry.lookup( command );
- if ( triggers ) {
- for ( j = triggers.length - 1; j >= 0; j-- ) {
- this.commandsByTrigger[ triggers[ j
].toString() ] = ve.init.target.commandRegistry.lookup( command );
- }
- this.triggers[ command ] = triggers;
- }
- }
- }
-};
/**
* Get list of commands.
--
To view, visit https://gerrit.wikimedia.org/r/274395
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Id975ea9da43654cd9991a302b49a9fb32782078e
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Esanders <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits