Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@project-sunbird/generic-editor-v2",
"version": "2.0.4",
"version": "2.0.5",
"description": "Sunbird Generic Editor — React content upload/generic editor (theme-reactive, multilingual). Fork of @sunbird/content-editor that previews via the legacy ekstep content renderer.",
"main": "dist/sunbird-generic-editor.umd.js",
"module": "dist/sunbird-generic-editor.es.js",
Expand Down
17 changes: 3 additions & 14 deletions src/ContentEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
/**
* ContentEditor — the editor root component.
*
* import { ContentEditor } from '@project-sunbird/generic-editor-v2';
* import '@project-sunbird/generic-editor-v2/dist/sunbird-generic-editor.css';
*
* <ContentEditor
* context={{ uid, sid, did, channel, pdata, user, framework }}
* contentId="do_123" // omit for a brand-new upload
* language="en"
* onClose={() => navigate('/workspace')}
* onTelemetryEvent={(e) => post(e)}
* />
*/
/** ContentEditor — the editor root component; render with a `context`, optional `contentId` (omit for a new upload), and its `onClose`/`onTelemetryEvent` callbacks. */
import React from 'react';
import './editor.scss';
import { useEditor, type UseEditorOptions } from './useEditor';
Expand All @@ -23,6 +10,7 @@ import MetadataDrawer from './components/MetadataDrawer';
import CollaboratorDrawer from './components/CollaboratorDrawer';
import ReviewDrawer from './components/ReviewDrawer';
import ReviewCommentsDrawer from './components/ReviewCommentsDrawer';
import TranscriptsDrawer from './components/TranscriptsDrawer';
import AssetPickerModal from './components/AssetPickerModal';
import Toast from './components/Toast';
import { t } from './i18n/i18n';
Expand Down Expand Up @@ -56,6 +44,7 @@ const ContentEditor: React.FC<ContentEditorProps> = (props) => {
<CollaboratorDrawer ed={ed} />
<ReviewDrawer ed={ed} />
<ReviewCommentsDrawer ed={ed} />
<TranscriptsDrawer ed={ed} />
<AssetPickerModal ed={ed} />

<Toast toast={ed.toast} />
Expand Down
27 changes: 24 additions & 3 deletions src/components/EditorPreview.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { render, screen } from '@testing-library/react';
import { describe, it, expect } from 'vitest';
import { render, screen, waitFor } from '@testing-library/react';
import { describe, it, expect, vi } from 'vitest';
import EditorPreview from './EditorPreview';
import { makeEd, mockContext, mockContent } from '../test/mockEd';
import { makeEd, mockContext, mockContent, mockService } from '../test/mockEd';

describe('<EditorPreview />', () => {
it('renders nothing when there is no content (negative)', () => {
Expand Down Expand Up @@ -31,4 +31,25 @@ describe('<EditorPreview />', () => {
const { container } = render(<EditorPreview ed={ed} context={mockContext} />);
expect(container.querySelector('iframe')).toHaveAttribute('src', '/preview.html?foo=1&webview=true');
});

it('does not fetch transcripts for non-video content (negative)', () => {
const service = mockService();
const ed = makeEd({ content: mockContent, service }); // mockContent is application/pdf
render(<EditorPreview ed={ed} context={mockContext} />);
expect(service.readTranscripts).not.toHaveBeenCalled();
});

it('fetches and maps transcripts for video content, remounting the iframe once they arrive', async () => {
const service = mockService({
readTranscripts: vi.fn().mockResolvedValue([
{ code: 'c_en', language: 'English', languageCode: 'en', captionsUrl: 'https://x/en.vtt', status: 'Live', sourceLanguage: true },
{ code: 'c_fr', language: 'French', languageCode: 'fr', status: 'Draft' }, // filtered: not Live
]),
});
const videoContent = { ...mockContent, mimeType: 'video/mp4' };
const ed = makeEd({ content: videoContent, service });
const { container } = render(<EditorPreview ed={ed} context={mockContext} />);
await waitFor(() => expect(service.readTranscripts).toHaveBeenCalledWith('do_1'));
await waitFor(() => expect(container.querySelector('iframe')).toBeInTheDocument());
});
});
59 changes: 45 additions & 14 deletions src/components/EditorPreview.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
import React, { useRef } from 'react';
import React, { useRef, useEffect, useState } from 'react';
import type { EditorController } from '../useEditor';
import type { ContentData, EditorContext } from '../types';
import type { ContentData, EditorContext, RawTranscript } from '../types';
import { getMimeTypeLabel, t } from '../i18n/i18n';
import { FileIcon } from '../icons';
import { isVideoMimeType } from '../constants';

/**
* Preview via the legacy ekstep content renderer — the same mechanism the old
* generic editor used (org.ekstep.genericeditorpreview). One renderer handles all
* mimeTypes (video/pdf/epub/ecml/html/scorm/h5p/youtube/url) through its coreplugins.
*
* Loads `content/preview/preview.html?webview=true` in an iframe (same-origin via the
* host proxy), then calls its global `initializePreview()` with the content id +
* metadata. No player-v2 dependency, no client-side unzip.
*/
interface PlayerTranscript {
language: string;
identifier: string;
languageCode: string;
artifactUrl: string;
wordByWordUrl?: string;
sourceLanguage?: boolean;
}

/** Maps raw enrichment.transcripts into the shape sunbird-video-player expects, mirroring the portal's ContentService mapping (artifactUrl here is the VTT, not transcript.json). */
function mapRawTranscripts(raw: RawTranscript[]): PlayerTranscript[] {
return raw
.filter((e): e is RawTranscript & { captionsUrl: string } =>
!!e.captionsUrl && (e.status ?? 'Live').toLowerCase() === 'live')
Comment thread
Deeksha1502 marked this conversation as resolved.
Resolved
.map((e) => ({
language: e.language || (e.languageCode || 'Unknown').toUpperCase(),
identifier: e.code ?? e.identifier ?? '',
languageCode: e.languageCode || '',
artifactUrl: e.captionsUrl,
wordByWordUrl: e.captionsUrl,
sourceLanguage: !!e.sourceLanguage,
}));
}

/** Preview via the legacy ekstep content renderer (all mimeTypes through its coreplugins), loaded in an iframe and driven via its global `initializePreview()`. */
const RendererPreview: React.FC<{
content: ContentData; context: EditorContext; previewUrl: string; previewConfig: Record<string, unknown>;
}> = ({ content, context, previewUrl, previewConfig }) => {
Expand Down Expand Up @@ -55,9 +72,23 @@ const RendererPreview: React.FC<{
};

const EditorPreview: React.FC<{ ed: EditorController; context: EditorContext }> = ({ ed, context }) => {
const { content, lang, previewUrl, previewConfig } = ed;
const { content, lang, previewUrl, previewConfig, service } = ed;
const [transcripts, setTranscripts] = useState<PlayerTranscript[]>([]);

useEffect(() => {
setTranscripts([]);
if (!content?.identifier || !isVideoMimeType(content.mimeType)) return;
service
.readTranscripts(content.identifier)
.then((raw) => setTranscripts(mapRawTranscripts(raw)))
.catch(() => setTranscripts([]));
}, [content?.identifier, content?.mimeType, service]);
Comment thread
Deeksha1502 marked this conversation as resolved.
Resolved

if (!content) return null;

// The iframe key below folds in transcripts.length to force one remount once they arrive, since merging them doesn't change identifier/artifactUrl.
const previewMetadata = transcripts.length ? { ...content, transcripts } : content;

return (
<div className="ce-preview-card">
<div className="ce-preview-bar">
Expand All @@ -68,8 +99,8 @@ const EditorPreview: React.FC<{ ed: EditorController; context: EditorContext }>
</div>
<div className="ce-preview-frame">
<RendererPreview
key={`${content.identifier}-${content.artifactUrl ?? ''}`}
content={content}
key={`${content.identifier}-${content.artifactUrl ?? ''}-${transcripts.length}`}
Comment thread
Deeksha1502 marked this conversation as resolved.
Resolved
content={previewMetadata}
context={context}
previewUrl={previewUrl}
previewConfig={previewConfig}
Expand Down
26 changes: 26 additions & 0 deletions src/components/Header.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,30 @@ describe('<Header />', () => {
fireEvent.click(btn);
expect(publish).toHaveBeenCalled();
});

it('shows "View transcript" for video content once transcripts exist, and opens the drawer', () => {
const setDrawer = vi.fn();
const ed = makeEd({
view: 'player', mode: 'edit', setDrawer, hasTranscripts: true,
content: { ...mockContent, mimeType: 'video/mp4' },
});
render(<Header ed={ed} />);
fireEvent.click(screen.getByRole('button', { name: 'View transcript' }));
expect(setDrawer).toHaveBeenCalledWith('transcripts');
});

it('hides "View transcript" for video content with no transcripts yet (negative)', () => {
const ed = makeEd({
view: 'player', mode: 'edit', hasTranscripts: false,
content: { ...mockContent, mimeType: 'video/mp4' },
});
render(<Header ed={ed} />);
expect(screen.queryByRole('button', { name: 'View transcript' })).not.toBeInTheDocument();
});

it('hides "View transcript" for non-video content (negative)', () => {
const ed = makeEd({ view: 'player', mode: 'edit', content: { ...mockContent, mimeType: 'application/pdf' } });
render(<Header ed={ed} />);
expect(screen.queryByRole('button', { name: 'View transcript' })).not.toBeInTheDocument();
});
});
17 changes: 15 additions & 2 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
import type { EditorController } from '../useEditor';
import { t } from '../i18n/i18n';
import {
ImageIcon, PencilIcon, SaveIcon, CloseIcon, SendIcon, UserPlusIcon, CheckIcon, CommentIcon,
ImageIcon, PencilIcon, SaveIcon, CloseIcon, SendIcon, UserPlusIcon, CheckIcon, CommentIcon, CaptionsIcon,
} from '../icons';
import { STATUS } from '../constants';

const Header: React.FC<{ ed: EditorController }> = ({ ed }) => {
const { content, lang, mode, setDrawer, saveDraft, close, busy, busyAction, setReviewErrors, setReviewSubmitMode, headerLogo, hasReviewComments } = ed;
const { content, lang, mode, setDrawer, saveDraft, close, busy, busyAction, setReviewErrors, setReviewSubmitMode, headerLogo, hasReviewComments, hasTranscripts } = ed;
const savingDraft = busyAction === 'save-draft';
const hasContent = !!content?.identifier && ed.view === 'player';
const isDraft = (content?.status ?? STATUS.DRAFT) === STATUS.DRAFT;

Check warning on line 13 in src/components/Header.tsx

View workflow job for this annotation

GitHub Actions / Lint, Build & Test

'isDraft' is assigned a value but never used
const title = content?.name || t(lang, 'UNTITLED');
const canEdit = mode === 'edit';
const isReviewer = mode === 'review';
Expand Down Expand Up @@ -61,6 +61,19 @@
</button>
)}

{hasContent && hasTranscripts && (
<button
type="button"
className="ce-icon-btn"
onClick={() => setDrawer('transcripts')}
disabled={busy}
data-tooltip={t(lang, 'VIEW_TRANSCRIPT')}
aria-label={t(lang, 'VIEW_TRANSCRIPT')}
>
<CaptionsIcon size={16} />
</button>
)}

{canEdit && !isReviewer && hasReviewComments && (
<button
type="button"
Expand Down
Loading
Loading