Skip to content

Commit 2beb91c

Browse files
authored
fix: set unit preview readonly on sidebar (#2008) (#2059)
Make the unit preview on the sidebar read-only and add `Truncate` to the `InplaceTextEditor`
1 parent d325a92 commit 2beb91c

File tree

4 files changed

+44
-21
lines changed

4 files changed

+44
-21
lines changed

src/generic/inplace-text-editor/index.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
Form,
77
Icon,
88
IconButton,
9+
Truncate,
910
Stack,
1011
} from '@openedx/paragon';
1112
import { Edit } from '@openedx/paragon/icons';
@@ -68,9 +69,9 @@ export const InplaceTextEditor: React.FC<InplaceTextEditorProps> = ({
6869
// In that case, we show the new text instead of the original in read-only mode as an optimistic update.
6970
if (readOnly || pendingSaveText) {
7071
return (
71-
<span className={textClassName}>
72+
<Truncate className={textClassName}>
7273
{pendingSaveText || text}
73-
</span>
74+
</Truncate>
7475
);
7576
}
7677

@@ -91,9 +92,9 @@ export const InplaceTextEditor: React.FC<InplaceTextEditorProps> = ({
9192
/>
9293
)
9394
: (
94-
<span className={textClassName}>
95+
<Truncate className={textClassName}>
9596
{text}
96-
</span>
97+
</Truncate>
9798
)}
9899
<IconButton
99100
src={Edit}

src/library-authoring/containers/UnitInfo.test.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,20 @@ describe('<UnitInfo />', () => {
108108
expect(await screen.findByTestId('unit-info-menu-toggle')).toBeInTheDocument();
109109
expect(screen.getByText(/text block published 1/i)).toBeInTheDocument();
110110
});
111+
112+
it('shows the preview tab by default and the component are readonly', async () => {
113+
render();
114+
const previewTab = await screen.findByText('Preview');
115+
expect(previewTab).toBeInTheDocument();
116+
expect(previewTab).toHaveAttribute('aria-selected', 'true');
117+
118+
// Check that there are no edit buttons for components titles
119+
expect(screen.queryAllByRole('button', { name: /edit/i }).length).toBe(0);
120+
121+
// Check that there are no drag handle for components
122+
expect(screen.queryAllByRole('button', { name: 'Drag to reorder' }).length).toBe(0);
123+
124+
// Check that there are no menu buttons for components
125+
expect(screen.queryAllByRole('button', { name: /component actions menu/i }).length).toBe(0);
126+
});
111127
});

src/library-authoring/containers/UnitInfo.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,11 @@ const UnitInfo = () => {
162162
activeKey={tab}
163163
onSelect={handleTabChange}
164164
>
165-
{renderTab(UNIT_INFO_TABS.Preview, <LibraryUnitBlocks preview />, intl.formatMessage(messages.previewTabTitle))}
165+
{renderTab(
166+
UNIT_INFO_TABS.Preview,
167+
<LibraryUnitBlocks readOnly />,
168+
intl.formatMessage(messages.previewTabTitle),
169+
)}
166170
{renderTab(UNIT_INFO_TABS.Manage, <ContainerOrganize />, intl.formatMessage(messages.manageTabTitle))}
167171
{renderTab(UNIT_INFO_TABS.Settings, 'Unit Settings', intl.formatMessage(messages.settingsTabTitle))}
168172
</Tabs>

src/library-authoring/units/LibraryUnitBlocks.tsx

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ interface LibraryBlockMetadataWithUniqueId extends LibraryBlockMetadata {
4949

5050
interface ComponentBlockProps {
5151
block: LibraryBlockMetadataWithUniqueId;
52-
preview?: boolean;
52+
readOnly?: boolean;
5353
isDragging?: boolean;
5454
}
5555

5656
/** Component header */
57-
const BlockHeader = ({ block }: ComponentBlockProps) => {
57+
const BlockHeader = ({ block, readOnly }: ComponentBlockProps) => {
5858
const intl = useIntl();
5959
const { showOnlyPublished } = useLibraryContext();
6060
const { showToast } = useContext(ToastContext);
@@ -98,21 +98,20 @@ const BlockHeader = ({ block }: ComponentBlockProps) => {
9898
gap={2}
9999
className="font-weight-bold"
100100
// Prevent parent card from being clicked.
101-
/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */
102101
onClick={(e) => e.stopPropagation()}
103102
>
104103
<Icon src={getItemIcon(block.blockType)} />
105104
<InplaceTextEditor
106105
onSave={handleSaveDisplayName}
107106
text={showOnlyPublished ? (block.publishedDisplayName ?? block.displayName) : block.displayName}
107+
readOnly={readOnly}
108108
/>
109109
</Stack>
110110
<ActionRow.Spacer />
111111
<Stack
112112
direction="horizontal"
113113
gap={3}
114114
// Prevent parent card from being clicked.
115-
/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */
116115
onClick={(e) => e.stopPropagation()}
117116
>
118117
{!showOnlyPublished && block.hasUnpublishedChanges && (
@@ -126,15 +125,15 @@ const BlockHeader = ({ block }: ComponentBlockProps) => {
126125
</Stack>
127126
</Badge>
128127
)}
129-
<TagCount size="sm" count={block.tagsCount} onClick={jumpToManageTags} />
130-
<ComponentMenu usageKey={block.originalId} />
128+
<TagCount size="sm" count={block.tagsCount} onClick={readOnly ? undefined : jumpToManageTags} />
129+
{!readOnly && <ComponentMenu usageKey={block.originalId} />}
131130
</Stack>
132131
</>
133132
);
134133
};
135134

136135
/** ComponentBlock to render preview of given component under Unit */
137-
const ComponentBlock = ({ block, preview, isDragging }: ComponentBlockProps) => {
136+
const ComponentBlock = ({ block, readOnly, isDragging }: ComponentBlockProps) => {
138137
const { showOnlyPublished } = useLibraryContext();
139138
const { navigateTo } = useLibraryRoutes();
140139

@@ -189,16 +188,16 @@ const ComponentBlock = ({ block, preview, isDragging }: ComponentBlockProps) =>
189188
<SortableItem
190189
id={block.id}
191190
componentStyle={getComponentStyle()}
192-
actions={<BlockHeader block={block} />}
191+
actions={<BlockHeader block={block} readOnly={readOnly} />}
193192
actionStyle={{
194193
borderRadius: '8px 8px 0px 0px',
195194
padding: '0.5rem 1rem',
196195
background: '#FBFAF9',
197196
borderBottom: 'solid 1px #E1DDDB',
198197
}}
199-
isClickable
200-
onClick={(e: { detail: number; }) => handleComponentSelection(e.detail)}
201-
disabled={preview}
198+
isClickable={!readOnly}
199+
onClick={!readOnly ? (e: { detail: number; }) => handleComponentSelection(e.detail) : undefined}
200+
disabled={readOnly}
202201
cardClassName={selected ? 'selected' : undefined}
203202
>
204203
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */}
@@ -223,20 +222,22 @@ const ComponentBlock = ({ block, preview, isDragging }: ComponentBlockProps) =>
223222

224223
interface LibraryUnitBlocksProps {
225224
/** set to true if it is rendered as preview
226-
* This disables drag and drop
225+
* This disables drag and drop, title edit and menus
227226
*/
228-
preview?: boolean;
227+
readOnly?: boolean;
229228
}
230229

231-
export const LibraryUnitBlocks = ({ preview }: LibraryUnitBlocksProps) => {
230+
export const LibraryUnitBlocks = ({ readOnly: componentReadOnly }: LibraryUnitBlocksProps) => {
232231
const intl = useIntl();
233232
const [orderedBlocks, setOrderedBlocks] = useState<LibraryBlockMetadataWithUniqueId[]>([]);
234233
const [isAddLibraryContentModalOpen, showAddLibraryContentModal, closeAddLibraryContentModal] = useToggle();
235234

236235
const [hidePreviewFor, setHidePreviewFor] = useState<string | null>(null);
237236
const { showToast } = useContext(ToastContext);
238237

239-
const { unitId, readOnly, showOnlyPublished } = useLibraryContext();
238+
const { unitId, readOnly: libraryReadOnly, showOnlyPublished } = useLibraryContext();
239+
240+
const readOnly = componentReadOnly || libraryReadOnly;
240241

241242
const { openAddContentSidebar } = useSidebarContext();
242243

@@ -301,10 +302,11 @@ export const LibraryUnitBlocks = ({ preview }: LibraryUnitBlocksProps) => {
301302
key={`${block.originalId}-${idx}-${block.modified}`}
302303
block={block}
303304
isDragging={hidePreviewFor === block.id}
305+
readOnly={readOnly}
304306
/>
305307
))}
306308
</DraggableList>
307-
{!preview && (
309+
{!readOnly && (
308310
<div className="d-flex">
309311
<div className="w-100 mr-2">
310312
<Button

0 commit comments

Comments
 (0)