Skip to content
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
30 changes: 29 additions & 1 deletion src/core/Components/ItemList/ListViewBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ export default class ListViewBase extends React.Component {
}
};

handleModalSelection = (details) => {
if (!details) return;
const modal = window?.parent?.Joomla.Modal?.getCurrent();
if (!modal) return;

window.parent.Joomla.selectedMediaFile = details;
Joomla.selectedMediaFile = details;

let selectButton = modal.querySelector('.btn.btn-secondary.button-save-selected') ??
modal.querySelector('.button.button-success.btn.btn-success') ??
modal.querySelector('.btn.btn-success.button-save-selected') ??
modal.querySelector('.button-save-selected');
if (selectButton) {
selectButton.click();
}
}

handleDoubleClick = item => e => {
e.preventDefault();
e.stopPropagation();
Expand All @@ -27,7 +44,18 @@ export default class ListViewBase extends React.Component {
toast.info('Unsupported file type.');
return;
}
handlers.handle(item);

let details;
if(Joomla.selectedMediaFile || window.parent.Joomla.selectedMediaFile){
details = JSON.parse(JSON.stringify(Joomla.selectedMediaFile || window.parent.Joomla.selectedMediaFile || {}));
}

try{
handlers.handle(item);
}catch (e) {
this.handleModalSelection(details);
console.log(e);
}
};

toggleSelect = (item, ctrlKey, shiftKey) => {
Expand Down
47 changes: 47 additions & 0 deletions src/core/Components/ItemList/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,54 @@ class ItemList extends Component {
};
};

/*
* @param {string} itemKey
* */
dispatchFileSelectedEvent(itemKey) {
const item = this.state.entries.files.find(file => file.id === itemKey);
const allowedExtensions = new Set(["bmp", "gif", "jpg", "png", "jpeg", "webp", "avif"]);
if(!item || !allowedExtensions.has(item.extension)) {
if(!item) {
console.log('item not found')
} else {
console.log('extension not allowed')
}
return;
}

const details = {
"type": "file",
"name": item.name,
"path": `local-images:${item.path}`,
"fileType": item.image_info.mime,
"extension": item.extension,
"width": item.image_info.width,
"height": item.image_info.height,
};
try {
window.parent.document.dispatchEvent(
new CustomEvent(
"onMediaFileSelected",
{
bubbles: true,
cancelable: false,
detail: details,
},
),
);
console.log('trying to dispatch the event')
} catch (error) {
if(Joomla.selectedMediaFile) Joomla.selectedMediaFile = details;
if (window.parent.Joomla.selectedMediaFile) window.parent.Joomla.selectedMediaFile = details;

console.error('error dispatching the event', error);
}
}

get selectedItems() {
// const keys = Object.keys(this.state.selected_entries);
// const itemKey = keys.length > 0 ? keys[0] : false;
// if(itemKey && (Joomla.selectedMediaFile || window.parent.Joomla.selectedMediaFile)) this.dispatchFileSelectedEvent(itemKey);
return Object.keys(this.state.selected_entries);
}

Expand Down