Issue #2103Opened June 27, 2019by imanubhardwaj1 reactions

[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.

Live demo

Answers (3)

artfJuly 31, 20191 reactions

With this

view: {
    dragstart: 'noDrag'
  }

you're still letting the view capturing your click events, so fix it:

view: {
    events: {
      dragstart: 'noDrag'
    }
  }
artfJuly 4, 20190 reactions

Well, I see 2 different solutions for this case:

  1. Easy and fast, make the custom-video component 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.
  2. More solid solution. Make the <a> element a custom component itself, something like custom-video-wrapper which should contain by default the custom-video component
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
imanubhardwajJuly 4, 20190 reactions

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.

Live Demo

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.