Skip to content

Commit fdce818

Browse files
Suggestion plugin refactor (#94)
* upgrade to react 18 * Maybe fixed React error in build * Added early implementation of bubble menu & editor in separate react package * Moved all hyperlink menus to react package * Major structural overhaul to menu factories * Added suggestions menu factory with new (hopefully final) factory structure * Refactored bubble menu code to use improved factory structure * Changes to bubble menu code and documentation * Changes to bubble menu code and documentation * General improvements, mostly to bubble and hyperlink menus * comments after call * Split bubble menu props into init & update props Moved animation delay timeout to bubble menu plugin * clean up packaging * fix react error * Changed bubble menu plugin view back to use a separate class * Split hyperlink menu props into init & update props * Changed hyperlink menu plugin view to use a separate class * fix gitignore * fix tsconfig * Major cleanup for bubble menu factory code * Major cleanup for hyperlink menu factory code * Minor fixes * Converted suggestion plugin view to use a class * Changed editor example to use React as before * Improved file structure and minor tweaks * Minor refactor * Fixed hyperlink menu behaviour * Separated drag handle UI elements to react package * Separated drag handle UI elements to react package * Minor improvements * fix lint * refactor entrypoints (#77) * refactor entrypoints * fix packages * fix build * move some more files / remove root.module.css * Fixed hyperlink edit menu bugs * re-add font * Removed bubble menu animation update delay hack * Changed bubble menu placement to be at the start of the selection bounding box * Fixed hyperlink menu naming inconsistencies * Refactored block/drag menu to use one factory and separate view class * Fixed bubble menu sometimes attempting to hide when already hidden * Fixed drag preview styling * Fixed tooltip styling * Removed unnecessary updates from block menu * Vanilla example (#78) * add vanilla example * add bold toggle * add other factories to vanilla example * add dragging * Updated naming for bubble, block, and hyperlink menus * Fixed drag handle tests * Fixed drag and drop tests * Improved how UI element factories are passed to extensions * Updated suggestions/slash menu naming * Updated slash menu selector for tests * Split factory params into static and dynamic * Minor fix to `BNSetContentType()` * Improved getting & setting block type in formatting toolbar factory API * Minor typing fix * Suggestions plugin refactor/rewrite * Combined factory `update()` and `show()` functions into single `render()` function * Changed item callback to delete both trigger character and query * Minor fix * rename props -> staticParams in vanilla example * Changed suggestions menu behaviour when typing multiple "/" characters * Minor change * Fixed build error and placeholder when adding block using side menu --------- Co-authored-by: yousefed <[email protected]>
1 parent e35f026 commit fdce818

File tree

10 files changed

+239
-314
lines changed

10 files changed

+239
-314
lines changed

examples/vanilla/src/ui/slashMenuFactory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const slashMenuFactory: SuggestionsMenuFactory<SlashMenuItem> = (
4242
updateItems(
4343
params.items,
4444
staticParams.itemCallback,
45-
params.selectedItemIndex
45+
params.keyboardHoveredItemIndex
4646
);
4747

4848
if (isHidden) {

packages/core/src/extensions/Placeholder/PlaceholderExtension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export const Placeholder = Extension.create<PlaceholderOptions>({
8181
}
8282

8383
// If slash menu is of drag type and active, show the filter placeholder
84-
if (menuState?.type === "drag" && menuState?.active) {
84+
if (menuState?.triggerCharacter === "" && menuState?.active) {
8585
classes.push(this.options.isFilterClass);
8686
}
8787
// using widget, didn't work (caret position bug)

packages/core/src/extensions/SlashMenu/SlashMenuExtension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const SlashMenuExtension = Extension.create<SlashMenuOptions>({
3131
createSuggestionPlugin<SlashMenuItem>({
3232
pluginKey: SlashMenuPluginKey,
3333
editor: this.editor,
34-
char: "/",
34+
defaultTriggerCharacter: "/",
3535
suggestionsMenuFactory: this.options.slashMenuFactory!,
3636
items: (query) => {
3737
const commands = [];

packages/core/src/extensions/SlashMenu/SlashMenuItem.ts

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,24 @@
11
import { Editor, Range } from "@tiptap/core";
22
import { SuggestionItem } from "../../shared/plugins/suggestion/SuggestionItem";
33

4-
export type SlashMenuCallback = (editor: Editor, range: Range) => boolean;
5-
6-
export enum SlashMenuGroups {
7-
HEADINGS = "Headings",
8-
BASIC_BLOCKS = "Basic Blocks",
9-
CODE = "Code Blocks",
10-
11-
// Just some examples, that are not currently in use
12-
INLINE = "Inline",
13-
EMBED = "Embed",
14-
PLUGIN = "Plugin",
15-
}
16-
174
/**
185
* A class that defines a slash command (/<command>).
196
*
207
* Not to be confused with ProseMirror commands nor TipTap commands.
218
*/
229
export class SlashMenuItem implements SuggestionItem {
23-
groupName: string;
24-
// other parameters initialized in the constructor
25-
2610
/**
2711
* Constructs a new slash-command.
2812
*
2913
* @param name The name of the command
30-
* @param group Used to organize the menu
3114
* @param execute The callback for creating a new node
3215
* @param aliases Aliases for this command
33-
* @param icon To be shown next to the name in the menu
34-
* @param hint Short description of command
35-
* @param shortcut Info about keyboard shortcut that would activate this command
3616
*/
3717
constructor(
3818
public readonly name: string,
39-
public readonly group: SlashMenuGroups,
40-
public readonly execute: SlashMenuCallback,
41-
public readonly aliases: string[] = [],
42-
public readonly hint?: string,
43-
public readonly shortcut?: string
44-
) {
45-
this.groupName = group;
46-
}
19+
public readonly execute: (editor: Editor, range: Range) => void,
20+
public readonly aliases: string[] = []
21+
) {}
4722

4823
match(query: string): boolean {
4924
return (

packages/core/src/extensions/SlashMenu/defaultCommands.tsx

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { formatKeyboardShortcut } from "../../shared/utils";
2-
import { SlashMenuGroups, SlashMenuItem } from "./SlashMenuItem";
1+
import { SlashMenuItem } from "./SlashMenuItem";
2+
import { Editor, Range } from "@tiptap/core";
33

44
/**
55
* An array containing commands for creating all default blocks.
@@ -8,8 +8,7 @@ const defaultCommands: { [key: string]: SlashMenuItem } = {
88
// Command for creating a level 1 heading
99
heading: new SlashMenuItem(
1010
"Heading",
11-
SlashMenuGroups.HEADINGS,
12-
(editor, range) => {
11+
(editor: Editor, range: Range) => {
1312
return editor
1413
.chain()
1514
.focus()
@@ -22,16 +21,13 @@ const defaultCommands: { [key: string]: SlashMenuItem } = {
2221
})
2322
.run();
2423
},
25-
["h", "heading1", "h1"],
26-
"Used for a top-level heading",
27-
formatKeyboardShortcut("Mod-Alt-1")
24+
["h", "heading1", "h1"]
2825
),
2926

3027
// Command for creating a level 2 heading
3128
heading2: new SlashMenuItem(
3229
"Heading 2",
33-
SlashMenuGroups.HEADINGS,
34-
(editor, range) => {
30+
(editor: Editor, range: Range) => {
3531
return editor
3632
.chain()
3733
.focus()
@@ -44,16 +40,13 @@ const defaultCommands: { [key: string]: SlashMenuItem } = {
4440
})
4541
.run();
4642
},
47-
["h2", "heading2", "subheading"],
48-
"Used for key sections",
49-
formatKeyboardShortcut("Mod-Alt-2")
43+
["h2", "heading2", "subheading"]
5044
),
5145

5246
// Command for creating a level 3 heading
5347
heading3: new SlashMenuItem(
5448
"Heading 3",
55-
SlashMenuGroups.HEADINGS,
56-
(editor, range) => {
49+
(editor: Editor, range: Range) => {
5750
return editor
5851
.chain()
5952
.focus()
@@ -66,16 +59,13 @@ const defaultCommands: { [key: string]: SlashMenuItem } = {
6659
})
6760
.run();
6861
},
69-
["h3", "heading3", "subheading"],
70-
"Used for subsections and group headings",
71-
formatKeyboardShortcut("Mod-Alt-3")
62+
["h3", "heading3", "subheading"]
7263
),
7364

7465
// Command for creating an ordered list
7566
numberedList: new SlashMenuItem(
7667
"Numbered List",
77-
SlashMenuGroups.BASIC_BLOCKS,
78-
(editor, range) => {
68+
(editor: Editor, range: Range) => {
7969
return editor
8070
.chain()
8171
.focus()
@@ -86,16 +76,13 @@ const defaultCommands: { [key: string]: SlashMenuItem } = {
8676
})
8777
.run();
8878
},
89-
["li", "list", "numberedlist", "numbered list"],
90-
"Used to display a numbered list",
91-
formatKeyboardShortcut("Mod-Shift-7")
79+
["li", "list", "numberedlist", "numbered list"]
9280
),
9381

9482
// Command for creating a bullet list
9583
bulletList: new SlashMenuItem(
9684
"Bullet List",
97-
SlashMenuGroups.BASIC_BLOCKS,
98-
(editor, range) => {
85+
(editor: Editor, range: Range) => {
9986
return editor
10087
.chain()
10188
.focus()
@@ -106,16 +93,13 @@ const defaultCommands: { [key: string]: SlashMenuItem } = {
10693
})
10794
.run();
10895
},
109-
["ul", "list", "bulletlist", "bullet list"],
110-
"Used to display an unordered list",
111-
formatKeyboardShortcut("Mod-Shift-8")
96+
["ul", "list", "bulletlist", "bullet list"]
11297
),
11398

11499
// Command for creating a paragraph (pretty useless)
115100
paragraph: new SlashMenuItem(
116101
"Paragraph",
117-
SlashMenuGroups.BASIC_BLOCKS,
118-
(editor, range) => {
102+
(editor: Editor, range: Range) => {
119103
return editor
120104
.chain()
121105
.focus()
@@ -126,9 +110,7 @@ const defaultCommands: { [key: string]: SlashMenuItem } = {
126110
})
127111
.run();
128112
},
129-
["p"],
130-
"Used for the body of your document",
131-
formatKeyboardShortcut("Mod-Alt-0")
113+
["p"]
132114
),
133115

134116
// replaceRangeWithNode(editor, range, node);
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import { SlashMenuExtension } from "./SlashMenuExtension";
22
import defaultCommands from "./defaultCommands";
3-
import { SlashMenuGroups, SlashMenuItem } from "./SlashMenuItem";
43

5-
export {
6-
defaultCommands,
7-
SlashMenuItem as SlashCommand,
8-
SlashMenuGroups as CommandGroup,
9-
};
4+
export { defaultCommands };
105

116
export default SlashMenuExtension;

packages/core/src/shared/plugins/suggestion/SuggestionItem.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@ export interface SuggestionItem {
77
*/
88
name: string;
99

10-
/**
11-
* The name of the group to which this item belongs
12-
*/
13-
groupName: string;
14-
15-
hint?: string;
16-
17-
shortcut?: string;
18-
1910
/**
2011
* This function matches this item against a query string, the function should return **true** if the item
2112
* matches the query or **false** otherwise.

0 commit comments

Comments
 (0)