Select some text and click the 'File' button on the toolbar.
<!DOCTYPE html>
<html>
<head>
<title>Redactor</title>
<meta charset="utf-8">
<!-- css -->
<link rel="stylesheet" href="/your-folder/redactor.css" />
</head>
<body>
<!-- element -->
<textarea id="entry">...</textarea>
<!-- js -->
<script src="/your-folder/redactor.js"></script>
<script src="/your-folder/plugins/filelink/filelink.js"></script>
<!-- call -->
<script>
Redactor('#entry', {
plugins: ['filelink'],
filelink: {
upload: '/your-server-side-upload/',
select: '/your-files.json'
}
});
</script>
</body>
</html>
By default, the plugin adds its button to the toolbar and context bar. To remove it from the context bar, disable the context
setting:
Redactor('#entry', {
plugins: ['filelink'],
filelink: {
context: false
}
});
Javascript doesn't allow the implementation of images and files uploads. Therefore, this feature utilizes server-side languages, such as PHP.
First of all, make sure that you have properly set a path to the upload's file. You can do this by modifying the option on load, like this:
Redactor('#entry', {
plugins: ['filelink'],
filelink: {
upload: '/your-server-side-upload/'
}
});
And the upload script will process file, it may look like this:
<?php
// This is a simplified example, which doesn't cover security of uploaded files.
// This example just demonstrate the logic behind the process.
$files = [];
if (isset($_FILES['file']))
{
foreach ($_FILES['file']['name'] as $key => $name)
{
move_uploaded_file($_FILES['file']['tmp_name'][$key], '/my-files/'.$name);
$files['file-'.$key] = [
'url' => '/my-files/'.$name,
'name' => $name,
'id' => md5(date('YmdHis'))
];
}
}
echo stripslashes(json_encode($files));
JSON example of response:
{
"file": {
"url": "some-file-url",
"name": "some-file-name",
"id": "some-id"
}
}
Set in the settings the path to the JSON file that defines the file list.
Redactor('#entry', {
plugins: ['filelink'],
filelink: {
select: '/your-files.json'
}
});
Specify the path to the JSON file to display a list of files in the file popup. A JSON file can look like this:
[
{
"title": "File 1",
"name": "1.txt",
"url": "/file-url/1.txt",
"id": 1,
"size": "301Kb"
},
{
"title": "File 2",
"name": "2.txt",
"url": "/file-url/2.txt",
"id": 2,
"size": "1MB"
}
]
Arguments:
Dom
Object
Occurs whenever a file is uploaded into the editor.
Redactor('#entry', {
plugins: ['filelink'],
filelink: {
upload: '/your-server-side-upload/',
select: '/your-files.json'
},
subscribe: {
'file.upload': function(event) {
let data = event.get('data');
let $file = event.get('$el');
$file.addClass('my-file');
}
}
});
Arguments:
Object
Object
Occurs whenever a file is selected from the list.
Redactor('#entry', {
plugins: ['filelink'],
filelink: {
upload: '/your-server-side-upload/',
select: '/your-files.json'
},
subscribe: {
'file.select': function(event) {
let data = event.get('data');
let $file = event.get('$el');
$file.addClass('my-file');
}
}
});
You can use the API method files.getStates
for tracking deleted files.
// call editor
let app = Redactor('#entry', {
plugins: ['filelink'],
filelink: {
upload: '/your-server-side-upload/',
select: '/your-files.json'
}
});
// api
let changes = app.filelink.getStates();
for (let key in changes) {
if (changes[key].status === false) {
// element was deleted
}
else {
// element is still in the content
}
}
How deleted files tracking works:
files.getStates
method, the editor will check the object of all uploaded files or those that were at the start of the editor and compare which ones have been deleted.Here's an example of an object that is returned by files.getStates
:
{
status: true,
url: "file-url-path",
$el: $node,
id: "1"
}