xref: /plugin/mdimport/script.js (revision 7cb424c90c7e1aca7edae5f79baaec2e55306143)
1function addBtnActionMdimport($btn, props, edid) {
2    $btn.click(function() {
3        var fileInput = document.createElement('input');
4        fileInput.type = 'file';
5        fileInput.accept = '.md,.txt,text/markdown,text/plain';
6        fileInput.style.display = 'none';
7
8        fileInput.onchange = function(event) {
9            var file = event.target.files[0];
10            if (!file) return;
11
12            var reader = new FileReader();
13            reader.onload = function(e) {
14                var content = e.target.result;
15
16                fetch('lib/plugins/mdimport/convert.php', {
17                    method: 'POST',
18                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
19                    body: 'content=' + encodeURIComponent(content)
20                })
21                .then(response => response.text())
22                .then(convertedContent => {
23                    insertAtCarret(edid, convertedContent);
24                })
25                .catch(error => {
26                    console.error('Conversion error:', error);
27                    alert('Error converting file.');
28                });
29            };
30            reader.readAsText(file);
31        };
32
33        document.body.appendChild(fileInput);
34        fileInput.click();
35        document.body.removeChild(fileInput);
36        return false;
37    });
38    return 'mdimport';
39}
40