Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion resources/js/components/code-block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { cn } from '@/lib/utils';

export function CodeBlock({
html,
wholeHtml,
language,
isCopyable = true,
}: {
html: string;
wholeHtml?: string;
language: string | null;
isCopyable?: boolean;
}) {
Expand All @@ -34,7 +36,15 @@ export function CodeBlock({
return;
}

if (await copy(codeRef.current.textContent ?? '')) {
// A template's content is inert: nothing in it loads or runs.
const whole = document.createElement('template');
whole.innerHTML = wholeHtml ?? '';
const code =
wholeHtml === undefined
? codeRef.current.textContent
: whole.content.textContent;

if (await copy(code ?? '')) {
setIsCopied(true);

return;
Expand Down
17 changes: 16 additions & 1 deletion resources/js/components/commit-body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export function BodySegments({
<CodeBlock
key={index}
html={segment.html}
wholeHtml={segment.wholeHtml}
language={segment.language}
isCopyable={!isStatic}
/>
Expand Down Expand Up @@ -128,6 +129,20 @@ export function FoldedCommitBody({
);
}

// The preview only ever cuts a code block that starts the body, and what comes before it is the same in both, so
// its first block copies the whole block at the same place.
const firstBlock = previewSegments.findIndex(
(segment) => segment.type === 'block',
);
const whole = segments[firstBlock];
const preview = previewSegments.map((segment, index) =>
index === firstBlock &&
segment.type === 'block' &&
whole?.type === 'block'
? { ...segment, wholeHtml: whole.html }
: segment,
);

// The button goes away, so focus moves to what was unfolded: the commit card, which the keyboard shortcuts walk,
// or the body itself inside a reply, which they don't.
const unfold = (button: HTMLButtonElement) => {
Expand All @@ -147,7 +162,7 @@ export function FoldedCommitBody({
return (
<div ref={bodyRef} tabIndex={-1} className="outline-none">
<CommitBody
segments={isUnfolded ? segments : previewSegments}
segments={isUnfolded ? segments : preview}
mentions={mentions}
className={className}
/>
Expand Down
8 changes: 7 additions & 1 deletion resources/js/types/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ export type CommitImage = {
export type BodySegment =
| { type: 'text'; text: string }
| { type: 'code'; code: string }
| { type: 'block'; language: string | null; html: string };
| {
type: 'block';
language: string | null;
html: string;
// The whole block, when a folded body shows only its first lines.
wholeHtml?: string;
};

export type CommitTranslation = {
language: string;
Expand Down