gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import {htmlEscape} from '../utils/html.ts';
  2. import {svg} from '../svg.ts';
  3. import {animateOnce, queryElems, showElem} from '../utils/dom.ts';
  4. import Toastify from 'toastify-js'; // don't use "async import", because when network error occurs, the "async import" also fails and nothing is shown
  5. import type {Intent} from '../types.ts';
  6. import type {SvgName} from '../svg.ts';
  7. import type {Options} from 'toastify-js';
  8. import type StartToastifyInstance from 'toastify-js';
  9. export type Toast = ReturnType<typeof StartToastifyInstance>;
  10. type ToastLevels = {
  11. [intent in Intent]: {
  12. icon: SvgName,
  13. background: string,
  14. duration: number,
  15. }
  16. };
  17. const levels: ToastLevels = {
  18. info: {
  19. icon: 'octicon-check',
  20. background: 'var(--color-green)',
  21. duration: 2500,
  22. },
  23. warning: {
  24. icon: 'gitea-exclamation',
  25. background: 'var(--color-orange)',
  26. duration: -1, // requires dismissal to hide
  27. },
  28. error: {
  29. icon: 'gitea-exclamation',
  30. background: 'var(--color-red)',
  31. duration: -1, // requires dismissal to hide
  32. },
  33. };
  34. type ToastOpts = {
  35. useHtmlBody?: boolean,
  36. preventDuplicates?: boolean | string,
  37. } & Options;
  38. type ToastifyElement = HTMLElement & {_giteaToastifyInstance?: Toast};
  39. /** See https://github.com/apvarun/toastify-js#api for options */
  40. function showToast(message: string, level: Intent, {gravity, position, duration, useHtmlBody, preventDuplicates = true, ...other}: ToastOpts = {}): Toast {
  41. const body = useHtmlBody ? message : htmlEscape(message);
  42. const parent = document.querySelector('.ui.dimmer.active') ?? document.body;
  43. const duplicateKey = preventDuplicates ? (preventDuplicates === true ? `${level}-${body}` : preventDuplicates) : '';
  44. // prevent showing duplicate toasts with the same level and message, and give visual feedback for end users
  45. if (preventDuplicates) {
  46. const toastEl = parent.querySelector(`:scope > .toastify.on[data-toast-unique-key="${CSS.escape(duplicateKey)}"]`);
  47. if (toastEl) {
  48. const toastDupNumEl = toastEl.querySelector('.toast-duplicate-number');
  49. showElem(toastDupNumEl);
  50. toastDupNumEl.textContent = String(Number(toastDupNumEl.textContent) + 1);
  51. animateOnce(toastDupNumEl, 'pulse-1p5-200');
  52. return;
  53. }
  54. }
  55. const {icon, background, duration: levelDuration} = levels[level ?? 'info'];
  56. const toast = Toastify({
  57. selector: parent,
  58. text: `
  59. <div class='toast-icon'>${svg(icon)}</div>
  60. <div class='toast-body'><span class="toast-duplicate-number tw-hidden">1</span>${body}</div>
  61. <button class='btn toast-close'>${svg('octicon-x')}</button>
  62. `,
  63. escapeMarkup: false,
  64. gravity: gravity ?? 'top',
  65. position: position ?? 'center',
  66. duration: duration ?? levelDuration,
  67. style: {background},
  68. ...other,
  69. });
  70. toast.showToast();
  71. toast.toastElement.querySelector('.toast-close').addEventListener('click', () => toast.hideToast());
  72. toast.toastElement.setAttribute('data-toast-unique-key', duplicateKey);
  73. (toast.toastElement as ToastifyElement)._giteaToastifyInstance = toast;
  74. return toast;
  75. }
  76. export function showInfoToast(message: string, opts?: ToastOpts): Toast {
  77. return showToast(message, 'info', opts);
  78. }
  79. export function showWarningToast(message: string, opts?: ToastOpts): Toast {
  80. return showToast(message, 'warning', opts);
  81. }
  82. export function showErrorToast(message: string, opts?: ToastOpts): Toast {
  83. return showToast(message, 'error', opts);
  84. }
  85. function hideToastByElement(el: Element): void {
  86. (el as ToastifyElement)?._giteaToastifyInstance?.hideToast();
  87. }
  88. export function hideToastsFrom(parent: Element): void {
  89. queryElems(parent, ':scope > .toastify.on', hideToastByElement);
  90. }
  91. export function hideToastsAll(): void {
  92. queryElems(document, '.toastify.on', hideToastByElement);
  93. }