Skip to content

Commit 29df16c

Browse files
committed
Fallback to existing mention label if provider does not return a label
1 parent bd02fca commit 29df16c

File tree

3 files changed

+10
-18
lines changed

3 files changed

+10
-18
lines changed

packages/forms/src/Components/RichEditor/RichContentRenderer.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,6 @@ protected function processMentions(Editor $editor): void
294294
return;
295295
}
296296

297-
if (filled($node->attrs->label ?? null)) {
298-
return;
299-
}
300-
301297
$char = $node->attrs->char ?? '@';
302298
$mentionsByChar[$char][] = (string) $id;
303299
});
@@ -330,7 +326,7 @@ protected function processMentions(Editor $editor): void
330326
return;
331327
}
332328

333-
$label = $node->attrs->label ?? $labelsByChar[$char][(string) $id] ?? (string) $id;
329+
$label = $labelsByChar[$char][(string) $id] ?? $node->attrs->label ?? (string) $id;
334330
$node->attrs->label = $label;
335331

336332
$url = $provider->getUrl((string) $id, $label);

packages/forms/src/Components/RichEditor/StateCasts/RichEditorStateCast.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public function get(mixed $state): string | array
5959
return;
6060
}
6161

62+
if (isset($node->attrs->label) && $node->attrs->label) {
63+
return;
64+
}
65+
6266
unset($node->attrs->label);
6367
});
6468
}

tests/src/Forms/Components/RichEditor/RichContentRendererTest.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -631,9 +631,7 @@ public function toHtml(): string
631631
expect($html)->toContain('#issue-123');
632632
});
633633

634-
it('preserves existing labels and does not refetch them', function (): void {
635-
$fetchCount = 0;
636-
634+
it('falls back to existing label when provider returns no label', function (): void {
637635
$renderer = RichContentRenderer::make([
638636
'type' => 'doc',
639637
'content' => [
@@ -643,9 +641,9 @@ public function toHtml(): string
643641
[
644642
'type' => 'mention',
645643
'attrs' => [
646-
'id' => '1',
647-
'label' => 'Existing Label',
644+
'id' => '999',
648645
'char' => '@',
646+
'label' => 'Existing Label',
649647
],
650648
],
651649
],
@@ -655,21 +653,15 @@ public function toHtml(): string
655653

656654
$renderer->mentions([
657655
MentionProvider::make('@')
658-
->getLabelsUsing(function (array $ids) use (&$fetchCount): array {
659-
$fetchCount++;
660-
661-
return ['1' => 'New Label'];
662-
}),
656+
->getLabelsUsing(fn (array $ids): array => []),
663657
]);
664658

665659
$html = $renderer->toUnsafeHtml();
666660

667-
expect($fetchCount)->toBe(0);
668661
expect($html)->toContain('@Existing Label');
669-
expect($html)->not->toContain('New Label');
670662
});
671663

672-
it('falls back to id as label when provider returns no label', function (): void {
664+
it('falls back to id as label when provider returns no label and no label is set', function (): void {
673665
$renderer = RichContentRenderer::make([
674666
'type' => 'doc',
675667
'content' => [

0 commit comments

Comments
 (0)