Skip to content

Commit

Permalink
feat(plugin-attachment): add file pasting functionality (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
BearToCode authored Apr 12, 2024
2 parents b7fecea + bed8ba5 commit 8f41889
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion packages/plugin-attachment/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ export const attachment = (options: AttachmentExtensionOptions): CartaExtension
for (const file of files) handleFile(file);
}

function handlePaste(this: HTMLTextAreaElement, e: ClipboardEvent) {
const items = e.clipboardData?.items;
if (!items) return;

const itemsArray = Array.from(items);
for (const item of itemsArray) {
if (item.kind === 'file') {
const file = item.getAsFile();
if (!file) continue;
e.preventDefault();
handleFile(file);
}
}
}

return {
onLoad: ({ carta: c }) => {
carta = c;
Expand All @@ -110,7 +125,8 @@ export const attachment = (options: AttachmentExtensionOptions): CartaExtension
['drop', handleDrop, false] satisfies CartaListener<'drop'>,
['dragenter', () => draggingOverTextArea.set(true)] satisfies CartaListener<'dragenter'>,
['dragleave', () => draggingOverTextArea.set(false)] satisfies CartaListener<'dragleave'>,
['dragover', (e) => e.preventDefault()] satisfies CartaListener<'dragover'>
['dragover', (e) => e.preventDefault()] satisfies CartaListener<'dragover'>,
['paste', handlePaste, false] satisfies CartaListener<'paste'>
],
components: [
{
Expand Down

0 comments on commit 8f41889

Please sign in to comment.