mirror of
https://github.com/docmost/docmost.git
synced 2025-11-19 06:01:10 +10:00
72 lines
1.6 KiB
TypeScript
72 lines
1.6 KiB
TypeScript
import { ReactRenderer, useEditor } from "@tiptap/react";
|
|
import CommandList from "@/features/editor/components/slash-menu/command-list";
|
|
import tippy from "tippy.js";
|
|
|
|
const renderItems = () => {
|
|
let component: ReactRenderer | null = null;
|
|
let popup: any | null = null;
|
|
|
|
return {
|
|
onStart: (props: {
|
|
editor: ReturnType<typeof useEditor>;
|
|
clientRect: DOMRect;
|
|
}) => {
|
|
component = new ReactRenderer(CommandList, {
|
|
props,
|
|
editor: props.editor,
|
|
});
|
|
|
|
if (!props.clientRect) {
|
|
return;
|
|
}
|
|
|
|
// @ts-ignore
|
|
popup = tippy("body", {
|
|
getReferenceClientRect: props.clientRect,
|
|
appendTo: () => document.body,
|
|
content: component.element,
|
|
showOnCreate: true,
|
|
interactive: true,
|
|
trigger: "manual",
|
|
placement: "bottom-start",
|
|
});
|
|
},
|
|
onUpdate: (props: {
|
|
editor: ReturnType<typeof useEditor>;
|
|
clientRect: DOMRect;
|
|
}) => {
|
|
component?.updateProps(props);
|
|
|
|
if (!props.clientRect) {
|
|
return;
|
|
}
|
|
|
|
popup &&
|
|
popup[0].setProps({
|
|
getReferenceClientRect: props.clientRect,
|
|
});
|
|
},
|
|
onKeyDown: (props: { event: KeyboardEvent }) => {
|
|
if (props.event.key === "Escape") {
|
|
popup?.[0].hide();
|
|
|
|
return true;
|
|
}
|
|
|
|
// @ts-ignore
|
|
return component?.ref?.onKeyDown(props);
|
|
},
|
|
onExit: () => {
|
|
if (popup && !popup[0].state.isDestroyed) {
|
|
popup[0].destroy();
|
|
}
|
|
|
|
if (component) {
|
|
component.destroy();
|
|
}
|
|
},
|
|
};
|
|
};
|
|
|
|
export default renderItems;
|