Skip to content

Commit 1d6c719

Browse files
André DietrichAndré Dietrich
authored andcommitted
fix: insert upload
1 parent e49f626 commit 1d6c719

1 file changed

Lines changed: 45 additions & 55 deletions

File tree

src/components/Editor.vue

Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,6 @@ export default {
124124
activeTab: "start",
125125
126126
online: null,
127-
upload: {
128-
image: null,
129-
audio: null,
130-
movie: null,
131-
},
132127
blob: null,
133128
projectDoc: null as ProjectDoc | null,
134129
activePath: "",
@@ -213,6 +208,45 @@ export default {
213208
}
214209
},
215210
211+
// Handle the file selection from one of the hidden upload <input> elements.
212+
// Bound via @change in the template so the listener is (re)attached every
213+
// time the "insert" tab — and thus the inputs — is rendered.
214+
uploadFile(media: string, event) {
215+
const file = event.target.files[0];
216+
217+
if (!file) return;
218+
219+
const self = this;
220+
const reader = new FileReader();
221+
222+
reader.onload = async function (e) {
223+
if (
224+
e.target?.result === null ||
225+
e.target?.result === undefined ||
226+
typeof e.target?.result === "string"
227+
) {
228+
console.warn("could not load file: ", file);
229+
return;
230+
}
231+
232+
const blob = new Uint8Array(e.target?.result);
233+
234+
if (blob) {
235+
// Keep the original file name (visible in the file explorer)
236+
// instead of a hash-based one.
237+
const path = self.storeUpload(file.name, blob, file.type);
238+
self.make("upload-" + media, path);
239+
} else {
240+
console.warn("could not load file: ", file);
241+
}
242+
};
243+
244+
reader.readAsArrayBuffer(file);
245+
246+
// Reset so selecting the same file again re-triggers @change.
247+
event.target.value = "";
248+
},
249+
216250
getValue() {
217251
if (Editor) {
218252
return Editor.getValue();
@@ -959,7 +993,7 @@ I (study) ~[[ am going to study ]]~ harder this term.
959993
op.text = `?[](${data})`;
960994
move = 2;
961995
} else {
962-
this.upload.audio.click();
996+
(document.getElementById("audioInput") as HTMLInputElement)?.click();
963997
}
964998
965999
break;
@@ -970,7 +1004,7 @@ I (study) ~[[ am going to study ]]~ harder this term.
9701004
op.text = `![](${data})`;
9711005
move = 2;
9721006
} else {
973-
this.upload.image.click();
1007+
(document.getElementById("imageInput") as HTMLInputElement)?.click();
9741008
}
9751009
9761010
break;
@@ -981,7 +1015,7 @@ I (study) ~[[ am going to study ]]~ harder this term.
9811015
op.text = `!?[](${data})`;
9821016
move = 3;
9831017
} else {
984-
this.upload.movie.click();
1018+
(document.getElementById("movieInput") as HTMLInputElement)?.click();
9851019
}
9861020
9871021
break;
@@ -1668,50 +1702,6 @@ I (study) ~[[ am going to study ]]~ harder this term.
16681702
}
16691703
});
16701704
1671-
const self = this;
1672-
1673-
const eventListener = function (media: string) {
1674-
return function (event) {
1675-
const file = event.target.files[0];
1676-
1677-
if (file) {
1678-
const reader = new FileReader();
1679-
1680-
reader.onload = async function (e) {
1681-
if (
1682-
e.target?.result === null ||
1683-
e.target?.result === undefined ||
1684-
typeof e.target?.result === "string"
1685-
) {
1686-
console.warn("could not load file: ", file);
1687-
return;
1688-
}
1689-
1690-
const blob = new Uint8Array(e.target?.result);
1691-
1692-
console.warn("liascript: upload", e.target);
1693-
1694-
if (blob) {
1695-
// Keep the original file name (visible in the file explorer)
1696-
// instead of a hash-based one.
1697-
const path = self.storeUpload(file.name, blob, file.type);
1698-
self.make("upload-" + media, path);
1699-
} else {
1700-
console.warn("could not load file: ", file);
1701-
}
1702-
};
1703-
1704-
reader.readAsArrayBuffer(file);
1705-
}
1706-
};
1707-
};
1708-
1709-
for (let media of ["image", "audio", "movie"]) {
1710-
this.upload[media] = document.getElementById(media + "Input");
1711-
1712-
if (this.upload[media])
1713-
this.upload[media].addEventListener("change", eventListener(media), false);
1714-
}
17151705
},
17161706
};
17171707
</script>
@@ -1822,9 +1812,9 @@ I (study) ~[[ am going to study ]]~ harder this term.
18221812
</div>
18231813
<div class="toolbar-label">{{ $t('toolbar.sections.link') }}</div>
18241814
</div>
1825-
<input type="file" id="imageInput" style="display: none" accept="image/*" />
1826-
<input type="file" id="audioInput" style="display: none" accept="audio/*" />
1827-
<input type="file" id="movieInput" style="display: none" accept="video/*" />
1815+
<input type="file" id="imageInput" style="display: none" accept="image/*" @change="uploadFile('image', $event)" />
1816+
<input type="file" id="audioInput" style="display: none" accept="audio/*" @change="uploadFile('audio', $event)" />
1817+
<input type="file" id="movieInput" style="display: none" accept="video/*" @change="uploadFile('movie', $event)" />
18281818
<div class="toolbar-section">
18291819
<div class="toolbar-buttons">
18301820
<button class="btn-fmt" type="button" :title="$t('toolbar.buttons.uploadImage')" @click="make('upload-image')">

0 commit comments

Comments
 (0)