Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy/Paste clipboard images #240

Open
kanishk619 opened this issue Apr 2, 2024 · 1 comment
Open

Copy/Paste clipboard images #240

kanishk619 opened this issue Apr 2, 2024 · 1 comment

Comments

@kanishk619
Copy link

kanishk619 commented Apr 2, 2024

Having a hook for the paste event would be really helpful in order to upload images directly from clipboard. I created a view in my project and added the below function for the paste event

function uploadClipboardImage(event, martorUploadUrl) {
    if (event.clipboardData.files.length >= 1) {
        let targetField = event.target.parentElement;
        if (targetField.id.startsWith('martor-')
            && targetField.classList.contains('martor-field')
            && targetField.classList.contains('ace_editor')
        ) {
            let aceEditor = ace.edit(targetField.id);
            for (const f of event.clipboardData.files) {
                const formData = new FormData();
                formData.append('martor-image-upload', f);
                fetch(martorUploadUrl, {
                    method: 'POST',
                    headers: {
                        'X-CSRFToken': '{{ csrf_token }}',
                    },
                    body: formData
                }).then(response => {
                    if (response.ok) {
                        response.json().then(
                            data => aceEditor.insert(`\n${data.link}\n`)
                        )
                    } else {
                        console.log(response.status)
                    }
                })
            }
        }
    }
}

data-upload-url attribute can also be used for getting the url from the event's target element. Official implementation would be highly appreciated.

@agusmakmun
Copy link
Owner

thank you @kanishk619, we will look into it 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants