Skip to content

Commit ff0b417

Browse files
authored
fix: fileinput change listener (#861)
1 parent 6e6e6c1 commit ff0b417

File tree

1 file changed

+21
-15
lines changed
  • src/unfold/static/unfold/js

1 file changed

+21
-15
lines changed

src/unfold/static/unfold/js/app.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -135,27 +135,33 @@ const dateTimeShortcutsOverlay = () => {
135135
* File upload path
136136
*************************************************************/
137137
const fileInputUpdatePath = () => {
138-
const observer = new MutationObserver((mutations) => {
139-
for (const mutation of mutations) {
140-
if (mutation.type === "childList") {
141-
for (const input of document.querySelectorAll("input[type=file]")) {
142-
input.addEventListener("change", (e) => {
143-
const parts = e.target.value.split("\\");
144-
const placeholder =
145-
input.parentNode.parentNode.parentNode.querySelector(
146-
"input[type=text]"
147-
);
148-
placeholder.setAttribute("value", parts[parts.length - 1]);
149-
});
150-
}
138+
const checkInputChanged = () => {
139+
for (const input of document.querySelectorAll("input[type=file]")) {
140+
if (input.hasChangeListener) {
141+
continue;
151142
}
143+
144+
input.addEventListener("change", (e) => {
145+
const parts = e.target.value.split("\\");
146+
const placeholder =
147+
input.parentNode.parentNode.parentNode.querySelector(
148+
"input[type=text]"
149+
);
150+
placeholder.setAttribute("value", parts[parts.length - 1]);
151+
});
152+
153+
input.hasChangeListener = true;
152154
}
153-
});
155+
};
154156

155-
observer.observe(document.body, {
157+
new MutationObserver(() => {
158+
checkInputChanged();
159+
}).observe(document.body, {
156160
childList: true,
157161
subtree: true,
158162
});
163+
164+
checkInputChanged();
159165
};
160166

161167
/*************************************************************

0 commit comments

Comments
 (0)