-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Try a generic undo handler #4959
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,13 @@ import { | |
mapValues, | ||
findIndex, | ||
reject, | ||
keys, | ||
isString, | ||
isArray, | ||
isPlainObject, | ||
every, | ||
} from 'lodash'; | ||
import diff from 'fast-diff'; | ||
|
||
/** | ||
* WordPress dependencies | ||
|
@@ -94,6 +100,55 @@ function getFlattenedBlocks( blocks ) { | |
return flattenedBlocks; | ||
} | ||
|
||
/** | ||
* Check whether two values are considered equivalent | ||
* Two values are considered equivalent if the only | ||
* possible difference between these two values is a character in a string. | ||
* | ||
* @param {*} value1 | ||
* @param {*} value2 | ||
* | ||
* @return {boolean} Whether the values are equivalent. | ||
*/ | ||
function areValuesEquivalent( value1, value2 ) { | ||
if ( isString( value1 ) && isString( value2 ) ) { | ||
const diffResult = diff( value1, value2 ).filter( ( [ type ] ) => type !== 0 ); | ||
if ( | ||
diffResult.length === 0 || | ||
( diffResult.length === 1 && diffResult[ 0 ][ 1 ].match( /\S/ ) !== null ) | ||
) { | ||
return true; | ||
} | ||
|
||
// For some reason comparing text adding a char after a space is creating two diffs, | ||
// let's run the diff again for more accurate results | ||
if ( diffResult.length === 2 ) { | ||
const diffResult2 = diff( diffResult[ 0 ][ 1 ], diffResult[ 1 ][ 1 ] ).filter( ( [ type ] ) => type !== 0 ); | ||
return diffResult2.length === 0 || | ||
( diffResult2.length === 1 && diffResult2[ 0 ][ 1 ].match( /\S/ ) !== null ); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
if ( isArray( value1 ) && isArray( value2 ) && value1.length === value2.length ) { | ||
return every( value1, ( subvalue, key ) => areValuesEquivalent( subvalue, value2[ key ] ) ); | ||
} | ||
|
||
if ( isPlainObject( value1 ) && isPlainObject( value2 ) ) { | ||
const keys1 = keys( value1 ); | ||
const keys2 = keys( value2 ); | ||
return ( | ||
keys1.length === keys2.length && | ||
every( keys1, ( key, index ) => | ||
keys2[ index ] === key && areValuesEquivalent( value1[ key ], value2[ key ] ) | ||
) | ||
); | ||
} | ||
|
||
return value1 === value2; | ||
} | ||
|
||
/** | ||
* Undoable reducer returning the editor post state, including blocks parsed | ||
* from current HTML markup. | ||
|
@@ -114,7 +169,26 @@ export const editor = flow( [ | |
combineReducers, | ||
|
||
// Track undo history, starting at editor initialization. | ||
partialRight( withHistory, { resetTypes: [ 'SETUP_NEW_POST', 'SETUP_EDITOR' ] } ), | ||
partialRight( withHistory, { | ||
resetTypes: [ 'SETUP_NEW_POST', 'SETUP_EDITOR' ], | ||
shouldOverwriteState: ( previous, next ) => { | ||
if ( previous.type !== next.type || previous.type !== 'UPDATE_BLOCK_ATTRIBUTES' ) { | ||
return false; | ||
} | ||
|
||
const previousAttributes = keys( previous.attributes ); | ||
const nextAttributes = keys( next.attributes ); | ||
if ( | ||
previousAttributes.length !== 1 || | ||
nextAttributes.length !== 1 || | ||
previousAttributes[ 0 ] !== nextAttributes[ 0 ] | ||
) { | ||
return false; | ||
} | ||
|
||
return areValuesEquivalent( previous.attributes[ previousAttributes[ 0 ] ], next.attributes[ nextAttributes[ 0 ] ] ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}, | ||
} ), | ||
|
||
// Track whether changes exist, resetting at each post save. Relies on | ||
// editor initialization firing post reset as an effect. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think we could generalise this more? Say e.g. someone changes the post format to "link", but then immediate changes it back to "standard", I think we should avoid to create an undo level as well. Similarly we could have input fields outside block scope, which would still add records for each character change?