repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/ui/web/components/long-output.js.ts

Raw
File outline
export type LongOutputModel = {
	id: string;
	text: string;
	truncated: boolean;
};

export function longOutputModel(
	id: string,
	text: string,
	expanded: Set<string>,
	limit = 4_000,
): LongOutputModel {
	if (expanded.has(id) || text.length <= limit)
		return { id, text, truncated: false };
	return { id, text: text.slice(0, limit), truncated: true };
}

export function toggleLongOutput(
	id: string,
	expanded: Set<string>,
): Set<string> {
	const next = new Set(expanded);
	if (next.has(id)) next.delete(id);
	else next.add(id);
	return next;
}

export function renderLongOutputJs(): string {
	return `const expandedOutputs = new Set();
function longOutputText(id, text, limit = 4000) {
	if (expandedOutputs.has(id) || text.length <= limit) return { text, truncated: false };
	return { text: text.slice(0, limit), truncated: true };
}
function applyLongOutput(row, id, text) {
	const model = longOutputText(id, text);
	row.textContent = model.text;
	if (!model.truncated) return;
	const button = document.createElement('button');
	button.type = 'button';
	button.className = 'ui-expand-output';
	button.textContent = 'expand output';
	button.addEventListener('click', () => { expandedOutputs.add(id); row.textContent = text; });
	row.append(button);
}
`;
}