Skip to content

Commit

Permalink
Tiptap: refactor to allow image upload from button
Browse files Browse the repository at this point in the history
Work in Progress
  • Loading branch information
yaaax committed Nov 20, 2024
1 parent cd147ed commit 627f805
Show file tree
Hide file tree
Showing 4 changed files with 302 additions and 205 deletions.
4 changes: 2 additions & 2 deletions confiture-web-app/src/components/ui/FileUpload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { computed, Ref, ref } from "vue";
import { useIsOffline } from "../../composables/useIsOffline";
import { useUniqueId } from "../../composables/useUniqueId";
import { FileErrorMessage } from "../../enums";
import { FileErrorMessage, Limitations } from "../../enums";
import { AuditFile } from "../../types";
import { formatBytes, getUploadUrl } from "../../utils";
Expand Down Expand Up @@ -71,7 +71,7 @@ const acceptedFormatsAttr = computed(() => {
function handleFileChange() {
if (fileInputRef.value?.files && fileInputRef.value?.files[0]) {
const file = fileInputRef.value?.files[0];
if (file.size > 2000000) {
if (file.size > Limitations.FILE_SIZE) {
localErrorMessage.value = FileErrorMessage.UPLOAD_SIZE;
return;
}
Expand Down
38 changes: 36 additions & 2 deletions confiture-web-app/src/components/ui/Tiptap.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ import html from "highlight.js/lib/languages/xml";
// load common languages
import { common, createLowlight } from "lowlight";
import { Markdown } from "tiptap-markdown";
import { computed, ShallowRef } from "vue";
import { computed, onMounted, ShallowRef } from "vue";
import { useRoute } from "vue-router";
import { useNotifications } from "../../composables/useNotifications";
import { ImageUploadTiptapExtension } from "../../tiptap/ImageUploadTiptapExtension";
import {
ImageUploadTiptapExtension,
insertFilesAtSelection
} from "../../tiptap/ImageUploadTiptapExtension";
// create a lowlight instance
const lowlight = createLowlight(common);
Expand Down Expand Up @@ -105,6 +108,27 @@ const editor = useEditor({
emit("update:content", JSON.stringify(editor.getJSON()));
}
}) as ShallowRef<Editor>;
onMounted(() => {
const browseButton = document.getElementById("tiptap-browse-btn")!;
const browseInput = document.getElementById("tiptap-browse-input")!;
browseButton.addEventListener(
"click",
() => {
browseInput.click();
},
false
);
browseInput.addEventListener(
"change",
(e) => {
const inputElement = e?.target as HTMLInputElement;
const files = inputElement.files!;
insertFilesAtSelection(uniqueId.value, editor.value, files);
},
false
);
});
</script>

<template>
Expand All @@ -114,5 +138,15 @@ const editor = useEditor({
utiliser les raccourcis clavier.
</p>
<editor-content :editor="editor" />
<input
id="tiptap-browse-input"
type="file"
class="fr-hidden"
hidden
multiple
/>
<button id="tiptap-browse-btn" class="fr-btn fr-btn--tertiary-no-outline">
parcourir
</button>
</div>
</template>
4 changes: 4 additions & 0 deletions confiture-web-app/src/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export enum Browsers {
EDGE = "Microsoft Edge"
}

export enum Limitations {
FILE_SIZE = 2000000
}

export enum FileErrorMessage {
UPLOAD_SIZE = "Votre fichier dépasse la limite de 2 Mo. Veuillez choisir un fichier plus léger.",
UPLOAD_FORMAT = "Format de fichier non supporté.",
Expand Down
Loading

0 comments on commit 627f805

Please sign in to comment.