@@ -2,6 +2,7 @@ import { consume } from '@lit/context';
2
2
import { css , html , LitElement } from 'lit' ;
3
3
import { customElement , property } from 'lit/decorators.js' ;
4
4
import { ifDefined } from 'lit/directives/if-defined.js' ;
5
+ import { createRef , ref , type Ref } from 'lit/directives/ref.js' ;
5
6
import { when } from 'lit/directives/when.js' ;
6
7
import { avatars , profiles } from '../../config.js' ;
7
8
import type { DiscordMessageProps , Profile , LightTheme , DiscordTimestamp } from '../../types.js' ;
@@ -35,17 +36,20 @@ export class DiscordMessage extends LitElement implements DiscordMessageProps, L
35
36
padding-right: 48px;
36
37
margin-top: inherit;
37
38
margin-bottom: inherit;
39
+ line-height: 16px;
38
40
}
39
41
40
42
.discord-message-inner {
41
43
display: flex;
44
+ align-items: center;
42
45
position: relative;
43
46
-webkit-box-flex: 0;
44
47
-ms-flex: 0 0 auto;
45
48
flex: 0 0 auto;
46
49
}
47
50
48
51
:host([message-body-only]) {
52
+ min-height: 24px !important;
49
53
margin-top: 0px !important;
50
54
padding-top: 0.125rem !important;
51
55
padding-bottom: 0.0625rem !important;
@@ -244,6 +248,8 @@ export class DiscordMessage extends LitElement implements DiscordMessageProps, L
244
248
}
245
249
` ;
246
250
251
+ protected messageBodyOnlyTimeRef : Ref < HTMLTimeElement > = createRef ( ) ;
252
+
247
253
/**
248
254
* The id of the profile data to use.
249
255
*/
@@ -337,6 +343,12 @@ export class DiscordMessage extends LitElement implements DiscordMessageProps, L
337
343
} )
338
344
public accessor timestamp : DiscordTimestamp = new Date ( ) ;
339
345
346
+ /**
347
+ * The timestamp to use for the message time if {@link DiscordMessage.messageBodyOnly} is `true`.
348
+ */
349
+ @property ( { type : String , attribute : 'message-body-only-time' } )
350
+ public accessor messageBodyOnlyTime : DiscordTimestamp | null = null ;
351
+
340
352
/**
341
353
* Whether to use 24-hour format for the timestamp.
342
354
*/
@@ -373,8 +385,17 @@ export class DiscordMessage extends LitElement implements DiscordMessageProps, L
373
385
) ;
374
386
}
375
387
376
- private resolveAvatar ( avatar : string | undefined ) : string {
377
- return avatar === undefined ? avatars . default : avatars [ avatar ] ?? avatar ?? avatars . default ;
388
+ protected handleMessageEnter ( ) {
389
+ if ( this . messageBodyOnly && this . messageBodyOnlyTimeRef . value && this . messageBodyOnlyTime ) {
390
+ const computedTimestamp = handleTimestamp ( this . messageBodyOnlyTime , true , this . twentyFour ) ;
391
+ this . messageBodyOnlyTimeRef . value . textContent = computedTimestamp ?? '' ;
392
+ }
393
+ }
394
+
395
+ protected handleMessageLeave ( ) {
396
+ if ( this . messageBodyOnlyTimeRef . value ) {
397
+ this . messageBodyOnlyTimeRef . value . textContent = '' ;
398
+ }
378
399
}
379
400
380
401
protected override render ( ) {
@@ -392,19 +413,24 @@ export class DiscordMessage extends LitElement implements DiscordMessageProps, L
392
413
const profileData : Profile = ( ( this . profile !== undefined && Reflect . get ( profiles , this . profile ) ) as Profile ) || { } ;
393
414
const profile : Profile = { ...defaultData , ...profileData , avatar : this . resolveAvatar ( profileData . avatar ?? this . avatar ) } ;
394
415
395
- const computedTimestamp = handleTimestamp ( this . timestamp , this . compactMode , this . twentyFour ) ;
416
+ const computedTimestamp = handleTimestamp ( this . timestamp , this . compactMode , this . twentyFour ) ?? undefined ;
396
417
397
418
return html `
398
419
< slot name ="reply "> </ slot >
399
- < div class ="discord-message-inner ">
420
+ < div class ="discord-message-inner " @mouseenter = ${ this . handleMessageEnter } @mouseleave = ${ this . handleMessageLeave } >
400
421
${ when (
401
422
this . compactMode ,
402
- ( ) => html `< span class ="discord-message-timestamp "> ${ computedTimestamp } </ span > ` ,
423
+ ( ) => html `< time datetime =" ${ ifDefined ( computedTimestamp ) } " class ="discord-message-timestamp "> ${ computedTimestamp } </ time > ` ,
403
424
( ) => null
404
425
) }
405
426
${ when (
406
- this . messageBodyOnly ,
407
- ( ) => html `< span class ="discord-message-timestamp discord-message-body-only-indent "> </ span > ` ,
427
+ this . messageBodyOnly && ! this . compactMode ,
428
+ ( ) =>
429
+ html `< time
430
+ ${ ref ( this . messageBodyOnlyTimeRef ) }
431
+ datetime ="${ ifDefined ( computedTimestamp ) } "
432
+ class ="discord-message-timestamp discord-message-body-only-indent "
433
+ > </ time > ` ,
408
434
( ) => null
409
435
) }
410
436
${ when (
@@ -432,7 +458,7 @@ export class DiscordMessage extends LitElement implements DiscordMessageProps, L
432
458
roleName=${ profile . roleName ?? '' }
433
459
?compact=${ false }
434
460
> </ discord-author-info
435
- > < span class ="discord-message-timestamp "> ${ computedTimestamp } </ span >
461
+ > < time datetime =" ${ ifDefined ( computedTimestamp ) } " class ="discord-message-timestamp "> ${ computedTimestamp } </ time >
436
462
`
437
463
) }
438
464
< div class ="discord-message-body ">
@@ -479,6 +505,10 @@ export class DiscordMessage extends LitElement implements DiscordMessageProps, L
479
505
</ div >
480
506
` ;
481
507
}
508
+
509
+ private resolveAvatar ( avatar : string | undefined ) : string {
510
+ return avatar === undefined ? avatars . default : avatars [ avatar ] ?? avatar ?? avatars . default ;
511
+ }
482
512
}
483
513
484
514
declare global {
0 commit comments