Esanders has uploaded a new change for review.
https://gerrit.wikimedia.org/r/159949
Change subject: Refactor out finding of CE focusedNodes
......................................................................
Refactor out finding of CE focusedNodes
Now it can be run against an arbitrary range.
Change-Id: Ie59239ec5fa3493dfc8f6a6b200a9a5405a96c66
---
M src/ce/ve.ce.Surface.js
1 file changed, 37 insertions(+), 25 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor
refs/changes/49/159949/1
diff --git a/src/ce/ve.ce.Surface.js b/src/ce/ve.ce.Surface.js
index 3e8e2e7..78db1e3 100644
--- a/src/ce/ve.ce.Surface.js
+++ b/src/ce/ve.ce.Surface.js
@@ -1482,9 +1482,7 @@
* @param {ve.Range|null} selection
*/
ve.ce.Surface.prototype.onModelSelect = function ( selection ) {
- var start, end, nativeRange,
- next = null,
- previous = this.focusedNode;
+ var nativeRange, focusedNode;
this.contentBranchNodeChanged = false;
@@ -1492,31 +1490,17 @@
return;
}
- // Detect when only a single inline element is selected
- if ( !selection.isCollapsed() ) {
- start = this.documentView.getDocumentNode().getNodeFromOffset(
selection.start + 1 );
- if ( start.isFocusable() ) {
- end =
this.documentView.getDocumentNode().getNodeFromOffset( selection.end - 1 );
- if ( start === end ) {
- next = start;
- }
- }
- } else {
- // Check we haven't been programmatically placed inside a
focusable node with a collapsed selection
- start = this.documentView.getDocumentNode().getNodeFromOffset(
selection.start );
- if ( start.isFocusable() ) {
- next = start;
- }
- }
+ focusedNode = this.getFocusedNodeAt( selection );
+
// If focus has changed, update nodes and this.focusedNode
- if ( previous !== next ) {
- if ( previous ) {
- previous.setFocused( false );
+ if ( focusedNode !== this.focusedNode ) {
+ if ( this.focusedNode ) {
+ this.focusedNode.setFocused( false );
this.focusedNode = null;
}
- if ( next ) {
- next.setFocused( true );
- this.focusedNode = next;
+ if ( focusedNode ) {
+ focusedNode.setFocused( true );
+ this.focusedNode = focusedNode;
// As FF won't fire a copy event with nothing selected,
make
// a dummy selection of one space in the pasteTarget.
@@ -1547,6 +1531,34 @@
};
/**
+ * Get the focused node at a specified range, or null if one is not present
+ *
+ * @param {ve.Range} range Range to check for focusable node
+ * @return {ve.ce.Node} Focused node
+ */
+ve.ce.Surface.prototype.getFocusedNodeAt = function ( range ) {
+ var startNode, endNode,
+ documentNode = this.documentView.getDocumentNode();
+ // Detect when only a single focusable element is selected
+ if ( !range.isCollapsed() ) {
+ startNode = documentNode.getNodeFromOffset( range.start + 1 );
+ if ( startNode.isFocusable() ) {
+ endNode = documentNode.getNodeFromOffset( range.end - 1
);
+ if ( startNode === endNode ) {
+ return startNode;
+ }
+ }
+ } else {
+ // Check we haven't been programmatically placed inside a
focusable node with a collapsed selection
+ startNode = documentNode.getNodeFromOffset( range.start );
+ if ( startNode.isFocusable() ) {
+ return startNode;
+ }
+ }
+ return null;
+};
+
+/**
* Handle documentUpdate events on the surface model.
*/
ve.ce.Surface.prototype.onModelDocumentUpdate = function () {
--
To view, visit https://gerrit.wikimedia.org/r/159949
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie59239ec5fa3493dfc8f6a6b200a9a5405a96c66
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