Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to set media element options via mediaElementAttributes option #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions dist/filepond-plugin-media-preview.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,33 @@ const createMediaView = (_) =>
tag: 'div',
ignoreRect: true,
create: ({ root, props }) => {
const { id } = props;
// request mediaElementAttributes option
const attributes = root.query('GET_MEDIA_ELEMENT_ATTRIBUTES');

// get all keys from attributes object
const attrsList = Object.keys(attributes);

// get item
const item = root.query('GET_ITEM', { id: props.id });
let tagName = isPreviewableAudio(item.file) ? 'audio' : 'video';

root.ref.media = document.createElement(tagName);
root.ref.media.setAttribute('controls', true);

// map through all given attributes and set'em
attrsList.forEach((attribute) => {
// null and false attributes' values won't be passed
// as soon as text is considered as truthy value and false
// being passed in setAttribute will be converted to string.
// Note: 0 is false, but it still can be treated as truthy value.
if (
typeof attributes[attribute] === 'undefined' ||
attributes[attribute] === null
)
return;

root.ref.media.setAttribute(attribute, attributes[attribute]);
});

root.element.appendChild(root.ref.media);

if (isPreviewableAudio(item.file)) {
Expand All @@ -165,8 +184,6 @@ const createMediaView = (_) =>
},
write: _.utils.createRoute({
DID_MEDIA_PREVIEW_LOAD: ({ root, props }) => {
const { id } = props;

// get item
const item = root.query('GET_ITEM', { id: props.id });
if (!item) return;
Expand Down Expand Up @@ -250,6 +267,7 @@ const createMediaWrapperView = (_) => {
const plugin = (fpAPI) => {
const { addFilter, utils } = fpAPI;
const { Type, createRoute } = utils;

const mediaWrapperView = createMediaWrapperView(fpAPI);

// called for each view that is created right after the 'create' method
Expand Down Expand Up @@ -317,6 +335,7 @@ const plugin = (fpAPI) => {
options: {
allowVideoPreview: [true, Type.BOOLEAN],
allowAudioPreview: [true, Type.BOOLEAN],
mediaElementAttributes: [{ controls: true }, Type.OBJECT],
},
};
};
Expand Down
2 changes: 1 addition & 1 deletion dist/filepond-plugin-media-preview.esm.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 25 additions & 5 deletions dist/filepond-plugin-media-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,29 @@
tag: 'div',
ignoreRect: true,
create: ({ root, props }) => {
const { id } = props; // get item
// request mediaElementAttributes option
const attributes = root.query('GET_MEDIA_ELEMENT_ATTRIBUTES'); // get all keys from attributes object

const attrsList = Object.keys(attributes); // get item

const item = root.query('GET_ITEM', {
id: props.id,
});
let tagName = isPreviewableAudio(item.file) ? 'audio' : 'video';
root.ref.media = document.createElement(tagName);
root.ref.media.setAttribute('controls', true);
root.ref.media = document.createElement(tagName); // map through all given attributes and set'em

attrsList.forEach((attribute) => {
// null and false attributes' values won't be passed
// as soon as text is considered as truthy value and false
// being passed in setAttribute will be converted to string.
// Note: 0 is false, but it still can be treated as truthy value.
if (
typeof attributes[attribute] === 'undefined' ||
attributes[attribute] === null
)
return;
root.ref.media.setAttribute(attribute, attributes[attribute]);
});
root.element.appendChild(root.ref.media);

if (isPreviewableAudio(item.file)) {
Expand All @@ -180,8 +195,7 @@
},
write: _.utils.createRoute({
DID_MEDIA_PREVIEW_LOAD: ({ root, props }) => {
const { id } = props; // get item

// get item
const item = root.query('GET_ITEM', {
id: props.id,
});
Expand Down Expand Up @@ -327,6 +341,12 @@
options: {
allowVideoPreview: [true, Type.BOOLEAN],
allowAudioPreview: [true, Type.BOOLEAN],
mediaElementAttributes: [
{
controls: true,
},
Type.OBJECT,
],
},
};
}; // fire pluginloaded event if running in browser, this allows registering the plugin when using async script tags
Expand Down
Loading