Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Implement file drop handlers in editor

Hakan Shehuc5f6b6741037
diff --git a/apps/desktop/src/renderer/editor/extensions/file-placeholder.tsx b/apps/desktop/src/renderer/editor/extensions/file-placeholder.tsx
index 9b058f67..663af3db 100644
--- a/apps/desktop/src/renderer/editor/extensions/file-placeholder.tsx
+++ b/apps/desktop/src/renderer/editor/extensions/file-placeholder.tsx
@@ -56,10 +56,12 @@ export const FilePlaceholderNode = Node.create<FilePlaceholderOptions>({
return {
addFilePlaceholder:
(metadata: FileMetadata) =>
- ({ chain }: CommandProps) => {
- return chain()
+ ({ editor, tr }: CommandProps) => {
+ const pos = tr.selection.$head.pos;
+ editor
+ .chain()
.focus()
- .insertContent({
+ .insertContentAt(pos, {
type: 'filePlaceholder',
attrs: {
id: generateId(IdType.FilePlaceholder),
@@ -71,6 +73,8 @@ export const FilePlaceholderNode = Node.create<FilePlaceholderOptions>({
},
})
.run();
+
+ return true;
},
};
},
@@ -124,7 +128,63 @@ export const FilePlaceholderNode = Node.create<FilePlaceholderOptions>({
return;
}
- editor.chain().focus().addFilePlaceholder(fileMetadata).run();
+ editor.commands.addFilePlaceholder(fileMetadata);
+ }
+ })();
+
+ return true;
+ },
+ },
+ }),
+ new Plugin({
+ key: new PluginKey('file-placeholder-drop'),
+ props: {
+ handleDrop(_, event) {
+ const files = Array.from(event.dataTransfer?.files || []);
+ if (files.length == 0) {
+ return false;
+ }
+
+ console.log('files dropped', files);
+
+ (async () => {
+ for (const file of files) {
+ const buffer = await file.arrayBuffer();
+ const fileSaveResult = await window.colanode.executeMutation({
+ type: 'file_save_temp',
+ name: file.name,
+ buffer,
+ userId: options.userId,
+ });
+
+ if (!fileSaveResult.success) {
+ toast({
+ variant: 'destructive',
+ title: 'Failed to add file',
+ description: fileSaveResult.error.message,
+ });
+
+ return;
+ }
+
+ const path = fileSaveResult.output.path;
+ const fileMetadata = await window.colanode.executeQuery({
+ type: 'file_metadata_get',
+ path: path,
+ });
+
+ if (fileMetadata === null) {
+ toast({
+ title: 'Failed to add file',
+ description:
+ 'Something went wrong adding file. Please try again!',
+ variant: 'destructive',
+ });
+
+ return;
+ }
+
+ editor.commands.addFilePlaceholder(fileMetadata);
}
})();
diff --git a/apps/desktop/src/renderer/editor/extensions/file.tsx b/apps/desktop/src/renderer/editor/extensions/file.tsx
index 4a84275b..dc3fa49e 100644
--- a/apps/desktop/src/renderer/editor/extensions/file.tsx
+++ b/apps/desktop/src/renderer/editor/extensions/file.tsx
@@ -67,35 +67,17 @@ export const FileNode = Node.create<FileNodeOptions>({
}
const fileId = fileCreateResult.output.id;
- const range = tr.selection.ranges[0];
-
- if (range) {
- editor
- .chain()
- .focus()
- .deleteRange({
- from: range.$from.pos,
- to: range.$to.pos,
- })
- .insertContent({
- type: 'file',
- attrs: {
- id: fileId,
- },
- })
- .run();
- } else {
- editor
- .chain()
- .focus()
- .insertContent({
- type: 'file',
- attrs: {
- id: fileId,
- },
- })
- .run();
- }
+ const pos = tr.selection.$head.pos;
+ editor
+ .chain()
+ .focus()
+ .insertContentAt(pos, {
+ type: 'file',
+ attrs: {
+ id: fileId,
+ },
+ })
+ .run();
})();
return true;
@@ -142,7 +124,45 @@ export const FileNode = Node.create<FileNodeOptions>({
}
const path = fileSaveResult.output.path;
- editor.chain().focus().addFile(path).run();
+ editor.commands.addFile(path);
+ }
+ })();
+
+ return true;
+ },
+ },
+ }),
+ new Plugin({
+ key: new PluginKey('file-drop'),
+ props: {
+ handleDrop(_, event) {
+ const files = Array.from(event.dataTransfer?.files || []);
+ if (files.length == 0) {
+ return false;
+ }
+
+ (async () => {
+ for (const file of files) {
+ const buffer = await file.arrayBuffer();
+ const fileSaveResult = await window.colanode.executeMutation({
+ type: 'file_save_temp',
+ name: file.name,
+ buffer,
+ userId: options.context.userId,
+ });
+
+ if (!fileSaveResult.success) {
+ toast({
+ variant: 'destructive',
+ title: 'Failed to add file',
+ description: fileSaveResult.error.message,
+ });
+
+ return;
+ }
+
+ const path = fileSaveResult.output.path;
+ editor.commands.addFile(path);
}
})();