Skip to content

Commit 8e894d5

Browse files
committed
Fix codeeditor styles
1 parent edeed89 commit 8e894d5

File tree

7 files changed

+363
-240
lines changed

7 files changed

+363
-240
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,7 @@
4040
"@codemirror/lang-json": "^6.0.1",
4141
"@codemirror/lang-php": "^6.0.1",
4242
"@codemirror/view": "^6.26.0",
43-
"cm6-theme-basic-dark": "^0.2.0",
44-
"cm6-theme-basic-light": "^0.2.0",
45-
"cm6-theme-gruvbox-dark": "^0.2.0",
46-
"cm6-theme-gruvbox-light": "^0.2.0",
47-
"cm6-theme-material-dark": "^0.2.0",
48-
"cm6-theme-nord": "^0.2.0",
49-
"cm6-theme-solarized-dark": "^0.2.0",
50-
"cm6-theme-solarized-light": "^0.2.0",
43+
"@fsegurai/codemirror-theme-bundle": "^6.2.0-beta",
5144
"codemirror": "^6.0.1"
5245
}
5346
}

resources/dist/components/code-editor.js

Lines changed: 15 additions & 15 deletions
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: 52 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,98 @@
11
import { basicSetup } from "codemirror";
22
import { EditorState } from '@codemirror/state';
33
import { EditorView, keymap } from "@codemirror/view";
4-
import { indentWithTab } from "@codemirror/commands";
5-
import { javascript } from "@codemirror/lang-javascript";
4+
5+
import { indentWithTab } from "@codemirror/commands"
6+
67
import { Compartment } from '@codemirror/state';
7-
import { json } from "@codemirror/lang-json";
8+
9+
import { basicLight } from '@fsegurai/codemirror-theme-basic-light'
10+
import { basicDark } from '@fsegurai/codemirror-theme-basic-dark'
11+
12+
import { javascript } from "@codemirror/lang-javascript";
813
import { php } from "@codemirror/lang-php";
914
import { css } from "@codemirror/lang-css";
1015
import { html } from "@codemirror/lang-html";
11-
import { basicLight } from 'cm6-theme-basic-light'
12-
import { basicDark } from 'cm6-theme-basic-dark'
13-
import { solarizedDark } from 'cm6-theme-solarized-dark'
14-
import { solarizedLight } from 'cm6-theme-solarized-light'
15-
import { materialDark } from 'cm6-theme-material-dark'
16-
import { nord } from 'cm6-theme-nord'
17-
import { gruvboxLight } from 'cm6-theme-gruvbox-light'
18-
import { gruvboxDark } from 'cm6-theme-gruvbox-dark'
19-
20-
export default function codeEditorFormComponentEnhace({
16+
import { json } from "@codemirror/lang-json";
17+
18+
export default function codeEditorFormComponent({
2119
state,
22-
darkTheme,
23-
lightTheme,
20+
isDarkMode,
2421
isReadOnly,
2522
}) {
2623
return {
2724
state,
25+
isDarkMode,
26+
isReadOnly,
27+
2828
editor: undefined,
29-
isReadOnly: false,
29+
3030
themeConfig: undefined,
31-
mode: 'light',
32-
theme: {
33-
styles: {
34-
'basic-light': {
35-
extension: basicLight,
36-
name: 'Basic Light'
37-
},
38-
'basic-dark': {
39-
extension: basicDark,
40-
name: 'Basic Dark'
41-
},
42-
'solarized-dark': {
43-
extension: solarizedDark,
44-
name: 'Solarized Dark'
45-
},
46-
'solarized-light': {
47-
extension: solarizedLight,
48-
name: 'Solarized Light'
49-
},
50-
'material-dark': {
51-
extension: materialDark,
52-
name: 'Material Dark'
53-
},
54-
'nord': {
55-
extension: nord,
56-
name: 'Nord'
57-
},
58-
'gruvbox-light': {
59-
extension: gruvboxLight,
60-
name: 'Gruvbox Light'
61-
},
62-
'gruvbox-dark': {
63-
extension: gruvboxDark,
64-
name: 'Gruvbox Dark'
65-
}
66-
},
67-
current: basicLight,
68-
light: basicDark,
69-
dark: basicDark,
70-
},
71-
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-
}
7631

77-
this.isReadOnly = isReadOnly;
78-
this.theme.dark = darkTheme;
79-
this.theme.light = lightTheme;
32+
init() {
8033
this.themeConfig = new Compartment();
81-
this.mode = theme;
82-
this.render();
83-
84-
this.toggleTheme(theme);
85-
34+
this.configureEditor();
8635
},
87-
toggleTheme(mode) {
88-
const selectedStyle = mode === 'dark' ? this.theme.dark : this.theme.light;
89-
this.theme.current = this.theme.styles[selectedStyle];
36+
getTheme() {
37+
return this.isDarkMode ? basicDark : basicLight;
38+
},
39+
toggleTheme(isDarkMode) {
40+
this.isDarkMode = isDarkMode;
9041

9142
if (this.editor) {
9243
this.editor.dispatch({
93-
effects: this.themeConfig.reconfigure(this.theme.current)
44+
effects: this.themeConfig.reconfigure(this.getTheme())
9445
});
9546
}
9647
},
97-
render() {
48+
configureEditor() {
49+
50+
const themeExtension = this.themeConfig.of(this.getTheme());
51+
9852
this.editor = new EditorView({
9953
state: EditorState.create({
100-
autofocus: true,
54+
autofocus: false,
10155
indentWithTabs: true,
10256
smartIndent: true,
10357
lineNumbers: true,
10458
matchBrackets: true,
105-
tabSize: 2,
59+
lineWrapping: true,
10660
styleSelectedText: true,
61+
indentUnit: 6,
62+
tabSize: 4,
10763
extensions: [
64+
10865
basicSetup,
109-
keymap.of([indentWithTab]),
66+
67+
keymap.of([
68+
indentWithTab,
69+
]),
70+
71+
html({
72+
matchClosingTags: true,
73+
selfClosingTags: true,
74+
autoCloseTags: true,
75+
}),
11076
javascript(),
77+
css(),
11178
php(),
11279
json(),
113-
css(),
114-
html(),
115-
this.themeConfig.of(this.theme.current),
80+
81+
themeExtension,
82+
11683
EditorView.updateListener.of((v) => {
11784
if (v.docChanged) {
11885
this.state = v.state.doc.toString();
11986
}
12087
}),
121-
EditorState.readOnly.of(this.isReadOnly)
88+
89+
EditorState.readOnly.of(this.isReadOnly),
90+
12291
],
92+
12393
doc: this.state
12494
}),
95+
12596
parent: this.$refs.codeEditor,
12697
});
12798
},

resources/views/filament/forms/components/code-editor.blade.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
"
2323
>
2424
<div class="code-editor-textarea-wrapper-ctn overflow-auto"
25-
@theme-changed.window="(e) => toggleTheme(e.detail)"
26-
x-data="codeEditorFormComponentEnhace({
25+
@theme-changed.window="(e) => toggleTheme(
26+
e.detail === 'system' ? window.matchMedia('(prefers-color-scheme: dark)').matches : e.detail === 'dark'
27+
)"
28+
x-data="codeEditorFormComponent({
2729
state: $wire.{{ $applyStateBindingModifiers('entangle(\'' . $getStatePath() . '\')') }},
28-
darkTheme: @js($getDarkModeTheme()),
29-
lightTheme: @js($getLightModeTheme()),
3030
isReadOnly: @js($isDisabled()),
31+
isDarkMode: (Alpine.store('theme') || 'light') === 'system' ? window.matchMedia('(prefers-color-scheme: dark)').matches : (Alpine.store('theme') || 'light') === 'dark',
3132
})"
3233
x-load
3334
x-load-src="{{ \Filament\Support\Facades\FilamentAsset::getAlpineComponentSrc('filament-code-editor', 'solution-forest/inspirecms') }}"

src/Filament/Forms/Components/CodeEditor.php

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,13 @@ class CodeEditor extends Field
1414

1515
protected int | string | Closure | null $minHeight = 420;
1616

17-
protected string | Closure | null $darkModeTheme = null;
18-
19-
protected string | Closure | null $lightModeTheme = null;
20-
2117
public function minHeight(int | string | Closure | null $minHeight = 768): static
2218
{
2319
$this->minHeight = $minHeight;
2420

2521
return $this;
2622
}
2723

28-
public function lightModeTheme(?string $lightModeTheme): static
29-
{
30-
$this->lightModeTheme = $lightModeTheme;
31-
32-
return $this;
33-
}
34-
35-
public function darkModeTheme(?string $darkModeTheme): static
36-
{
37-
$this->darkModeTheme = $darkModeTheme;
38-
39-
return $this;
40-
}
41-
4224
/**
4325
* @return ?string
4426
*/
@@ -56,20 +38,4 @@ public function getMinHeight()
5638

5739
return null;
5840
}
59-
60-
/**
61-
* @return ?string
62-
*/
63-
public function getDarkModeTheme()
64-
{
65-
return $this->evaluate($this->darkModeTheme);
66-
}
67-
68-
/**
69-
* @return ?string
70-
*/
71-
public function getLightModeTheme()
72-
{
73-
return $this->evaluate($this->lightModeTheme);
74-
}
7541
}

src/Filament/Resources/Helpers/TemplateResourceHelper.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ public static function getThemeFormComponent()
3838
public static function getContentFormComponent($name = 'content')
3939
{
4040
return \SolutionForest\InspireCms\Filament\Forms\Components\CodeEditor::make($name)
41-
->darkModeTheme('basic-dark')
42-
->lightModeTheme('basic-light')
4341
->minHeight('48rem');
4442
}
4543

0 commit comments

Comments
 (0)