Skip to content

Commit

Permalink
Disable undo when sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
mirka committed Jul 26, 2021
1 parent d104c1f commit f13f1b1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
42 changes: 24 additions & 18 deletions src/components/block-editor-toolbar/header-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,32 @@ function HeaderToolbar( props ) {
const inserterButton = useRef();
const { setIsInserterOpened } = useDispatch( 'isolated/editor' );
const isMobileViewport = useViewportMatch( 'medium', '<' );
const { hasFixedToolbar, isInserterEnabled, isTextModeEnabled, previewDeviceType, isInserterOpened } = useSelect(
( select ) => {
const { hasInserterItems, getBlockRootClientId, getBlockSelectionEnd } = select( 'core/block-editor' );
const {
hasFixedToolbar,
hasPeers,
isInserterEnabled,
isTextModeEnabled,
previewDeviceType,
isInserterOpened,
} = useSelect( ( select ) => {
const { hasInserterItems, getBlockRootClientId, getBlockSelectionEnd } = select( 'core/block-editor' );

return {
hasFixedToolbar: select( 'isolated/editor' ).isFeatureActive( 'fixedToolbar' ),
// This setting (richEditingEnabled) should not live in the block editor's setting.
isInserterEnabled:
select( 'isolated/editor' ).getEditorMode() === 'visual' &&
select( 'core/editor' ).getEditorSettings().richEditingEnabled &&
hasInserterItems( getBlockRootClientId( getBlockSelectionEnd() ) ),
isTextModeEnabled: select( 'isolated/editor' ).getEditorMode() === 'text',
previewDeviceType: 'Desktop',
isInserterOpened: select( 'isolated/editor' ).isInserterOpened(),
};
},
[]
);
return {
hasFixedToolbar: select( 'isolated/editor' ).isFeatureActive( 'fixedToolbar' ),
hasPeers: select( 'isolated/editor' ).hasPeers(),
// This setting (richEditingEnabled) should not live in the block editor's setting.
isInserterEnabled:
select( 'isolated/editor' ).getEditorMode() === 'visual' &&
select( 'core/editor' ).getEditorSettings().richEditingEnabled &&
hasInserterItems( getBlockRootClientId( getBlockSelectionEnd() ) ),
isTextModeEnabled: select( 'isolated/editor' ).getEditorMode() === 'text',
previewDeviceType: 'Desktop',
isInserterOpened: select( 'isolated/editor' ).isInserterOpened(),
};
}, [] );
const isLargeViewport = useViewportMatch( 'medium' );
const { inserter, toc, navigation, undo } = props.settings.iso.toolbar;
const { inserter, toc, navigation, undo: undoSetting } = props.settings.iso.toolbar;
const undo = undoSetting && ! hasPeers;
const displayBlockToolbar = ! isLargeViewport || previewDeviceType !== 'Desktop' || hasFixedToolbar;
const toolbarAriaLabel = displayBlockToolbar
? /* translators: accessibility text for the editor toolbar when Top Toolbar is on */
Expand Down
21 changes: 18 additions & 3 deletions src/components/block-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,26 @@ function BlockEditor( props ) {
);
}

export default withDispatch( ( dispatch ) => {
export default withDispatch( ( dispatch, _ownProps, { select } ) => {
const hasPeers = select( 'isolated/editor' ).hasPeers;
const { redo, undo } = dispatch( 'isolated/editor' );

const maybeUndo = ( actionCreator ) => () => {
if ( hasPeers() ) {
const noticeId = 'isolated/undo-disabled';
dispatch( 'core/notices' ).removeNotice( noticeId );
return dispatch( 'core/notices' ).createNotice(
'warning',
'Undo/redo is disabled while editing with other users.',
{ id: noticeId }
);
} else {
return actionCreator();
}
};

return {
redo,
undo,
redo: maybeUndo( redo ),
undo: maybeUndo( undo ),
};
} )( BlockEditor );
4 changes: 4 additions & 0 deletions src/store/peers/selectors.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export function getPeers( state ) {
return state.peers;
}

export function hasPeers( state ) {
return Object.keys( state.peers ).length > 0;
}

0 comments on commit f13f1b1

Please sign in to comment.