Skip to content

Commit bf43b84

Browse files
authored
fix: filter out invalid heading items based on the current block schema in the slash menu #2253 (#2259)
1 parent 316bfdf commit bf43b84

File tree

1 file changed

+40
-68
lines changed

1 file changed

+40
-68
lines changed

packages/core/src/extensions/SuggestionMenu/getDefaultSlashMenuItems.ts

Lines changed: 40 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -89,41 +89,24 @@ export function getDefaultSlashMenuItems<
8989
const items: DefaultSuggestionItem[] = [];
9090

9191
if (editorHasBlockWithType(editor, "heading", { level: "number" })) {
92-
items.push(
93-
{
94-
onItemClick: () => {
95-
insertOrUpdateBlockForSlashMenu(editor, {
96-
type: "heading",
97-
props: { level: 1 },
98-
});
99-
},
100-
badge: formatKeyboardShortcut("Mod-Alt-1"),
101-
key: "heading",
102-
...editor.dictionary.slash_menu.heading,
103-
},
104-
{
105-
onItemClick: () => {
106-
insertOrUpdateBlockForSlashMenu(editor, {
107-
type: "heading",
108-
props: { level: 2 },
109-
});
110-
},
111-
badge: formatKeyboardShortcut("Mod-Alt-2"),
112-
key: "heading_2",
113-
...editor.dictionary.slash_menu.heading_2,
114-
},
115-
{
116-
onItemClick: () => {
117-
insertOrUpdateBlockForSlashMenu(editor, {
118-
type: "heading",
119-
props: { level: 3 },
120-
});
121-
},
122-
badge: formatKeyboardShortcut("Mod-Alt-3"),
123-
key: "heading_3",
124-
...editor.dictionary.slash_menu.heading_3,
125-
},
126-
);
92+
(editor.schema.blockSchema.heading.propSchema.level.values || [])
93+
.filter((level): level is 1 | 2 | 3 => level <= 3)
94+
.forEach((level) => {
95+
items.push({
96+
onItemClick: () => {
97+
insertOrUpdateBlockForSlashMenu(editor, {
98+
type: "heading",
99+
props: { level: level },
100+
});
101+
},
102+
badge: formatKeyboardShortcut(`Mod-Alt-${level}`),
103+
key:
104+
level === 1 ? ("heading" as const) : (`heading_${level}` as const),
105+
...editor.dictionary.slash_menu[
106+
level === 1 ? ("heading" as const) : (`heading_${level}` as const)
107+
],
108+
});
109+
});
127110
}
128111

129112
if (editorHasBlockWithType(editor, "quote")) {
@@ -316,39 +299,27 @@ export function getDefaultSlashMenuItems<
316299
isToggleable: "boolean",
317300
})
318301
) {
319-
items.push(
320-
{
321-
onItemClick: () => {
322-
insertOrUpdateBlockForSlashMenu(editor, {
323-
type: "heading",
324-
props: { level: 1, isToggleable: true },
325-
});
326-
},
327-
key: "toggle_heading",
328-
...editor.dictionary.slash_menu.toggle_heading,
329-
},
330-
{
331-
onItemClick: () => {
332-
insertOrUpdateBlockForSlashMenu(editor, {
333-
type: "heading",
334-
props: { level: 2, isToggleable: true },
335-
});
336-
},
337-
338-
key: "toggle_heading_2",
339-
...editor.dictionary.slash_menu.toggle_heading_2,
340-
},
341-
{
342-
onItemClick: () => {
343-
insertOrUpdateBlockForSlashMenu(editor, {
344-
type: "heading",
345-
props: { level: 3, isToggleable: true },
346-
});
347-
},
348-
key: "toggle_heading_3",
349-
...editor.dictionary.slash_menu.toggle_heading_3,
350-
},
351-
);
302+
(editor.schema.blockSchema.heading.propSchema.level.values || [])
303+
.filter((level): level is 1 | 2 | 3 => level <= 3)
304+
.forEach((level) => {
305+
items.push({
306+
onItemClick: () => {
307+
insertOrUpdateBlockForSlashMenu(editor, {
308+
type: "heading",
309+
props: { level: level, isToggleable: true },
310+
});
311+
},
312+
key:
313+
level === 1
314+
? ("toggle_heading" as const)
315+
: (`toggle_heading_${level}` as const),
316+
...editor.dictionary.slash_menu[
317+
level === 1
318+
? ("toggle_heading" as const)
319+
: (`toggle_heading_${level}` as const)
320+
],
321+
});
322+
});
352323
}
353324

354325
if (editorHasBlockWithType(editor, "heading", { level: "number" })) {
@@ -362,6 +333,7 @@ export function getDefaultSlashMenuItems<
362333
props: { level: level },
363334
});
364335
},
336+
badge: formatKeyboardShortcut(`Mod-Alt-${level}`),
365337
key: `heading_${level}`,
366338
...editor.dictionary.slash_menu[`heading_${level}`],
367339
});

0 commit comments

Comments
 (0)