optionally insert space before image

This commit is contained in:
hzrd149 2023-12-31 12:22:34 -06:00
parent c74475194d
commit ebe0d7451e

View File

@ -42,8 +42,22 @@ export default function useTextAreaUploadFile(
const content = getText();
const position = ref.current?.getCaretPosition();
if (position !== undefined) {
setText(content.slice(0, position) + imageUrl + " " + content.slice(position));
} else setText(content + imageUrl + " ");
let inject = imageUrl;
// add a space before
if (position >= 1 && content.slice(position - 1, position) !== " ") inject = " " + inject;
// add a space after
if (position < content.length && content.slice(position, position + 1) !== " ") inject = inject + " ";
setText(content.slice(0, position) + inject + content.slice(position));
} else {
let inject = imageUrl;
// add a space before if there isn't one
if (content.slice(content.length - 1) !== " ") inject = " " + inject;
setText(content + inject + " ");
}
} catch (e) {
if (e instanceof Error) toast({ description: e.message, status: "error" });
}