Skip to content

Commit

Permalink
Allow uploading and editing files
Browse files Browse the repository at this point in the history
  • Loading branch information
danharrin committed Feb 7, 2025
1 parent 997e84c commit 5df772b
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 4 deletions.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/forms/dist/components/rich-editor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/forms/resources/js/components/rich-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function richEditorFormComponent({ key, livewireId, state, stateP
return {
state,

editorSelection: null,
editorSelection: { type: 'text', anchor: 1, head: 1 },

isUploadingFile: false,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ class="h-5 w-5"
@if ($hasToolbarButton('attachFiles'))
<x-filament-forms::rich-editor.toolbar.group>
<x-filament-forms::rich-editor.toolbar.button
type="image"
:x-on:click="$getAction('attachFiles')->getAlpineClickHandler()"
title="{{ __('filament-forms::components.rich_editor.toolbar_buttons.attach_files') }}"
tabindex="-1"
>
Expand Down
2 changes: 2 additions & 0 deletions packages/forms/src/Components/RichEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Filament\Forms\Components;

use Closure;
use Filament\Forms\Components\RichEditor\Actions\AttachFilesAction;
use Filament\Forms\Components\RichEditor\Actions\LinkAction;
use Filament\Forms\Components\RichEditor\EditorCommand;
use Filament\Forms\Components\StateCasts\RichEditorStateCast;
Expand Down Expand Up @@ -52,6 +53,7 @@ protected function setUp(): void
parent::setUp();

$this->registerActions([
AttachFilesAction::make(),
LinkAction::make(),
]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace Filament\Forms\Components\RichEditor\Actions;

use Filament\Actions\Action;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\RichEditor\EditorCommand;
use Filament\Forms\Components\TextInput;
use Filament\Support\Enums\Width;
use Illuminate\Support\Js;
use Illuminate\Support\Str;
use Livewire\Component;

class AttachFilesAction
{
public static function make(): Action
{
return Action::make('attachFiles')
->alpineClickHandler(fn (RichEditor $component): string => '$wire.mountAction(\'attachFiles\', { alt: getEditor().getAttributes(\'image\')?.alt, id: getEditor().getAttributes(\'image\')?.id, src: getEditor().getAttributes(\'image\')?.src, editorSelection }, ' . Js::from(['schemaComponent' => $component->getKey()]) . ')')
->modalHeading('Upload file')
->modalWidth(Width::Large)
->fillForm(fn (array $arguments): array => [
'alt' => $arguments['alt'] ?? null,
])
->form(fn (array $arguments): array => [
FileUpload::make('file')
->label(filled($arguments['src'] ?? null) ? 'Replace file' : 'File')
->acceptedFileTypes(['image/png', 'image/jpeg', 'image/gif', 'image/webp'])
->storeFiles(false)
->required(blank($arguments['src'] ?? null))
->hiddenLabel(blank($arguments['src'] ?? null)),
TextInput::make('alt')
->label(filled($arguments['src'] ?? null) ? 'Change alt text' : 'Alt text'),
])
->action(function (array $arguments, array $data, RichEditor $component, Component $livewire) {
if ($data['file'] ?? null) {
$id = (string) Str::orderedUuid();

data_set($livewire, "componentFileAttachments.{$component->getStatePath()}.{$id}", $data['file']);
$src = $component->saveUploadedFileAttachment($id);
}

if (filled($arguments['src'] ?? null)) {
$id ??= $arguments['id'] ?? null;
$src ??= $arguments['src'];

$component->runCommands(
[
new EditorCommand(name: 'updateAttributes', arguments: [
'image',
[
'alt' => $data['alt'] ?? null,
'id' => $id,
'src' => $src,
],
]),
],
editorSelection: $arguments['editorSelection'],
);

return;
}

if (blank($id ?? null)) {
return;
}

if (blank($src ?? null)) {
return;
}

$component->runCommands(
[
new EditorCommand(name: 'insertContent', arguments: [[
'type' => 'image',
'attrs' => [
'alt' => $data['alt'] ?? null,
'id' => $id,
'src' => $src,
],
]]),
],
editorSelection: $arguments['editorSelection'],
);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class LinkAction
public static function make(): Action
{
return Action::make('link')
->alpineClickHandler(fn (RichEditor $component): string => '$wire.mountAction(\'link\', { url: getEditor().getAttributes(\'link\').href, shouldOpenInNewTab: getEditor().getAttributes(\'link\').target === \'_blank\', editorSelection }, ' . Js::from(['schemaComponent' => $component->getKey()]) . ')')
->alpineClickHandler(fn (RichEditor $component): string => '$wire.mountAction(\'link\', { url: getEditor().getAttributes(\'link\')?.href, shouldOpenInNewTab: getEditor().getAttributes(\'link\')?.target === \'_blank\', editorSelection }, ' . Js::from(['schemaComponent' => $component->getKey()]) . ')')
->modalHeading('Link')
->modalWidth(Width::Large)
->fillForm(fn (array $arguments): array => [
Expand Down

0 comments on commit 5df772b

Please sign in to comment.