Skip to content
Open
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
39 changes: 19 additions & 20 deletions src/components/collaboration-mode-picker.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
import { Button, Popover, Icon } from '@wordpress/components';
import {
store as coreDataStore,
type CoreDataActions,
type CoreDataSelectors,
} from '@wordpress/core-data';
import { useSelect, useDispatch } from '@wordpress/data';
import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { pencil, seen } from '@wordpress/icons';

import {
store as collaborationModeStore,
type CollaborationModeStoreSelectors,
type CollaborationModeStoreActions,
CollaborationMode,
} from '@/store/collaboration-mode-store';
import { pencil, seen, chevronDown } from '@wordpress/icons';

import '@/components/collaboration-mode-picker.scss';

interface ModeOption {
value: CollaborationMode;
value: 'edit' | 'view';
label: string;
description: string;
icon: JSX.Element;
}

const MODES: ModeOption[] = [
{
value: CollaborationMode.EDIT,
value: 'edit',
label: __( 'Editing', 'vip-real-time-collaboration' ),
description: __( 'Edit document directly', 'vip-real-time-collaboration' ),
icon: pencil,
},
{
value: CollaborationMode.VIEW,
value: 'view',
label: __( 'Viewing', 'vip-real-time-collaboration' ),
description: __( 'Focus on content', 'vip-real-time-collaboration' ),
icon: seen,
Expand All @@ -43,17 +41,16 @@ export function CollaborationModePicker() {
const [ isPopoverVisible, setIsPopoverVisible ] = useState( false );
const [ popoverAnchor, setPopoverAnchor ] = useState< HTMLElement | null >( null );

// Default value is Edit mode
const selectedMode = useSelect< CollaborationModeStoreSelectors, CollaborationMode >( select =>
select( collaborationModeStore ).getMode()
const selectedCollaborationEditorMode = useSelect< CoreDataSelectors, 'edit' | 'view' >( select =>
select( coreDataStore ).getCollaboratorMode()
);

const { setMode } = useDispatch< CollaborationModeStoreActions >( collaborationModeStore );
const { setCollaboratorMode } = useDispatch< CoreDataActions >( coreDataStore );

const currentMode = MODES.find( mode => mode.value === selectedMode );
const currentMode = MODES.find( mode => mode.value === selectedCollaborationEditorMode );

const handleModeSelect = ( mode: CollaborationMode ) => {
setMode( mode );
const handleModeSelect = ( mode: 'edit' | 'view' ) => {
setCollaboratorMode( mode );
setIsPopoverVisible( false );
};

Expand All @@ -68,7 +65,9 @@ export function CollaborationModePicker() {
ref={ setPopoverAnchor }
text={ currentMode?.label }
icon={ currentMode?.icon }
/>
>
<Icon icon={ chevronDown } />
</Button>
{ isPopoverVisible && (
<Popover
anchor={ popoverAnchor }
Expand All @@ -79,7 +78,7 @@ export function CollaborationModePicker() {
>
<div className="vip-collaboration-mode-menu">
{ MODES.map( ( { value, label, description, icon } ) => {
const isSelected = value === selectedMode;
const isSelected = value === selectedCollaborationEditorMode;
return (
<button
key={ value }
Expand Down
13 changes: 2 additions & 11 deletions src/components/rtc-settings-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
SettingsStoreActions,
type SettingsStoreSelectors,
} from '../store/settings-store';
import { useModifyCodeEditor } from '@/hooks/use-modify-code-editor';
import { useDisableSidebarInteraction } from '@/hooks/use-disable-sidebar-interaction';
import { isDevelopment } from '@/utilities/config';
import { CursorRegistry } from '@/utilities/cursor-registry';

Expand Down Expand Up @@ -70,8 +70,7 @@ export function RTCSettingsPanel() {
// RTCOverlay. A ref is used to persist the instance across re-renders.
const cursorRegistry = useRef< CursorRegistry >( new CursorRegistry() );

// Manage read-only state for the code editor.
useModifyCodeEditor();
useDisableSidebarInteraction();

return (
<>
Expand Down Expand Up @@ -128,14 +127,6 @@ export function RTCSettingsPanel() {
checked={ isDebugToolsEnabled }
onChange={ ( enabled: boolean ) => setSetting( Setting.DEBUG_TOOLS, enabled ) }
/>

<ToggleControl
label={ __( 'Enable collaboration modes', 'vip-real-time-collaboration' ) }
checked={ isCollaborationModePickerEnabled }
onChange={ ( enabled: boolean ) =>
setSetting( Setting.COLLABORATION_MODE_PICKER, enabled )
}
/>
</>
) }

Expand Down
13 changes: 4 additions & 9 deletions src/hooks/use-disable-block-editing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@
* WordPress dependencies
*/
import { useBlockEditingMode } from '@wordpress/block-editor';
import { store as coreDataStore, type CoreDataSelectors } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import { addFilter, removeFilter } from '@wordpress/hooks';

/**
* Internal dependencies
*/
import {
type CollaborationModeStoreSelectors,
store as CollaborationModeStore,
CollaborationMode,
} from '@/store/collaboration-mode-store';
import {
Setting,
store as rtcSettingsStore,
Expand All @@ -35,14 +31,13 @@ export function useDisableBlockEditing() {
);

// Get the current collaboration mode (view or edit).
const collaborationMode = useSelect< CollaborationModeStoreSelectors, CollaborationMode >(
select => select( CollaborationModeStore ).getMode()
const currentCollaborationEditorMode = useSelect< CoreDataSelectors, 'view' | 'edit' >( select =>
select( coreDataStore ).getCollaboratorMode()
);

// Determine if view-only mode should be active.
// TODO: Remove the Collaboration mode enabled check once we complete the feature.
const isViewOnlyActive =
isCollaborationModeEnabled && collaborationMode === CollaborationMode.VIEW;
const isViewOnlyActive = isCollaborationModeEnabled && currentCollaborationEditorMode === 'view';

// Set the block editing mode based on whether view-only is active.
// This hook must be called unconditionally to follow the Rules of Hooks.
Expand Down
48 changes: 48 additions & 0 deletions src/hooks/use-disable-sidebar-interaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* WordPress dependencies
*/
import { store as coreDataStore, type CoreDataSelectors } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { useEffect } from '@wordpress/element';

/**
* Internal dependencies
*/
import {
Setting,
store as rtcSettingsStore,
type SettingsStoreSelectors,
} from '@/store/settings-store';

/**
* Custom hook that disables sidebar interaction when in view mode.
*/
export function useDisableSidebarInteraction() {
// Get the current collaboration mode (view or edit).
const currentCollaborationEditorMode = useSelect< CoreDataSelectors, 'view' | 'edit' >( select =>
select( coreDataStore ).getCollaboratorMode()
);

// Check if the Collaboration Mode Picker setting is enabled.
// TODO: Delete this once we complete the feature.
const isCollaborationModeEnabled = useSelect< SettingsStoreSelectors, boolean >( select =>
select( rtcSettingsStore ).getSetting( Setting.COLLABORATION_MODE_PICKER )
);

const shouldSidebarBeReadOnly =
isCollaborationModeEnabled && currentCollaborationEditorMode === 'view';

// Manage sidebar interaction.
useEffect( () => {
const sidebarElement = document.querySelector( '.editor-sidebar' );
const headerSettingsElement = document.querySelector( '.editor-header__settings' );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did try locking post saving, and auto saving instead of doing this. That is not the best solution because if the pre-publish panel is enabled, then any changes made there result in UI quirks. This could include a tag not saving for example which wouldn't be the best experience.

The thought I had is that, we can slowly remove each CSS tweak as we make changes within Gutenberg to allow native "read only" modes everywhere.


if (
sidebarElement instanceof HTMLDivElement &&
headerSettingsElement instanceof HTMLDivElement
) {
sidebarElement.style.pointerEvents = shouldSidebarBeReadOnly ? 'none' : 'auto';
headerSettingsElement.style.pointerEvents = shouldSidebarBeReadOnly ? 'none' : 'auto';
}
}, [ shouldSidebarBeReadOnly ] );
}
69 changes: 0 additions & 69 deletions src/hooks/use-modify-code-editor.ts

This file was deleted.

75 changes: 0 additions & 75 deletions src/store/collaboration-mode-store.ts

This file was deleted.

Loading
Loading