xref: /plugin/diagrams/script/DiagramsMenuItemDispatcher.js (revision d40ddfceecc44927ffac0a9344681144f8217f11)
1class DiagramsMenuItemDispatcherMediaFile extends AbstractMenuItemDispatcher {
2
3    static type = 'mediafile';
4
5    static isAvailable(schema) {
6        return !!schema.nodes.diagrams;
7    }
8
9    static getMenuItem(schema) {
10        if (!this.isAvailable(schema)) {
11            throw new Error('Diagrams is missing in provided Schema!');
12        }
13
14        return new MenuItem({
15            command: (state, dispatch) => {
16                const {$from} = state.selection;
17                const index = $from.index();
18                if (!$from.parent.canReplaceWith(index, index, schema.nodes.diagrams)) {
19                    return false;
20                }
21                if (dispatch) {
22                    let textContent = '';
23                    state.selection.content().content.descendants((node) => {
24                        textContent += node.textContent;
25                        return false;
26                    });
27
28
29                    const dForm = new DiagramsForm(
30                        {
31                            title: textContent ? textContent : '',
32                            type: this.type,
33                        },
34                        (attributes) => {
35                            dispatch(
36                                state.tr.replaceSelectionWith(
37                                    schema.nodes.diagrams.create(attributes)
38                                )
39                            )
40                        }
41                    );
42                    dForm.show();
43                }
44                return true;
45            },
46            icon: document.createElement('span'), // FIXME
47            label: LANG.plugins.diagrams['PMMenuItem-' + this.type],
48        });
49    }
50}
51
52class DiagramsMenuItemDispatcherEmbedded extends DiagramsMenuItemDispatcherMediaFile {
53    static type = 'embed';
54}
55