1/**
2 * Creates a menu entry to insert a new mediafile diagram
3 */
4class DiagramsMenuItemDispatcherMediaFile extends AbstractMenuItemDispatcher {
5    /** The type of the node to be inserted */
6    static type = 'mediafile';
7
8    /**
9     * Check if the schema is available
10     *
11     * @param schema
12     * @returns {boolean}
13     */
14    static isAvailable(schema) {
15        return !!schema.nodes.diagrams;
16    }
17
18    /**
19     * Get the menu icon
20     *
21     * @todo the inline styles here should be part of the prosemirror plugin default styles
22     * @returns {HTMLSpanElement}
23     */
24    static getIcon() {
25        const svgIcon = document.createElement('img');
26        svgIcon.src = DOKU_BASE + 'lib/plugins/diagrams/img/diagramsnet.svg';
27        svgIcon.style.width = '1.2em';
28        svgIcon.style.height = '1.2em';
29        svgIcon.style.float = 'none';
30
31        const wrapper = document.createElement('span');
32        wrapper.appendChild(svgIcon);
33        wrapper.className = 'menuicon';
34
35        return wrapper;
36    }
37
38    /**
39     * Return the menu item
40     *
41     * @param schema
42     * @returns {MenuItem}
43     */
44    static getMenuItem(schema) {
45        if (!this.isAvailable(schema)) {
46            throw new Error('Diagrams is missing in provided Schema!');
47        }
48
49        return new MenuItem({
50            command: (state, dispatch) => {
51                const {$from} = state.selection;
52                const index = $from.index();
53                if (!$from.parent.canReplaceWith(index, index, schema.nodes.diagrams)) {
54                    return false;
55                }
56                if (dispatch) {
57                    let textContent = '';
58                    state.selection.content().content.descendants((node) => {
59                        textContent += node.textContent;
60                        return false;
61                    });
62
63
64                    const dForm = new DiagramsForm(
65                        {
66                            title: textContent ? textContent : '',
67                            type: this.type,
68                        },
69                        (attributes) => {
70                            dispatch(
71                                state.tr.replaceSelectionWith(
72                                    schema.nodes.diagrams.create(attributes)
73                                )
74                            )
75                        }
76                    );
77                    dForm.show();
78                }
79                return true;
80            },
81            icon: this.getIcon(),
82            label: LANG.plugins.diagrams['PMMenuItem-' + this.type],
83        });
84    }
85}
86
87/**
88 * Creates a menu entry to insert a new embedded diagram
89 */
90class DiagramsMenuItemDispatcherEmbedded extends DiagramsMenuItemDispatcherMediaFile {
91    static type = 'embed';
92}
93