Skip to content
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

Record selection changes in history (undo) #17824

Merged
merged 4 commits into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,20 @@ _Returns_

- `Object`: Action object.

<a name="resetSelection" href="#resetSelection">#</a> **resetSelection**

Returns an action object used in signalling that selection state should be
reset to the specified selection.

_Parameters_

- _selectionStart_ `WPBlockSelection`: The selection start.
- _selectionEnd_ `WPBlockSelection`: The selection end.

_Returns_

- `Object`: Action object.

<a name="selectBlock" href="#selectBlock">#</a> **selectBlock**

Returns an action object used in signalling that the block with the
Expand Down
24 changes: 24 additions & 0 deletions docs/designers-developers/developers/data/data-core-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,30 @@ _Returns_

- `Array`: Block list.

<a name="getEditorSelectionEnd" href="#getEditorSelectionEnd">#</a> **getEditorSelectionEnd**

Returns the current selection end.

_Parameters_

- _state_ `Object`:

_Returns_

- `WPBlockSelection`: The selection end.

<a name="getEditorSelectionStart" href="#getEditorSelectionStart">#</a> **getEditorSelectionStart**

Returns the current selection start.

_Parameters_

- _state_ `Object`:

_Returns_

- `WPBlockSelection`: The selection start.

<a name="getEditorSettings" href="#getEditorSettings">#</a> **getEditorSettings**

Returns the post editor settings.
Expand Down
18 changes: 16 additions & 2 deletions packages/block-editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class BlockEditorProvider extends Component {
updateSettings,
value,
resetBlocks,
selectionStart,
selectionEnd,
resetSelection,
registry,
} = this.props;

Expand Down Expand Up @@ -58,6 +61,10 @@ class BlockEditorProvider extends Component {
this.isSyncingOutcomingValue = [];
this.isSyncingIncomingValue = value;
resetBlocks( value );

if ( selectionStart && selectionEnd ) {
epiqueras marked this conversation as resolved.
Show resolved Hide resolved
resetSelection( selectionStart, selectionEnd );
}
}
}

Expand Down Expand Up @@ -86,6 +93,8 @@ class BlockEditorProvider extends Component {

const {
getBlocks,
getSelectionStart,
getSelectionEnd,
isLastBlockChangePersistent,
__unstableIsLastBlockChangeIgnored,
} = registry.select( 'core/block-editor' );
Expand Down Expand Up @@ -128,10 +137,13 @@ class BlockEditorProvider extends Component {
blocks = newBlocks;
isPersistent = newIsPersistent;

const selectionStart = getSelectionStart();
const selectionEnd = getSelectionEnd();

if ( isPersistent ) {
onChange( blocks );
onChange( blocks, { selectionStart, selectionEnd } );
} else {
onInput( blocks );
onInput( blocks, { selectionStart, selectionEnd } );
}
}
} );
Expand All @@ -150,11 +162,13 @@ export default compose( [
const {
updateSettings,
resetBlocks,
resetSelection,
} = dispatch( 'core/block-editor' );

return {
updateSettings,
resetBlocks,
resetSelection,
};
} ),
] )( BlockEditorProvider );
25 changes: 25 additions & 0 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,31 @@ export function resetBlocks( blocks ) {
};
}

/**
* @typedef {WPBlockSelection} A block selection object.
*
* @property {string} clientId A block client ID.
* @property {string} attributeKey A block attribute key.
* @property {number} offset A block attribute offset.
*/

/**
* Returns an action object used in signalling that selection state should be
* reset to the specified selection.
*
* @param {WPBlockSelection} selectionStart The selection start.
* @param {WPBlockSelection} selectionEnd The selection end.
*
* @return {Object} Action object.
*/
export function resetSelection( selectionStart, selectionEnd ) {
return {
type: 'RESET_SELECTION',
selectionStart,
selectionEnd,
};
}

/**
* Returns an action object used in signalling that blocks have been received.
* Unlike resetBlocks, these should be appended to the existing known set, not
Expand Down
6 changes: 5 additions & 1 deletion packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ function* loadPostTypeEntities() {
kind: 'postType',
baseURL: '/wp/v2/' + postType.rest_base,
name,
transientEdits: { blocks: true },
transientEdits: {
blocks: true,
selectionStart: true,
selectionEnd: true,
},
mergedEdits: { meta: true },
};
} );
Expand Down
Loading