Skip to content

Commit

Permalink
added new Code field (flask js plugin)
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-to committed Jun 9, 2022
1 parent 8f65ff1 commit 8584127
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/Fields/Code.php
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();
}
}
28 changes: 28 additions & 0 deletions src/views/fields/code.blade.php
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>

0 comments on commit 8584127

Please sign in to comment.