2024-07-25 11:25:08 -04:00
|
|
|
import {
|
|
|
|
DOMConversion,
|
|
|
|
DOMConversionMap, DOMConversionOutput,
|
|
|
|
ElementNode,
|
|
|
|
LexicalEditor,
|
|
|
|
LexicalNode,
|
|
|
|
SerializedElementNode, Spread
|
|
|
|
} from 'lexical';
|
|
|
|
import type {EditorConfig} from "lexical/LexicalEditor";
|
2024-08-03 13:14:01 -04:00
|
|
|
|
|
|
|
import {el} from "../utils/dom";
|
2024-09-06 09:07:10 -04:00
|
|
|
import {
|
|
|
|
CommonBlockAlignment,
|
|
|
|
SerializedCommonBlockNode,
|
|
|
|
setCommonBlockPropsFromElement,
|
|
|
|
updateElementWithCommonBlockProps
|
|
|
|
} from "./_common";
|
|
|
|
import {elem} from "../../services/dom";
|
2024-07-25 11:25:08 -04:00
|
|
|
|
|
|
|
export type MediaNodeTag = 'iframe' | 'embed' | 'object' | 'video' | 'audio';
|
|
|
|
export type MediaNodeSource = {
|
|
|
|
src: string;
|
|
|
|
type: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type SerializedMediaNode = Spread<{
|
|
|
|
tag: MediaNodeTag;
|
|
|
|
attributes: Record<string, string>;
|
|
|
|
sources: MediaNodeSource[];
|
2024-09-06 09:07:10 -04:00
|
|
|
}, SerializedCommonBlockNode>
|
2024-07-25 11:25:08 -04:00
|
|
|
|
|
|
|
const attributeAllowList = [
|
2024-09-06 09:07:10 -04:00
|
|
|
'width', 'height', 'style', 'title', 'name',
|
2024-07-25 11:25:08 -04:00
|
|
|
'src', 'allow', 'allowfullscreen', 'loading', 'sandbox',
|
|
|
|
'type', 'data', 'controls', 'autoplay', 'controlslist', 'loop',
|
|
|
|
'muted', 'playsinline', 'poster', 'preload'
|
|
|
|
];
|
|
|
|
|
|
|
|
function filterAttributes(attributes: Record<string, string>): Record<string, string> {
|
|
|
|
const filtered: Record<string, string> = {};
|
2024-07-27 12:25:30 -04:00
|
|
|
for (const key of Object.keys(attributes)) {
|
2024-07-25 11:25:08 -04:00
|
|
|
if (attributeAllowList.includes(key)) {
|
|
|
|
filtered[key] = attributes[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filtered;
|
|
|
|
}
|
|
|
|
|
2024-09-06 09:07:10 -04:00
|
|
|
function domElementToNode(tag: MediaNodeTag, element: HTMLElement): MediaNode {
|
2024-07-25 11:25:08 -04:00
|
|
|
const node = $createMediaNode(tag);
|
|
|
|
|
|
|
|
const attributes: Record<string, string> = {};
|
|
|
|
for (const attribute of element.attributes) {
|
|
|
|
attributes[attribute.name] = attribute.value;
|
|
|
|
}
|
|
|
|
node.setAttributes(attributes);
|
|
|
|
|
|
|
|
const sources: MediaNodeSource[] = [];
|
|
|
|
if (tag === 'video' || tag === 'audio') {
|
|
|
|
for (const child of element.children) {
|
|
|
|
if (child.tagName === 'SOURCE') {
|
|
|
|
const src = child.getAttribute('src');
|
|
|
|
const type = child.getAttribute('type');
|
|
|
|
if (src && type) {
|
|
|
|
sources.push({ src, type });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node.setSources(sources);
|
|
|
|
}
|
|
|
|
|
2024-09-06 09:07:10 -04:00
|
|
|
setCommonBlockPropsFromElement(element, node);
|
|
|
|
|
2024-07-25 11:25:08 -04:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class MediaNode extends ElementNode {
|
2024-09-06 09:07:10 -04:00
|
|
|
__id: string = '';
|
|
|
|
__alignment: CommonBlockAlignment = '';
|
2024-07-25 11:25:08 -04:00
|
|
|
__tag: MediaNodeTag;
|
|
|
|
__attributes: Record<string, string> = {};
|
|
|
|
__sources: MediaNodeSource[] = [];
|
|
|
|
|
|
|
|
static getType() {
|
|
|
|
return 'media';
|
|
|
|
}
|
|
|
|
|
|
|
|
static clone(node: MediaNode) {
|
2024-08-11 11:08:51 -04:00
|
|
|
const newNode = new MediaNode(node.__tag, node.__key);
|
|
|
|
newNode.__attributes = Object.assign({}, node.__attributes);
|
|
|
|
newNode.__sources = node.__sources.map(s => Object.assign({}, s));
|
|
|
|
return newNode;
|
2024-07-25 11:25:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(tag: MediaNodeTag, key?: string) {
|
|
|
|
super(key);
|
|
|
|
this.__tag = tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
setTag(tag: MediaNodeTag) {
|
|
|
|
const self = this.getWritable();
|
|
|
|
self.__tag = tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
getTag(): MediaNodeTag {
|
|
|
|
const self = this.getLatest();
|
|
|
|
return self.__tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
setAttributes(attributes: Record<string, string>) {
|
|
|
|
const self = this.getWritable();
|
|
|
|
self.__attributes = filterAttributes(attributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
getAttributes(): Record<string, string> {
|
|
|
|
const self = this.getLatest();
|
|
|
|
return self.__attributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
setSources(sources: MediaNodeSource[]) {
|
|
|
|
const self = this.getWritable();
|
|
|
|
self.__sources = sources;
|
|
|
|
}
|
|
|
|
|
|
|
|
getSources(): MediaNodeSource[] {
|
|
|
|
const self = this.getLatest();
|
|
|
|
return self.__sources;
|
|
|
|
}
|
|
|
|
|
|
|
|
setSrc(src: string): void {
|
|
|
|
const attrs = Object.assign({}, this.getAttributes());
|
|
|
|
if (this.__tag ==='object') {
|
|
|
|
attrs.data = src;
|
|
|
|
} else {
|
|
|
|
attrs.src = src;
|
|
|
|
}
|
|
|
|
this.setAttributes(attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
setWidthAndHeight(width: string, height: string): void {
|
|
|
|
const attrs = Object.assign(
|
|
|
|
{},
|
|
|
|
this.getAttributes(),
|
|
|
|
{width, height},
|
|
|
|
);
|
|
|
|
this.setAttributes(attrs);
|
|
|
|
}
|
|
|
|
|
2024-09-06 09:07:10 -04:00
|
|
|
setId(id: string) {
|
|
|
|
const self = this.getWritable();
|
|
|
|
self.__id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
getId(): string {
|
|
|
|
const self = this.getLatest();
|
|
|
|
return self.__id;
|
|
|
|
}
|
|
|
|
|
|
|
|
setAlignment(alignment: CommonBlockAlignment) {
|
|
|
|
const self = this.getWritable();
|
|
|
|
self.__alignment = alignment;
|
|
|
|
}
|
|
|
|
|
|
|
|
getAlignment(): CommonBlockAlignment {
|
|
|
|
const self = this.getLatest();
|
|
|
|
return self.__alignment;
|
|
|
|
}
|
|
|
|
|
2024-07-25 11:25:08 -04:00
|
|
|
createDOM(_config: EditorConfig, _editor: LexicalEditor) {
|
|
|
|
const sources = (this.__tag === 'video' || this.__tag === 'audio') ? this.__sources : [];
|
|
|
|
const sourceEls = sources.map(source => el('source', source));
|
2024-09-06 09:07:10 -04:00
|
|
|
const element = el(this.__tag, this.__attributes, sourceEls);
|
|
|
|
updateElementWithCommonBlockProps(element, this);
|
|
|
|
return element;
|
2024-07-25 11:25:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
updateDOM(prevNode: unknown, dom: HTMLElement) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static importDOM(): DOMConversionMap|null {
|
|
|
|
|
|
|
|
const buildConverter = (tag: MediaNodeTag) => {
|
|
|
|
return (node: HTMLElement): DOMConversion|null => {
|
|
|
|
return {
|
|
|
|
conversion: (element: HTMLElement): DOMConversionOutput|null => {
|
|
|
|
return {
|
|
|
|
node: domElementToNode(tag, element),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
priority: 3,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
iframe: buildConverter('iframe'),
|
|
|
|
embed: buildConverter('embed'),
|
|
|
|
object: buildConverter('object'),
|
|
|
|
video: buildConverter('video'),
|
|
|
|
audio: buildConverter('audio'),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
exportJSON(): SerializedMediaNode {
|
|
|
|
return {
|
|
|
|
...super.exportJSON(),
|
2024-07-27 12:25:30 -04:00
|
|
|
type: 'media',
|
2024-07-25 11:25:08 -04:00
|
|
|
version: 1,
|
2024-09-06 09:07:10 -04:00
|
|
|
id: this.__id,
|
|
|
|
alignment: this.__alignment,
|
2024-07-25 11:25:08 -04:00
|
|
|
tag: this.__tag,
|
|
|
|
attributes: this.__attributes,
|
|
|
|
sources: this.__sources,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static importJSON(serializedNode: SerializedMediaNode): MediaNode {
|
2024-09-06 09:07:10 -04:00
|
|
|
const node = $createMediaNode(serializedNode.tag);
|
|
|
|
node.setId(serializedNode.id);
|
|
|
|
node.setAlignment(serializedNode.alignment);
|
|
|
|
return node;
|
2024-07-25 11:25:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export function $createMediaNode(tag: MediaNodeTag) {
|
|
|
|
return new MediaNode(tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function $createMediaNodeFromHtml(html: string): MediaNode | null {
|
|
|
|
const parser = new DOMParser();
|
|
|
|
const doc = parser.parseFromString(`<body>${html}</body>`, 'text/html');
|
|
|
|
|
|
|
|
const el = doc.body.children[0];
|
2024-09-06 09:07:10 -04:00
|
|
|
if (!(el instanceof HTMLElement)) {
|
2024-07-25 11:25:08 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tag = el.tagName.toLowerCase();
|
|
|
|
const validTypes = ['embed', 'iframe', 'video', 'audio', 'object'];
|
|
|
|
if (!validTypes.includes(tag)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return domElementToNode(tag as MediaNodeTag, el);
|
|
|
|
}
|
|
|
|
|
2024-07-27 12:25:30 -04:00
|
|
|
const videoExtensions = ['mp4', 'mpeg', 'm4v', 'm4p', 'mov'];
|
|
|
|
const audioExtensions = ['3gp', 'aac', 'flac', 'mp3', 'm4a', 'ogg', 'wav', 'webm'];
|
|
|
|
const iframeExtensions = ['html', 'htm', 'php', 'asp', 'aspx'];
|
|
|
|
|
|
|
|
export function $createMediaNodeFromSrc(src: string): MediaNode {
|
|
|
|
let nodeTag: MediaNodeTag = 'iframe';
|
|
|
|
const srcEnd = src.split('?')[0].split('/').pop() || '';
|
|
|
|
const extension = (srcEnd.split('.').pop() || '').toLowerCase();
|
|
|
|
if (videoExtensions.includes(extension)) {
|
|
|
|
nodeTag = 'video';
|
|
|
|
} else if (audioExtensions.includes(extension)) {
|
|
|
|
nodeTag = 'audio';
|
|
|
|
} else if (extension && !iframeExtensions.includes(extension)) {
|
|
|
|
nodeTag = 'embed';
|
|
|
|
}
|
|
|
|
|
|
|
|
return new MediaNode(nodeTag);
|
|
|
|
}
|
|
|
|
|
2024-08-11 11:08:51 -04:00
|
|
|
export function $isMediaNode(node: LexicalNode | null | undefined): node is MediaNode {
|
2024-07-25 11:25:08 -04:00
|
|
|
return node instanceof MediaNode;
|
|
|
|
}
|
|
|
|
|
2024-08-11 11:08:51 -04:00
|
|
|
export function $isMediaNodeOfTag(node: LexicalNode | null | undefined, tag: MediaNodeTag): boolean {
|
2024-07-25 11:25:08 -04:00
|
|
|
return node instanceof MediaNode && (node as MediaNode).getTag() === tag;
|
|
|
|
}
|