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

Add simple audio support for WYSIWYG #7452

Merged
merged 1 commit into from Aug 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions app/src/interfaces/input-rich-text-html/useMedia.ts
Expand Up @@ -7,6 +7,8 @@ type MediaSelection = {
source: string;
width?: number;
height?: number;
tag?: 'video' | 'audio';
type?: string;
};

type MediaButton = {
Expand Down Expand Up @@ -34,7 +36,7 @@ type UsableMedia = {
export default function useMedia(editor: Ref<any>, imageToken: Ref<string | undefined>): UsableMedia {
const mediaDrawerOpen = ref(false);
const mediaSelection = ref<MediaSelection | null>(null);
const openMediaTab = ref(['video']);
const openMediaTab = ref(['video', 'audio']);
const embed = ref('');
const startEmbed = ref('');

Expand Down Expand Up @@ -100,29 +102,36 @@ export default function useMedia(editor: Ref<any>, imageToken: Ref<string | unde
watch(mediaSelection, (vid) => {
if (embed.value === '') {
if (vid === null) return;
embed.value = `<video width="${vid.width}" height="${vid.height}" controls="controls"><source src="${vid.source}" /></video>`;
embed.value = `<${vid.tag} width="${vid.width}" height="${vid.height}" controls><source src="${vid.source}" type="${vid.type}" /></${vid.tag}>`;
} else {
embed.value = embed.value
.replace(/src=".*?"/g, `src="${vid?.source}"`)
.replace(/width=".*?"/g, `width="${vid?.width}"`)
.replace(/height=".*?"/g, `height="${vid?.height}"`);
.replace(/height=".*?"/g, `height="${vid?.height}"`)
.replace(/type=".*?"/g, `type="${vid?.type}"`)
.replaceAll(/<(video|audio)/g, `<${vid?.tag}`)
.replaceAll(/<\/(video|audio)/g, `</${vid?.tag}`);
}
});

watch(embed, (newEmbed) => {
if (newEmbed === '') {
mediaSelection.value = null;
} else {
const tag = /<(video|audio)/g.exec(newEmbed)?.[1];
const source = /src="(.*?)"/g.exec(newEmbed)?.[1] || undefined;
const width = Number(/width="(.*?)"/g.exec(newEmbed)?.[1]) || undefined;
const height = Number(/height="(.*?)"/g.exec(newEmbed)?.[1]) || undefined;
const type = /type="(.*?)"/g.exec(newEmbed)?.[1] || undefined;

if (source === undefined) return;

mediaSelection.value = {
tag: tag === 'audio' ? 'audio' : 'video',
source,
width,
height,
type,
};
}
});
Expand Down Expand Up @@ -152,12 +161,15 @@ export default function useMedia(editor: Ref<any>, imageToken: Ref<string | unde

function onMediaSelect(media: Record<string, any>) {
const url = getPublicURL() + 'assets/' + media.id;
const tag = media.type.startsWith('audio') ? 'audio' : 'video';
const source = imageToken.value ? addTokenToURL(url, imageToken.value) : url;

mediaSelection.value = {
source,
width: media.width || 300,
height: media.height || 150,
tag,
type: media.type,
};
}

Expand Down