2023-08-15 09:34:46 +00:00
|
|
|
import { EditorProps } from "@tiptap/pm/view";
|
|
|
|
import { startImageUpload } from "./plugins/upload-image";
|
2023-08-31 08:11:41 +00:00
|
|
|
import { findTableAncestor } from "./table-menu";
|
2023-08-15 09:34:46 +00:00
|
|
|
|
2023-09-03 13:20:30 +00:00
|
|
|
export function TiptapEditorProps(
|
|
|
|
workspaceSlug: string,
|
|
|
|
setIsSubmitting?: (isSubmitting: "submitting" | "submitted" | "saved") => void
|
|
|
|
): EditorProps {
|
2023-08-19 13:28:54 +00:00
|
|
|
return {
|
|
|
|
attributes: {
|
|
|
|
class: `prose prose-brand max-w-full prose-headings:font-display font-default focus:outline-none`,
|
|
|
|
},
|
|
|
|
handleDOMEvents: {
|
|
|
|
keydown: (_view, event) => {
|
|
|
|
// prevent default event listeners from firing when slash command is active
|
|
|
|
if (["ArrowUp", "ArrowDown", "Enter"].includes(event.key)) {
|
|
|
|
const slashCommand = document.querySelector("#slash-command");
|
|
|
|
if (slashCommand) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-08-15 09:34:46 +00:00
|
|
|
}
|
2023-08-19 13:28:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
handlePaste: (view, event) => {
|
2023-08-31 08:11:41 +00:00
|
|
|
if (typeof window !== "undefined") {
|
|
|
|
const selection: any = window?.getSelection();
|
|
|
|
if (selection.rangeCount !== 0) {
|
|
|
|
const range = selection.getRangeAt(0);
|
|
|
|
if (findTableAncestor(range.startContainer)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-03 13:20:30 +00:00
|
|
|
if (event.clipboardData && event.clipboardData.files && event.clipboardData.files[0]) {
|
2023-08-19 13:28:54 +00:00
|
|
|
event.preventDefault();
|
|
|
|
const file = event.clipboardData.files[0];
|
|
|
|
const pos = view.state.selection.from;
|
|
|
|
startImageUpload(file, view, pos, workspaceSlug, setIsSubmitting);
|
|
|
|
return true;
|
2023-08-15 09:34:46 +00:00
|
|
|
}
|
2023-08-19 13:28:54 +00:00
|
|
|
return false;
|
2023-08-15 09:34:46 +00:00
|
|
|
},
|
2023-08-19 13:28:54 +00:00
|
|
|
handleDrop: (view, event, _slice, moved) => {
|
2023-08-31 08:11:41 +00:00
|
|
|
if (typeof window !== "undefined") {
|
|
|
|
const selection: any = window?.getSelection();
|
|
|
|
if (selection.rangeCount !== 0) {
|
|
|
|
const range = selection.getRangeAt(0);
|
|
|
|
if (findTableAncestor(range.startContainer)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-03 13:20:30 +00:00
|
|
|
if (!moved && event.dataTransfer && event.dataTransfer.files && event.dataTransfer.files[0]) {
|
2023-08-19 13:28:54 +00:00
|
|
|
event.preventDefault();
|
|
|
|
const file = event.dataTransfer.files[0];
|
|
|
|
const coordinates = view.posAtCoords({
|
|
|
|
left: event.clientX,
|
|
|
|
top: event.clientY,
|
|
|
|
});
|
|
|
|
// here we deduct 1 from the pos or else the image will create an extra node
|
|
|
|
if (coordinates) {
|
|
|
|
startImageUpload(file, view, coordinates.pos - 1, workspaceSlug, setIsSubmitting);
|
|
|
|
}
|
|
|
|
return true;
|
2023-08-15 09:34:46 +00:00
|
|
|
}
|
2023-08-19 13:28:54 +00:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|