Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix rich-text editor shortcuts and missing list, quote, and link styles (#14532)

Damian Legawiecbe76364d999f
diff --git a/.changeset/rich-text-editor-toolbar-and-shortcuts.md b/.changeset/rich-text-editor-toolbar-and-shortcuts.md
new file mode 100644
index 0000000000..24e052adce
--- /dev/null
+++ b/.changeset/rich-text-editor-toolbar-and-shortcuts.md
@@ -0,0 +1,6 @@
+---
+"@spree/dashboard-ui": patch
+"@spree/dashboard": patch
+---
+
+Stop the admin sidebar's Cmd/Ctrl+B shortcut from firing while typing, and restore list, quote and link styling plus live toolbar states in the rich-text editor.
diff --git a/packages/dashboard-ui/src/styles.css b/packages/dashboard-ui/src/styles.css
index 42298e8500..b392dc2705 100644
--- a/packages/dashboard-ui/src/styles.css
+++ b/packages/dashboard-ui/src/styles.css
@@ -617,6 +617,96 @@
5px -5px,
-5px 0;
}
+
+ /* Tiptap editor surface. Tailwind preflight strips list markers, link
+ underlines and block margins, so lists, quotes and links would save
+ correctly but look like plain paragraphs until the storefront rendered
+ them. These rules restore that chrome only inside the editor. */
+ .tiptap {
+ min-height: 8rem;
+ outline: none;
+
+ & p.is-editor-empty:first-child::before {
+ color: var(--muted-foreground);
+ content: attr(data-placeholder);
+ float: left;
+ height: 0;
+ pointer-events: none;
+ }
+
+ & p {
+ margin-block: 0.5em;
+ }
+
+ & p:first-child {
+ margin-block-start: 0;
+ }
+
+ & p:last-child {
+ margin-block-end: 0;
+ }
+
+ & ul {
+ list-style-type: disc;
+ margin-block: 0.5em;
+ padding-inline-start: 1.5em;
+ }
+
+ & ol {
+ list-style-type: decimal;
+ margin-block: 0.5em;
+ padding-inline-start: 1.5em;
+ }
+
+ & ul ul,
+ & ol ul {
+ list-style-type: circle;
+ margin-block: 0.15em;
+ }
+
+ & ul ul ul,
+ & ol ul ul,
+ & ol ol ul,
+ & ul ol ul {
+ list-style-type: square;
+ }
+
+ & ol ol,
+ & ul ol {
+ list-style-type: lower-alpha;
+ margin-block: 0.15em;
+ }
+
+ & li {
+ margin-block: 0.15em;
+ }
+
+ & li > p {
+ margin-block: 0;
+ }
+
+ & blockquote {
+ border-inline-start: 3px solid var(--border);
+ color: var(--muted-foreground);
+ margin-block: 0.75em;
+ padding-inline-start: 1em;
+ }
+
+ & a {
+ color: var(--brand-blue);
+ text-decoration: underline;
+ text-underline-offset: 2px;
+ }
+
+ & img {
+ max-width: 100%;
+ border-radius: var(--radius-md);
+ }
+
+ & img.ProseMirror-selectednode {
+ outline: 2px solid var(--brand-blue);
+ }
+ }
}
/* Reduced motion. Deliberately NOT inside `@layer base`: unlayered CSS always
diff --git a/packages/dashboard-ui/src/ui/rich-text-editor.tsx b/packages/dashboard-ui/src/ui/rich-text-editor.tsx
index 4fc664f21d..0e310f4eb1 100644
--- a/packages/dashboard-ui/src/ui/rich-text-editor.tsx
+++ b/packages/dashboard-ui/src/ui/rich-text-editor.tsx
@@ -1,7 +1,8 @@
import Image from '@tiptap/extension-image'
import Link from '@tiptap/extension-link'
import Placeholder from '@tiptap/extension-placeholder'
-import { EditorContent, useEditor } from '@tiptap/react'
+import type { Editor } from '@tiptap/react'
+import { EditorContent, useEditor, useEditorState } from '@tiptap/react'
import StarterKit from '@tiptap/starter-kit'
import {
BoldIcon,
@@ -167,99 +168,152 @@ export function RichTextEditor({
return (
<div
ref={wrapperRef}
+ data-slot="rich-text-editor"
className={cn(
'rounded-lg border border-border bg-card text-foreground shadow-xs transition-[color,background-color,border-color,box-shadow] duration-100 ease-out focus-within:border-blue-500 focus-within:shadow-[0_0_0_3px_color-mix(in_srgb,var(--ring)_15%,transparent)]',
disabled && 'pointer-events-none bg-muted border-border',
className,
)}
>
- {/* Toolbar */}
- <div className="flex items-center gap-0.5 border-b border-border px-2 py-1.5">
- <ToolbarButton
- active={editor.isActive('bold')}
- onClick={() => editor.chain().focus().toggleBold().run()}
- title={t('admin.components.rich_text_editor.bold')}
- >
- <BoldIcon className="size-4" />
- </ToolbarButton>
- <ToolbarButton
- active={editor.isActive('italic')}
- onClick={() => editor.chain().focus().toggleItalic().run()}
- title={t('admin.components.rich_text_editor.italic')}
- >
- <ItalicIcon className="size-4" />
- </ToolbarButton>
- <ToolbarButton
- active={editor.isActive('strike')}
- onClick={() => editor.chain().focus().toggleStrike().run()}
- title={t('admin.components.rich_text_editor.strikethrough')}
- >
- <StrikethroughIcon className="size-4" />
- </ToolbarButton>
+ <EditorToolbar
+ editor={editor}
+ onRequestImage={onRequestImage}
+ onSetLink={setLink}
+ onInsertImage={insertImage}
+ />
- <ToolbarSeparator />
+ <EditorContent editor={editor} className="px-3 py-2" />
+ </div>
+ )
+}
- <ToolbarButton
- active={editor.isActive('bulletList')}
- onClick={() => editor.chain().focus().toggleBulletList().run()}
- title={t('admin.components.rich_text_editor.bullet_list')}
- >
- <ListIcon className="size-4" />
- </ToolbarButton>
- <ToolbarButton
- active={editor.isActive('orderedList')}
- onClick={() => editor.chain().focus().toggleOrderedList().run()}
- title={t('admin.components.rich_text_editor.ordered_list')}
- >
- <ListOrderedIcon className="size-4" />
+/**
+ * Isolated so `useEditorState` can re-render the buttons when the selection
+ * moves. Tiptap v3 no longer re-renders `useEditor` on every transaction, so
+ * reading `editor.isActive(...)` in the parent would stay stale.
+ */
+function EditorToolbar({
+ editor,
+ onRequestImage,
+ onSetLink,
+ onInsertImage,
+}: {
+ editor: Editor
+ onRequestImage?: RichTextEditorProps['onRequestImage']
+ onSetLink: () => void
+ onInsertImage: () => Promise<void>
+}) {
+ const { t } = useTranslation()
+ const toolbar = useEditorState({
+ editor,
+ selector: ({ editor: current }) => ({
+ isBold: current.isActive('bold'),
+ isItalic: current.isActive('italic'),
+ isStrike: current.isActive('strike'),
+ isBulletList: current.isActive('bulletList'),
+ isOrderedList: current.isActive('orderedList'),
+ isBlockquote: current.isActive('blockquote'),
+ isLink: current.isActive('link'),
+ canUndo: current.can().undo(),
+ canRedo: current.can().redo(),
+ }),
+ })
+
+ const runToolbarCommand = (command: () => void) => {
+ command()
+ // Ctrl/Cmd+A in Tiptap is an AllSelection that also covers the trailing
+ // empty paragraph the editor always keeps. Block formats then wrap the
+ // written text but `isActive` stays false because the selection still
+ // spans mixed nodes — collapse so the button the merchant just used
+ // lights up against the formatted text.
+ if (editor.state.selection.toJSON().type === 'all') {
+ editor.commands.setTextSelection(1)
+ }
+ }
+
+ return (
+ <div
+ data-slot="rich-text-editor-toolbar"
+ className="flex items-center gap-0.5 border-b border-border px-2 py-1.5"
+ >
+ <ToolbarButton
+ active={toolbar.isBold}
+ onClick={() => runToolbarCommand(() => editor.chain().focus().toggleBold().run())}
+ title={t('admin.components.rich_text_editor.bold')}
+ >
+ <BoldIcon className="size-4" />
+ </ToolbarButton>
+ <ToolbarButton
+ active={toolbar.isItalic}
+ onClick={() => runToolbarCommand(() => editor.chain().focus().toggleItalic().run())}
+ title={t('admin.components.rich_text_editor.italic')}
+ >
+ <ItalicIcon className="size-4" />
+ </ToolbarButton>
+ <ToolbarButton
+ active={toolbar.isStrike}
+ onClick={() => runToolbarCommand(() => editor.chain().focus().toggleStrike().run())}
+ title={t('admin.components.rich_text_editor.strikethrough')}
+ >
+ <StrikethroughIcon className="size-4" />
+ </ToolbarButton>
+
+ <ToolbarSeparator />
+
+ <ToolbarButton
+ active={toolbar.isBulletList}
+ onClick={() => runToolbarCommand(() => editor.chain().focus().toggleBulletList().run())}
+ title={t('admin.components.rich_text_editor.bullet_list')}
+ >
+ <ListIcon className="size-4" />
+ </ToolbarButton>
+ <ToolbarButton
+ active={toolbar.isOrderedList}
+ onClick={() => runToolbarCommand(() => editor.chain().focus().toggleOrderedList().run())}
+ title={t('admin.components.rich_text_editor.ordered_list')}
+ >
+ <ListOrderedIcon className="size-4" />
+ </ToolbarButton>
+ <ToolbarButton
+ active={toolbar.isBlockquote}
+ onClick={() => runToolbarCommand(() => editor.chain().focus().toggleBlockquote().run())}
+ title={t('admin.components.rich_text_editor.blockquote')}
+ >
+ <QuoteIcon className="size-4" />
+ </ToolbarButton>
+
+ <ToolbarSeparator />
+
+ <ToolbarButton
+ active={toolbar.isLink}
+ onClick={() => runToolbarCommand(onSetLink)}
+ title={t('admin.components.rich_text_editor.link')}
+ >
+ <LinkIcon className="size-4" />
+ </ToolbarButton>
+
+ {onRequestImage && (
+ <ToolbarButton onClick={onInsertImage} title={t('admin.components.rich_text_editor.image')}>
+ <ImageIcon className="size-4" />
</ToolbarButton>
+ )}
+
+ <div className="ml-auto flex items-center gap-0.5">
<ToolbarButton
- active={editor.isActive('blockquote')}
- onClick={() => editor.chain().focus().toggleBlockquote().run()}
- title={t('admin.components.rich_text_editor.blockquote')}
+ onClick={() => editor.chain().focus().undo().run()}
+ disabled={!toolbar.canUndo}
+ title={t('admin.components.rich_text_editor.undo')}
>
- <QuoteIcon className="size-4" />
+ <UndoIcon className="size-4" />
</ToolbarButton>
-
- <ToolbarSeparator />
-
<ToolbarButton
- active={editor.isActive('link')}
- onClick={setLink}
- title={t('admin.components.rich_text_editor.link')}
+ onClick={() => editor.chain().focus().redo().run()}
+ disabled={!toolbar.canRedo}
+ title={t('admin.components.rich_text_editor.redo')}
>
- <LinkIcon className="size-4" />
+ <RedoIcon className="size-4" />
</ToolbarButton>
-
- {onRequestImage && (
- <ToolbarButton onClick={insertImage} title={t('admin.components.rich_text_editor.image')}>
- <ImageIcon className="size-4" />
- </ToolbarButton>
- )}
-
- <div className="ml-auto flex items-center gap-0.5">
- <ToolbarButton
- onClick={() => editor.chain().focus().undo().run()}
- disabled={!editor.can().undo()}
- title={t('admin.components.rich_text_editor.undo')}
- >
- <UndoIcon className="size-4" />
- </ToolbarButton>
- <ToolbarButton
- onClick={() => editor.chain().focus().redo().run()}
- disabled={!editor.can().redo()}
- title={t('admin.components.rich_text_editor.redo')}
- >
- <RedoIcon className="size-4" />
- </ToolbarButton>
- </div>
</div>
-
- {/* Editor */}
- <EditorContent
- editor={editor}
- className="prose prose-sm max-w-none px-3 py-2 dark:prose-invert [&_.tiptap]:min-h-32 [&_.tiptap]:outline-none [&_.tiptap.is-editor-empty:first-child::before]:text-muted-foreground [&_.tiptap.is-editor-empty:first-child::before]:content-[attr(data-placeholder)] [&_.tiptap.is-editor-empty:first-child::before]:float-left [&_.tiptap.is-editor-empty:first-child::before]:h-0 [&_.tiptap.is-editor-empty:first-child::before]:pointer-events-none [&_.tiptap_img]:max-w-full [&_.tiptap_img]:rounded-md [&_.tiptap_img.ProseMirror-selectednode]:outline-2 [&_.tiptap_img.ProseMirror-selectednode]:outline-blue-500"
- />
</div>
)
}
@@ -283,6 +337,8 @@ function ToolbarButton({
onClick={onClick}
disabled={disabled}
title={title}
+ aria-label={title}
+ aria-pressed={active}
className={cn(
'inline-flex items-center justify-center rounded-md p-1.5 text-muted-foreground hover:bg-accent hover:text-foreground transition-colors disabled:opacity-40 disabled:pointer-events-none',
active && 'bg-accent text-foreground',
diff --git a/packages/dashboard-ui/src/ui/sidebar.tsx b/packages/dashboard-ui/src/ui/sidebar.tsx
index 29880f4ccb..effccf9720 100644
--- a/packages/dashboard-ui/src/ui/sidebar.tsx
+++ b/packages/dashboard-ui/src/ui/sidebar.tsx
@@ -32,6 +32,12 @@ export const mobileDrawerClassName =
const SIDEBAR_WIDTH_ICON = '59px'
const SIDEBAR_KEYBOARD_SHORTCUT = 'b'
+function isEditableKeyboardTarget(target: EventTarget | null): boolean {
+ if (!(target instanceof HTMLElement)) return false
+ if (target.isContentEditable) return true
+ return Boolean(target.closest('input, textarea, select'))
+}
+
type SidebarContextProps = {
state: 'expanded' | 'collapsed'
open: boolean
@@ -120,13 +126,17 @@ function SidebarProvider({
return isMobile ? setOpenMobile((open) => !open) : setOpen((open) => !open)
}, [isMobile, setOpen, setOpenMobile])
- // Adds a keyboard shortcut to toggle the sidebar.
+ // Cmd/Ctrl+B is also "bold" in every text field. Skip the shortcut when
+ // the merchant is typing so it cannot collapse the nav out from under
+ // a rich-text editor (or steal the keystroke from an input).
React.useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
- if (event.key === SIDEBAR_KEYBOARD_SHORTCUT && (event.metaKey || event.ctrlKey)) {
- event.preventDefault()
- toggleSidebar()
- }
+ if (event.key !== SIDEBAR_KEYBOARD_SHORTCUT || !(event.metaKey || event.ctrlKey)) return
+ if (event.defaultPrevented) return
+ if (isEditableKeyboardTarget(event.target)) return
+
+ event.preventDefault()
+ toggleSidebar()
}
window.addEventListener('keydown', handleKeyDown)
diff --git a/packages/dashboard/e2e/products-edit.spec.ts b/packages/dashboard/e2e/products-edit.spec.ts
index 618b309cd0..d174b07b2c 100644
--- a/packages/dashboard/e2e/products-edit.spec.ts
+++ b/packages/dashboard/e2e/products-edit.spec.ts
@@ -13,6 +13,73 @@ test.describe('product edit', () => {
await expect(page.getByLabel(/^name$/i)).toHaveValue(name)
})
+ test('reflects list, quote and link formatting in the description editor', async ({ page }) => {
+ const creds = await login(page)
+ const name = `E2E Product Editor Styles ${Date.now()}`
+
+ await createProduct(page, creds.store_id, name)
+
+ const description = page.locator('#product-description')
+ await typeDescription(page, 'Formatted line')
+
+ const bulletBtn = page.getByRole('button', { name: /^bullet list$/i })
+ await bulletBtn.click()
+ const bulletList = description.locator('ul')
+ await expect(bulletList).toBeVisible()
+ await expect(bulletList).toHaveCSS('list-style-type', 'disc')
+ await expect(bulletBtn).toHaveAttribute('aria-pressed', 'true')
+ await bulletBtn.click()
+
+ const orderedBtn = page.getByRole('button', { name: /^ordered list$/i })
+ await orderedBtn.click()
+ const orderedList = description.locator('ol')
+ await expect(orderedList).toBeVisible()
+ await expect(orderedList).toHaveCSS('list-style-type', 'decimal')
+ await expect(orderedBtn).toHaveAttribute('aria-pressed', 'true')
+ await orderedBtn.click()
+
+ const quoteBtn = page.getByRole('button', { name: /^blockquote$/i })
+ await quoteBtn.click()
+ const quote = description.locator('blockquote')
+ await expect(quote).toBeVisible()
+ await expect(quote).toHaveCSS('border-inline-start-width', '3px')
+ await expect(quoteBtn).toHaveAttribute('aria-pressed', 'true')
+
+ await quote.click({ clickCount: 3 })
+ page.once('dialog', (dialog) => dialog.accept('https://example.com'))
+ await page.getByRole('button', { name: /^link$/i }).click()
+ const link = description.locator('a[href="https://example.com"]')
+ await expect(link).toBeVisible()
+ await expect(link).toHaveCSS('text-decoration-line', 'underline')
+ await expect(page.getByRole('button', { name: /^link$/i })).toHaveAttribute(
+ 'aria-pressed',
+ 'true',
+ )
+ })
+
+ test('cmd+b bolds description text without collapsing the sidebar', async ({ page }) => {
+ const creds = await login(page)
+ const name = `E2E Product Editor Bold ${Date.now()}`
+
+ await createProduct(page, creds.store_id, name)
+
+ const sidebar = page.locator('[data-slot="sidebar"][data-state]').first()
+ await expect(sidebar).toHaveAttribute('data-state', 'expanded')
+
+ await typeDescription(page, 'Bold this')
+ await page.keyboard.press('ControlOrMeta+A')
+ await page.keyboard.press('ControlOrMeta+B')
+
+ const boldText = page.locator('#product-description strong')
+ await expect(boldText).toHaveText('Bold this')
+ await boldText.click()
+ await expect(page.getByRole('button', { name: /^bold$/i })).toHaveAttribute(
+ 'aria-pressed',
+ 'true',
+ )
+ await expect(sidebar).toHaveAttribute('data-state', 'expanded')
+ })
+
test('preserves multi-paragraph description formatting across reload', async ({ page }) => {
const creds = await login(page)
const name = `E2E Product Rich Text ${Date.now()}`