Welcome to mirror list, hosted at ThFree Co, Russian Federation.

playable.js « nodes « markdown « behaviors « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9cbd95a7bd89bf082d3376fde7db22f876412695 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* eslint-disable class-methods-use-this */
/* eslint-disable @gitlab/require-i18n-strings */

import { Node } from 'tiptap';
import { defaultMarkdownSerializer } from 'prosemirror-markdown';
import { HIGHER_PARSE_RULE_PRIORITY } from '../constants';

/**
 * 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
 */
export default class Playable extends Node {
  constructor() {
    super();
    this.mediaType = '';
    this.extraElementAttrs = {};
  }

  get name() {
    return this.mediaType;
  }

  get schema() {
    const attrs = {
      src: {},
      alt: {
        default: null,
      },
    };

    const parseDOM = [
      {
        tag: `.${this.mediaType}-container`,
        skip: true,
      },
      {
        tag: `.${this.mediaType}-container p`,
        priority: HIGHER_PARSE_RULE_PRIORITY,
        ignore: true,
      },
      {
        tag: `${this.mediaType}[src]`,
        getAttrs: (el) => ({ src: el.src, alt: el.dataset.title }),
      },
    ];

    const toDOM = (node) => [
      this.mediaType,
      {
        src: node.attrs.src,
        controls: true,
        'data-setup': '{}',
        'data-title': node.attrs.alt,
        ...this.extraElementAttrs,
      },
    ];

    return {
      attrs,
      group: 'block',
      draggable: true,
      parseDOM,
      toDOM,
    };
  }

  toMarkdown(state, node) {
    defaultMarkdownSerializer.nodes.image(state, node);
    state.closeBlock(node);
  }
}