20
common/resources/client/utils/urls/get-asset-url.ts
Executable file
20
common/resources/client/utils/urls/get-asset-url.ts
Executable file
@@ -0,0 +1,20 @@
|
||||
import {getBootstrapData} from '@common/core/bootstrap-data/use-backend-bootstrap-data';
|
||||
import {isAbsoluteUrl} from '@common/utils/urls/is-absolute-url';
|
||||
|
||||
export function getAssetUrl(url: string) {
|
||||
if (isAbsoluteUrl(url)) {
|
||||
return url;
|
||||
}
|
||||
const assetUrl =
|
||||
getBootstrapData().settings.asset_url ||
|
||||
getBootstrapData().settings.base_url;
|
||||
|
||||
//remove leading slash
|
||||
url = url.replace(/^\/+/g, '');
|
||||
|
||||
if (url.startsWith('assets/')) {
|
||||
return `${assetUrl}/build/${url}`;
|
||||
}
|
||||
|
||||
return `${assetUrl}/${url}`;
|
||||
}
|
||||
4
common/resources/client/utils/urls/is-absolute-url.ts
Executable file
4
common/resources/client/utils/urls/is-absolute-url.ts
Executable file
@@ -0,0 +1,4 @@
|
||||
export function isAbsoluteUrl(url?: string): boolean {
|
||||
if (!url) return false;
|
||||
return /^[a-zA-Z][a-zA-Z\d+\-.]*?:/.test(url);
|
||||
}
|
||||
4
common/resources/client/utils/urls/remove-protocol.ts
Executable file
4
common/resources/client/utils/urls/remove-protocol.ts
Executable file
@@ -0,0 +1,4 @@
|
||||
export function removeProtocol(url: string) {
|
||||
if (!url) return url;
|
||||
return url.replace(/(^\w+:|^)\/\//, '');
|
||||
}
|
||||
72
common/resources/client/utils/urls/share-link-socially.ts
Executable file
72
common/resources/client/utils/urls/share-link-socially.ts
Executable file
@@ -0,0 +1,72 @@
|
||||
export type ShareableNetworks =
|
||||
| 'facebook'
|
||||
| 'twitter'
|
||||
| 'pinterest'
|
||||
| 'tumblr'
|
||||
| 'blogger'
|
||||
| 'mail';
|
||||
|
||||
export function shareLinkSocially(
|
||||
network: ShareableNetworks,
|
||||
link: string,
|
||||
name?: string,
|
||||
image?: string
|
||||
) {
|
||||
const url = generateShareUrl(network, link, name, image);
|
||||
|
||||
if (network === 'mail') {
|
||||
window.location.href = url;
|
||||
} else {
|
||||
openNewWindow(url);
|
||||
}
|
||||
}
|
||||
|
||||
function openNewWindow(url: string) {
|
||||
const width = 575,
|
||||
height = 400,
|
||||
left = (window.innerWidth - width) / 2,
|
||||
top = (window.innerHeight - height) / 2,
|
||||
opts =
|
||||
'status=1, scrollbars=1' +
|
||||
',width=' +
|
||||
width +
|
||||
',height=' +
|
||||
height +
|
||||
',top=' +
|
||||
top +
|
||||
',left=' +
|
||||
left;
|
||||
|
||||
window.open(url, 'share', opts);
|
||||
}
|
||||
|
||||
function generateShareUrl(
|
||||
type: ShareableNetworks,
|
||||
link: string,
|
||||
name?: string,
|
||||
image?: string
|
||||
): string {
|
||||
switch (type) {
|
||||
case 'facebook':
|
||||
return 'https://www.facebook.com/sharer/sharer.php?u=' + link;
|
||||
case 'twitter':
|
||||
return `https://twitter.com/intent/tweet?text=${name}&url=${link}`;
|
||||
case 'pinterest':
|
||||
return (
|
||||
'https://pinterest.com/pin/create/button/?url=' +
|
||||
link +
|
||||
'&media=' +
|
||||
image
|
||||
);
|
||||
case 'tumblr':
|
||||
const base =
|
||||
'https://www.tumblr.com/widgets/share/tool?shareSource=legacy&canonicalUrl=&posttype=photo&title=&caption=';
|
||||
return base + name + '&content=' + image + '&url=' + link;
|
||||
case 'blogger':
|
||||
return (
|
||||
'https://www.blogger.com/blog_this.pyra?t&u=' + link + '&n=' + name
|
||||
);
|
||||
case 'mail':
|
||||
return `mailto:?subject=Check out this link.&body=${link}`;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user