Skip to content

Commit

Permalink
chore(edit-content): remove inputs #29875
Browse files Browse the repository at this point in the history
  • Loading branch information
nicobytes committed Oct 22, 2024
1 parent 3ad703d commit 6d2545b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
@use "variables" as *;

:host ::ng-deep {
.editor-container {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
}

.file-field__editor-container {
display: flex;
justify-content: center;
Expand All @@ -18,6 +28,7 @@
min-height: 20rem;
border-radius: $border-radius-md;
overflow: auto;
position: relative;
}

.file-field__code-editor--disabled {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import {
Component,
effect,
inject,
input,
output,
viewChild
viewChild, OnInit
} from '@angular/core';
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';

Expand All @@ -24,9 +23,14 @@ import { DotMessagePipe, DotFieldValidationMessageComponent } from '@dotcms/ui';

import { FormFileEditorStore } from './store/form-file-editor.store';

import { INPUT_TYPE } from '../../../dot-edit-content-text-field/utils';
import { UploadedFile } from '../../models';

type DialogProps = {
allowFileNameEdit: boolean;
userMonacoOptions: Partial<MonacoEditorConstructionOptions>;
uploadedFile: UploadedFile | null;
}

@Component({
selector: 'dot-form-file-editor',
standalone: true,
Expand All @@ -43,44 +47,25 @@ import { UploadedFile } from '../../models';
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [FormFileEditorStore]
})
export class DotFormFileEditorComponent {
export class DotFormFileEditorComponent implements OnInit {
readonly store = inject(FormFileEditorStore);
readonly #formBuilder = inject(FormBuilder);
readonly #dotMessageService = inject(DotMessageService);
readonly #dialogRef = inject(DynamicDialogRef);
readonly #dialogConfig = inject(
DynamicDialogConfig<{ inputType: INPUT_TYPE; acceptedFiles: string[] }>
);
readonly #dialogConfig = inject(DynamicDialogConfig<DialogProps>);

readonly form = this.#formBuilder.nonNullable.group({
name: ['', [Validators.required, Validators.pattern(/^[^.]+\.[^.]+$/)]],
content: ['']
});

$fileName = input.required<string>({ alias: 'fileName' });
$fileContent = input.required<string>({ alias: 'fileContent' });
$allowFileNameEdit = input(false, { alias: 'allowFileNameEdit' });
$userMonacoOptions = input<MonacoEditorConstructionOptions>({});

tempFileUploaded = output<UploadedFile>();
cancel = output<void>();

$editorRef = viewChild.required(MonacoEditorComponent);

constructor() {
effect(() => {
const name = this.$fileName();
const content = this.$fileContent();

this.form.patchValue({
name,
content
});
});

effect(() => {
this.store.setMonacoOptions(this.$userMonacoOptions());
});

effect(() => {
const isUploading = this.store.isUploading();
Expand All @@ -93,6 +78,24 @@ export class DotFormFileEditorComponent {
});
}

ngOnInit(): void {
const data = this.#dialogConfig?.data as DialogProps;
if (!data) {
return;
}

const { uploadedFile, userMonacoOptions, allowFileNameEdit } = data;

if (uploadedFile) {
this.#initValuesForm(uploadedFile);
}

this.store.initLoad({
monacoOptions: userMonacoOptions || {},
allowFileNameEdit: allowFileNameEdit || true
});
}

onSubmit(): void {
if (this.form.invalid) {
this.form.markAsDirty();
Expand Down Expand Up @@ -121,6 +124,13 @@ export class DotFormFileEditorComponent {
#enableEditor() {
this.form.enable();
const editor = this.$editorRef().editor;
editor.updateOptions({ readOnly: true });
editor.updateOptions({ readOnly: false });
}

#initValuesForm({ source, file }: UploadedFile): void {
this.form.patchValue({
name: source === 'temp' ? file.fileName : file.title,
content: file.content
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,18 @@ export const FormFileEditorStore = signalStore(
setFile(file: FileInfo) {
patchState(store, { file });
},
setMonacoOptions(monacoOptions: Partial<MonacoEditorConstructionOptions>) {
initLoad({monacoOptions, allowFileNameEdit}:{
monacoOptions: Partial<MonacoEditorConstructionOptions>,
allowFileNameEdit: boolean
}) {
const prevState = store.monacoOptions();

patchState(store, {
monacoOptions: {
...prevState,
...monacoOptions
}
},
allowFileNameEdit
});
},
uploadFile(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class DotEditContentFileFieldComponent implements ControlValueAccessor, O
return this.#dotMessageService.get('dot.file.field.action.generate.with.tooltip');
}

return null;
return '';
});

private onChange: (value: string) => void;
Expand Down Expand Up @@ -235,7 +235,11 @@ export class DotEditContentFileFieldComponent implements ControlValueAccessor, O
*
* @return {void}
*/
fileSelected(files: FileList) {
fileSelected(files: FileList | null) {
if (!files || files.length === 0) {
return;
}

const file = files[0];

if (!file) {
Expand Down Expand Up @@ -360,7 +364,11 @@ export class DotEditContentFileFieldComponent implements ControlValueAccessor, O
resizable: false,
modal: true,
width: '90%',
style: { 'max-width': '1040px' }
style: { 'max-width': '1040px' },
data: {
uploadedFile: this.store.uploadedFile(),
allowFileNameEdit: true
}
});

this.#dialogRef.onClose.pipe(takeUntilDestroyed(this.#destroyRef)).subscribe((file) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const FileFieldStore = signalStore(
* @param initState
*/
initLoad: (initState: {
inputType: FileFieldState['inputType'];
inputType: INPUT_TYPES;
fieldVariable: FileFieldState['fieldVariable'];
isAIPluginInstalled?: boolean;
}) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class DotDropZoneComponent {
* Max file size in bytes.
* See Docs: https://www.dotcms.com/docs/latest/binary-field#FieldVariables
*/
@Input() maxFileSize: number;
@Input() maxFileSize: number | null = null;

@Input() set accept(types: string[]) {
this._accept = types
Expand Down

0 comments on commit 6d2545b

Please sign in to comment.