Skip to content

Commit d4bd99b

Browse files
committed
fix
1 parent 35ce7a5 commit d4bd99b

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

templates/devtest/gitea-ui.tmpl

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,6 @@
182182
</div>
183183
</div>
184184

185-
<div>
186-
<h1>Toast</h1>
187-
<div>
188-
<button class="ui button" id="info-toast">Show Info Toast</button>
189-
<button class="ui button" id="warning-toast">Show Warning Toast</button>
190-
<button class="ui button" id="error-toast">Show Error Toast</button>
191-
</div>
192-
</div>
193-
194185
<div>
195186
<h1>ComboMarkdownEditor</h1>
196187
<div>ps: no JS code attached, so just a layout</div>
@@ -201,7 +192,5 @@
201192
<div>
202193
<button class="{{if true}}tw-bg-red{{end}} tw-p-5 tw-border tw-rounded hover:tw-bg-blue active:tw-bg-yellow">Button</button>
203194
</div>
204-
205-
<script src="{{AssetUrlPrefix}}/js/devtest.js?v={{AssetVersion}}"></script>
206195
</div>
207196
{{template "base/footer" .}}

templates/devtest/toast.tmpl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{{template "base/head" .}}
2+
3+
<div>
4+
<h1>Toast</h1>
5+
<div>
6+
<button class="ui button" id="info-toast">Show Info Toast</button>
7+
<button class="ui button" id="warning-toast">Show Warning Toast</button>
8+
<button class="ui button" id="error-toast">Show Error Toast</button>
9+
</div>
10+
</div>
11+
12+
<script src="{{AssetUrlPrefix}}/js/devtest.js?v={{AssetVersion}}"></script>
13+
14+
{{template "base/footer" .}}

web_src/js/modules/toast.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,24 @@ const levels = {
2121
};
2222

2323
// See https://github.com/apvarun/toastify-js#api for options
24-
function showToast(message, level, {gravity, position, duration, useHtmlBody, ...other} = {}) {
24+
function showToast(message, level, {gravity, position, duration, useHtmlBody, preventDuplicates = true, ...other} = {}) {
25+
const body = useHtmlBody ? String(message) : htmlEscape(message);
26+
const key = `${level}-${body}`;
27+
28+
// prevent showing duplicate toasts with same level and message, hide all existing toasts with same key
29+
if (preventDuplicates) {
30+
const toastElements = document.querySelectorAll(`.toastify[data-toast-unique-key="${CSS.escape(key)}"]`);
31+
for (const el of toastElements) {
32+
el.remove(); // "hideToast" only removes the toast after an unchangeable delay, so we need to remove it immediately to make the "reposition" work with new toasts
33+
el._toastInst?.hideToast();
34+
}
35+
}
36+
2537
const {icon, background, duration: levelDuration} = levels[level ?? 'info'];
2638
const toast = Toastify({
2739
text: `
2840
<div class='toast-icon'>${svg(icon)}</div>
29-
<div class='toast-body'>${useHtmlBody ? message : htmlEscape(message)}</div>
41+
<div class='toast-body'>${body}</div>
3042
<button class='toast-close'>${svg('octicon-x')}</button>
3143
`,
3244
escapeMarkup: false,
@@ -39,6 +51,8 @@ function showToast(message, level, {gravity, position, duration, useHtmlBody, ..
3951

4052
toast.showToast();
4153
toast.toastElement.querySelector('.toast-close').addEventListener('click', () => toast.hideToast());
54+
toast.toastElement.setAttribute('data-toast-unique-key', key);
55+
toast.toastElement._toastInst = toast;
4256
return toast;
4357
}
4458

0 commit comments

Comments
 (0)