workadventure/front/src/Components/Chat/ChatElement.svelte

95 lines
3 KiB
Svelte
Raw Normal View History

<script lang="ts">
2021-12-06 16:12:37 +01:00
import { ChatMessageTypes } from "../../Stores/ChatStore";
import type { ChatMessage } from "../../Stores/ChatStore";
import { HtmlUtils } from "../../WebRtc/HtmlUtils";
import ChatPlayerName from "./ChatPlayerName.svelte";
import type { PlayerInterface } from "../../Phaser/Game/PlayerInterface";
export let message: ChatMessage;
2021-07-13 11:00:32 +02:00
export let line: number;
const chatStyleLink = "color: white; text-decoration: underline;";
2021-12-06 16:12:37 +01:00
$: author = message.author as PlayerInterface;
$: targets = message.targets || [];
$: texts = message.text || [];
2021-12-06 16:12:37 +01:00
function urlifyText(text: string): string {
return HtmlUtils.urlify(text, chatStyleLink);
}
function renderDate(date: Date) {
return date.toLocaleTimeString(navigator.language, {
2021-12-06 16:12:37 +01:00
hour: "2-digit",
minute: "2-digit",
});
}
2021-07-13 11:00:32 +02:00
function isLastIteration(index: number) {
2021-12-06 16:12:37 +01:00
return targets.length - 1 === index;
2021-07-13 11:00:32 +02:00
}
</script>
<div class="chatElement">
<div class="messagePart">
{#if message.type === ChatMessageTypes.userIncoming}
2021-12-06 16:12:37 +01:00
&gt;&gt; {#each targets as target, index}<ChatPlayerName
player={target}
{line}
/>{#if !isLastIteration(index)}, {/if}{/each} entered
<span class="date">({renderDate(message.date)})</span>
{:else if message.type === ChatMessageTypes.userOutcoming}
2021-12-06 16:12:37 +01:00
&lt;&lt; {#each targets as target, index}<ChatPlayerName
player={target}
{line}
/>{#if !isLastIteration(index)}, {/if}{/each} left
<span class="date">({renderDate(message.date)})</span>
{:else if message.type === ChatMessageTypes.me}
2021-07-13 11:00:32 +02:00
<h4>Me: <span class="date">({renderDate(message.date)})</span></h4>
{#each texts as text}
<div><p class="my-text">{@html urlifyText(text)}</p></div>
{/each}
{:else}
2021-12-06 16:12:37 +01:00
<h4><ChatPlayerName player={author} {line} />: <span class="date">({renderDate(message.date)})</span></h4>
{#each texts as text}
<div><p class="other-text">{@html urlifyText(text)}</p></div>
{/each}
{/if}
</div>
</div>
<style lang="scss">
2021-12-06 16:12:37 +01:00
h4,
p {
font-family: Lato;
}
div.chatElement {
2021-12-06 16:12:37 +01:00
display: flex;
margin-bottom: 20px;
2021-12-06 16:12:37 +01:00
.messagePart {
flex-grow: 1;
max-width: 100%;
2021-12-31 15:27:08 +01:00
user-select: text;
2021-12-06 16:12:37 +01:00
span.date {
font-size: 80%;
color: gray;
}
div > p {
border-radius: 8px;
margin-bottom: 10px;
padding: 6px;
overflow-wrap: break-word;
max-width: 100%;
display: inline-block;
&.other-text {
background: gray;
}
2021-07-13 11:00:32 +02:00
2021-12-06 16:12:37 +01:00
&.my-text {
background: #6489ff;
}
}
}
}
2021-12-06 16:12:37 +01:00
</style>