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

feat: Add better speaker handling #419

Merged
merged 1 commit into from
Oct 3, 2024
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
7 changes: 5 additions & 2 deletions src/stories/transcript/extensions/theme/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ const DefaultSpeakerWrapper = ({
end,
setCurrentTime,
speaker,
totalSpeakers,
}: {
start: number;
end: number;
setCurrentTime?: ({ start, end }: { start: number; end: number }) => void;
speaker: string;
speaker: number;
totalSpeakers: number | null;
}) => {
return (
<p
Expand All @@ -65,7 +67,8 @@ const DefaultSpeakerWrapper = ({
}}
contentEditable={false}
>
{speaker} ({formatTime(start)} - {formatTime(end)})
Speaker {speaker} (su {totalSpeakers}) ({formatTime(start)} -{" "}
{formatTime(end)})
</p>
);
};
Expand Down
24 changes: 18 additions & 6 deletions src/stories/transcript/getParsedContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,18 @@ export type ObservationType = {
class ContentParser {
private observations?: ObservationType[];
private sentences?: SentenceType[];
private numberOfSpeakers: number | null = null;

constructor(observations?: ObservationType[], sentences?: SentenceType[]) {
constructor(
observations?: ObservationType[],
sentences?: SentenceType[],
numberOfSpeakers?: number
) {
this.observations = observations;
this.sentences = sentences;
if (numberOfSpeakers) {
this.numberOfSpeakers = numberOfSpeakers;
}
}

private wrapWordInObservations(word: WordType): TipTapContent {
Expand Down Expand Up @@ -84,7 +92,8 @@ class ContentParser {
return {
type: "Paragraph",
attrs: {
speakername: `Speaker ${paragraph.speaker}`,
totalSpeakers: this.numberOfSpeakers,
speaker: paragraph.speaker,
start: paragraph.start,
end: paragraph.end,
sentences: s,
Expand All @@ -108,9 +117,12 @@ class ContentParser {
export function getParsedContent(
content?: ParagraphType[],
observations?: ObservationType[],
sentences?: SentenceType[]
sentences?: SentenceType[],
numberOfSpeakers?: number
) {
return new ContentParser(observations, sentences).getParsedContent(
content
) as TipTapContent | undefined;
return new ContentParser(
observations,
sentences,
numberOfSpeakers
).getParsedContent(content) as TipTapContent | undefined;
}
3 changes: 2 additions & 1 deletion src/stories/transcript/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const Template: StoryFn<StoryArgs> = (args) => {
onSetCurrentTime: args.onSetCurrentTime
? args.onSetCurrentTime(setCurrentTime)
: undefined,
numberOfSpeakers: 1,
});

if (!editor) return <></>;
Expand Down Expand Up @@ -295,7 +296,7 @@ WithCustomTheme.args = {
speakerWrapper: ({ start, end, setCurrentTime, speaker }) => {
return (
<p>
{speaker} ({start} - {end}){" "}
Speaker {speaker + 1} ({start} - {end}){" "}
<IconButton
onClick={() => {
setCurrentTime && setCurrentTime({ start, end });
Expand Down
3 changes: 2 additions & 1 deletion src/stories/transcript/nodes/paragraph/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const Content = ({ node, editor }: { node: PMNode; editor: Editor }) => {
}}
start={node.attrs.start}
end={node.attrs.end}
speaker={node.attrs.speakername}
speaker={node.attrs.speaker}
totalSpeakers={node.attrs.totalSpeakers}
/>
<NodeViewContent className="content is-editable" />
</ParagraphWrapper>
Expand Down
7 changes: 5 additions & 2 deletions src/stories/transcript/nodes/paragraph/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ export const Paragraph = Node.create({

addAttributes() {
return {
speakername: {
default: "Speaker",
totalSpeakers: {
default: null,
},
speaker: {
default: 0,
},
start: {
default: 0,
Expand Down
9 changes: 8 additions & 1 deletion src/stories/transcript/useEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const useEditor = (
onSetCurrentTime,
themeExtension,
isEditable,
numberOfSpeakers,
}: {
content?: ParagraphType[];
observations?: ObservationType[];
Expand All @@ -32,6 +33,7 @@ export const useEditor = (
onSetCurrentTime?: (time: number) => void;
themeExtension?: Extension;
isEditable?: boolean;
numberOfSpeakers?: number;
},
deps?: React.DependencyList
) => {
Expand All @@ -53,7 +55,12 @@ export const useEditor = (
editorProps: {
handlePaste: () => true,
},
content: getParsedContent(content, observations, translations),
content: getParsedContent(
content,
observations,
translations,
numberOfSpeakers
),
},
deps
);
Expand Down
Loading