Initial commit
This commit is contained in:
567
src/lib/components/MudTerminal.svelte
Normal file
567
src/lib/components/MudTerminal.svelte
Normal file
@@ -0,0 +1,567 @@
|
||||
<script lang="ts">
|
||||
import { onMount, onDestroy, createEventDispatcher } from 'svelte';
|
||||
import { outputHistory, addToOutputHistory, addToInputHistory, navigateInputHistory, inputHistoryIndex, activeConnection, uiSettings, accessibilitySettings, inputHistory } from '$lib/stores/mudStore';
|
||||
import { tick } from 'svelte';
|
||||
import AnsiToHtml from 'ansi-to-html';
|
||||
import { AccessibilityManager } from '$lib/accessibility/AccessibilityManager';
|
||||
|
||||
// Create event dispatcher
|
||||
const dispatch = createEventDispatcher();
|
||||
|
||||
// Props
|
||||
export let autofocus = true;
|
||||
export let placeholder = 'Enter command...';
|
||||
export let aria_label = 'MUD Input';
|
||||
|
||||
// Local state
|
||||
let terminalElement: HTMLDivElement;
|
||||
let inputElement: HTMLInputElement;
|
||||
let currentInput = '';
|
||||
let accessibilityManager: AccessibilityManager | null = null;
|
||||
let ansiConverter = new AnsiToHtml({
|
||||
fg: '#f8f8f2',
|
||||
bg: '#282a36',
|
||||
newline: false, // We'll handle newlines ourselves
|
||||
escapeXML: true,
|
||||
stream: false
|
||||
});
|
||||
|
||||
// Message navigation state
|
||||
let currentFocusedMessageIndex: number = -1;
|
||||
let messageElements: HTMLElement[] = [];
|
||||
|
||||
// Process ANSI color codes
|
||||
function processAnsi(text: string): string {
|
||||
if ($uiSettings.ansiColor) {
|
||||
try {
|
||||
// First process ANSI to HTML without replacing newlines
|
||||
const ansiProcessed = ansiConverter.toHtml(text);
|
||||
|
||||
// Then replace newlines with <br> tags
|
||||
return ansiProcessed.replace(/\r\n|\r|\n/g, '<br>');
|
||||
} catch (error) {
|
||||
console.error('Error processing ANSI colors:', error);
|
||||
// Fallback to just replacing newlines
|
||||
return text.replace(/\r\n|\r|\n/g, '<br>');
|
||||
}
|
||||
} else {
|
||||
// Strip ANSI codes if color is disabled
|
||||
return text.replace(/\u001b\[\d+(;\d+)*m/g, '')
|
||||
.replace(/\r\n|\r|\n/g, '<br>');
|
||||
}
|
||||
}
|
||||
|
||||
// Split text into individual lines (for better screen reader navigation)
|
||||
function splitIntoLines(text: string): string[] {
|
||||
// First handle any text that already has <br> tags from ANSI processing
|
||||
if (text.includes('<br>')) {
|
||||
return text.split('<br>').filter(line => line.trim().length > 0);
|
||||
}
|
||||
|
||||
// Otherwise split by newlines
|
||||
return text.split(/\r\n|\r|\n/).filter(line => line.trim().length > 0);
|
||||
}
|
||||
|
||||
// Apply highlighting to text
|
||||
function applyHighlights(text: string, highlights: { pattern: string; color: string; isRegex: boolean }[]): string {
|
||||
if (!highlights || highlights.length === 0) return text;
|
||||
|
||||
let highlightedText = text;
|
||||
|
||||
highlights.forEach(({ pattern, color, isRegex }) => {
|
||||
if (isRegex) {
|
||||
try {
|
||||
const regex = new RegExp(pattern, 'g');
|
||||
highlightedText = highlightedText.replace(regex, (match) => {
|
||||
return `<span style="background-color: ${color};">${match}</span>`;
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Invalid regex pattern:', pattern, error);
|
||||
}
|
||||
} else {
|
||||
// Escape special characters in the pattern for use in a regex
|
||||
const safePattern = pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
const regex = new RegExp(safePattern, 'g');
|
||||
highlightedText = highlightedText.replace(regex, (match) => {
|
||||
return `<span style="background-color: ${color};">${match}</span>`;
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return highlightedText;
|
||||
}
|
||||
|
||||
// Handle input submission
|
||||
function handleSubmit(event: Event) {
|
||||
event.preventDefault();
|
||||
|
||||
if (!currentInput.trim()) return;
|
||||
|
||||
// Interrupt speech if enabled
|
||||
if ($accessibilitySettings.interruptSpeechOnEnter && accessibilityManager && accessibilityManager.isSpeaking()) {
|
||||
accessibilityManager.stopSpeech();
|
||||
}
|
||||
|
||||
// Add to input history
|
||||
addToInputHistory(currentInput);
|
||||
|
||||
// Show the command in the output (only if not password - for privacy)
|
||||
const isPassword = currentInput.startsWith('password') || currentInput.toLowerCase() === $inputHistory[$inputHistory.length - 2]?.toLowerCase().replace('username', 'password');
|
||||
if (!isPassword) {
|
||||
addToOutputHistory(`> ${currentInput}`, true);
|
||||
} else {
|
||||
addToOutputHistory(`> ********`, true);
|
||||
}
|
||||
|
||||
// Send the command if connected
|
||||
if ($activeConnection) {
|
||||
try {
|
||||
$activeConnection.send(currentInput);
|
||||
} catch (error) {
|
||||
console.error('Error sending command:', error);
|
||||
addToOutputHistory(`Error sending command: ${error}`, false, [{ pattern: 'Error', color: '#ff5555', isRegex: false }]);
|
||||
}
|
||||
} else {
|
||||
addToOutputHistory('Not connected to any MUD server.', false, [{ pattern: 'Not connected', color: '#ff5555', isRegex: false }]);
|
||||
}
|
||||
|
||||
// Clear the input
|
||||
currentInput = '';
|
||||
dispatch('input', { text: currentInput });
|
||||
|
||||
// Focus the input again
|
||||
if (inputElement) {
|
||||
inputElement.focus();
|
||||
}
|
||||
}
|
||||
|
||||
// Handle keyboard shortcuts
|
||||
function handleKeyDown(event: KeyboardEvent) {
|
||||
if (event.key === 'ArrowUp') {
|
||||
event.preventDefault();
|
||||
currentInput = navigateInputHistory('up', currentInput);
|
||||
} else if (event.key === 'ArrowDown') {
|
||||
event.preventDefault();
|
||||
currentInput = navigateInputHistory('down', currentInput);
|
||||
} else if (event.key === 'Escape') {
|
||||
event.preventDefault();
|
||||
currentInput = '';
|
||||
inputHistoryIndex.set(-1);
|
||||
}
|
||||
}
|
||||
|
||||
// Scroll to bottom of terminal
|
||||
async function scrollToBottom() {
|
||||
await tick();
|
||||
if (terminalElement) {
|
||||
terminalElement.scrollTop = terminalElement.scrollHeight;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle keyboard navigation in output window
|
||||
function handleOutputKeyDown(event: KeyboardEvent) {
|
||||
if (event.key === 'PageUp') {
|
||||
event.preventDefault();
|
||||
if (terminalElement) {
|
||||
terminalElement.scrollTop -= terminalElement.clientHeight;
|
||||
}
|
||||
} else if (event.key === 'PageDown') {
|
||||
event.preventDefault();
|
||||
if (terminalElement) {
|
||||
terminalElement.scrollTop += terminalElement.clientHeight;
|
||||
}
|
||||
} else if (event.key === 'Home') {
|
||||
event.preventDefault();
|
||||
if (terminalElement) {
|
||||
terminalElement.scrollTop = 0;
|
||||
// Focus first message
|
||||
navigateToMessage(0);
|
||||
}
|
||||
} else if (event.key === 'End') {
|
||||
event.preventDefault();
|
||||
scrollToBottom();
|
||||
// Focus last message
|
||||
navigateToMessage(messageElements.length - 1);
|
||||
} else if (event.key === 'ArrowUp') {
|
||||
event.preventDefault();
|
||||
navigatePreviousMessage();
|
||||
} else if (event.key === 'ArrowDown') {
|
||||
event.preventDefault();
|
||||
navigateNextMessage();
|
||||
}
|
||||
}
|
||||
|
||||
// Update message elements reference and prepare for navigation
|
||||
function updateMessageElements() {
|
||||
if (terminalElement) {
|
||||
// Get all line elements to make them individually navigable
|
||||
messageElements = Array.from(terminalElement.querySelectorAll('.mud-terminal-line'));
|
||||
|
||||
// Add tabindex and aria attributes for accessibility
|
||||
messageElements.forEach((el, index) => {
|
||||
const element = el as HTMLElement;
|
||||
element.setAttribute('tabindex', '-1'); // Can be focused but not in tab order
|
||||
element.setAttribute('aria-posinset', (index + 1).toString());
|
||||
element.setAttribute('aria-setsize', messageElements.length.toString());
|
||||
|
||||
// Don't add navigation instructions text to the elements
|
||||
// Let the screen reader just read the content
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Navigate to specific message by index
|
||||
function navigateToMessage(index: number) {
|
||||
if (index < 0 || index >= messageElements.length) return;
|
||||
|
||||
// Remove focus from current message
|
||||
if (currentFocusedMessageIndex >= 0 && currentFocusedMessageIndex < messageElements.length) {
|
||||
messageElements[currentFocusedMessageIndex].classList.remove('focused-message');
|
||||
}
|
||||
|
||||
// Set current message index
|
||||
currentFocusedMessageIndex = index;
|
||||
|
||||
// Add focus class to current message
|
||||
const messageElement = messageElements[currentFocusedMessageIndex];
|
||||
messageElement.classList.add('focused-message');
|
||||
messageElement.focus();
|
||||
|
||||
// Make sure the message is in view
|
||||
messageElement.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
|
||||
|
||||
// Announce for screen readers - simplified and concise announcement
|
||||
const messageNumber = currentFocusedMessageIndex + 1;
|
||||
const totalMessages = messageElements.length;
|
||||
const messageContent = messageElement.textContent || '';
|
||||
|
||||
// Only announce the message number and content, not terminal instructions
|
||||
const announcement = `${messageNumber} of ${totalMessages}: ${messageContent.substring(0, 100)}`;
|
||||
|
||||
// Use aria-live region for announcement
|
||||
const announcementElement = document.getElementById('message-announcement');
|
||||
if (announcementElement) {
|
||||
announcementElement.textContent = announcement;
|
||||
}
|
||||
}
|
||||
|
||||
// Navigate to previous message
|
||||
function navigatePreviousMessage() {
|
||||
if (currentFocusedMessageIndex <= 0) {
|
||||
// Already at the first message - go to the first one
|
||||
navigateToMessage(0);
|
||||
} else {
|
||||
navigateToMessage(currentFocusedMessageIndex - 1);
|
||||
}
|
||||
}
|
||||
|
||||
// Navigate to next message
|
||||
function navigateNextMessage() {
|
||||
if (currentFocusedMessageIndex >= messageElements.length - 1) {
|
||||
// Already at the last message
|
||||
navigateToMessage(messageElements.length - 1);
|
||||
} else {
|
||||
navigateToMessage(currentFocusedMessageIndex + 1);
|
||||
}
|
||||
}
|
||||
|
||||
// Watch output history changes to scroll to bottom
|
||||
$: {
|
||||
if ($outputHistory) {
|
||||
scrollToBottom();
|
||||
// Update message elements when output history changes
|
||||
setTimeout(updateMessageElements, 0);
|
||||
}
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
// Initialize accessibility manager
|
||||
accessibilityManager = new AccessibilityManager();
|
||||
|
||||
// Focus input on mount if autofocus is true
|
||||
if (autofocus && inputElement) {
|
||||
inputElement.focus();
|
||||
}
|
||||
|
||||
// Initial scroll to bottom
|
||||
scrollToBottom();
|
||||
|
||||
// Initialize message elements for navigation
|
||||
updateMessageElements();
|
||||
});
|
||||
|
||||
// Format timestamp
|
||||
function formatTimestamp(timestamp: number): string {
|
||||
const date = new Date(timestamp);
|
||||
return date.toLocaleTimeString();
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="mud-terminal-container"
|
||||
class:dark-mode={$uiSettings.isDarkMode}
|
||||
class:high-contrast={$accessibilitySettings.highContrast}
|
||||
role="region"
|
||||
aria-label="MUD Terminal"
|
||||
tabindex="-1">
|
||||
|
||||
<!-- Screen reader announcements -->
|
||||
<div id="message-announcement" class="sr-only" aria-live="polite"></div>
|
||||
|
||||
<div class="mud-terminal-output"
|
||||
bind:this={terminalElement}
|
||||
role="log"
|
||||
aria-live="polite"
|
||||
aria-atomic="false"
|
||||
aria-relevant="additions"
|
||||
aria-label="MUD output"
|
||||
tabindex="0"
|
||||
on:keydown={handleOutputKeyDown}
|
||||
style="font-family: {$uiSettings.font}; font-size: {$accessibilitySettings.fontSize}px; line-height: {$accessibilitySettings.lineSpacing};">
|
||||
{#each $outputHistory as item (item.id)}
|
||||
<!-- For input lines, keep them as a single block -->
|
||||
{#if item.isInput}
|
||||
<div class="mud-terminal-line mud-input-line" tabindex="-1">
|
||||
{#if $uiSettings.showTimestamps}
|
||||
<span class="mud-timestamp" aria-hidden="true">[{formatTimestamp(item.timestamp)}]</span>
|
||||
{/if}
|
||||
<div class="mud-terminal-content">
|
||||
{@html item.text}
|
||||
</div>
|
||||
</div>
|
||||
<!-- For MUD output, split into individual navigable lines -->
|
||||
{:else}
|
||||
<!-- Process the content first -->
|
||||
{@const processedContent = applyHighlights(processAnsi(item.text), item.highlights || [])}
|
||||
{@const lines = splitIntoLines(processedContent)}
|
||||
|
||||
<!-- If no lines or only one line, render as is -->
|
||||
{#if lines.length <= 1}
|
||||
<div class="mud-terminal-line" tabindex="-1">
|
||||
{#if $uiSettings.showTimestamps}
|
||||
<span class="mud-timestamp" aria-hidden="true">[{formatTimestamp(item.timestamp)}]</span>
|
||||
{/if}
|
||||
<div class="mud-terminal-content">
|
||||
{@html processedContent}
|
||||
</div>
|
||||
</div>
|
||||
<!-- Otherwise render each line separately for better navigation -->
|
||||
{:else}
|
||||
{#each lines as line, lineIndex}
|
||||
<div class="mud-terminal-line mud-terminal-subline" tabindex="-1">
|
||||
{#if $uiSettings.showTimestamps && lineIndex === 0}
|
||||
<span class="mud-timestamp" aria-hidden="true">[{formatTimestamp(item.timestamp)}]</span>
|
||||
{/if}
|
||||
<div class="mud-terminal-content">
|
||||
{@html line}
|
||||
</div>
|
||||
</div>
|
||||
{/each}
|
||||
{/if}
|
||||
{/if}
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
<form class="mud-terminal-input-form" on:submit={handleSubmit} aria-label="MUD command input form">
|
||||
<input
|
||||
type="text"
|
||||
class="mud-terminal-input"
|
||||
bind:this={inputElement}
|
||||
bind:value={currentInput}
|
||||
on:keydown={handleKeyDown}
|
||||
placeholder={placeholder}
|
||||
aria-label={aria_label}
|
||||
autocomplete="off"
|
||||
spellcheck="false"
|
||||
style="font-family: {$uiSettings.font}; font-size: {$accessibilitySettings.fontSize}px;"
|
||||
/>
|
||||
<button type="submit" class="mud-terminal-submit-button" aria-label="Send command">Send</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.mud-terminal-container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100%;
|
||||
width: 100%; /* Ensure full width */
|
||||
background-color: #f8f8f8;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 4px;
|
||||
overflow: hidden;
|
||||
position: relative; /* Ensure proper stacking */
|
||||
z-index: 1; /* Ensure proper stacking */
|
||||
}
|
||||
|
||||
.mud-terminal-container.dark-mode {
|
||||
background-color: #282a36;
|
||||
border-color: #44475a;
|
||||
color: #f8f8f2;
|
||||
}
|
||||
|
||||
.mud-terminal-container.high-contrast {
|
||||
background-color: #000;
|
||||
color: #fff;
|
||||
border-color: #fff;
|
||||
}
|
||||
|
||||
.mud-terminal-output {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding: 10px;
|
||||
font-family: monospace;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
}
|
||||
|
||||
.mud-terminal-output:focus {
|
||||
outline: 2px solid var(--color-primary, #2196f3);
|
||||
outline-offset: -2px;
|
||||
}
|
||||
|
||||
.mud-terminal-output:focus:not(:focus-visible) {
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.mud-terminal-line {
|
||||
margin-bottom: 4px;
|
||||
overflow-wrap: break-word;
|
||||
padding: 2px;
|
||||
border-radius: 2px;
|
||||
}
|
||||
|
||||
.mud-terminal-subline {
|
||||
margin-bottom: 1px; /* Less space between split lines */
|
||||
margin-left: 10px; /* Indent sublines to show they belong together */
|
||||
padding: 1px;
|
||||
}
|
||||
|
||||
.mud-terminal-content {
|
||||
display: inline-block;
|
||||
width: 100%;
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
|
||||
/* Ensure <br> tags are properly rendered */
|
||||
:global(.mud-terminal-content br) {
|
||||
display: block;
|
||||
content: "";
|
||||
margin-top: 0.25em;
|
||||
}
|
||||
|
||||
.mud-input-line {
|
||||
color: #6272a4;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.dark-mode .mud-input-line {
|
||||
color: #8be9fd;
|
||||
}
|
||||
|
||||
.high-contrast .mud-input-line {
|
||||
color: #ff0;
|
||||
}
|
||||
|
||||
.mud-timestamp {
|
||||
color: #999;
|
||||
margin-right: 8px;
|
||||
font-size: 0.9em;
|
||||
display: inline-block;
|
||||
vertical-align: top;
|
||||
}
|
||||
|
||||
.dark-mode .mud-timestamp {
|
||||
color: #6272a4;
|
||||
}
|
||||
|
||||
.high-contrast .mud-timestamp {
|
||||
color: #0f0;
|
||||
}
|
||||
|
||||
.mud-terminal-input-form {
|
||||
display: flex;
|
||||
padding: 10px;
|
||||
border-top: 1px solid #ddd;
|
||||
}
|
||||
|
||||
.dark-mode .mud-terminal-input-form {
|
||||
border-top-color: #44475a;
|
||||
}
|
||||
|
||||
.high-contrast .mud-terminal-input-form {
|
||||
border-top-color: #fff;
|
||||
}
|
||||
|
||||
.mud-terminal-input {
|
||||
flex: 1;
|
||||
padding: 8px;
|
||||
font-family: monospace;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.dark-mode .mud-terminal-input {
|
||||
background-color: #383a59;
|
||||
color: #f8f8f2;
|
||||
border-color: #44475a;
|
||||
}
|
||||
|
||||
.high-contrast .mud-terminal-input {
|
||||
background-color: #000;
|
||||
color: #fff;
|
||||
border-color: #fff;
|
||||
}
|
||||
|
||||
.mud-terminal-submit-button {
|
||||
margin-left: 10px;
|
||||
padding: 8px 16px;
|
||||
background-color: #6272a4;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.dark-mode .mud-terminal-submit-button {
|
||||
background-color: #ff79c6;
|
||||
}
|
||||
|
||||
.high-contrast .mud-terminal-submit-button {
|
||||
background-color: #fff;
|
||||
color: #000;
|
||||
border: 2px solid #fff;
|
||||
}
|
||||
|
||||
.mud-terminal-submit-button:hover {
|
||||
opacity: 0.9;
|
||||
}
|
||||
|
||||
.mud-terminal-submit-button:active {
|
||||
opacity: 0.8;
|
||||
}
|
||||
|
||||
.sr-only {
|
||||
position: absolute;
|
||||
width: 1px;
|
||||
height: 1px;
|
||||
padding: 0;
|
||||
margin: -1px;
|
||||
overflow: hidden;
|
||||
clip: rect(0, 0, 0, 0);
|
||||
white-space: nowrap;
|
||||
border-width: 0;
|
||||
}
|
||||
|
||||
.focused-message {
|
||||
outline: 2px solid var(--color-primary, #2196f3);
|
||||
background-color: rgba(33, 150, 243, 0.1);
|
||||
}
|
||||
|
||||
.dark-mode .focused-message {
|
||||
background-color: rgba(255, 121, 198, 0.2); /* Use a pink highlight for dark mode */
|
||||
}
|
||||
|
||||
.high-contrast .focused-message {
|
||||
outline: 3px solid #fff;
|
||||
background-color: #444;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user