Allow internal links to be opened in a new tab

This commit is contained in:
Andrew Kingston 2021-12-08 09:03:56 +00:00
parent 49551ceb54
commit 6b20d5cf46
1 changed files with 18 additions and 4 deletions

View File

@ -21,11 +21,25 @@
$: target = openInNewTab ? "_blank" : "_self" $: target = openInNewTab ? "_blank" : "_self"
$: placeholder = $builderStore.inBuilder && !text $: placeholder = $builderStore.inBuilder && !text
$: componentText = getComponentText(text, $builderStore, $component) $: componentText = getComponentText(text, $builderStore, $component)
$: sanitizedUrl = getSanitizedUrl(url, externalLink, openInNewTab)
// Add color styles to main styles object, otherwise the styleable helper // Add color styles to main styles object, otherwise the styleable helper
// overrides the color when it's passed as inline style. // overrides the color when it's passed as inline style.
$: styles = enrichStyles($component.styles, color) $: 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) => { const getComponentText = (text, builderState, componentState) => {
if (!builderState.inBuilder || componentState.editing) { if (!builderState.inBuilder || componentState.editing) {
return text || "" return text || ""
@ -65,10 +79,10 @@
{componentText} {componentText}
</div> </div>
{:else if $builderStore.inBuilder || componentText} {:else if $builderStore.inBuilder || componentText}
{#if externalLink} {#if externalLink || openInNewTab}
<a <a
{target} {target}
href={url || "/"} href={sanitizedUrl}
use:styleable={styles} use:styleable={styles}
class:placeholder class:placeholder
class:bold class:bold
@ -79,10 +93,10 @@
{componentText} {componentText}
</a> </a>
{:else} {:else}
{#key url} {#key sanitizedUrl}
<a <a
use:linkable use:linkable
href={url || "/"} href={sanitizedUrl}
use:styleable={styles} use:styleable={styles}
class:placeholder class:placeholder
class:bold class:bold