Skip to content

Commit 048133d

Browse files
committed
wip html live refresh
1 parent 070c6ce commit 048133d

5 files changed

Lines changed: 53 additions & 22 deletions

File tree

demo/app/Sharp/Posts/PostForm.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use App\Sharp\Utils\Embeds\CodeEmbed;
1010
use App\Sharp\Utils\Embeds\RelatedPostEmbed;
1111
use App\Sharp\Utils\Embeds\TableOfContentsEmbed;
12+
use Carbon\Carbon;
1213
use Code16\Sharp\Form\Eloquent\Uploads\Transformers\SharpUploadModelFormAttributeTransformer;
1314
use Code16\Sharp\Form\Eloquent\WithSharpFormEloquentUpdater;
1415
use Code16\Sharp\Form\Fields\Editor\Uploads\SharpFormEditorUpload;
@@ -28,7 +29,10 @@
2829
use Code16\Sharp\Form\Layout\FormLayoutTab;
2930
use Code16\Sharp\Form\SharpForm;
3031
use Code16\Sharp\Utils\Fields\FieldsContainer;
32+
use Illuminate\Support\Arr;
3133
use Illuminate\Support\Facades\Blade;
34+
use Illuminate\Support\Facades\Storage;
35+
use Illuminate\Support\Str;
3236

3337
class PostForm extends SharpForm
3438
{
@@ -152,6 +156,23 @@ public function buildFormFields(FieldsContainer $formFields): void
152156
->setStorageDisk('local')
153157
->setStorageBasePath('data/posts/{id}')
154158
->addConditionalDisplay('!is_link'),
159+
)
160+
->addItemField(
161+
SharpFormHtmlField::make('document_infos')
162+
->setTemplate(function (array $data, string $fieldKey) {
163+
$itemData = Arr::get($data, Str::beforeLast($fieldKey, '.'));
164+
if (! isset($itemData['document']['file_name'])) {
165+
return '';
166+
}
167+
168+
return sprintf(
169+
'File last modified at : %s',
170+
Carbon::createFromTimestamp(
171+
Storage::disk($itemData['document']['disk'])
172+
->lastModified($itemData['document']['file_name'])
173+
)
174+
);
175+
})
155176
),
156177
)
157178
->when(sharp()->context()->isUpdate(), fn ($formFields) => $formFields->addField(
@@ -193,7 +214,8 @@ public function buildFormLayout(FormLayout $formLayout): void
193214
->withListField('attachments', function (FormLayoutColumn $item) {
194215
$item->withFields(title: 8, is_link: 4)
195216
->withField('link_url')
196-
->withField('document');
217+
->withField('document')
218+
->withField('document_infos');
197219
});
198220
})
199221
->addColumn(6, function (FormLayoutColumn $column) {

resources/js/form/components/fields/List.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,14 @@
152152
if(i === itemIndex) {
153153
return {
154154
...item,
155-
...(!inputOptions.force ? getDependantFieldsResetData(props.field.itemFields, itemFieldKey) : null),
155+
...(!inputOptions?.force ? getDependantFieldsResetData(props.field.itemFields, itemFieldKey) : null),
156156
[itemFieldKey]: itemFieldValue,
157157
}
158158
}
159159
return item;
160160
}), {
161-
force: inputOptions.force,
162-
skipRefresh: inputOptions.skipRefresh,
161+
force: inputOptions?.force,
162+
skipRefresh: inputOptions?.skipRefresh,
163163
shouldRefresh: form.shouldRefresh(itemFieldKey, props.field.itemFields)
164164
});
165165
}

src/Form/Fields/Formatters/UploadFormatter.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function fromFront(SharpFormField $field, string $attribute, $value): ?ar
3131
);
3232

3333
return tap($this->normalizeFromFront($value, [
34-
'file_name' => $field->storageDisk()
34+
'file_name' => $field->storageDisk() && ! sharp()->context()->isFormRefresh()
3535
? sprintf(
3636
'%s/%s',
3737
str($field->storageBasePath())->replace('{id}', $this->instanceId ?? '{id}'),
@@ -44,7 +44,9 @@ public function fromFront(SharpFormField $field, string $attribute, $value): ?ar
4444
->size($uploadedFieldRelativePath),
4545
'mime_type' => Storage::disk(sharp()->config()->get('uploads.tmp_disk'))
4646
->mimeType($uploadedFieldRelativePath),
47-
'disk' => $field->storageDisk() ?: sharp()->config()->get('uploads.tmp_disk'),
47+
'disk' => $field->storageDisk() && ! sharp()->context()->isFormRefresh()
48+
? $field->storageDisk()
49+
: sharp()->config()->get('uploads.tmp_disk'),
4850
'filters' => $field->isImageTransformOriginal()
4951
? null
5052
: $value['filters'] ?? null,

src/Http/Context/SharpContext.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ public function instanceId(): ?string
9595
return $current?->instanceId();
9696
}
9797

98+
public function isFormRefresh(): bool
99+
{
100+
return request()->routeIs('code16.sharp.api.form.refresh.update');
101+
}
102+
98103
public function findListInstance(string $instanceId, ?Closure $notFoundCallback = null): mixed
99104
{
100105
if (isset($this->cachedListInstances)) {

src/Utils/Fields/HandleFormHtmlFields.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,27 @@ final public function formatHtmlFields(array $frontData, bool $keepOnlyHtmlField
4444
}
4545

4646
if ($field instanceof SharpFormListField && $field->itemFields()->whereInstanceOf(SharpFormHtmlField::class)->isNotEmpty()) {
47-
return collect($value)->map(function ($item, $index) use ($field, $key, $formattedData, $keepOnlyHtmlFields) {
48-
return collect($item)->mapWithKeys(function ($itemValue, $itemKey) use ($field, $key, $index, $formattedData, $keepOnlyHtmlFields) {
49-
$itemField = $field->findItemFormFieldByKey($itemKey);
47+
return [
48+
$key => collect($value)->map(function ($item, $index) use ($field, $key, $formattedData, $keepOnlyHtmlFields) {
49+
return collect($item)->mapWithKeys(function ($itemValue, $itemKey) use ($field, $key, $index, $formattedData, $keepOnlyHtmlFields) {
50+
$itemField = $field->findItemFormFieldByKey($itemKey);
5051

51-
if ($itemField instanceof SharpFormHtmlField) {
52-
$fieldKey = "$key.$index.$itemKey";
52+
if ($itemField instanceof SharpFormHtmlField) {
53+
$fieldKey = "$key.$index.$itemKey";
5354

54-
return [
55-
$itemKey => $itemField->render([
56-
'fieldKey' => $fieldKey,
57-
...$formattedData,
58-
...(is_array($itemValue) ? $itemValue : []),
59-
], $fieldKey),
60-
];
61-
}
55+
return [
56+
$itemKey => $itemField->render([
57+
'fieldKey' => $fieldKey,
58+
...$formattedData,
59+
...(is_array($itemValue) ? $itemValue : []),
60+
], $fieldKey),
61+
];
62+
}
6263

63-
return $keepOnlyHtmlFields ? [] : [$key => $itemValue];
64-
})->all();
65-
})->all();
64+
return $keepOnlyHtmlFields ? [] : [$itemKey => $itemValue];
65+
})->all();
66+
})->all(),
67+
];
6668
}
6769

6870
return $keepOnlyHtmlFields ? [] : [$key => $value];

0 commit comments

Comments
 (0)