[QUESTION] Custom Component dragging issue
Question
This is how I've created a custom component:
comps.addType('custom-video', {
extend: 'image',
isComponent: function (el) {
if (el.getAttribute && el.getAttribute('element-type') === 'custom-video') {
return {type: 'custom-video'};
}
},
model: {
defaults: {
'video-preview-type': 'gif',
traits: [{
type: 'select',
label: 'Type',
name: 'video-preview-type',
options: [{value: 'gif', name: 'GIF'},
{value: 'image', name: 'Image'}],
changeProp: 1
}],
selectable: true
},
init: function () {
this.listenTo(this, 'change:video-preview-type', this.updateSrc);
},
updateSrc: function (component, value) {
const attributes = component.attributes.attributes;
let src;
if (value === 'gif') {
src = attributes['gif'];
} else if (value === 'image') {
src = attributes['image'];
}
component.view.$el.find('img').attr('src', src);
this.set('src', src);
}
},
view: {
events: {
dblclick: function () {
postDoubleClickEvent(EditorEvents.VIDEO_DOUBLE_CLICK);
}
}
}
});
This is my custom block:
blockManager.add('Video', {
label: 'Video',
content: '<a style="display: inline-block"><img element-type="vaetas-video" style="width: 400px; object-fit: cover" src="https://argoswimvideo.com/wp-content/uploads/2017/11/video-placeholder-1280x720-40.jpg"></a>',
attributes: {
class: 'gjs-block fa fa-film',
title: 'Insert Video'
}
});
The thing is that I have wrapped the image tag inside <a> tag when it is rendered to canvas. When I click on the element and try to drag it, only <img> tag gets dragged and <a> remains on it's original place.
Answers (3)
With this
view: {
dragstart: 'noDrag'
}
you're still letting the view capturing your click events, so fix it:
view: {
events: {
dragstart: 'noDrag'
}
}
Well, I see 2 different solutions for this case:
- Easy and fast, make the
custom-videocomponent not draggable, so you have to drag the parent to move it. The tiny downside is more from UX side, you force users to select the parent. - More solid solution. Make the
<a>element a custom component itself, something likecustom-video-wrapperwhich should contain by default thecustom-videocomponent
comps.addType('custom-video-wrapper', {
model: {
defaults: {
tagName: 'a',
components: { // here you define default components
type: 'custom-video',
selectable: false,
hoverable: false,
},
...
traits: ...
// Probably you'll need the same traits/properties
// but on their changes you'll propagate them to the
// children 'custom-video' component
Thanks @artf but a new problem is arising now, when I drag the custom-video-wrapper component across the canvas, custom-video isn't dragged with it, only the <a> tag is dragged.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #2697
[QUESTION] Problem with custom component type and table cell
Hello! I'm trying to add custom component type: And also I'm adding new block: But when I drop this block on the canvas the <td> tag doesn'...
Issue #2777
[QUESTION] How to update styles in Style Manager
Hello. I created custom component where I set backgraound-image via custom asset manager Custom block code: Command which I run to set new...
Issue #2696
[QUESTION]: How can I create block with background image with behavior like built-in "image"?
Hello, I'm trying to create block with background image (similar to Grapedrop Image Box). I added new component like this: And also I added...
Issue #2456
[QUESTION] (Possibly a bug) How to apply traits to components used for GrapesJs initialization
I am using This works but only for blocks which are added/drag-dropped to the editor manually after it is initialized. If we supply compone...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.