Skip to content

Commit cbc1c78

Browse files
authored
Merge pull request #5 from Muetze42/development
0.0.2 with requirements
2 parents 267d0e5 + 34c9b1a commit cbc1c78

File tree

13 files changed

+229
-35
lines changed

13 files changed

+229
-35
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# Nova Textarea with BBCodes
1+
# Nova Textarea- & Text field with BBCodes
22
Quickly developed. Short tutorial.
33

4-
With this Nova field you have the _Textarea_ with _BBCodes_.
4+
With this Nova field you have the _Textarea_ or _Text_ with _BBCodes_.
55

66
(Tested with Nova 4)
77

@@ -20,6 +20,16 @@ BBCode::make(__('Name'), 'name')->codes([
2020
'{date}' => __('This code insert the date'),
2121
]),
2222
```
23+
Or Text Field:
24+
```php
25+
26+
use NormanHuth\BBCode\BB;
27+
28+
BB::make(__('Name'), 'name')->codes([
29+
'{author}' => __('This code insert the author'),
30+
'{date}' => __('This code insert the date'),
31+
]),
32+
```
2333

2434
### Don't always show the content of textarea fields inside Nova
2535
```php

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "norman-huth/nova-bbcode-textarea",
3-
"description": "Textarea with BBCodes. A Laravel Nova field.",
3+
"description": "Laravel Nova Textarea and Text field with BBCodes",
44
"keywords": [
55
"laravel",
66
"nova"

dist/js/field.js

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

dist/mix-manifest.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
{
2-
"/js/field.js": "/js/field.js",
3-
"/css/field.css": "/css/field.css"
2+
"/js/field.js": "/js/field.js"
43
}

resources/css/field.css

Lines changed: 0 additions & 1 deletion
This file was deleted.

resources/js/components/DetailField.vue

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<template>
2+
<DefaultField
3+
:field="currentField"
4+
:errors="errors"
5+
:show-help-text="showHelpText"
6+
>
7+
<template #field>
8+
<input
9+
v-bind="extraAttributes"
10+
class="w-full form-control form-input form-input-bordered mb-2"
11+
@input="handleChange"
12+
:value="value"
13+
:id="currentField.uniqueKey"
14+
:dusk="field.attribute"
15+
:disabled="currentlyIsReadonly"
16+
:list="`${field.attribute}-list`"
17+
/>
18+
<span v-for="(title, code) in currentField.codes" :class="currentField.btnClass" :title="title" @submit.prevent="" @click="addCode(code)" :style="currentField.btnStyle">
19+
{{ code }}
20+
</span>
21+
<datalist
22+
v-if="currentField.suggestions && currentField.suggestions.length > 0"
23+
:id="`${field.attribute}-list`"
24+
>
25+
<option
26+
:key="suggestion"
27+
v-for="suggestion in currentField.suggestions"
28+
:value="suggestion"
29+
/>
30+
</datalist>
31+
</template>
32+
</DefaultField>
33+
</template>
34+
35+
<script>
36+
import {DependentFormField, HandlesValidationErrors} from './../../mixins'
37+
38+
export default {
39+
mixins: [HandlesValidationErrors, DependentFormField],
40+
41+
computed: {
42+
defaultAttributes() {
43+
return {
44+
type: this.currentField.type || 'text',
45+
min: this.currentField.min,
46+
max: this.currentField.max,
47+
step: this.currentField.step,
48+
pattern: this.currentField.pattern,
49+
placeholder: this.currentField.placeholder || this.field.name,
50+
class: this.errorClasses,
51+
codes: this.currentField.codes,
52+
btnClass: this.currentField.btnClass,
53+
btnStyle: this.currentField.btnStyle,
54+
}
55+
},
56+
57+
extraAttributes() {
58+
const attrs = this.currentField.extraAttributes
59+
60+
return {
61+
// Leave the default attributes even though we can now specify
62+
// whatever attributes we like because the old number field still
63+
// uses the old field attributes
64+
...this.defaultAttributes,
65+
...attrs,
66+
}
67+
},
68+
},
69+
methods: {
70+
addCode(code) {
71+
this.value = this.value + " " + code
72+
}
73+
},
74+
}
75+
</script>

resources/js/components/FormField.vue renamed to resources/js/components/bbcode/FormField.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</template>
2323

2424
<script>
25-
import {DependentFormField, HandlesValidationErrors} from './../mixins'
25+
import {DependentFormField, HandlesValidationErrors} from './../../mixins'
2626
2727
export default {
2828
mixins: [HandlesValidationErrors, DependentFormField],

resources/js/field.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import DetailField from './components/DetailField'
2-
import FormField from './components/FormField'
1+
import FormField from './components/bbcode/FormField'
2+
import FormField2 from './components/bb/FormField'
33

44
Nova.booting((app, store) => {
5-
app.component('detail-bbcode', DetailField)
6-
app.component('form-bbcode', FormField)
5+
app.component('form-bbcode', FormField)
6+
app.component('form-bb', FormField2)
77
})

src/BB.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
namespace NormanHuth\BBCode;
4+
5+
use Laravel\Nova\Fields\Text as Field;
6+
use Laravel\Nova\Http\Requests\NovaRequest;
7+
8+
class BB extends Field
9+
{
10+
/**
11+
* The codes in key => value: code => title
12+
*
13+
* @var array
14+
*/
15+
protected array $codes = [];
16+
17+
/**
18+
* The button class attribute
19+
*
20+
* @var string
21+
*/
22+
protected string $btnClass = 'shadow relative bg-primary-500 hover:bg-primary-400 active:bg-primary-600 text-white dark:text-gray-900 cursor-pointer rounded text-sm font-bold focus:outline-none focus:ring inline-flex items-center justify-center h-9 px-3 shadow relative bg-primary-500 hover:bg-primary-400 active:bg-primary-600 text-white dark:text-gray-900 mr-2';
23+
24+
/**
25+
* Button inline style attribute
26+
*
27+
* @var string
28+
*/
29+
protected string $btnStyle = '';
30+
31+
/**
32+
* Get the component name for the field.
33+
*
34+
* @return string
35+
*/
36+
public function component(): string
37+
{
38+
if (isset(static::$customComponents[get_class($this)])) {
39+
return static::$customComponents[get_class($this)];
40+
}
41+
42+
if (request()->input('editing')) {
43+
return 'bb';
44+
}
45+
46+
return 'text-field';
47+
}
48+
49+
/**
50+
* @param array $codes
51+
* @return $this
52+
*/
53+
public function codes(array $codes): static
54+
{
55+
$this->codes = $codes;
56+
57+
return $this;
58+
}
59+
60+
/**
61+
* Change the button inline style attribute
62+
*
63+
* @param string $style
64+
* @return $this
65+
*/
66+
public function btnStyle(string $style): static
67+
{
68+
$this->btnStyle = $style;
69+
70+
return $this;
71+
}
72+
73+
/**
74+
* Change the button class attribute
75+
*
76+
* @param string $class
77+
* @return $this
78+
*/
79+
public function btnClass(string $class): static
80+
{
81+
$this->btnClass = $class;
82+
83+
return $this;
84+
}
85+
86+
/**
87+
* Prepare the element for JSON serialization.
88+
*
89+
* @return array<string, mixed>
90+
*/
91+
public function jsonSerialize(): array
92+
{
93+
$request = app(NovaRequest::class);
94+
95+
if ($request->isCreateOrAttachRequest()
96+
|| $request->isUpdateOrUpdateAttachedRequest()
97+
|| $request->isActionRequest() === true
98+
) {
99+
return array_merge(parent::jsonSerialize(), [
100+
'suggestions' => $this->resolveSuggestions($request),
101+
'codes' => $this->codes,
102+
'btnStyle' => $this->btnStyle,
103+
'btnClass' => $this->btnClass,
104+
]);
105+
}
106+
107+
return array_merge(parent::jsonSerialize(), [
108+
'codes' => $this->codes,
109+
'btnStyle' => $this->btnStyle,
110+
'btnClass' => $this->btnClass,
111+
]);
112+
}
113+
}

0 commit comments

Comments
 (0)