From 6b20d5cf4616b656eaf7739e17e33b7a743ea329 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 8 Dec 2021 09:03:56 +0000 Subject: [PATCH] Allow internal links to be opened in a new tab --- .../client/src/components/app/Link.svelte | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/client/src/components/app/Link.svelte b/packages/client/src/components/app/Link.svelte index 91537b5b38..f47a2a0522 100644 --- a/packages/client/src/components/app/Link.svelte +++ b/packages/client/src/components/app/Link.svelte @@ -21,11 +21,25 @@ $: target = openInNewTab ? "_blank" : "_self" $: placeholder = $builderStore.inBuilder && !text $: componentText = getComponentText(text, $builderStore, $component) + $: sanitizedUrl = getSanitizedUrl(url, externalLink, openInNewTab) // Add color styles to main styles object, otherwise the styleable helper // overrides the color when it's passed as inline style. $: styles = enrichStyles($component.styles, color) + const getSanitizedUrl = (url, externalLink, newTab) => { + if (!url) { + return externalLink || newTab ? "#/" : "/" + } + if (externalLink) { + return url + } + if (openInNewTab) { + return `#${url}` + } + return url + } + const getComponentText = (text, builderState, componentState) => { if (!builderState.inBuilder || componentState.editing) { return text || "" @@ -65,10 +79,10 @@ {componentText} {:else if $builderStore.inBuilder || componentText} - {#if externalLink} + {#if externalLink || openInNewTab} {:else} - {#key url} + {#key sanitizedUrl}