Skip to content
2 changes: 2 additions & 0 deletions resources/js/components/fieldtypes/bard/BardFieldtype.vue
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ export default {

htmlWithReplacedLinks() {
return this.html.replaceAll(/\"statamic:\/\/(.*?)\"/g, (match, ref) => {
// Get everything in the "ref" string before a ? or #.
ref = ref.split(/[?#]/)[0]
const linkData = this.meta.linkData[ref];
if (! linkData) {
this.$toast.error(`${__('No link data found for')} ${ref}`);
Expand Down
40 changes: 35 additions & 5 deletions resources/js/components/fieldtypes/bard/LinkToolbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@
</div>


<!-- Append string -->
<div
v-if="linkType === 'entry'"
class="h-8 mb-4 p-2 bg-gray-100 dark:bg-dark-600 text-gray-800 dark:text-dark-150 w-full border dark:border-dark-200 rounded shadow-inner placeholder:text-gray-600 dark:placeholder:dark-text-dark-175 flex items-center"
>
<input
type="text"
ref="input"
v-model="appends"
class="input h-auto text-sm placeholder-gray-50"
:placeholder="`${__('Append To URL')} (${__('Optional')})`"
/>
</div>

<!-- Title attribute -->
<div class="h-8 mb-4 p-2 bg-gray-100 dark:bg-dark-600 text-gray-800 dark:text-dark-150 w-full border dark:border-dark-200 rounded shadow-inner placeholder:text-gray-600 dark:placeholder:dark-text-dark-175 flex items-center" >
<input
Expand Down Expand Up @@ -211,6 +225,7 @@ export default {
url: {},
urlData: {},
itemData: {},
appends: null,
title: null,
rel: null,
targetBlank: null,
Expand Down Expand Up @@ -313,7 +328,11 @@ export default {

watch: {

linkType() {
linkType(type) {
if (type != 'entry') {
this.appends = null;
}

this.autofocus();
},

Expand Down Expand Up @@ -357,10 +376,10 @@ export default {
applyAttrs(attrs) {
this.linkType = this.getLinkTypeForUrl(attrs.href);

this.url = { [this.linkType]: attrs.href };
this.appends = this.getAppendsForUrl(attrs.href);
this.url = { [this.linkType]: attrs.href?.replace(this.appends, '' ) };
this.urlData = { [this.linkType]: this.getUrlDataForUrl(attrs.href) };
this.itemData = { [this.linkType]: this.getItemDataForUrl(attrs.href) };

this.title = attrs.title;
this.rel = attrs.href
? attrs.rel
Expand Down Expand Up @@ -404,7 +423,7 @@ export default {
}

this.$emit('updated', {
href: this.href,
href: this.href + (this.appends ?? ''),
rel: this.rel,
target: (this.canHaveTarget && this.targetBlank) ? '_blank' : null,
title: this.title,
Expand Down Expand Up @@ -507,14 +526,25 @@ export default {
return this.bard.meta.linkData[ref];
},

getAppendsForUrl(urlString) {

// appends is only relevant to entry links
if (! urlString?.includes('statamic://entry::')) {
return null;
}

return urlString.replace(urlString.split(/[?#]/)[0], '');
},

parseDataUrl(url) {
if (! url) {
return {}
}

const appends = this.getAppendsForUrl(url);
const regex = /^statamic:\/\/((.*?)::(.*))$/;

const matches = url.match(regex);
const matches = url.replace(appends, '').match(regex);
if (! matches) {
return {};
}
Expand Down
9 changes: 6 additions & 3 deletions src/Fieldtypes/Bard.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class Bard extends Replicator
];

protected $categories = ['text', 'structured'];

protected $keywords = ['rich', 'richtext', 'rich text', 'editor', 'wysiwg', 'builder', 'page builder', 'gutenberg', 'content'];

protected $rules = [];

protected function configFieldItems(): array
Expand Down Expand Up @@ -711,14 +713,15 @@ private function extractLinkDataFromNode($node)

private function getLinkDataForUrl($url)
{
$ref = Str::after($url, 'statamic://');
$ref = str($url)->after('statamic://');
[$type, $id] = explode('::', $ref, 2);

$data = null;

switch ($type) {
case 'entry':
if ($entry = Entry::find($id)) {
$ref = $ref->before('?')->before('#');
if ($entry = Entry::find($ref->after('entry::'))) {
$data = [
'title' => $entry->get('title'),
'permalink' => $entry->absoluteUrl(),
Expand All @@ -735,7 +738,7 @@ private function getLinkDataForUrl($url)
break;
}

return [$ref => $data];
return [$ref->toString() => $data];
}

private function wrapInlineValue($value)
Expand Down
Loading