Skip to content

Commit

Permalink
style: format code
Browse files Browse the repository at this point in the history
  • Loading branch information
BearToCode committed Sep 21, 2024
1 parent 780b1d9 commit 5789977
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
35 changes: 20 additions & 15 deletions packages/plugin-attachment/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ export interface AttachmentExtensionOptions {
loadingOverlay?: false | typeof SvelteComponent<{ uploadingFiles: Writable<File[]> }>;
}

const ImageMimeTypes = ['image/png', 'image/jpeg', 'image/gif', 'image/svg+xml', 'image/webp', 'image/avif'];
const ImageMimeTypes = [
'image/png',
'image/jpeg',
'image/gif',
'image/svg+xml',
'image/webp',
'image/avif'
];

/**
* Carta attachment plugin.
Expand Down Expand Up @@ -64,27 +71,27 @@ export const attachment = (options: AttachmentExtensionOptions): Plugin => {
async function handleFile(file: File) {
if (!allowedMimeTypes.includes(file.type)) return;
if (!carta?.input) return;
const input = carta.input
const input = carta.input;
const textarea = input.textarea;

let pos = input.textarea.selectionStart;
const loadingStr = `[Uploading ${file.name}](loading)`;
const isImage = ImageMimeTypes.includes(file.type)
const isImage = ImageMimeTypes.includes(file.type);

if (isImage) {
// assume images are being inserted as blocks
const line = carta.input.getLine();
pos = line.end
pos = line.end;
if (line.value) {
input.insertAt(pos, '\n\n')
pos +=2
input.insertAt(pos, '\n\n');
pos += 2;
}
input.insertAt(pos, loadingStr + '\n')
input.insertAt(pos, loadingStr + '\n');
pos += loadingStr.length + 1;
} else {
// non image attachments are inline (could make multiple into comma separated list or bullets)
carta.input.insertAt(pos, loadingStr + ' ');
pos += loadingStr.length + 1
pos += loadingStr.length + 1;
}

carta.input.update();
Expand All @@ -103,29 +110,27 @@ export const attachment = (options: AttachmentExtensionOptions): Plugin => {

if (!path) return;

const str = isImage
? `![${file.name}](${path})`
: `[${file.name}](${path})`;
const str = isImage ? `![${file.name}](${path})` : `[${file.name}](${path})`;

carta.input.insertAt(loadingStrIndex, str);
carta.input.update();

// update cursor position to account for the string replacement
if (input.textarea.selectionStart < loadingStrIndex) {
// caret is before the loading string, no change required
pos = input.textarea.selectionStart
pos = input.textarea.selectionStart;
} else if (input.textarea.selectionStart >= loadingStrIndex + str.length) {
// caret is after the loading string, adjust position by the difference
pos = input.textarea.selectionStart - loadingStr.length + str.length
pos = input.textarea.selectionStart - loadingStr.length + str.length;
} else if (input.textarea.selectionStart >= loadingStrIndex) {
// caret is within the loading string, position it just after
pos = loadingStrIndex + str.length + 1
pos = loadingStrIndex + str.length + 1;
}
textarea.setSelectionRange(pos, pos);

carta.input.history.saveState(textarea.value, textarea.selectionStart);

return
return;
}

function handleDrop(this: HTMLTextAreaElement, e: DragEvent) {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-attachment/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
attachment({
async upload(file) {
await new Promise((resolve) => setTimeout(resolve, 1_000));
return URL.createObjectURL(file)
return URL.createObjectURL(file);
}
})
]
Expand Down

0 comments on commit 5789977

Please sign in to comment.