@@ -6,7 +6,7 @@ import { Tooltip } from 'primeng/tooltip';
66
77import { map, of } from 'rxjs';
88
9- import { CommonModule } from '@angular/common';
9+ import { CommonModule, isPlatformBrowser } from '@angular/common';
1010import {
1111 ChangeDetectionStrategy,
1212 Component,
@@ -16,6 +16,7 @@ import {
1616 inject,
1717 input,
1818 output,
19+ PLATFORM_ID,
1920 signal,
2021 viewChild,
2122 ViewEncapsulation,
@@ -24,9 +25,7 @@ import { toSignal } from '@angular/core/rxjs-interop';
2425import { ActivatedRoute } from '@angular/router';
2526
2627import { ENVIRONMENT } from '@core/provider/environment.provider';
27-
28- import 'cedar-artifact-viewer';
29- import 'cedar-embeddable-editor';
28+ import { LoadingSpinnerComponent } from '@osf/shared/components/loading-spinner/loading-spinner.component';
3029
3130import { CEDAR_CONFIG, CEDAR_VIEWER_CONFIG } from '../../constants';
3231import { CedarMetadataHelper } from '../../helpers';
@@ -39,7 +38,7 @@ import {
3938
4039@Component({
4140 selector: 'osf-cedar-template-form',
42- imports: [CommonModule, Button, TranslatePipe, Tooltip, Menu],
41+ imports: [CommonModule, Button, TranslatePipe, Tooltip, Menu, LoadingSpinnerComponent ],
4342 templateUrl: './cedar-template-form.component.html',
4443 styleUrl: './cedar-template-form.component.scss',
4544 schemas: [CUSTOM_ELEMENTS_SCHEMA],
@@ -66,11 +65,15 @@ export class CedarTemplateFormComponent {
6665
6766 private route = inject(ActivatedRoute);
6867 readonly environment = inject(ENVIRONMENT);
68+ private platformId = inject(PLATFORM_ID);
69+ readonly cedarLoaded = signal<boolean>(false);
6970
7071 readonly recordId = signal<string>('');
7172 readonly downloadUrl = signal<string>('');
7273 readonly schemaName = signal<string>('');
7374
75+ readonly fileGuid = toSignal(this.route.params.pipe(map((params) => params['fileGuid'])) ?? of(undefined));
76+
7477 shareItems = [
7578 {
7679 label: 'files.detail.actions.share.email',
@@ -90,15 +93,15 @@ export class CedarTemplateFormComponent {
9093 effect(() => {
9194 const tpl = this.template();
9295 if (tpl?.attributes?.template) {
93- this.initializeCedar();
96+ this.loadCedarLibraries().then(() => this. initializeCedar() );
9497 }
9598 });
9699
97100 effect(() => {
98101 const record = this.existingRecord();
99102 this.schemaName.set(record?.embeds?.template.data.attributes.schema_name || '');
100103 if (record) {
101- this.initializeCedar();
104+ this.loadCedarLibraries().then(() => this. initializeCedar() );
102105 }
103106 });
104107 }
@@ -123,7 +126,30 @@ export class CedarTemplateFormComponent {
123126 this.validateCedarMetadata();
124127 }
125128
126- readonly fileGuid = toSignal(this.route.params.pipe(map((params) => params['fileGuid'])) ?? of(undefined));
129+ private initializeFormData(): void {
130+ const template = this.template()?.attributes?.template;
131+ if (!template) return;
132+ const metadata = this.existingRecord()?.attributes?.metadata;
133+ if (this.existingRecord()) {
134+ const structuredMetadata = CedarMetadataHelper.buildStructuredMetadata(metadata);
135+ this.formData.set(structuredMetadata);
136+ } else {
137+ this.formData.set(CedarMetadataHelper.buildEmptyMetadata());
138+ }
139+ }
140+
141+ private async loadCedarLibraries(): Promise<void> {
142+ if (!isPlatformBrowser(this.platformId) || this.cedarLoaded()) {
143+ return;
144+ }
145+
146+ try {
147+ await Promise.all([import('cedar-artifact-viewer'), import('cedar-embeddable-editor')]);
148+ this.cedarLoaded.set(true);
149+ } catch {
150+ this.cedarLoaded.set(false);
151+ }
152+ }
127153
128154 downloadMetadadaRecord() {
129155 if (this.fileGuid()) {
@@ -173,19 +199,6 @@ export class CedarTemplateFormComponent {
173199 this.emitData.emit(finalData as CedarRecordDataBinding);
174200 }
175201 }
176-
177- private initializeFormData(): void {
178- const template = this.template()?.attributes?.template;
179- if (!template) return;
180- const metadata = this.existingRecord()?.attributes?.metadata;
181- if (this.existingRecord()) {
182- const structuredMetadata = CedarMetadataHelper.buildStructuredMetadata(metadata);
183- this.formData.set(structuredMetadata);
184- } else {
185- this.formData.set(CedarMetadataHelper.buildEmptyMetadata());
186- }
187- }
188-
189202 handleEmailShare(): void {
190203 const url = window.location.href;
191204 window.location.href = `mailto:?subject=${this.schemaName()}&body=${url}`;
0 commit comments