debian-mirror-gitlab/app/assets/javascripts/behaviors/markdown/nodes/playable.js

57 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-04-04 11:22:00 +05:30
import { defaultMarkdownSerializer } from '~/lib/prosemirror_markdown_serializer';
2020-03-13 15:44:24 +05:30
/**
* Abstract base class for playable media, like video and audio.
* Must not be instantiated directly. Subclasses must set
* the `mediaType` property in their constructors.
* @abstract
*/
2022-05-07 20:08:51 +05:30
export default ({ mediaType, extraElementAttrs = {} }) => {
const attrs = {
src: {},
alt: {
default: null,
},
};
const parseDOM = [
{
// eslint-disable-next-line @gitlab/require-i18n-strings
tag: `.${mediaType}-container`,
getAttrs: (el) => ({
src: el.querySelector(mediaType).src,
alt: el.querySelector(mediaType).dataset.title,
}),
},
];
const toDOM = (node) => [
'span',
{ class: `media-container ${mediaType}-container` },
[
mediaType,
2020-03-13 15:44:24 +05:30
{
2022-05-07 20:08:51 +05:30
src: node.attrs.src,
controls: true,
'data-setup': '{}',
'data-title': node.attrs.alt,
...extraElementAttrs,
2020-03-13 15:44:24 +05:30
},
2022-05-07 20:08:51 +05:30
],
['a', { href: node.attrs.src }, node.attrs.alt],
];
2020-03-13 15:44:24 +05:30
2022-05-07 20:08:51 +05:30
return {
name: mediaType,
schema: {
2020-03-13 15:44:24 +05:30
attrs,
2021-11-11 11:23:49 +05:30
group: 'inline',
inline: true,
2020-03-13 15:44:24 +05:30
draggable: true,
parseDOM,
toDOM,
2022-05-07 20:08:51 +05:30
},
toMarkdown(state, node) {
defaultMarkdownSerializer.nodes.image(state, node);
},
};
};