Skip to content

Commit d3b1219

Browse files
klopez4212wesbillmanCarl
authored
Fix composer selection formatting and drop overlay (#3172)
## Summary - scope code blocks and list formatting to the selected composer text - use the Buzz primary color for the selection formatter - extend the channel drop overlay over the composer with matching corners, blur, and accessible contrast across themes ## Validation - `just ci` - composer selection formatting E2E tests - file attachment and all-theme drop contrast E2E tests --------- Signed-off-by: kenny lopez <[email protected]> Signed-off-by: Wes <[email protected]> Co-authored-by: Wes <[email protected]> Co-authored-by: Carl <c7ebe626f000404285d3686e1dc74cc07cc60a9754a150041ba132e14bd3e2ec@buzz.block.builderlab.xyz>
1 parent c244a00 commit d3b1219

9 files changed

Lines changed: 598 additions & 24 deletions

File tree

‎desktop/playwright.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export default defineConfig({
5959
"**/video-attachment.spec.ts",
6060
"**/spoiler.spec.ts",
6161
"**/composer-link-shortcut.spec.ts",
62+
"**/composer-selection-formatting.spec.ts",
6263
"**/composer-tooltip-dismiss.spec.ts",
6364
"**/mentions.spec.ts",
6465
"**/team-mentions.spec.ts",

‎desktop/src/features/channels/ui/ChannelPane.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ export const ChannelPane = React.memo(function ChannelPane({
820820
</div>
821821
)}
822822
{canDropInMainColumn && mainComposerMedia.isDragOver ? (
823-
<DropZoneOverlay className="z-30 rounded-none" />
823+
<DropZoneOverlay className="z-50 rounded-2xl bg-primary/20 backdrop-blur-sm" />
824824
) : null}
825825
</section>
826826
) : null}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { TextSelection, type Transaction } from "@tiptap/pm/state";
2+
import { canSplit } from "@tiptap/pm/transform";
3+
4+
function canSplitInsideTextblock(
5+
transaction: Transaction,
6+
position: number,
7+
): boolean {
8+
const $position = transaction.doc.resolve(position);
9+
return (
10+
$position.parent.inlineContent &&
11+
$position.parentOffset > 0 &&
12+
$position.parentOffset < $position.parent.content.size &&
13+
canSplit(transaction.doc, position)
14+
);
15+
}
16+
17+
function mapRangeThroughLatestStep(
18+
transaction: Transaction,
19+
from: number,
20+
to: number,
21+
): { from: number; to: number } {
22+
const stepMap = transaction.steps.at(-1)?.getMap();
23+
return stepMap
24+
? {
25+
from: stepMap.map(from, 1),
26+
to: stepMap.map(to, -1),
27+
}
28+
: { from, to };
29+
}
30+
31+
/**
32+
* Isolate a non-empty text selection at exact block boundaries.
33+
*
34+
* ProseMirror's block commands operate on whole textblocks. The composer can
35+
* hold an entire draft in one paragraph, so toggling a list or code block for
36+
* a substring otherwise formats the whole draft. Splitting at the selection
37+
* end and start first gives the selected text its own block while preserving
38+
* the surrounding content as sibling paragraphs.
39+
*
40+
* This mutates the transaction supplied by a Tiptap command chain so the
41+
* isolation and the following block toggle remain one undoable edit.
42+
*/
43+
export function isolateSelectionForBlockFormatting(
44+
transaction: Transaction,
45+
): boolean {
46+
if (
47+
!(transaction.selection instanceof TextSelection) ||
48+
transaction.selection.empty
49+
) {
50+
return false;
51+
}
52+
53+
const isBackward = transaction.selection.anchor > transaction.selection.head;
54+
let { from, to } = transaction.selection;
55+
56+
const nodeAfterSelection = transaction.doc.resolve(to).nodeAfter;
57+
if (nodeAfterSelection?.type.name === "hardBreak") {
58+
transaction.delete(to, to + nodeAfterSelection.nodeSize);
59+
({ from, to } = mapRangeThroughLatestStep(transaction, from, to));
60+
}
61+
62+
const nodeBeforeSelection = transaction.doc.resolve(from).nodeBefore;
63+
if (nodeBeforeSelection?.type.name === "hardBreak") {
64+
transaction.delete(from - nodeBeforeSelection.nodeSize, from);
65+
({ from, to } = mapRangeThroughLatestStep(transaction, from, to));
66+
}
67+
68+
if (canSplitInsideTextblock(transaction, to)) {
69+
transaction.split(to);
70+
({ from, to } = mapRangeThroughLatestStep(transaction, from, to));
71+
}
72+
73+
if (canSplitInsideTextblock(transaction, from)) {
74+
transaction.split(from);
75+
({ from, to } = mapRangeThroughLatestStep(transaction, from, to));
76+
}
77+
78+
transaction.setSelection(
79+
TextSelection.create(
80+
transaction.doc,
81+
isBackward ? to : from,
82+
isBackward ? from : to,
83+
),
84+
);
85+
return true;
86+
}

‎desktop/src/features/messages/ui/ComposerAttachments.tsx‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
HatGlasses,
88
Pencil,
99
Play,
10+
UploadCloud,
1011
Users,
1112
X,
1213
} from "lucide-react";
@@ -39,13 +40,18 @@ import { ComposerImageEditor } from "./ComposerImageEditor";
3940
export function DropZoneOverlay({ className }: { className?: string }) {
4041
return (
4142
<div
43+
data-testid="drop-zone-overlay"
4244
className={cn(
4345
"pointer-events-none absolute inset-0 z-10 flex items-center justify-center rounded-2xl border-2 border-dashed border-primary bg-primary/10",
4446
className,
4547
)}
4648
>
47-
<span className="text-sm font-medium text-primary">
48-
Drop files to upload
49+
<span
50+
className="flex items-center gap-2 rounded-full bg-foreground px-4 py-2 text-sm font-semibold text-background shadow-sm ring-1 ring-background/15"
51+
data-testid="drop-zone-label"
52+
>
53+
<UploadCloud aria-hidden="true" className="size-4" />
54+
<span>Drop files to upload</span>
4955
</span>
5056
</div>
5157
);

‎desktop/src/features/messages/ui/FormattingToolbar.tsx‎

Lines changed: 91 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from "react";
2+
import { TextSelection } from "@tiptap/pm/state";
23
import type { Editor } from "@tiptap/react";
34
import {
45
Bold,
@@ -15,6 +16,7 @@ import {
1516

1617
import { cn } from "@/shared/lib/cn";
1718
import { Tooltip, TooltipContent, TooltipTrigger } from "@/shared/ui/tooltip";
19+
import { isolateSelectionForBlockFormatting } from "@/features/messages/lib/selectionBlockFormatting";
1820
import { getEditorSpoilerRangeState } from "@/features/messages/lib/spoilerFormatting";
1921
import { SPOILER_MARK_NAME } from "@/features/messages/lib/spoilerMark";
2022

@@ -29,6 +31,11 @@ type FormattingToolbarProps = {
2931
onLinkButton?: () => void;
3032
};
3133

34+
type FormattingSelectionRange = {
35+
anchor: number;
36+
head: number;
37+
};
38+
3239
type ActiveStates = {
3340
bold: boolean;
3441
italic: boolean;
@@ -117,6 +124,9 @@ export const FormattingToolbar = React.memo(function FormattingToolbar({
117124
disabled = false,
118125
onLinkButton,
119126
}: FormattingToolbarProps) {
127+
const pendingSelectionRef = React.useRef<FormattingSelectionRange | null>(
128+
null,
129+
);
120130
const [activeStates, setActiveStates] = React.useState<ActiveStates | null>(
121131
() => (editor ? getActiveStates(editor) : null),
122132
);
@@ -137,36 +147,83 @@ export const FormattingToolbar = React.memo(function FormattingToolbar({
137147
};
138148
}, [editor]);
139149

140-
const toggleBold = React.useCallback(() => {
141-
editor?.chain().focus().toggleBold().run();
150+
const captureSelection = React.useCallback(() => {
151+
if (!editor || editor.state.selection.empty) {
152+
pendingSelectionRef.current = null;
153+
return;
154+
}
155+
156+
const { anchor, head } = editor.state.selection;
157+
pendingSelectionRef.current = { anchor, head };
142158
}, [editor]);
143159

144-
const toggleItalic = React.useCallback(() => {
145-
editor?.chain().focus().toggleItalic().run();
160+
const formattingChain = React.useCallback(() => {
161+
if (!editor) return null;
162+
163+
const range = pendingSelectionRef.current;
164+
pendingSelectionRef.current = null;
165+
const chain = editor.chain();
166+
167+
if (
168+
range &&
169+
range.anchor !== range.head &&
170+
range.anchor <= editor.state.doc.content.size &&
171+
range.head <= editor.state.doc.content.size
172+
) {
173+
chain.command(({ tr }) => {
174+
tr.setSelection(TextSelection.create(tr.doc, range.anchor, range.head));
175+
return true;
176+
});
177+
}
178+
179+
return chain.focus();
146180
}, [editor]);
147181

182+
const toggleBold = React.useCallback(() => {
183+
formattingChain()?.toggleBold().run();
184+
}, [formattingChain]);
185+
186+
const toggleItalic = React.useCallback(() => {
187+
formattingChain()?.toggleItalic().run();
188+
}, [formattingChain]);
189+
148190
const toggleStrike = React.useCallback(() => {
149-
editor?.chain().focus().toggleStrike().run();
150-
}, [editor]);
191+
formattingChain()?.toggleStrike().run();
192+
}, [formattingChain]);
151193

152194
const toggleCode = React.useCallback(() => {
153-
editor?.chain().focus().toggleCode().run();
154-
}, [editor]);
195+
formattingChain()?.toggleCode().run();
196+
}, [formattingChain]);
155197

156198
const toggleCodeBlock = React.useCallback(() => {
157-
editor?.chain().focus().toggleCodeBlock().run();
158-
}, [editor]);
199+
formattingChain()
200+
?.command(({ tr }) => {
201+
isolateSelectionForBlockFormatting(tr);
202+
return true;
203+
})
204+
.toggleCodeBlock()
205+
.run();
206+
}, [formattingChain]);
207+
208+
const restorePendingSelection = React.useCallback(() => {
209+
formattingChain()?.run();
210+
}, [formattingChain]);
159211

160212
const toggleLink = React.useCallback(() => {
161213
if (!editor) return;
162214

163-
// Preferred path: open the link-edit modal, which handles add, edit, and
164-
// remove with proper display-text + URL fields.
215+
// Restore the range captured on pointer-down before opening the modal.
216+
// WKWebView may otherwise collapse it as focus moves to the toolbar.
165217
if (onLinkButton) {
218+
restorePendingSelection();
166219
onLinkButton();
167220
return;
168221
}
169222

223+
const chain = formattingChain();
224+
if (!chain) return;
225+
chain.run();
226+
170227
// Legacy fallback (no modal wired): the native prompts below are a no-op
171228
// in the Tauri WebView, so this path effectively does nothing there.
172229
if (editor.isActive("link")) {
@@ -189,24 +246,37 @@ export const FormattingToolbar = React.memo(function FormattingToolbar({
189246
editor.chain().focus().insertContent(`[${label}](${url})`).run();
190247
}
191248
}
192-
}, [editor, onLinkButton]);
249+
}, [editor, formattingChain, onLinkButton, restorePendingSelection]);
193250

194251
const toggleBulletList = React.useCallback(() => {
195-
editor?.chain().focus().toggleBulletList().run();
196-
}, [editor]);
252+
formattingChain()
253+
?.command(({ tr }) => {
254+
isolateSelectionForBlockFormatting(tr);
255+
return true;
256+
})
257+
.toggleBulletList()
258+
.run();
259+
}, [formattingChain]);
197260

198261
const toggleOrderedList = React.useCallback(() => {
199-
editor?.chain().focus().toggleOrderedList().run();
200-
}, [editor]);
262+
formattingChain()
263+
?.command(({ tr }) => {
264+
isolateSelectionForBlockFormatting(tr);
265+
return true;
266+
})
267+
.toggleOrderedList()
268+
.run();
269+
}, [formattingChain]);
201270

202271
const toggleBlockquote = React.useCallback(() => {
203-
editor?.chain().focus().toggleBlockquote().run();
204-
}, [editor]);
272+
formattingChain()?.toggleBlockquote().run();
273+
}, [formattingChain]);
205274

206275
const toggleSpoiler = React.useCallback(() => {
207276
if (!editor) return;
277+
restorePendingSelection();
208278
toggleSpoilerFormatting(editor);
209-
}, [editor]);
279+
}, [editor, restorePendingSelection]);
210280

211281
if (!editor || !activeStates) return null;
212282

@@ -289,6 +359,7 @@ export const FormattingToolbar = React.memo(function FormattingToolbar({
289359
aria-pressed={item.active}
290360
disabled={disabled}
291361
onClick={() => item.action()}
362+
onMouseDown={captureSelection}
292363
className={cn(
293364
"inline-flex h-7 w-7 min-w-7 items-center justify-center rounded-md text-sm font-medium transition-colors",
294365
"hover:bg-muted hover:text-foreground",

‎desktop/src/features/messages/ui/SelectionFormattingTray.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ export function SelectionFormattingTray({
195195
? "-translate-x-1/2 -translate-y-full"
196196
: "-translate-x-1/2",
197197
)}
198+
data-buzz-selection-formatting-tray
198199
data-testid="selection-formatting-tray"
199200
onMouseDown={(event) => event.preventDefault()}
200201
role="toolbar"

‎desktop/src/shared/styles/globals/theme.css‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,28 @@
245245
--buzz-active-foreground: 0 0% 100%;
246246
}
247247

248+
/*
249+
* Use the Buzz primary color for the floating selection-formatting tray.
250+
* Other themes retain the standard popover treatment.
251+
*/
252+
:root[data-buzz-sidebar] [data-buzz-selection-formatting-tray] {
253+
border-color: hsl(var(--primary-foreground) / 0.18);
254+
background-color: hsl(var(--primary));
255+
color: hsl(var(--primary-foreground));
256+
}
257+
258+
:root[data-buzz-sidebar] [data-buzz-selection-formatting-tray] button {
259+
color: hsl(var(--primary-foreground));
260+
}
261+
262+
:root[data-buzz-sidebar] [data-buzz-selection-formatting-tray] button:hover,
263+
:root[data-buzz-sidebar]
264+
[data-buzz-selection-formatting-tray]
265+
button[aria-pressed="true"] {
266+
background-color: hsl(var(--primary-foreground) / 0.16);
267+
color: hsl(var(--primary-foreground));
268+
}
269+
248270
/* Keep the subtle content-card lift in Buzz Light only. */
249271
:root[data-buzz-sidebar]:not(.dark) [data-buzz-content-surface] {
250272
box-shadow:

0 commit comments

Comments
 (0)