Skip to content

Commit

Permalink
Merge pull request #1 from codebar-ag/feature-updates
Browse files Browse the repository at this point in the history
Handle formatting
  • Loading branch information
StanBarrows authored Mar 22, 2024
2 parents 5b51545 + fc64bc9 commit 3f18734
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 14 deletions.
21 changes: 12 additions & 9 deletions resources/views/forms/components/json-input.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
:field="$field"

>
@php
ray($getStatePath())
@endphp
<div
x-data="{ state: $wire.{{ $applyStateBindingModifiers("\$entangle('{$getStatePath()}')") }} }"
style="position: relative; border-radius: 0.375rem;"
Expand All @@ -14,7 +11,7 @@
<div
wire:ignore
x-init="
codeMirrorEditor = CodeMirror($refs.input, {
{{ str_replace('.', '', $getId()) }} = CodeMirror($refs.{{ str_replace('.', '', $getId()) }}, {
mode: 'application/json',
lineNumbers: {{ json_encode($getHasLineNumbers()) }},
lineWrapping: {{ json_encode($getHasLineWrapping()) }},
Expand Down Expand Up @@ -57,19 +54,25 @@
}
});
codeMirrorEditor.setSize('100%', '100%');
codeMirrorEditor.setValue(state ?? '{}');
{{ str_replace('.', '', $getId()) }}.setSize('100%', '100%');
{{ str_replace('.', '', $getId()) }}.setValue({{ json_encode(json_encode($getState(), JSON_PRETTY_PRINT), JSON_UNESCAPED_SLASHES) }} ?? '{}');
setTimeout(function() {
codeMirrorEditor.refresh();
{{ str_replace('.', '', $getId()) }}.refresh();
}, 1);
codeMirrorEditor.on('change', () => state = codeMirrorEditor.getValue())
{{ str_replace('.', '', $getId()) }}.on('change', function () {
try {
state = JSON.parse({{ str_replace('.', '', $getId()) }}.getValue())
} catch (e) {
state = {{ str_replace('.', '', $getId()) }}.getValue();
}
});
"
>
<div
wire:ignore
x-ref="input"
x-ref="{{ str_replace('.', '', $getId()) }}"
></div>
</div>
</div>
Expand Down
11 changes: 6 additions & 5 deletions resources/views/infolists/components/json-entry.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
<div
wire:ignore
x-init="
codeMirrorEditor = CodeMirror($refs.input, {
{{ str_replace('.', '', $getId()) }} = CodeMirror($refs.{{ str_replace('.', '', $getId()) }}, {
mode: 'application/json',
readOnly: true,
lineNumbers: {{ json_encode($getHasLineNumbers()) }},
lineWrapping: {{ json_encode($getHasLineWrapping()) }},
autoCloseBrackets: {{ json_encode($getHasAutoCloseBrackets()) }},
lineNumbers: {{ json_encode($getHasLineNumbers()) }},
viewportMargin: Infinity,
theme: '{{ $getHasDarkTheme() ? 'darcula' : 'default' }}',
foldGutter: {{ json_encode($getHasFoldingCode()) }},
Expand Down Expand Up @@ -50,17 +51,17 @@
}
});
codeMirrorEditor.setSize(null, '100%');
codeMirrorEditor.setValue({{ json_encode($getState()) }} ?? '{}');
{{ str_replace('.', '', $getId()) }}.setSize(null, '100%');
{{ str_replace('.', '', $getId()) }}.setValue({{ json_encode(json_encode($getState(), JSON_PRETTY_PRINT), JSON_UNESCAPED_SLASHES) }} ?? '{}');
setTimeout(function() {
codeMirrorEditor.refresh();
{{ str_replace('.', '', $getId()) }}.refresh();
}, 1);
"
>
<div
wire:ignore
x-ref="input"
x-ref="{{ str_replace('.', '', $getId()) }}"
></div>
</div>
</div>
Expand Down
11 changes: 11 additions & 0 deletions src/Forms/Components/JsonInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,15 @@ class JsonInput extends Field
use HasLineWrapping;

protected string $view = 'filament-json-field::forms.components.json-input';

public function setUp(): void
{
parent::setUp();

$this
->rules(['array'])
->validationMessages([
'array' => __('The :attribute must be valid JSON.'),
]);
}
}
12 changes: 12 additions & 0 deletions tests/JsonEntryFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@
expect($field->getHasLineNumbers())->toBe(true);
});

it('can have line wrapping code ', function () {
$field = JsonEntry::make('json');

expect($field->getHasLineWrapping())->toBe(true);

$field->lineWrapping(false);
expect($field->getHasLineWrapping())->toBe(false);

$field->lineWrapping(true);
expect($field->getHasLineWrapping())->toBe(true);
});

it('can have code ', function () {
$field = JsonEntry::make('json');

Expand Down
12 changes: 12 additions & 0 deletions tests/JsonInputFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@
expect($field->getHasLineNumbers())->toBe(true);
});

it('can have line wrapping code ', function () {
$field = JsonInput::make('json');

expect($field->getHasLineWrapping())->toBe(true);

$field->lineWrapping(false);
expect($field->getHasLineWrapping())->toBe(false);

$field->lineWrapping(true);
expect($field->getHasLineWrapping())->toBe(true);
});

it('can have code ', function () {
$field = JsonInput::make('json');

Expand Down

0 comments on commit 3f18734

Please sign in to comment.