Merge pull request #5562 from christianbeeznest/ofaj-21647-2

Editor: Add filemanager for document image/video in tiniMCE legacy - refs BT#21647
pull/5563/head
christianbeeznest 6 months ago committed by GitHub
commit 35a4a45130
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 62
      src/CoreBundle/Component/Editor/CkEditor/CkEditor.php
  2. 2
      src/CoreBundle/Component/Editor/CkEditor/Toolbar/Basic.php

@ -67,9 +67,17 @@ class CkEditor extends Editor
$javascript = $this->toJavascript($config);
// it replaces [browser] by image picker callback
$javascript = str_replace('"[browser]"', $this->getImagePicker(), $javascript);
$javascript = str_replace('"[browser]"', $this->getFileManagerPicker(), $javascript);
return "<script>
window.addEventListener('message', function(event) {
if (event.data.url) {
if (window.parent.tinyMCECallback) {
window.parent.tinyMCECallback(event.data.url);
delete window.parent.tinyMCECallback;
}
}
});
document.addEventListener('DOMContentLoaded', function() {
window.chEditors = window.chEditors || [];
window.chEditors.push($javascript)
@ -145,7 +153,7 @@ class CkEditor extends Editor
*
* @return string
*/
private function getImagePicker()
private function getImagePicker(): string
{
return 'function (cb, value, meta) {
var input = document.createElement("input");
@ -168,6 +176,56 @@ class CkEditor extends Editor
}';
}
/**
* Generates the JavaScript function for opening the file manager picker in TinyMCE.
*
* Determines the URL based on whether the context is a course or a user.
* Falls back to the image picker if neither is found.
*
* @return string The JavaScript function for TinyMCE's file manager picker.
*/
private function getFileManagerPicker(): string
{
$course = api_get_course_entity();
$user = api_get_user_entity();
$url = null;
if (null !== $course) {
$resourceNodeId = $course->getResourceNode()->getId();
$url = api_get_path(WEB_PATH).'resources/document/'.$resourceNodeId.'/manager?'.api_get_cidreq().'&type=images';
} else {
if (null !== $user) {
$resourceNodeId = $user->getResourceNode()->getId();
$url = api_get_path(WEB_PATH).'resources/filemanager/personal_list/'.$resourceNodeId;
}
}
if (null === $url) {
return $this->getImagePicker();
}
return '
function(cb, value, meta) {
window.tinyMCECallback = cb;
let fileType = meta.filetype;
let fileManagerUrl = "'.$url.'";
if (fileType === "image") {
fileManagerUrl += "?type=images";
} else if (fileType === "file") {
fileManagerUrl += "?type=files";
}
tinymce.activeEditor.windowManager.openUrl({
title: "File Manager",
url: fileManagerUrl,
width: 950,
height: 450
});
}
';
}
/**
* Get the empty template.
*/

@ -186,7 +186,7 @@ class Basic extends Toolbar
// enable automatic uploads of images represented by blob or data URIs
$config['automatic_uploads'] = true;
// custom filepicker only to Image dialog
$config['file_picker_types'] = 'image';
$config['file_picker_types'] = 'file image media';
$config['file_picker_callback'] = '[browser]';

Loading…
Cancel
Save