Issue #2143💬 AnsweredOpened July 16, 2019by chauhankiran0 reactions

How can I create an existing image as link

快速解答by miky9585

I've solved in this way: on backend script: var originalImage = editor.DomComponents.getType('image'); editor.DomComponents.addType('image', { model: originalImage.model.extend({ defaults: Object.assign({}, originalImage.model.prototype.defaults, { traits:[{ type: "text", label: "ID", name: "id" }, { type: "text", lab...

Read full answer below ↓

Question

I have an existing template that is currently displaying the image in the middle of the page. Now, I want to make it link or clickable so that if the user clicks on the image it should open given href. How can do that? Currently, we have link block and inside of it, we can place an image. But, I'm asking for the reverse - Existing image to link.

Answers (3)

miky9585July 24, 2019
代码片段TEXT
I've solved in this way:
on backend script:

	var originalImage = editor.DomComponents.getType('image');
	editor.DomComponents.addType('image', {
		model: originalImage.model.extend({
			defaults: Object.assign({},
	            originalImage.model.prototype.defaults, { 
	                traits:[{
		                	type: "text",
		                	label: "ID",
		                	name: "id"
	                	},
	                	{
		                	type: "text",
		                	label: "URL",
		                	name: "data-img-url"	
	                	},
	                	{
		                	type: 'select',
				            label: 'Target',
				            name: 'data-img-target',
				            value: "_top",
				            options: [
				              {value: '_top', name: 'Same Page'},
				              {value: '_blank', name: 'New Page'}, 
				            ]	
	                	}
	                ]
	            }), 
	        
			
		},
		{
			isComponent: function(el) {
				if("IMG"==el.tagName) return {
                    type: "image"
                }

			},
		}),
		view: originalImage.view
	});

When show page (I've stored on db)

	
	if( $('[data-img-url]').length > 0 ){  
		$.each($('[data-img-url]'), function(i,e){ 
			if( typeof $(e).data('img-url') != 'undefined' && $(e).data('img-url').length > 0){
				var href = $(e).data('img-url'), target = $(e).data('img-target') || '_top';
				var a = $('<a/>').attr('href', href).attr('target', target);
				$(e).wrap(a);
				$(e).removeAttr('data-img-url').removeAttr('data-img-target'); // KEEP CODE CLEAN
			} 
		})
	}
miky9585July 24, 2019
代码片段TEXT
SEO Friendly backend (added Alt attr)

	var originalImage = editor.DomComponents.getType('image');
	editor.DomComponents.addType('image', {
		model: originalImage.model.extend({
			defaults: Object.assign({},
	            originalImage.model.prototype.defaults, { 
	                traits:[{
		                	type: "text",
		                	label: "ID",
		                	name: "id"
	                	},
	                	{
		                	type: "text",
		                	label: "URL",
		                	name: "data-img-url"	
	                	},
	                	{
		                	type: "text",
		                	label: "Alternative Text",
		                	name: "alt"	
	                	},
	                	{
		                	type: 'select',
				            label: 'Target',
				            name: 'data-img-target',
				            value: "_top",
				            options: [
				              {value: '_top', name: 'This window'},
				              {value: '_blank', name: 'New window'}, 
				            ]	
	                	}
	                ]
	            }), 
	        
			
		},
		{
			isComponent: function(el) {
				if("IMG"==el.tagName) return {
                    type: "image"
                }

			},
		}),
		view: originalImage.view
	});
artfJuly 31, 2019

@chauhankiran the correct step would be to create a custom component, kind of link-image which would replace the image one. The root of this component will be the <a> element and the inner one will be the image.

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

Free option

Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.

Browse free plugins →
Premium option

Premium plugins ship with support, regular updates, and production-ready features — save days of integration work.

Browse premium plugins →

Related tutorials

In-depth guides on the same topic.

All tutorials →

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.