DLynch has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/287972

Change subject: Insert undo breakpoints at word breaks
......................................................................

Insert undo breakpoints at word breaks

When users interact with the contenteditable and enter text, check the last
few characters and see if we have a word-characters followed by a non-word-
character. If so, set a breakpoint. For languages with word breaks, this is a
fairly accurate heuristic for having undo work per-word.

Leave the existing interval behavior in place, with a much longer interval, as
a fallback for people working in languages with infrequent non-word
characters, or people who're entering very long words slowly.

In languages with word breaks

Bug: T127834
Change-Id: I937df66706e533687f327396b76f75d686cfacd1
---
M src/ce/ve.ce.Surface.js
M src/dm/ve.dm.Surface.js
2 files changed, 55 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor 
refs/changes/72/287972/1

diff --git a/src/ce/ve.ce.Surface.js b/src/ce/ve.ce.Surface.js
index 525cf94..90bd980 100644
--- a/src/ce/ve.ce.Surface.js
+++ b/src/ce/ve.ce.Surface.js
@@ -2712,6 +2712,7 @@
                // Use setTimeout to escape current renderLock
                setTimeout( function () {
                        surface.checkSequences();
+                       surface.maybeSetBreakpoint();
                } );
        }
        if ( newState.branchNodeChanged && newState.node ) {
@@ -2850,6 +2851,49 @@
 };
 
 /**
+ * See if the just-entered content fits our criteria for setting a history 
breakpoint
+ */
+ve.ce.Surface.prototype.maybeSetBreakpoint = function () {
+       var data, offset, textStart, plaintext,
+               mode = 0,
+               model = this.getModel(),
+               selection = this.getSelection();
+
+       if ( !selection.isNativeCursor() ) {
+               return;
+       }
+
+       // TODO: factor this out into something reusable for SequenceRegistry 
(and others?)
+
+       data = model.getDocument().data;
+       offset = selection.getModel().getCoveringRange().end;
+
+       for ( textStart = offset - 1; textStart >= 0 && ( offset - textStart ) 
<= 16; textStart-- ) {
+               if ( mode === 0 && !data.isOpenElementData( textStart ) ) {
+                       mode++;
+               }
+               if ( mode === 1 && !data.isCloseElementData( textStart ) ) {
+                       mode++;
+               }
+               if ( mode === 2 && data.isElementData( textStart ) ) {
+                       break;
+               }
+       }
+
+       plaintext = data.getText( true, new ve.Range( textStart + 1, offset ) );
+
+       if ( plaintext && plaintext.match( /\w[\W]$/ ) ) {
+               // If the recent text ends with a word-character followed by a 
non-
+               // word character, set a breakpoint. This should cover most 
languages
+               // that use spaces and punctuation. Languages without these 
handy
+               // signposts for us will get breakpoints set by the fallback 
timer
+               // anyway.
+               this.getModel().breakpoint();
+       }
+};
+
+
+/**
  * Handle window resize event.
  *
  * @param {jQuery.Event} e Window resize event
diff --git a/src/dm/ve.dm.Surface.js b/src/dm/ve.dm.Surface.js
index 7941306..40e1405 100644
--- a/src/dm/ve.dm.Surface.js
+++ b/src/dm/ve.dm.Surface.js
@@ -134,7 +134,7 @@
                return;
        }
        if ( this.historyTrackingInterval === null ) {
-               this.historyTrackingInterval = setInterval( 
this.breakpoint.bind( this ), 750 );
+               this.historyTrackingInterval = setInterval( 
this.breakpoint.bind( this ), 3000 );
        }
 };
 
@@ -150,6 +150,15 @@
                this.historyTrackingInterval = null;
        }
 };
+
+/**
+ * Reset the timer for automatic history-tracking
+ */
+ve.dm.Surface.prototype.resetHistoryTrackingInterval = function () {
+       this.stopHistoryTracking();
+       this.startHistoryTracking();
+};
+
 
 /**
  * Get a list of all applied history states.
@@ -854,6 +863,7 @@
        if ( !this.enabled ) {
                return false;
        }
+       this.resetHistoryTrackingInterval();
        if ( this.newTransactions.length > 0 ) {
                this.undoStack.push( {
                        transactions: this.newTransactions,

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I937df66706e533687f327396b76f75d686cfacd1
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: DLynch <[email protected]>

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

Reply via email to