28 lines
884 B
TypeScript
28 lines
884 B
TypeScript
export function handleImagePaste(event: ClipboardEvent, onImage: (markdown: string) => void): void {
|
|
const items = event.clipboardData?.items;
|
|
if (!items) return;
|
|
for (const item of Array.from(items)) {
|
|
if (item.type.startsWith('image/')) {
|
|
const file = item.getAsFile();
|
|
if (!file) continue;
|
|
event.preventDefault();
|
|
const reader = new FileReader();
|
|
reader.onload = (e) => onImage(``);
|
|
reader.readAsDataURL(file);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
export function insertAtSelection(
|
|
textarea: HTMLTextAreaElement,
|
|
currentValue: string,
|
|
start: number,
|
|
end: number,
|
|
insertion: string,
|
|
): string {
|
|
const next = currentValue.slice(0, start) + insertion + currentValue.slice(end);
|
|
setTimeout(() => { textarea.selectionStart = textarea.selectionEnd = start + insertion.length; });
|
|
return next;
|
|
}
|