jenkins-bot has submitted this change and it was merged.

Change subject: Add optimised method hasAnnotationsInRange
......................................................................


Add optimised method hasAnnotationsInRange

Selecting the whole document causes the ClearAnnotationTool to
aggregate all the annotations in the document and check if it is
an empty set. This can be improved with a custom method that bails
at the first sight of an annotation.

Change-Id: I2815d45a21454b11fbd04541469192b2da39511e
---
M modules/ve/dm/lineardata/ve.dm.ElementLinearData.js
M modules/ve/dm/ve.dm.SurfaceFragment.js
M modules/ve/ui/tools/ve.ui.ClearAnnotationTool.js
3 files changed, 35 insertions(+), 1 deletion(-)

Approvals:
  Catrope: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/modules/ve/dm/lineardata/ve.dm.ElementLinearData.js 
b/modules/ve/dm/lineardata/ve.dm.ElementLinearData.js
index 39fefd5..8d4bc7c 100644
--- a/modules/ve/dm/lineardata/ve.dm.ElementLinearData.js
+++ b/modules/ve/dm/lineardata/ve.dm.ElementLinearData.js
@@ -451,6 +451,22 @@
 };
 
 /**
+ * Check if the range has any annotations
+ *
+ * @method
+ * @returns {boolean} The range contains at least one annotation
+ */
+ve.dm.ElementLinearData.prototype.hasAnnotationsInRange = function ( range ) {
+       var i;
+       for ( i = range.start; i < range.end; i++ ) {
+               if ( this.getAnnotationIndexesFromOffset( i, true ).length ) {
+                       return true;
+               }
+       }
+       return false;
+};
+
+/**
  * Get a range without any whitespace content at the beginning and end.
  *
  * @method
diff --git a/modules/ve/dm/ve.dm.SurfaceFragment.js 
b/modules/ve/dm/ve.dm.SurfaceFragment.js
index 81cbc43..0d11aeb 100644
--- a/modules/ve/dm/ve.dm.SurfaceFragment.js
+++ b/modules/ve/dm/ve.dm.SurfaceFragment.js
@@ -495,6 +495,24 @@
 };
 
 /**
+ * Check if the fragment has any annotations
+ *
+ * Quicker than doing !fragment.getAnnotations( true ).isEmpty() as
+ * it stops at the first sight of an annotation.
+ *
+ * @method
+ * @returns {boolean} The fragment contains at least one annotation
+ */
+ve.dm.SurfaceFragment.prototype.hasAnnotations = function () {
+       // Handle null fragment
+       if ( this.isNull() ) {
+               return false;
+       }
+
+       return this.getDocument().data.hasAnnotationsInRange( this.getRange( 
true ) );
+};
+
+/**
  * Get all leaf nodes covered by the fragment.
  *
  * @see ve.Document#selectNodes Used to get the return value
diff --git a/modules/ve/ui/tools/ve.ui.ClearAnnotationTool.js 
b/modules/ve/ui/tools/ve.ui.ClearAnnotationTool.js
index a733f2a..4cc0c77 100644
--- a/modules/ve/ui/tools/ve.ui.ClearAnnotationTool.js
+++ b/modules/ve/ui/tools/ve.ui.ClearAnnotationTool.js
@@ -51,7 +51,7 @@
        ve.ui.Tool.prototype.onUpdateState.apply( this, arguments );
 
        if ( !this.isDisabled() ) {
-               this.setDisabled( fragment.getAnnotations( true ).isEmpty() );
+               this.setDisabled( !fragment.hasAnnotations() );
        }
 };
 

-- 
To view, visit https://gerrit.wikimedia.org/r/148097
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I2815d45a21454b11fbd04541469192b2da39511e
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Esanders <[email protected]>
Gerrit-Reviewer: Catrope <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to