File size: 1,720 Bytes
4af09f9 | 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 | import { FileNode } from '../lib/db';
/**
* Extracts raw text from Lexical JSON payload if necessary,
* then prepends emdash '— ' to each non-empty text line.
*/
export function formatContentForCopy(content: string): string {
if (!content) return '';
let rawText = content;
// If content is a Lexical JSON string
try {
const parsed = JSON.parse(content);
if (parsed.root && parsed.root.children) {
// Very basic extraction of paragraphs for copy buffer
// In reality, you'd use Lexical's @lexical/text plugin or simple tree iteration
rawText = parsed.root.children
.map((p: any) => p.children?.map((n: any) => n.text).join('') || '')
.join('\n');
}
} catch (e) {
// not JSON, fallback to rawText as is
}
const lines = rawText.split('\n');
const formattedLines = lines.map(line => {
if (line.trim().length > 0) {
return `— ${line}`;
}
return line; // Leave empty lines untouched
});
return formattedLines.join('\n');
}
export async function copyFilesContentToClipboard(files: FileNode[], idsToCopy: string[]) {
const contentArray: string[] = [];
const addContentRecursive = (nodeId: string) => {
const node = files.find(f => f.id === nodeId);
if (!node) return;
if (node.type === 'file' && node.content) {
contentArray.push(formatContentForCopy(node.content));
}
const children = files.filter(f => f.parentId === nodeId);
children.forEach(c => addContentRecursive(c.id));
};
for (const id of idsToCopy) {
addContentRecursive(id);
}
if (contentArray.length > 0) {
const combined = contentArray.join('\n\n');
await navigator.clipboard.writeText(combined);
}
}
|