Skip to content

Commit bc62ed5

Browse files
favnaeumarciel404
andauthored
fix: added the possibility of skipping lines for discord-embed titles (#426)
* Adding the possibility of skipping lines Adding the possibility of skipping lines if you use a textarea to insert text and problems of not giving more than one space * Remove "style="white-space: pre"" This property was making the content of the message go beyond the body of the embed * style: prettier formatting * fix: follow our linting rules * refactor: strictly type `el` and fix bugs * refactor: change inline styles to a class --------- Co-authored-by: Marciel404 <48138111+eumarciel404@users.noreply.github.com>
1 parent 3cb9f3b commit bc62ed5

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

packages/core/src/components/discord-embed/DiscordEmbed.ts

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { consume } from '@lit/context';
2-
import { css, html, LitElement } from 'lit';
2+
import { css, html, LitElement, type TemplateResult } from 'lit';
33
import { customElement, property } from 'lit/decorators.js';
44
import { classMap } from 'lit/directives/class-map.js';
55
import { ifDefined } from 'lit/directives/if-defined.js';
@@ -122,6 +122,11 @@ export class DiscordEmbed extends LitElement implements DiscordEmbedProps, Light
122122
width: 24px;
123123
}
124124
125+
:host .discord-embed-author-block,
126+
:host .discord-embed-author-block > span {
127+
max-width: 95%;
128+
}
129+
125130
:host .discord-embed-provider {
126131
font-size: 0.75rem;
127132
line-height: 1rem;
@@ -328,10 +333,15 @@ export class DiscordEmbed extends LitElement implements DiscordEmbedProps, Light
328333
${when(
329334
this.authorUrl,
330335
() =>
331-
html`<a href=${ifDefined(this.authorUrl)} target="_blank" rel="noopener noreferrer">
332-
${emojiParsedAuthorName}
336+
html`<a
337+
href=${ifDefined(this.authorUrl)}
338+
target="_blank"
339+
rel="noopener noreferrer"
340+
class="discord-embed-author-block"
341+
>
342+
<span class="discord-embed-author-block">${emojiParsedAuthorName}</span>
333343
</a>`,
334-
() => html`${emojiParsedAuthorName}`
344+
() => html`<span class="discord-embed-author-block">${emojiParsedAuthorName}</span>`
335345
)}
336346
</div>`
337347
)}
@@ -388,18 +398,35 @@ export class DiscordEmbed extends LitElement implements DiscordEmbedProps, Light
388398
private parseTitle(title?: string) {
389399
if (!title) return null;
390400

391-
const words = title.split(' ');
401+
const el: (TemplateResult<1> | string)[] = [];
402+
let complete = '';
403+
404+
for (const words of title.split('\n')) {
405+
for (const word of words.split(' ')) {
406+
const emoji = getGlobalEmojiUrl(word) ?? this.embedEmojisMap[word] ?? ({} as Emoji);
407+
408+
if (emoji.name) {
409+
el.push(html`<discord-custom-emoji name=${emoji.name} url=${ifDefined(emoji.url)} embed-emoji></discord-custom-emoji>`);
410+
} else {
411+
complete += `${word} `;
412+
}
413+
414+
if (complete === ' ') {
415+
el.push(html`<br />`);
416+
}
417+
}
418+
419+
el.push(complete);
420+
421+
complete = '';
422+
}
392423

393-
return words.map((word: string, idx: number) => {
394-
const emoji = getGlobalEmojiUrl(word) ?? this.embedEmojisMap[word] ?? ({} as Emoji);
395-
let el;
396-
if (emoji.name) {
397-
el = html`<discord-custom-emoji name=${emoji.name} url=${ifDefined(emoji.url)} embed-emoji></discord-custom-emoji>`;
398-
} else {
399-
el = idx < words.length - 1 ? `${word} ` : word;
424+
return el.map((wordOrHtmlTemplate) => {
425+
if (typeof wordOrHtmlTemplate === 'string') {
426+
return html`<span>${wordOrHtmlTemplate}</span>`;
400427
}
401428

402-
return el;
429+
return wordOrHtmlTemplate;
403430
});
404431
}
405432
}

0 commit comments

Comments
 (0)