debian-mirror-gitlab/app/assets/javascripts/content_editor/extensions/playable.js
2022-07-17 14:43:12 +02:00

69 lines
1.5 KiB
JavaScript

/* eslint-disable @gitlab/require-i18n-strings */
import { Node } from '@tiptap/core';
const queryPlayableElement = (element, mediaType) => element.querySelector(mediaType);
export default Node.create({
group: 'inline',
inline: true,
draggable: true,
addAttributes() {
return {
uploading: {
default: false,
},
src: {
default: null,
parseHTML: (element) => {
const playable = queryPlayableElement(element, this.options.mediaType);
return playable.src;
},
},
canonicalSrc: {
default: null,
parseHTML: (element) => {
const playable = queryPlayableElement(element, this.options.mediaType);
return playable.dataset.canonicalSrc;
},
},
alt: {
default: null,
parseHTML: (element) => {
const playable = queryPlayableElement(element, this.options.mediaType);
return playable.dataset.title;
},
},
};
},
parseHTML() {
return [
{
tag: `.${this.options.mediaType}-container`,
},
];
},
renderHTML({ node }) {
return [
'span',
{ class: `media-container ${this.options.mediaType}-container` },
[
this.options.mediaType,
{
src: node.attrs.src,
controls: true,
'data-setup': '{}',
'data-title': node.attrs.alt,
...this.extraElementAttrs,
},
],
['a', { href: node.attrs.src }, node.attrs.title || node.attrs.alt || ''],
];
},
});