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

[5.2] Media field: selectable folders #43579

Merged
merged 17 commits into from
Jul 23, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ export default {
{
bubbles: true,
cancelable: false,
detail: {},
detail: {
type: 'dir',
path: destination.path,
},
},
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ export default {
/* Handle the on preview double click event */
onPreviewDblClick() {
this.navigateTo(this.item.path);

window.parent.document.dispatchEvent(
new CustomEvent('onMediaFileSelected', {
bubbles: true,
cancelable: false,
detail: {
type: this.item.type,
path: this.item.path,
},
}),
);
},
/* Hide actions dropdown */
hideActions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export default {
bubbles: true,
cancelable: false,
detail: {
type: this.item.type,
path: this.item.path,
thumb: this.item.thumb,
fileType: this.item.mime_type ? this.item.mime_type : false,
Expand All @@ -142,7 +143,10 @@ export default {
new CustomEvent('onMediaFileSelected', {
bubbles: true,
cancelable: false,
detail: {},
detail: {
type: this.item.type,
path: this.item.path,
},
}),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,32 +136,31 @@ export default {
* @param event
*/
onClick(event) {
const path = false;
const data = {
path,
path: this.item.path,
type: this.item.type,
thumb: false,
fileType: this.item.mime_type ? this.item.mime_type : false,
extension: this.item.extension ? this.item.extension : false,
};

if (this.item.type === 'file') {
data.path = this.item.path;
data.thumb = this.item.thumb ? this.item.thumb : false;
data.width = this.item.width ? this.item.width : 0;
data.height = this.item.height ? this.item.height : 0;

window.parent.document.dispatchEvent(
new CustomEvent(
'onMediaFileSelected',
{
bubbles: true,
cancelable: false,
detail: data,
},
),
);
}

window.parent.document.dispatchEvent(
new CustomEvent(
'onMediaFileSelected',
{
bubbles: true,
cancelable: false,
detail: data,
},
),
);

// Handle clicks when the item was not selected
if (!this.isSelected()) {
// Unselect all other selected items,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ export default {
/* Handle the on drive click event */
onDriveClick() {
this.navigateTo(this.drive.root);

window.parent.document.dispatchEvent(
new CustomEvent('onMediaFileSelected', {
bubbles: true,
cancelable: false,
detail: {
type: 'dir',
path: this.drive.root,
},
}),
);
},
moveFocusToChildElement(nextRoot) {
this.$refs[nextRoot].setFocusToFirstChild();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ export default {
{
bubbles: true,
cancelable: false,
detail: {},
detail: {
type: item.type,
path: item.path,
},
},
),
);
Expand Down
1 change: 1 addition & 0 deletions administrator/language/en-GB/plg_editors_tinymce.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ PLG_TINY_CORE_BUTTONS="CMS Content"
PLG_TINY_DND_ADDITIONALDATA="Additional Data"
PLG_TINY_DND_ALTTEXT="Image Description (Alt Text)"
PLG_TINY_DND_EMPTY_ALT="No Description"
PLG_TINY_DND_FILE_EXISTS_ERROR="File \"%s\" already exists."
PLG_TINY_DND_LAZYLOADED="Image will be lazyloaded"
PLG_TINY_ERR_CUSTOMCSSFILENOTPRESENT="The file name %s was entered in the TinyMCE Custom CSS field. This file could not be found in the default template folder. No styles are available."
PLG_TINY_ERR_EDITORCSSFILENOTPRESENT="Could not find the file 'editor.css' in the template or templates/system folder. No styles are available."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ function readFile(file, callback) {
window.tinymce.PluginManager.add('jdragndrop', (editor) => {
const registerOption = editor.options.register;
registerOption('uploadUri', { processor: 'string' });
registerOption('comMediaAdapter', { processor: 'string' });
registerOption('parentUploadFolder', { processor: 'string' });
registerOption('csrfToken', { processor: 'string' });

// Reset the drop area border
const dragleaveCallback = (e) => {
Expand Down Expand Up @@ -61,22 +59,23 @@ window.tinymce.PluginManager.add('jdragndrop', (editor) => {
const settings = editor.options.get;

Joomla.request({
url: `${settings('uploadUri')}&path=${settings('comMediaAdapter')}${settings('parentUploadFolder')}`,
url: `${settings('uploadUri')}&path=${settings('parentUploadFolder')}`,
method: 'POST',
data: JSON.stringify({
[settings('csrfToken')]: 1,
name,
content,
parent: settings('parentUploadFolder'),
}),
headers: {
'Content-Type': 'application/json',
},
onSuccess: (resp) => {
promise: true,
})
.then((resp) => {
let response;

try {
response = JSON.parse(resp);
response = JSON.parse(resp.responseText);
} catch (e) {
editor.windowManager.alert(`${Joomla.Text._('ERROR')}: {${e}}`);
}
Expand Down Expand Up @@ -145,9 +144,14 @@ window.tinymce.PluginManager.add('jdragndrop', (editor) => {
onCancel: (api) => dialogClose(api),
});
}
},
onError: (xhr) => editor.windowManager.alert(`Error: ${xhr.statusText}`),
});
})
.catch((xhr) => {
let message = `Error: ${xhr.statusText}`;
if (xhr.status === 409) {
message = Joomla.Text._('PLG_TINY_DND_FILE_EXISTS_ERROR').replace('%s', `${settings('parentUploadFolder')}/${name}`);
}
editor.windowManager.alert(message);
});
}

// Logic for the dropped file
Expand Down
14 changes: 10 additions & 4 deletions build/media_source/system/js/fields/joomla-field-media.w-c.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ class JoomlaFieldMedia extends HTMLElement {
}

static get observedAttributes() {
return ['type', 'base-path', 'root-folder', 'url', 'modal-title', 'modal-width', 'modal-height', 'input', 'button-select', 'button-clear', 'preview', 'preview-width', 'preview-height'];
return ['base-path', 'root-folder', 'url', 'modal-title', 'modal-width', 'modal-height', 'input', 'button-select', 'button-clear', 'preview', 'preview-width', 'preview-height'];
}

get type() { return this.getAttribute('type'); }
get types() { return this.getAttribute('types') || ''; }

set type(value) { this.setAttribute('type', value); }
set types(value) { this.setAttribute('types', value); }

get basePath() { return this.getAttribute('base-path'); }

Expand Down Expand Up @@ -175,7 +175,13 @@ class JoomlaFieldMedia extends HTMLElement {

async modalClose() {
try {
await Joomla.getMedia(Joomla.selectedMediaFile, this.inputElement, this);
const item = Joomla.selectedMediaFile;
if (item && item.type === 'dir') {
// Set directory path as value only when the field is configured to support of directories
this.setValue(this.types.includes('directories') ? item.path : '');
} else {
await Joomla.getMedia(item, this.inputElement, this);
}
} catch (err) {
Joomla.renderMessages({
error: [Joomla.Text._('JLIB_APPLICATION_ERROR_SERVER')],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ document.addEventListener('onMediaFileSelected', async (e) => {
images, audios, videos, documents,
} = supportedExtensions;

if (Joomla.selectedMediaFile.path) {
if (Joomla.selectedMediaFile.path && Joomla.selectedMediaFile.type === 'file') {
let type;
if (images.includes(Joomla.selectedMediaFile.extension.toLowerCase())) {
type = 'images';
Expand Down
6 changes: 4 additions & 2 deletions layouts/joomla/form/field/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
* @var integer $size The size text
* @var string $value The value text
* @var string $src The path and filename of the image
* @var array $mediaTypes The supported media types for the Media Manager
* @var string $mediaTypes The ids of supported media types for the Media Manager
* @var array $mediaTypeNames The names of supported media types for the Media Manager
* @var array $imagesExt The supported extensions for images
* @var array $audiosExt The supported extensions for audios
* @var array $videosExt The supported extensions for videos
Expand Down Expand Up @@ -155,7 +156,8 @@
}

?>
<joomla-field-media class="field-media-wrapper" type="image" <?php // @TODO add this attribute to the field in order to use it for all media types ?>
<joomla-field-media class="field-media-wrapper"
types="<?php echo $this->escape(implode(',', $mediaTypeNames)); ?>"
base-path="<?php echo $this->escape(Uri::root()); ?>"
root-folder="<?php echo $this->escape(ComponentHelper::getParams('com_media')->get('file_path', 'images')); ?>"
url="<?php echo $url; ?>"
Expand Down
4 changes: 4 additions & 0 deletions libraries/src/Form/Field/MediaField.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,9 @@ public function getLayoutData()
array_map(
function ($mediaType) use (&$types, &$imagesAllowedExt, &$audiosAllowedExt, &$videosAllowedExt, &$documentsAllowedExt, $imagesExt, $audiosExt, $videosExt, $documentsExt) {
switch ($mediaType) {
case 'directories':
$types[] = '-1';
break;
case 'images':
$types[] = '0';
$imagesAllowedExt = $imagesExt;
Expand Down Expand Up @@ -417,6 +420,7 @@ function ($mediaType) use (&$types, &$imagesAllowedExt, &$audiosAllowedExt, &$vi
'previewHeight' => $this->previewHeight,
'previewWidth' => $this->previewWidth,
'mediaTypes' => implode(',', $types),
'mediaTypeNames' => $mediaTypes,
'imagesExt' => $imagesExt,
'audiosExt' => $audiosExt,
'videosExt' => $videosExt,
Expand Down
4 changes: 3 additions & 1 deletion plugins/editors/tinymce/forms/setoptions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@

<field
name="path"
type="uploaddirs"
type="media"
label="PLG_TINY_FIELD_CUSTOM_PATH_LABEL"
showon="drag_drop:1"
preview="false"
types="directories"
/>

<field
Expand Down
8 changes: 8 additions & 0 deletions plugins/editors/tinymce/src/Field/UploaddirsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
* @package Joomla.Plugin
* @subpackage Editors.tinymce
* @since 3.7.0
*
* @deprecated __DEPLOY_VERSION__ will be removed in 7.0
* Use Joomla\CMS\Form\Field\FolderlistField.
*/
class UploaddirsField extends FolderlistField
{
Expand Down Expand Up @@ -52,6 +55,11 @@ public function setup(\SimpleXMLElement $element, $value, $group = null)
$this->recursive = true;
$this->hideDefault = true;

@trigger_error(
__CLASS__ . ' is deprecated, use Joomla\CMS\Form\Field\FolderlistField instead. Will be removed in 7.0.',
\E_USER_DEPRECATED
);

return $return;
}

Expand Down
25 changes: 18 additions & 7 deletions plugins/editors/tinymce/src/PluginTraits/DisplayTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Joomla\CMS\Layout\LayoutHelper;
use Joomla\CMS\Session\Session;
use Joomla\CMS\Uri\Uri;
use Joomla\Component\Media\Administrator\Provider\ProviderManagerHelperTrait;
use Joomla\Registry\Registry;

// phpcs:disable PSR1.Files.SideEffects
Expand Down Expand Up @@ -324,22 +325,32 @@ public function display(string $name, string $content = '', array $attributes =

if ($dragdrop && $user->authorise('core.create', 'com_media')) {
$wa->useScript('plg_editors_tinymce.jdragndrop');
$plugins[] = 'jdragndrop';
$uploadUrl = Uri::base(true) . '/index.php?option=com_media&format=json&url=1&task=api.files';
$plugins[] = 'jdragndrop';
$uploadUrl = Uri::base(true) . '/index.php?option=com_media&format=json&url=1&task=api.files';
$uploadPath = $levelParams->get('path', '');

// Make sure the path is full, and contain the media adapter in it.
$mediaHelper = new class () {
use ProviderManagerHelperTrait;

public function prepareTinyMCEUploadPath(string $path): string
{
$result = $this->resolveAdapterAndPath($path);

return implode(':', $result);
}
};

Text::script('PLG_TINY_ERR_UNSUPPORTEDBROWSER');
Text::script('ERROR');
Text::script('PLG_TINY_DND_ADDITIONALDATA');
Text::script('PLG_TINY_DND_ALTTEXT');
Text::script('PLG_TINY_DND_LAZYLOADED');
Text::script('PLG_TINY_DND_EMPTY_ALT');
Text::script('PLG_TINY_DND_FILE_EXISTS_ERROR');

$scriptOptions['parentUploadFolder'] = $levelParams->get('path', '');
$scriptOptions['csrfToken'] = $csrf;
$scriptOptions['parentUploadFolder'] = $mediaHelper->prepareTinyMCEUploadPath($uploadPath);
$scriptOptions['uploadUri'] = $uploadUrl;

// @TODO have a way to select the adapter, similar to $levelParams->get('path', '');
$scriptOptions['comMediaAdapter'] = 'local-images:';
}

// Convert pt to px in dropdown
Expand Down
7 changes: 3 additions & 4 deletions plugins/fields/media/media.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@
<fieldset name="basic">
<field
name="directory"
type="folderlist"
type="media"
label="PLG_FIELDS_MEDIA_PARAMS_DIRECTORY_LABEL"
directory="images"
hide_none="true"
recursive="true"
preview="false"
types="directories"
/>

<field
Expand Down
8 changes: 3 additions & 5 deletions plugins/fields/media/params/media.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
<fieldset name="fieldparams">
<field
name="directory"
type="folderlist"
type="media"
label="PLG_FIELDS_MEDIA_PARAMS_DIRECTORY_LABEL"
directory="images"
hide_none="true"
recursive="true"
validate="options"
preview="false"
types="directories"
/>

<field
Expand Down