Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 16s
Gemini PR Review / Gemini PR Review (pull_request) Failing after 17s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 17s
Laravel Pint / pint (pull_request) Successful in 2m6s
Laravel Larastan / larastan (pull_request) Failing after 4m55s
103 lines
2.6 KiB
PHP
103 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Pages;
|
|
|
|
use App\Models\Configuration;
|
|
use App\Models\SharedClipBoard;
|
|
use Filament\Pages\Page;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
|
|
class SharedEditor extends Page
|
|
{
|
|
protected static ?string $navigationIcon = 'heroicon-s-clipboard-document';
|
|
|
|
protected static string $view = 'filament.pages.shared-editor';
|
|
|
|
protected static ?string $navigationLabel = 'Shared Clipboard';
|
|
|
|
protected static ?string $title = 'Shared Clipboard';
|
|
|
|
public string $content = '';
|
|
|
|
public string $lastSyncedContent = '';
|
|
|
|
public function mount(): void
|
|
{
|
|
$clipboard = SharedClipBoard::firstOrCreate(
|
|
['id' => 1],
|
|
['content' => '']
|
|
);
|
|
|
|
$this->content = $clipboard->content ?? '';
|
|
|
|
$this->lastSyncedContent = $this->content;
|
|
}
|
|
|
|
public function updatedContent(): void
|
|
{
|
|
SharedClipBoard::where('id', 1)->update([
|
|
'content' => $this->content,
|
|
]);
|
|
|
|
$this->lastSyncedContent = $this->content;
|
|
}
|
|
|
|
public function refreshSharedContent(): void
|
|
{
|
|
$sharedContent = SharedClipBoard::find(1)?->content ?? '';
|
|
|
|
// Do not overwrite text that this user is editing.
|
|
if (
|
|
$this->content === $this->lastSyncedContent
|
|
&& $sharedContent !== $this->lastSyncedContent
|
|
) {
|
|
$this->content = $sharedContent;
|
|
|
|
$this->lastSyncedContent = $sharedContent;
|
|
}
|
|
}
|
|
|
|
public function copyContent(): void
|
|
{
|
|
$this->dispatch('copy-to-clipboard', content: $this->content);
|
|
}
|
|
public function downloadTxt(): StreamedResponse
|
|
{
|
|
$content = $this->content;
|
|
|
|
return response()->streamDownload(
|
|
function () use ($content) {
|
|
echo $content;
|
|
},
|
|
'shared-clipboard.txt',
|
|
[
|
|
'Content-Type' => 'text/plain; charset=UTF-8',
|
|
]
|
|
);
|
|
}
|
|
|
|
public static function canAccess(): bool
|
|
{
|
|
// return Auth::check() && Auth::user()->can('view shared clipboard page');
|
|
|
|
$user = Auth::user();
|
|
|
|
if(Auth::check() && !Auth::user()->can('view shared clipboard page'))
|
|
{
|
|
if (!$user) {
|
|
return false;
|
|
}
|
|
|
|
$configuration = Configuration::where('c_group', 'Shared Clip Board')->where('c_value', $user->name)->first();
|
|
|
|
return $configuration ? true:false;
|
|
|
|
}
|
|
else{
|
|
return Auth::check() && Auth::user()->can('view shared clipboard page');
|
|
}
|
|
}
|
|
|
|
}
|