Skip to content

Commit

Permalink
tinyMce with Laravel Filemanager
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-to committed Dec 15, 2022
1 parent 7d40b7a commit 71b7398
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 4 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"ext-json": "*",
"laravel/framework": "^9.21",
"symfony/var-dumper": "^6.0",
"phpoffice/phpspreadsheet": "1.24.1"
"phpoffice/phpspreadsheet": "1.24.1",
"unisharp/laravel-filemanager": "^2.5"
},
"require-dev": {
"fakerphp/faker": "^1.9.2",
Expand Down
4 changes: 4 additions & 0 deletions config/moonshine.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
],
],
'middlewares' => [],
'tinymce' => [
'token' => env('MOONSHINE_TINYMCE_TOKEN', ''),
'version' => env('MOONSHINE_TINYMCE_VERSION', '6')
],
'extensions' => [
//
],
Expand Down
41 changes: 41 additions & 0 deletions resources/views/fields/tinymce.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<textarea id='ckeditor_{{ $field->id() }}' name="{{ $field->name() }}">
{!! $field->formViewValue($item) ?? '' !!}
</textarea>

<script>
var editor_config = {
path_absolute : "/",
selector: 'textarea#ckeditor_{{ $field->id() }}',
relative_urls: false,
plugins: '{{ $field->plugins }}',
toolbar: '{{ $field->toolbar }}',
tinycomments_mode: 'embedded',
tinycomments_author: '{{ $field->commentAuthor }}',
mergetags_list: @json($field->mergeTags),
file_picker_callback : function(callback, value, meta) {
var x = window.innerWidth || document.documentElement.clientWidth || document.getElementsByTagName('body')[0].clientWidth;
var y = window.innerHeight|| document.documentElement.clientHeight|| document.getElementsByTagName('body')[0].clientHeight;
var cmsURL = editor_config.path_absolute + 'laravel-filemanager?editor=' + meta.fieldname;
if (meta.filetype == 'image') {
cmsURL = cmsURL + "&type=Images";
} else {
cmsURL = cmsURL + "&type=Files";
}
tinyMCE.activeEditor.windowManager.openUrl({
url : cmsURL,
title : 'Filemanager',
width : x * 0.8,
height : y * 0.8,
resizable : "yes",
close_previous : "no",
onMessage: (api, message) => {
callback(message.content);
}
});
}
};
tinymce.init(editor_config);
</script>
9 changes: 8 additions & 1 deletion src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function handle(): void
$this->initDirectories();
$this->initDashboard();
$this->initServiceProvider();
$this->initLfm();

$this->components->info('Installation completed');

Expand Down Expand Up @@ -82,7 +83,7 @@ protected function initServiceProvider(): void
$this->components->task('Service Provider created');
}

protected function registerServiceProvider()
protected function registerServiceProvider(): void
{
$namespace = Str::replaceLast('\\', '', $this->laravel->getNamespace());

Expand Down Expand Up @@ -112,4 +113,10 @@ protected function registerServiceProvider()
file_get_contents(app_path('Providers/MoonShineServiceProvider.php'))
));
}

protected function initLfm(): void
{
Artisan::call('vendor:publish', ['--tag' => 'lfm_config']);
Artisan::call('vendor:publish', ['--tag' => 'lfm_public']);
}
}
63 changes: 63 additions & 0 deletions src/Fields/TinyMce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Leeto\MoonShine\Fields;

final class TinyMce extends Field
{
protected static string $view = 'moonshine::fields.tinymce';

public string $plugins = 'anchor autolink charmap codesample emoticons image link lists media searchreplace table visualblocks wordcount checklist mediaembed casechange export formatpainter pageembed linkchecker a11ychecker tinymcespellchecker permanentpen powerpaste advtable advcode editimage tinycomments tableofcontents footnotes mergetags autocorrect typography inlinecss';

public string $toolbar = 'undo redo | blocks fontfamily fontsize | bold italic underline strikethrough | link image media table mergetags | addcomment showcomments | spellcheckdialog a11ycheck typography | align lineheight | checklist numlist bullist indent outdent | emoticons charmap | removeformat';

public array $mergeTags = [];

public string $commentAuthor = 'Author name';

public function getAssets(): array
{
return [
"https://cdn.tiny.cloud/1/{$this->token()}/tinymce/{$this->version()}/tinymce.min.js"
];
}

public function mergeTags(array $mergeTags): self
{
$this->mergeTags = $mergeTags;

return $this;
}

public function commentAuthor(string $commentAuthor): self
{
$this->commentAuthor = $commentAuthor;

return $this;
}

public function plugins(string $plugins): self
{
$this->plugins = $plugins;

return $this;
}

public function toolbar(string $toolbar): self
{
$this->toolbar = $toolbar;

return $this;
}

protected function token(): string
{
return config('moonshine.tinymce.token', '');
}

protected function version(): string
{
return (string) config('moonshine.tinymce.version', 6);
}
}
6 changes: 4 additions & 2 deletions src/Http/Controllers/MoonShineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use Leeto\MoonShine\Exceptions\ResourceException;
use Leeto\MoonShine\Resources\Resource;

use Throwable;

use function auth;
use function redirect;
use function request;
Expand All @@ -47,7 +49,7 @@ public function action($id, $index): RedirectResponse

try {
$action->callback($item);
} catch (Exception $e) {
} catch (Throwable $e) {
throw_if(!app()->isProduction(), $e);

return redirect($this->resource->route('index'))
Expand All @@ -72,7 +74,7 @@ public function bulk($index): RedirectResponse
->findMany(explode(';', request('ids')));

$items->each(fn($item) => $action->callback($item));
} catch (Exception $e) {
} catch (Throwable $e) {
throw_if(!app()->isProduction(), $e);

return redirect($this->resource->route('index'))
Expand Down
7 changes: 7 additions & 0 deletions src/MoonShine.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Leeto\MoonShine\Menu\MenuItem;
use Leeto\MoonShine\Resources\CustomPage;
use Leeto\MoonShine\Resources\Resource;
use UniSharp\LaravelFilemanager\Lfm;

class MoonShine
{
Expand Down Expand Up @@ -117,6 +118,12 @@ public function getPages(): Collection
*/
protected function addRoutes(): void
{
Route::prefix('laravel-filemanager')
->middleware(config('moonshine.route.middleware'))
->group(function () {
Lfm::routes();
});

Route::prefix(config('moonshine.route.prefix'))
->middleware(config('moonshine.route.middleware'))
->name(config('moonshine.route.prefix').'.')->group(function () {
Expand Down

0 comments on commit 71b7398

Please sign in to comment.