Skip to content

Commit f2dd1e4

Browse files
committed
refactor(file-share-embed): created service for share and embed
1 parent 6ad1024 commit f2dd1e4

5 files changed

Lines changed: 70 additions & 2 deletions

File tree

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export * from './embed-content.constants';
21
export * from './file-browser-info.constants';
32
export * from './file-metadata-fields.constants';
43
export * from './file-provider.constants';

src/app/features/files/pages/file-detail/file-detail.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import { MetaTagsBuilderService } from '@osf/shared/services/meta-tags-builder.s
5151
import { SignpostingService } from '@osf/shared/services/signposting.service';
5252
import { ToastService } from '@osf/shared/services/toast.service';
5353
import { ViewOnlyLinkHelperService } from '@osf/shared/services/view-only-link-helper.service';
54+
import { embedDynamicJs, embedStaticHtml } from '@shared/constants/file-embed.constants';
5455
import { FileDetailsModel } from '@shared/models/files/file.model';
5556
import { MetadataTabsModel } from '@shared/models/metadata-tabs.model';
5657

@@ -60,7 +61,6 @@ import {
6061
FileResourceMetadataComponent,
6162
FileRevisionsComponent,
6263
} from '../../components';
63-
import { embedDynamicJs, embedStaticHtml } from '../../constants';
6464
import { FileDetailTab } from '../../enums';
6565
import {
6666
DeleteEntry,

src/app/features/files/constants/embed-content.constants.ts renamed to src/app/shared/constants/file-embed.constants.ts

File renamed without changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface FileShareLink {
2+
link: string;
3+
target: '_self' | '_blank';
4+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { Clipboard } from '@angular/cdk/clipboard';
2+
import { inject, Injectable } from '@angular/core';
3+
4+
import { ENVIRONMENT } from '@osf/core/provider/environment.provider';
5+
import { embedDynamicJs, embedStaticHtml } from '@shared/constants/file-embed.constants';
6+
7+
import { FileModel } from '../models/files/file.model';
8+
import { FileShareLink } from '../models/files/file-share-link.model';
9+
10+
@Injectable({
11+
providedIn: 'root',
12+
})
13+
export class FilesShareEmbedService {
14+
private readonly environment = inject(ENVIRONMENT);
15+
private readonly clipboard = inject(Clipboard);
16+
17+
private readonly EMBED_PLACEHOLDER = 'ENCODED_URL';
18+
19+
getShareLink(file: FileModel, shareType?: string): FileShareLink | null {
20+
const url = file.links?.html;
21+
const name = file.name;
22+
23+
if (!url || !name) return null;
24+
25+
const encodedUrl = encodeURIComponent(url);
26+
const encodedName = encodeURIComponent(name);
27+
28+
switch (shareType) {
29+
case 'email':
30+
return {
31+
link: `mailto:?subject=${encodedName}&body=${encodedUrl}`,
32+
target: '_self',
33+
};
34+
case 'twitter':
35+
return {
36+
link: `https://x.com/intent/tweet?url=${encodedUrl}&text=${encodedName}&via=OSFramework`,
37+
target: '_blank',
38+
};
39+
case 'facebook': {
40+
const appId = this.environment.facebookAppId;
41+
return {
42+
link: `https://www.facebook.com/dialog/share?app_id=${appId}&display=popup&href=${encodedUrl}&redirect_uri=${encodedUrl}`,
43+
target: '_blank',
44+
};
45+
}
46+
default:
47+
return null;
48+
}
49+
}
50+
51+
getEmbedHtml(file: FileModel, embedType?: string): string {
52+
switch (embedType) {
53+
case 'dynamic':
54+
return embedDynamicJs.replace(this.EMBED_PLACEHOLDER, file.links.render);
55+
case 'static':
56+
return embedStaticHtml.replace(this.EMBED_PLACEHOLDER, file.links.render);
57+
default:
58+
return '';
59+
}
60+
}
61+
62+
copyToClipboard(value: string): boolean {
63+
return this.clipboard.copy(value);
64+
}
65+
}

0 commit comments

Comments
 (0)