File size: 6,685 Bytes
31dd200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import { toast } from 'svelte-sonner';
import { AttachmentType } from '$lib/enums';
import type {
	DatabaseMessageExtra,
	DatabaseMessageExtraTextFile,
	DatabaseMessageExtraLegacyContext,
	ClipboardTextAttachment,
	ParsedClipboardContent
} from '$lib/types';

/**

 * Copy text to clipboard with toast notification

 * Uses modern clipboard API when available, falls back to legacy method for non-secure contexts

 * @param text - Text to copy to clipboard

 * @param successMessage - Custom success message (optional)

 * @param errorMessage - Custom error message (optional)

 * @returns Promise<boolean> - True if successful, false otherwise

 */
export async function copyToClipboard(

	text: string,

	successMessage = 'Copied to clipboard',

	errorMessage = 'Failed to copy to clipboard'

): Promise<boolean> {
	try {
		// Try modern clipboard API first (secure contexts only)
		if (navigator.clipboard && navigator.clipboard.writeText) {
			await navigator.clipboard.writeText(text);
			toast.success(successMessage);
			return true;
		}

		// Fallback for non-secure contexts
		const textArea = document.createElement('textarea');
		textArea.value = text;
		textArea.style.position = 'fixed';
		textArea.style.left = '-999999px';
		textArea.style.top = '-999999px';
		document.body.appendChild(textArea);
		textArea.focus();
		textArea.select();

		const successful = document.execCommand('copy');
		document.body.removeChild(textArea);

		if (successful) {
			toast.success(successMessage);
			return true;
		} else {
			throw new Error('execCommand failed');
		}
	} catch (error) {
		console.error('Failed to copy to clipboard:', error);
		toast.error(errorMessage);
		return false;
	}
}

/**

 * Copy code with HTML entity decoding and toast notification

 * @param rawCode - Raw code string that may contain HTML entities

 * @param successMessage - Custom success message (optional)

 * @param errorMessage - Custom error message (optional)

 * @returns Promise<boolean> - True if successful, false otherwise

 */
export async function copyCodeToClipboard(

	rawCode: string,

	successMessage = 'Code copied to clipboard',

	errorMessage = 'Failed to copy code'

): Promise<boolean> {
	return copyToClipboard(rawCode, successMessage, errorMessage);
}

/**

 * Formats a message with text attachments for clipboard copying.

 *

 * Default format (asPlainText = false):

 * ```

 * "Text message content"

 * [

 *   {"type":"TEXT","name":"filename.txt","content":"..."},

 *   {"type":"TEXT","name":"another.txt","content":"..."}

 * ]

 * ```

 *

 * Plain text format (asPlainText = true):

 * ```

 * Text message content

 *

 * file content here

 *

 * another file content

 * ```

 *

 * @param content - The message text content

 * @param extras - Optional array of message attachments

 * @param asPlainText - If true, format as plain text without JSON structure

 * @returns Formatted string for clipboard

 */
export function formatMessageForClipboard(

	content: string,

	extras?: DatabaseMessageExtra[],

	asPlainText: boolean = false

): string {
	// Filter only text attachments (TEXT type and legacy CONTEXT type)
	const textAttachments =
		extras?.filter(
			(extra): extra is DatabaseMessageExtraTextFile | DatabaseMessageExtraLegacyContext =>
				extra.type === AttachmentType.TEXT || extra.type === AttachmentType.LEGACY_CONTEXT
		) ?? [];

	if (textAttachments.length === 0) {
		return content;
	}

	if (asPlainText) {
		const parts = [content];
		for (const att of textAttachments) {
			parts.push(att.content);
		}
		return parts.join('\n\n');
	}

	const clipboardAttachments: ClipboardTextAttachment[] = textAttachments.map((att) => ({
		type: AttachmentType.TEXT,
		name: att.name,
		content: att.content
	}));

	return `${JSON.stringify(content)}\n${JSON.stringify(clipboardAttachments, null, 2)}`;
}

/**

 * Parses clipboard content to extract message and text attachments.

 * Supports both plain text and the special format with attachments.

 *

 * @param clipboardText - Raw text from clipboard

 * @returns Parsed content with message and attachments

 */
export function parseClipboardContent(clipboardText: string): ParsedClipboardContent {
	const defaultResult: ParsedClipboardContent = {
		message: clipboardText,
		textAttachments: []
	};

	if (!clipboardText.startsWith('"')) {
		return defaultResult;
	}

	try {
		let stringEndIndex = -1;
		let escaped = false;

		for (let i = 1; i < clipboardText.length; i++) {
			const char = clipboardText[i];

			if (escaped) {
				escaped = false;
				continue;
			}

			if (char === '\\') {
				escaped = true;
				continue;
			}

			if (char === '"') {
				stringEndIndex = i;
				break;
			}
		}

		if (stringEndIndex === -1) {
			return defaultResult;
		}

		const jsonStringPart = clipboardText.substring(0, stringEndIndex + 1);
		const remainingPart = clipboardText.substring(stringEndIndex + 1).trim();

		const message = JSON.parse(jsonStringPart) as string;

		if (!remainingPart || !remainingPart.startsWith('[')) {
			return {
				message,
				textAttachments: []
			};
		}

		const attachments = JSON.parse(remainingPart) as unknown[];

		const validAttachments: ClipboardTextAttachment[] = [];

		for (const att of attachments) {
			if (isValidTextAttachment(att)) {
				validAttachments.push({
					type: AttachmentType.TEXT,
					name: att.name,
					content: att.content
				});
			}
		}

		return {
			message,
			textAttachments: validAttachments
		};
	} catch {
		return defaultResult;
	}
}

/**

 * Type guard to validate a text attachment object

 * @param obj The object to validate

 * @returns true if the object is a valid text attachment

 */
function isValidTextAttachment(

	obj: unknown

): obj is { type: string; name: string; content: string } {
	if (typeof obj !== 'object' || obj === null) {
		return false;
	}

	const record = obj as Record<string, unknown>;

	return (
		(record.type === AttachmentType.TEXT || record.type === 'TEXT') &&
		typeof record.name === 'string' &&
		typeof record.content === 'string'
	);
}

/**

 * Checks if clipboard content contains our special format with attachments

 * @param clipboardText - Raw text from clipboard

 * @returns true if the clipboard content contains our special format with attachments

 */
export function hasClipboardAttachments(clipboardText: string): boolean {
	if (!clipboardText.startsWith('"')) {
		return false;
	}

	const parsed = parseClipboardContent(clipboardText);
	return parsed.textAttachments.length > 0;
}