-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added new Code field (flask js plugin)
- Loading branch information
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Leeto\MoonShine\Fields; | ||
|
||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Code extends BaseField | ||
{ | ||
protected static string $view = 'code'; | ||
|
||
public string $language = 'php'; | ||
|
||
public bool $lineNumbers = false; | ||
|
||
protected array $assets = [ | ||
'js' => ['https://unpkg.com/codeflask/build/codeflask.min.js'], | ||
]; | ||
|
||
public function language(string $language): static | ||
{ | ||
$this->language = $language; | ||
|
||
return $this; | ||
} | ||
|
||
public function lineNumbers(): static | ||
{ | ||
$this->lineNumbers = true; | ||
|
||
return $this; | ||
} | ||
|
||
public function indexViewValue(Model $item, bool $container = true): string | ||
{ | ||
return str($item->{$this->field()}) | ||
->before('<pre>') | ||
->after('</pre>') | ||
->stripTags(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<div id="flask_{{ $field->id() }}" class="w-100 relative border" style="min-height: 300px"></div> | ||
|
||
<input type="hidden" | ||
id="{{ $field->id() }}" | ||
name="{{ $field->name() }}" | ||
value="{{ $field->formViewValue($item) ?? '' }}" | ||
/> | ||
|
||
<script> | ||
(function() { | ||
document.addEventListener("DOMContentLoaded", function(event) { | ||
const input = document.getElementById('{{ $field->id() }}'); | ||
const flask = new CodeFlask('#flask_{{ $field->id() }}', { | ||
lineNumbers: {{ $field->lineNumbers ? 'true' : 'false' }}, | ||
language: '{{ $field->language }}', | ||
readonly: {{ $field->isReadonly() ? 'true' : 'false' }}, | ||
}); | ||
flask.onUpdate((code) => { | ||
input.value = code; | ||
}); | ||
flask.updateCode(input.value); | ||
}); | ||
})(); | ||
</script> | ||
|