Skip to content

Commit 36f35ec

Browse files
committed
Refactor Code Editor Component
- Enhanced the code editor component to support dynamic themes based on user preferences. - Updated the initialization logic to determine the theme from the Alpine store or system settings. - Removed the isReadOnly property from the CodeEditor class and replaced it with a disabled state. - Simplified the Blade view for the code editor, removing unnecessary elements and scripts. - Added a new Tailwind CSS configuration file for the code editor component. - Ensured proper loading of CSS and JS assets for the code editor in the service provider.
1 parent da13cbf commit 36f35ec

File tree

12 files changed

+96
-112
lines changed

12 files changed

+96
-112
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"require": {
2525
"php": ">=8.2",
2626
"laravel/framework": "^11.0|^12.0",
27-
"dotswan/filament-code-editor": "^1.1",
2827
"filament/spatie-laravel-translatable-plugin": "^3.2",
2928
"guava/filament-icon-picker": "^2.0",
3029
"pboivin/filament-peek": "^2.0",

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
"private": true,
33
"type": "module",
44
"scripts": {
5-
"dev:styles": "npx tailwindcss -i ./resources/css/index.css -o ./resources/dist/inspirecms.css --postcss --watch",
5+
"dev:code-editor-css": "npx tailwindcss -c ./tailwind-code-editor.config.js -i ./resources/css/components/code-editor.css -o ./resources/dist/components/code-editor.css --watch",
6+
"dev:main-css": "npx tailwindcss -i ./resources/css/index.css -o ./resources/dist/inspirecms.css --postcss --watch",
7+
"dev:styles": "npm run dev:main-css && npm run dev:code-editor-css",
68
"dev:scripts": "node ./bin/build.js --dev",
7-
"build:styles": "npx tailwindcss -i ./resources/css/index.css -o ./resources/dist/inspirecms.css --postcss --minify",
9+
"build:code-editor-css": "npx tailwindcss -c ./tailwind-code-editor.config.js -i ./resources/css/components/code-editor.css -o ./resources/dist/components/code-editor.css --postcss --minify",
10+
"build:main-css": "npx tailwindcss -i ./resources/css/index.css -o ./resources/dist/inspirecms.css --postcss --minify",
11+
"build:styles": "npm run build:main-css && npm run build:code-editor-css",
812
"build:scripts": "node ./bin/build.js",
913
"purge": "filament-purge -i resources/dist/inspirecms.css -o resources/dist/inspirecms.css -v 3.x",
1014
"dev": "npm run dev:styles && npm run dev:scripts",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.cm-scroller {
2+
min-height: inherit !important;
3+
height: 70vh;
4+
}
5+
6+
.cm-gutter{
7+
overflow: visible !important;
8+
}
9+
10+
.cm-editor {
11+
min-height: inherit !important;
12+
position: relative;
13+
overflow: auto !important;
14+
height: inherit;
15+
}
16+
17+
.code-editor-textarea {
18+
width: auto;
19+
}

resources/dist/components/code-editor.css

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/dist/components/code-editor.js

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/dist/inspirecms.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/js/components/code-editor.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import { gruvboxDark } from 'cm6-theme-gruvbox-dark'
1919

2020
export default function codeEditorFormComponentEnhace({
2121
state,
22-
isReadOnly,
2322
darkTheme,
2423
lightTheme,
24+
isReadOnly,
2525
}) {
2626
return {
2727
state,
@@ -69,12 +69,20 @@ export default function codeEditorFormComponentEnhace({
6969
dark: basicDark,
7070
},
7171
init: function () {
72+
let theme = window.Alpine.store('theme') || 'system';
73+
if (theme === 'system') {
74+
theme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
75+
}
76+
7277
this.isReadOnly = isReadOnly;
7378
this.theme.dark = darkTheme;
7479
this.theme.light = lightTheme;
7580
this.themeConfig = new Compartment();
76-
this.mode = 'dark';
81+
this.mode = theme;
7782
this.render();
83+
84+
this.toggleTheme(theme);
85+
7886
},
7987
toggleTheme(mode) {
8088
const selectedStyle = mode === 'dark' ? this.theme.dark : this.theme.light;
Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
<x-filament-forms::field-wrapper
1+
@php
2+
$statePath = $getStatePath();
3+
@endphp
4+
<x-dynamic-component
5+
:component="$getFieldWrapperView()"
6+
:field="$field"
27
:id="$getId()"
38
:label="$getLabel()"
49
:label-sr-only="$isLabelHidden()"
@@ -8,57 +13,40 @@
813
:required="$isRequired()"
914
:state-path="$getStatePath()"
1015
>
11-
12-
<div class="code-editor-textarea">
13-
14-
<div x-data="{ copied: false}" x-show="{{ $getShowCopyButton() }}">
15-
<div class="copy-button"
16-
@click="copyContent(`{{ $getState() }}`); copied = true; setTimeout(() => { copied = false; }, 5000)">
17-
<span x-show="!copied">Copy Content</span>
18-
<span x-show="copied">Copied</span>
19-
</div>
20-
</div>
21-
22-
<div style="overflow: auto;"
23-
@theme-changed.window="function(e) {toggleTheme(e.detail)}"
24-
x-init="() => {
25-
let theme = $store.theme ?? 'light';
26-
if (theme === 'system') {
27-
theme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
28-
}
29-
toggleTheme(theme);
30-
}"
16+
<x-filament::input.wrapper
17+
:disabled="$isDisabled"
18+
:valid="! $errors->has($statePath)"
19+
:attributes="
20+
\Filament\Support\prepare_inherited_attributes($getExtraAttributeBag())
21+
->class(['code-editor-textarea relative overflow-hidden'])
22+
"
23+
>
24+
<div class="code-editor-textarea-wrapper-ctn overflow-auto"
25+
@theme-changed.window="(e) => toggleTheme(e.detail)"
3126
x-data="codeEditorFormComponentEnhace({
3227
state: $wire.{{ $applyStateBindingModifiers('entangle(\'' . $getStatePath() . '\')') }},
33-
isReadOnly: @js($getIsReadOnly()),
3428
darkTheme: @js($getDarkModeTheme()),
3529
lightTheme: @js($getLightModeTheme()),
30+
isReadOnly: @js($isDisabled()),
3631
})"
3732
x-load
3833
x-load-src="{{ \Filament\Support\Facades\FilamentAsset::getAlpineComponentSrc('filament-code-editor', 'solution-forest/inspirecms') }}"
34+
x-load-css="[@js(\Filament\Support\Facades\FilamentAsset::getStyleHref('filament-code-editor', package: 'solution-forest/inspirecms'))]"
3935
>
40-
<div wire:ignore class="w-full code-editor-textarea-wrapper" x-ref="codeEditor"
41-
style="height:{{ $getMinHeight() }};overflow: hidden; {{ $getCustomStyle() }}">
36+
<div wire:ignore
37+
x-ref="codeEditor"
38+
{{
39+
$getExtraInputAttributeBag()
40+
->class([
41+
'code-editor-textarea-wrapper',
42+
'w-full overflow-hidden',
43+
])
44+
->style([
45+
'height:' . ($getMinHeight() ?? '200px'),
46+
])
47+
}}
48+
>
4249
</div>
4350
</div>
44-
</div>
45-
46-
<script>
47-
async function copyContent(content) {
48-
try {
49-
if (navigator.clipboard && window.isSecureContext) {
50-
await navigator.clipboard.writeText(content);
51-
} else {
52-
const el = document.createElement('textarea');
53-
el.value = content;
54-
document.body.appendChild(el);
55-
el.select();
56-
document.execCommand('copy');
57-
document.body.removeChild(el);
58-
}
59-
} catch (err) {
60-
console.error('Failed to copy text: ', err);
61-
}
62-
}
63-
</script>
64-
</x-filament-forms::field-wrapper>
51+
</x-filament::input.wrapper>
52+
</x-dynamic-component>

src/Filament/Forms/Components/CodeEditor.php

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,21 @@
33
namespace SolutionForest\InspireCms\Filament\Forms\Components;
44

55
use Closure;
6+
use Filament\Forms\Components\Concerns\HasExtraInputAttributes;
67
use Filament\Forms\Components\Field;
78

89
class CodeEditor extends Field
910
{
11+
use HasExtraInputAttributes;
12+
1013
protected string $view = 'inspirecms::filament.forms.components.code-editor';
1114

1215
protected int | string | Closure | null $minHeight = 420;
1316

14-
protected string | Closure | null $customStyle = null;
15-
1617
protected string | Closure | null $darkModeTheme = null;
1718

1819
protected string | Closure | null $lightModeTheme = null;
1920

20-
protected bool | Closure $isReadOnly = false;
21-
22-
protected bool | Closure $showCopyButton = false;
23-
24-
public function customStyle(?string $customStyle): static
25-
{
26-
$this->customStyle = $customStyle;
27-
28-
return $this;
29-
}
30-
3121
public function minHeight(int | string | Closure | null $minHeight = 768): static
3222
{
3323
$this->minHeight = $minHeight;
@@ -49,25 +39,6 @@ public function darkModeTheme(?string $darkModeTheme): static
4939
return $this;
5040
}
5141

52-
public function isReadOnly(bool | Closure $isReadOnly = false): static
53-
{
54-
$this->isReadOnly = $isReadOnly;
55-
56-
return $this;
57-
}
58-
59-
public function showCopyButton(bool | Closure $showCopyButton = true): static
60-
{
61-
$this->showCopyButton = $showCopyButton;
62-
63-
return $this;
64-
}
65-
66-
public function getIsReadOnly(): bool
67-
{
68-
return boolval($this->evaluate($this->isReadOnly));
69-
}
70-
7142
/**
7243
* @return ?string
7344
*/
@@ -86,22 +57,6 @@ public function getMinHeight()
8657
return null;
8758
}
8859

89-
/**
90-
* @return string
91-
*/
92-
public function getShowCopyButton()
93-
{
94-
return $this->evaluate($this->showCopyButton ? 'true' : 'false');
95-
}
96-
97-
/**
98-
* @return ?string
99-
*/
100-
public function getCustomStyle()
101-
{
102-
return $this->evaluate($this->customStyle);
103-
}
104-
10560
/**
10661
* @return ?string
10762
*/

src/Filament/Resources/TemplateResource/Pages/ListTemplates.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function table(Table $table): Table
6161
})
6262
->form([
6363
TemplateResourceHelper::getThemeFormComponent()->disabled(),
64-
TemplateResourceHelper::getContentFormComponent()->isReadOnly(true),
64+
TemplateResourceHelper::getContentFormComponent()->disabled(),
6565
])
6666
->mutateRecordDataUsing(fn (Template $record) => [
6767
'theme' => $this->theme,

0 commit comments

Comments
 (0)