Skip to content

Commit

Permalink
Small refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
BobWez98 committed Nov 18, 2024
1 parent 25b280c commit 51c3386
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ composer require justbetter/statamic-translation-management

This addon makes use of [spatie/laravel-translation-loader](https://github.com/spatie/laravel-translation-loader), and has migrations.
To publish and migrate, you can run:

```bash
php artisan vendor:publish --provider="Spatie\TranslationLoader\TranslationServiceProvider" --tag="migrations"
php artisan migrate
```

This addon adds two custom field types, if you want to use them include the script in your cp.js.
```js
import 'Vendor/justbetter/statamic-translation-management/resources/js/translation-manager.js'
```

## How to Use

This addon adds a "translations" menu item in the control panel through [Runway](https://github.com/statamic-rad-pack/runway). Here you can add/edit the translations.
18 changes: 8 additions & 10 deletions resources/js/components/fieldtypes/LocaleSelectArrayFieldtype.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,25 @@
</thead>
<tbody>
<tr v-for="(row, index) in rows" :key="index">
<!-- Replace the key input with a select dropdown -->
<td>
<v-select v-model="row.key" :options="configOptions" label="label" value-field="value"
placeholder="Select a key" />
<v-select
v-model="row.key"
:options="configOptions"
label="label"
value-field="value"
placeholder="Select a key"
/>
</td>
<!-- Value input -->
<td>
<text-input v-model="row.value" placeholder="Enter value" :name="'value-' + index" />
</td>
<!-- Actions -->
<td>
<button type="button" @click="removeRow(index)">Remove</button>
</td>
</tr>
</tbody>
</table>

<!-- Add Row Button -->
<button type="button" class="btn-primary" @click="addRow">Add Row</button>
</div>
</template>
Expand All @@ -36,7 +37,7 @@
export default {
props: {
value: {
type: Object, // Adjusted to match the desired data structure
type: Object,
default: () => ({}),
},
config: {
Expand All @@ -51,7 +52,6 @@ export default {
},
computed: {
configOptions() {
// Transform the `options` metadata into label/value pairs
return Object.values(this.$attrs.meta.options).map((option) => ({
label: option,
value: option,
Expand All @@ -68,14 +68,12 @@ export default {
},
methods: {
parseInitialValue(value) {
// Convert the initial object to an array for easier manipulation in the UI
return Object.entries(value).map(([key, val]) => ({
key: { label: key, value: key },
value: val,
}));
},
transformToDatabaseFormat() {
// Transform rows array back into the desired key-value object
return this.rows.reduce((acc, row) => {
if (row.key && row.key.value) {
acc[row.key.value] = row.value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export default {
},
computed: {
parsedKeys() {
console.log(this.value)
if (Array.isArray(this.value)) {
return this.value.map((item) => item.key?.value || item.key);
} else if (typeof this.value === 'object') {
Expand Down
File renamed without changes.
18 changes: 12 additions & 6 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ public function register()

public function bootAddon(): void
{
TranslationKey::register();
LocaleSelectArray::register();

$this->bootRunway()
->bootPublishables();
->bootPublishables()
->bootFieldTypes();
}

public function bootRunway(): self
protected function bootRunway(): self
{
config(['runway.resources' => array_merge(
[LanguageLine::class => [
Expand All @@ -36,12 +34,20 @@ public function bootRunway(): self
return $this;
}

public function bootPublishables(): self
protected function bootPublishables(): self
{
$this->publishes([
__DIR__.'/../resources/blueprints/vendor/runway' => resource_path('blueprints/vendor/runway'),
]);

return $this;
}

protected function bootFieldTypes(): self
{
TranslationKey::register();
LocaleSelectArray::register();

return $this;
}
}

0 comments on commit 51c3386

Please sign in to comment.