xref: /plugin/diagrams/script/embed-editbutton.js (revision 8893185b4446e39887031ebc3302ceed2ada0740)
1/**
2 * Attach editing button to editable diagrams
3 */
4document.addEventListener('DOMContentLoaded', () => {
5    // check if the current page is editable by the current user
6    if (!document.querySelector('head link[rel="edit"]')) return;
7
8    document.querySelectorAll('object.diagrams-svg[data-pos]').forEach(embed => {
9        const button = document.createElement('button');
10        button.className = 'diagrams-btn';
11        button.innerText = LANG.plugins.diagrams.editButtonShort;
12        button.title = LANG.plugins.diagrams.editButton;
13
14        const icon = ButtonFunctions.getButtonIcon('edit');
15        button.prepend(icon);
16
17        button.addEventListener('click', event => {
18            event.preventDefault();
19            const diagramsEditor = new DiagramsEditor(() => {
20                window.location.reload();
21            });
22            diagramsEditor.editEmbed(
23                JSINFO.id,
24                parseInt(embed.getAttribute('data-pos')),
25                parseInt(embed.getAttribute('data-len'))
26            );
27        });
28
29        embed.parentNode.querySelector('.diagrams-buttons').appendChild(button);
30    });
31});
32