forked from motla/vue-document-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseImageUpload.ts
48 lines (39 loc) · 1.58 KB
/
useImageUpload.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import {insertHtmlAtCursor} from "@/utils/insertHTMLStCursor";
interface UseImageUploadComposable {
addImage: () => void;
}
export default (): UseImageUploadComposable => {
// Create a hidden file input element
const addImage = () => {
let fileInput = document.getElementById('fileInput') as HTMLInputElement;
if (!fileInput) {
fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = 'image/*';
fileInput.id = 'fileInput';
fileInput.style.display = 'none';
document.body.appendChild(fileInput);
}
// Trigger the click event on the file input element
fileInput.click();
// When file input changes (user has selected a file),
// read the image file and convert to a base64 string
fileInput.onchange = (event: Event) => {
const file = (event.target as HTMLInputElement).files?.[0]; // Get the selected file
const reader = new FileReader();
reader.onloadend = () => {
const base64String = reader.result as string;
// Assume index is the location where the image tag is to be added
let index = 0;
// Check if there is an element at this index
insertHtmlAtCursor(`<img class="resizable-image" src="${base64String}" alt="User image" width="200px" height="200px">`);
};
if (file) {
reader.readAsDataURL(file);
}
}
}
return {
addImage,
};
}