Skip to content

Instantly share code, notes, and snippets.

@bettysteger
Last active February 8, 2024 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bettysteger/9d613904c2b14c15182e6204863b79b3 to your computer and use it in GitHub Desktop.
Save bettysteger/9d613904c2b14c15182e6204863b79b3 to your computer and use it in GitHub Desktop.
Editorjs: Overwrite Embed Block Tool to show an input field where you can paste the URL
// Overwrite Embed Block Tool
import EmbedTool from '@editorjs/embed';
export default class Embed extends EmbedTool {
static get toolbox() {
return {
title: 'YouTube',
icon: '<i class="fab fa-youtube"></i>'
};
}
/**
* Render Embed tool content
*
* @returns {HTMLElement}
*/
render() {
if (!this.data.service) {
const container = document.createElement('div');
this.element = container;
const input = document.createElement('input');
input.classList.add('cdx-input');
input.placeholder = 'https://www.youtube.com/watch?v=w8vsuOXZBXc';
input.type = 'url';
input.addEventListener('paste', (event) => {
const url = event.clipboardData.getData('text');
const service = Object.keys(Embed.services).find((key) => Embed.services[key].regex.test(url));
if (service) {
this.onPaste({detail: {key: service, data: url}});
}
});
container.appendChild(input);
return container;
}
return super.render();
}
validate(savedData) {
return savedData.service && savedData.source ? true : false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment