-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(block-editor): add import from url feature #29874
- Loading branch information
Showing
11 changed files
with
335 additions
and
41 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
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
52 changes: 52 additions & 0 deletions
52
...edit-content-file-field/components/dot-form-import-url/dot-form-import-url.component.html
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,52 @@ | ||
<form (ngSubmit)="onSubmit()" [formGroup]="form" class="url-mode__form" data-testId="form"> | ||
<div class="url-mode__input-container"> | ||
<label for="url-input">{{ 'dot.file.field.action.import.from.url' | dm }}</label> | ||
<input | ||
id="url-input" | ||
type="text" | ||
autocomplete="off" | ||
formControlName="url" | ||
pInputText | ||
placeholder="https://www.dotcms.com/image.png" | ||
aria-label="URL input field" | ||
data-testId="url-input" /> | ||
<div class="error-messsage__container"> | ||
@let error = store.error(); | ||
@if (error) { | ||
<dot-field-validation-message | ||
[message]="'dot.file.field.action.import.from.url.error.message' | dm" | ||
[field]="form.get('url')" | ||
data-testId="error-message" /> | ||
} @else { | ||
<small class="p-invalid" data-testId="error-msg"> | ||
{{ error | dm }} | ||
</small> | ||
} | ||
</div> | ||
</div> | ||
<div class="url-mode__actions"> | ||
<p-button | ||
(click)="cancelUpload()" | ||
[label]="'dot.common.cancel' | dm" | ||
styleClass="p-button-outlined" | ||
type="button" | ||
aria-label="Cancel button" | ||
data-testId="cancel-button" /> | ||
<div [style]="{ width: '6.85rem' }"> | ||
@if (store.isLoading()) { | ||
<p-button | ||
[icon]="'pi pi-spin pi-spinner'" | ||
type="button" | ||
aria-label="Loading button" | ||
data-testId="loading-button" /> | ||
} @else { | ||
<p-button | ||
[label]="'dot.common.import' | dm" | ||
[icon]="'pi pi-download'" | ||
type="submit" | ||
aria-label="Import button" | ||
data-testId="import-button" /> | ||
} | ||
</div> | ||
</div> | ||
</form> |
37 changes: 37 additions & 0 deletions
37
...edit-content-file-field/components/dot-form-import-url/dot-form-import-url.component.scss
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,37 @@ | ||
@use "variables" as *; | ||
|
||
:host ::ng-deep { | ||
display: block; | ||
width: 32rem; | ||
|
||
.p-button { | ||
width: 100%; | ||
} | ||
|
||
.error-messsage__container { | ||
min-height: $spacing-4; | ||
} | ||
} | ||
|
||
.url-mode__form { | ||
display: flex; | ||
flex-direction: column; | ||
gap: $spacing-3; | ||
justify-content: center; | ||
align-items: flex-start; | ||
} | ||
|
||
.url-mode__input-container { | ||
width: 100%; | ||
display: flex; | ||
gap: $spacing-1; | ||
flex-direction: column; | ||
} | ||
|
||
.url-mode__actions { | ||
width: 100%; | ||
display: flex; | ||
gap: $spacing-1; | ||
align-items: center; | ||
justify-content: flex-end; | ||
} |
71 changes: 71 additions & 0 deletions
71
...t-edit-content-file-field/components/dot-form-import-url/dot-form-import-url.component.ts
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,71 @@ | ||
import { ChangeDetectionStrategy, Component, effect, inject, OnInit } from '@angular/core'; | ||
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms'; | ||
|
||
import { ButtonModule } from 'primeng/button'; | ||
import { DynamicDialogRef, DialogService } from 'primeng/dynamicdialog'; | ||
import { InputTextModule } from 'primeng/inputtext'; | ||
|
||
import { DotMessagePipe, DotFieldValidationMessageComponent } from '@dotcms/ui'; | ||
|
||
import { FormImportUrlStore } from './store/form-import-url.store'; | ||
|
||
@Component({ | ||
selector: 'dot-form-import-url', | ||
standalone: true, | ||
imports: [ | ||
DotMessagePipe, | ||
ReactiveFormsModule, | ||
DotFieldValidationMessageComponent, | ||
ButtonModule, | ||
InputTextModule | ||
], | ||
templateUrl: './dot-form-import-url.component.html', | ||
styleUrls: ['./dot-form-import-url.component.scss'], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
providers: [FormImportUrlStore] | ||
}) | ||
export class DotFormImportUrlComponent implements OnInit { | ||
readonly #formBuilder = inject(FormBuilder); | ||
readonly store = inject(FormImportUrlStore); | ||
readonly #dialogRef = inject(DynamicDialogRef); | ||
readonly #dialogService = inject(DialogService); | ||
readonly #instance = this.#dialogService.getInstance(this.#dialogRef); | ||
|
||
readonly form = this.#formBuilder.group({ | ||
url: ['', [Validators.required, Validators.pattern(/^(ftp|http|https):\/\/[^ "]+$/)]] | ||
}); | ||
|
||
constructor() { | ||
effect( | ||
() => { | ||
const file = this.store.file(); | ||
const isDone = this.store.isDone(); | ||
|
||
if (file && isDone) { | ||
this.#dialogRef.close(file); | ||
} | ||
}, | ||
{ | ||
allowSignalWrites: true | ||
} | ||
); | ||
} | ||
|
||
ngOnInit(): void { | ||
const uploadType = this.#instance?.data?.inputType === 'Binary' ? 'temp' : 'dotasset'; | ||
this.store.setUploadType(uploadType); | ||
} | ||
|
||
onSubmit(): void { | ||
if (this.form.invalid) { | ||
return; | ||
} | ||
|
||
const { url } = this.form.getRawValue(); | ||
this.store.uploadFileByUrl(url); | ||
} | ||
|
||
cancelUpload(): void { | ||
this.#dialogRef.close(); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...dot-edit-content-file-field/components/dot-form-import-url/store/form-import-url.store.ts
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,56 @@ | ||
import { tapResponse } from '@ngrx/operators'; | ||
import { patchState, signalStore, withComputed, withMethods, withState } from '@ngrx/signals'; | ||
import { rxMethod } from '@ngrx/signals/rxjs-interop'; | ||
import { pipe } from 'rxjs'; | ||
|
||
import { computed, inject } from '@angular/core'; | ||
|
||
import { switchMap, tap } from 'rxjs/operators'; | ||
|
||
import { UploadedFile, UPLOAD_TYPE } from '../../../models'; | ||
import { DotFileFieldUploadService } from '../../../services/upload-file/upload-file.service'; | ||
|
||
export interface FormImportUrlState { | ||
file: UploadedFile | null; | ||
status: 'init' | 'uploading' | 'done' | 'error'; | ||
error: string | null; | ||
uploadType: UPLOAD_TYPE; | ||
} | ||
|
||
const initialState: FormImportUrlState = { | ||
file: null, | ||
status: 'init', | ||
error: null, | ||
uploadType: 'temp' | ||
}; | ||
|
||
export const FormImportUrlStore = signalStore( | ||
withState(initialState), | ||
withComputed((state) => ({ | ||
isLoading: computed(() => state.status() === 'uploading'), | ||
isDone: computed(() => state.status() === 'done') | ||
})), | ||
withMethods((store, uploadService = inject(DotFileFieldUploadService)) => ({ | ||
uploadFileByUrl: rxMethod<string>( | ||
pipe( | ||
tap(() => patchState(store, { status: 'uploading' })), | ||
switchMap((fileUrl) => { | ||
return uploadService | ||
.uploadFile({ | ||
file: fileUrl, | ||
uploadType: store.uploadType() | ||
}) | ||
.pipe( | ||
tapResponse({ | ||
next: (file) => patchState(store, { file, status: 'done' }), | ||
error: console.error | ||
}) | ||
); | ||
}) | ||
) | ||
), | ||
setUploadType: (uploadType: FormImportUrlState['uploadType']) => { | ||
patchState(store, { uploadType }); | ||
} | ||
})) | ||
); |
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
Oops, something went wrong.