uploading video in the image element
Question
hi. i was trying to implement uploading videos using the image element with the asset manager i stopped at the step where i wanted to replace the img tag with a video tag in the updateTarget call back, i am asking if there is an easy way for implementing that? thanks in advance. here is a snippet from my code
var assets = [];
var editor = grapesjs.init({
height: '100%',
showOffsets: 1,
noticeOnUnload: 0,
components: '',
style: '',
container: '#gjs',
fromElement: true,
plugins: ['gjs-preset-webpage'],
pluginsOpts: {
'gjs-preset-webpage': {}
},
assetManager: {
assets: assets,
noAssets: '',
upload: '/owner/website_front/media/asset_upload',
uploadName:'Media',
headers: {},
autoAdd: 1,
addBtnText: 'Add image',
// Text on upload input
uploadText: 'Drop files here or click to upload',
modalTitle: 'Select Image',
params: {},
},
storageManager: {
type: 'remote',
stepsBeforeSave: 1,
urlStore: '',
urlLoad: '',
params: {}, // For custom values on requests
}
});
// Get the Asset Manager module first
const am = editor.AssetManager;
am.addType('video', {
view: {
updateTarget(target) {
const src = this.model.get('src');
// Just to make things bit interesting, if it's an image type
// I put the svg as a data uri, content otherwise
if (target.get('type') == 'image') {
// Tip: you can also use `data:image/svg+xml;utf8,<svg ...` but you
// have to escape few chars
// target.set('src', `data:mime/type;base64,${btoa(svg)}`);
} else {
// target.set('content', svg);
}
},
getPreview() {
const src = this.model.get('src');
return `<div>
<video
class="gjs-no-pointer"
style="height: 100%; width: 100%;"
src="${src}">
</video></div>`;
},
getInfo() {
return '<div>'+ this.model.get('src') +'</div>';
}
},
isType(value) {
// eg. here you might check/parse/fetch url and try to
// understand the type
if (typeof value == 'string' ) {
value_parts = value.split('.');
ext = value_parts[value_parts.length - 1];
if(ext == 'mp4'){
console.log('4444');
return { type: 'video', src: value };
}
}
}
});
am.add({type:'video',src:'https://oidev.daftra.dev/files/db4715bd/medias/d9717_SampleVideo_1280x720_1mb.mp4'});
am.render(am.getAll().filter(asset => asset.get('type') == 'video'
));```
Answers (3)
You can just add it to the video by using the asset manager...
`this.editor.on('component:create', model => {
if (model.get('type') === 'video') {
this.editor.runCommand('open-assets', {
target: model
});
}
});`You can update the tag name of the component in this way model.set('tagName', 'new-tag')
@ecmascriptguru I would rather suggest to extend the video component and implement the modal (like in ComponentImageView)
Related Questions and Answers
Continue research with similar issue discussions.
Issue #2710
[QUESTION] Unsplash images in asset manager modal
Hello, I want to add a support for unsplash API when user opens AsssetManager modal with search input (similar to Grapedrop) Is there an ea...
Issue #3027
BUG: Image asset manager, on remove no ability to re-upload the same image that was removed
Are you able to reproduce the bug from the demo? No, in the image asset manager in the demo there is no option to remove images. What is th...
Issue #957
[Question] Add custom attributes (data-) to assets/images on Upload/Selection
Hi, I am trying my best to find the proper/best way to do this, but I need to attach two custom HTML5 data tags to an image once it is sele...
Issue #1218
[QUESTION] Asset Manager - Upload Image
Hey @artf , I have a question around Asset Manager. When uploading an image via computer or url the preview shows up at the asset collectio...
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.