Skip to content

Commit 6c06579

Browse files
committed
fix: backward compat for toggleOrderedList
1 parent 87694e2 commit 6c06579

3 files changed

Lines changed: 32 additions & 11 deletions

File tree

packages/super-editor/src/headless-toolbar/create-headless-toolbar.test.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -554,15 +554,13 @@ describe('createHeadlessToolbar', () => {
554554
controller.destroy();
555555
});
556556

557-
// PR-2873 (SD-2527): registry rename has compatibility implications.
558-
// Hosts that wrap or mock the OLD command names (toggleBulletList,
559-
// toggleOrderedList) without exposing the new style-aware ones now silently
560-
// fail — this test documents that contract.
561-
it('returns false for bullet-list when only the old toggleBulletList exists on the host', () => {
557+
// PR-2873 (SD-2527): the registry prefers the new style-aware commands
558+
// but falls back to the legacy ones so hosts that only expose
559+
// toggleBulletList / toggleOrderedList keep working.
560+
it('falls back to toggleBulletList when toggleBulletListStyle is unavailable', () => {
562561
const toggleBulletList = vi.fn(() => true);
563562
const superdoc = createActiveEditorHost({
564563
commands: {
565-
// legacy command only — no toggleBulletListStyle
566564
toggleBulletList,
567565
},
568566
state: createSelectionState({
@@ -581,13 +579,13 @@ describe('createHeadlessToolbar', () => {
581579
commands: ['bullet-list'],
582580
});
583581

584-
expect(controller.execute?.('bullet-list')).toBe(false);
585-
expect(toggleBulletList).not.toHaveBeenCalled();
582+
expect(controller.execute?.('bullet-list')).toBe(true);
583+
expect(toggleBulletList).toHaveBeenCalledTimes(1);
586584

587585
controller.destroy();
588586
});
589587

590-
it('returns false for numbered-list when only the old toggleOrderedList exists on the host', () => {
588+
it('falls back to toggleOrderedList when toggleOrderedListStyle is unavailable', () => {
591589
const toggleOrderedList = vi.fn(() => true);
592590
const superdoc = createActiveEditorHost({
593591
commands: {
@@ -609,8 +607,8 @@ describe('createHeadlessToolbar', () => {
609607
commands: ['numbered-list'],
610608
});
611609

612-
expect(controller.execute?.('numbered-list')).toBe(false);
613-
expect(toggleOrderedList).not.toHaveBeenCalled();
610+
expect(controller.execute?.('numbered-list')).toBe(true);
611+
expect(toggleOrderedList).toHaveBeenCalledTimes(1);
614612

615613
controller.destroy();
616614
});

packages/super-editor/src/headless-toolbar/helpers/paragraph.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,25 @@ export const createIndentIncreaseExecute =
144144
return createDirectCommandExecute('increaseTextIndent')({ context });
145145
};
146146

147+
const createListToggleExecute =
148+
(styleCommand: string, legacyCommand: string) =>
149+
({ context, payload }: { context: ToolbarContext | null; payload?: unknown }) => {
150+
const editor = resolveStateEditor(context);
151+
const commands = editor?.commands;
152+
if (typeof commands?.[styleCommand] === 'function') {
153+
const result = payload === undefined ? commands[styleCommand]() : commands[styleCommand](payload);
154+
return Boolean(result);
155+
}
156+
if (typeof commands?.[legacyCommand] === 'function') {
157+
return Boolean(commands[legacyCommand]());
158+
}
159+
return false;
160+
};
161+
162+
export const createBulletListExecute = () => createListToggleExecute('toggleBulletListStyle', 'toggleBulletList');
163+
164+
export const createOrderedListExecute = () => createListToggleExecute('toggleOrderedListStyle', 'toggleOrderedList');
165+
147166
export const createIndentDecreaseExecute =
148167
() =>
149168
({ context }: { context: ToolbarContext | null; payload?: unknown }) => {

packages/super-editor/src/headless-toolbar/toolbar-registry.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ import {
2828
createUnderlineExecute,
2929
} from './helpers/formatting.js';
3030
import {
31+
createBulletListExecute,
3132
createIndentDecreaseExecute,
3233
createIndentIncreaseExecute,
3334
createLineHeightStateDeriver,
3435
createLinkedStyleStateDeriver,
3536
createListStateDeriver,
37+
createOrderedListExecute,
3638
createTextAlignStateDeriver,
3739
} from './helpers/paragraph.js';
3840
import { createDirectCommandExecute, createDisabledStateDeriver } from './helpers/general.js';
@@ -121,11 +123,13 @@ export const createToolbarRegistry = (): Partial<Record<PublicToolbarItemId, Bui
121123
id: 'bullet-list',
122124
directCommandName: 'toggleBulletListStyle',
123125
state: createListStateDeriver('bullet'),
126+
execute: createBulletListExecute(),
124127
},
125128
'numbered-list': {
126129
id: 'numbered-list',
127130
directCommandName: 'toggleOrderedListStyle',
128131
state: createListStateDeriver('ordered'),
132+
execute: createOrderedListExecute(),
129133
},
130134
'indent-increase': {
131135
id: 'indent-increase',

0 commit comments

Comments
 (0)