Skip to content

feat: add slash command to insert remirror table #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions ui/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,30 @@
.remirror-editor.ProseMirror {
overflow-y: hidden !important;
}


.mention {
background: #7963d266;
padding: 2px 4px;
border-radius: 4px;
}

.remirror-theme {
/* Provide sufficient space to see the popup */
--rmr-space-6: 400px;
}
.suggestions {
border: 1px solid darkgray;
border-radius: 4px;
background: white;
cursor: pointer;
}
.suggestion {
padding: 2px 8px;
}
.highlighted {
background: #7963d233;
}
.hovered {
background: #7963d222;
}
107 changes: 105 additions & 2 deletions ui/src/components/nodes/Rich.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ import {
ImageExtension,
ItalicExtension,
LinkExtension,
MentionExtension,
MentionExtensionAttributes,
PlaceholderExtension,
ShortcutHandlerProps,
SubExtension,
Expand Down Expand Up @@ -95,14 +97,15 @@ import {
useAttrs,
useUpdateReason,
FloatingWrapper,
useMention,
} from "@remirror/react";
import { WysiwygEditor } from "@remirror/react-editors/wysiwyg";
import { FloatingToolbar, useExtensionEvent } from "@remirror/react";
import { TableExtension } from "@remirror/extension-react-tables";
import { GenIcon, IconBase } from "@remirror/react-components";
import "remirror/styles/all.css";

import { ProsemirrorPlugin, htmlToProsemirrorNode } from "remirror";
import { ProsemirrorPlugin, cx, htmlToProsemirrorNode } from "remirror";
import { styled } from "@mui/material";

import { MyYjsExtension } from "./YjsRemirror";
Expand Down Expand Up @@ -371,6 +374,92 @@ export const SetHighlightButton: React.FC<
);
};

/**
* Build a slash command using Remirror's MentionExtension.
*/

function UserSuggestor({
allUsers,
}: {
allUsers: MentionExtensionAttributes[];
}): JSX.Element {
const [users, setUsers] = useState<MentionExtensionAttributes[]>([]);
const { createTable } = useCommands();
const { state, getMenuProps, getItemProps, indexIsHovered, indexIsSelected } =
useMention({
items: users,

onExit: (props, command) => {
console.log("props", props);
// console.log("command", command);
// get the command
const { query } = props;
const { full } = query;
switch (full) {
case "table":
// insert table
console.log("Inserting table ..");
createTable({
rowsCount: 3,
columnsCount: 3,
withHeaderRow: false,
});
break;
default:
break;
}
},
});

useEffect(() => {
if (!state) {
return;
}

const searchTerm = state.query.full.toLowerCase();
const filteredUsers = allUsers
.filter((user) => user.label.toLowerCase().includes(searchTerm))
.sort()
.slice(0, 5);
setUsers(filteredUsers);
}, [state, allUsers]);

const enabled = !!state;

return (
<FloatingWrapper
positioner="cursor"
enabled={enabled}
placement="bottom-start"
>
<div {...getMenuProps()} className="suggestions">
{enabled &&
users.map((user, index) => {
const isHighlighted = indexIsSelected(index);
const isHovered = indexIsHovered(index);

return (
<div
key={user.id}
className={cx(
"suggestion",
isHighlighted && "highlighted",
isHovered && "hovered"
)}
{...getItemProps({
item: user,
index,
})}
>
{user.label}
</div>
);
})}
</div>
</FloatingWrapper>
);
}

const MyStyledWrapper = styled("div")(
() => `
.remirror-editor-wrapper {
Expand Down Expand Up @@ -421,6 +510,12 @@ const MyEditor = ({
new ImageExtension({ enableResizing: true }),
new DropCursorExtension(),
new MyYjsExtension({ getProvider: () => provider, id }),
new MentionExtension({
extraAttributes: { type: "user" },
matchers: [
{ name: "slash", char: "/", appendText: " ", matchOffset: 0 },
],
}),
// new CalloutExtension({ defaultType: "warn" }),
...wysiwygPreset(),
],
Expand Down Expand Up @@ -478,6 +573,15 @@ const MyEditor = ({
<EditorComponent />

<TableComponents />
<UserSuggestor
allUsers={[
{ id: "table", label: "table" },
// { id: "sue", label: "Sue" },
// { id: "pat", label: "Pat" },
// { id: "tom", label: "Tom" },
// { id: "jim", label: "Jim" },
]}
/>

{!isGuest && (
<FloatingLinkToolbar>
Expand All @@ -493,7 +597,6 @@ const MyEditor = ({
<SetHighlightButton color="lightcyan" />
<SetHighlightButton />
</CommandButtonGroup>
<VerticalDivider />
{/* <FormattingButtonGroup /> */}
{/* <DecreaseIndentButton /> */}
{/* <IncreaseIndentButton /> */}
Expand Down