Toast

Toast或Toast[type] 调用

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/411093/1081231/Toast.js

  1. /******/ (() => { // webpackBootstrap
  2. /******/ "use strict";
  3. var __webpack_exports__ = {};
  4.  
  5. ;// CONCATENATED MODULE: external "Vue"
  6. const external_Vue_namespaceObject = Vue;
  7. ;// CONCATENATED MODULE: ./src/helpers/toast.tsx
  8.  
  9.  
  10. /* Toast */
  11.  
  12. const toastTypes = ['info', 'success', 'warning', 'error'];
  13.  
  14. function normalizeOptions(options, duration) {
  15. if (typeof options === 'string' || (0,external_Vue_namespaceObject.isVNode)(options)) {
  16. options = {
  17. content: options
  18. };
  19. }
  20.  
  21. options.duration = duration ?? options.duration;
  22. return options;
  23. }
  24.  
  25. const Toast = function (_opts, duration) {
  26. const options = normalizeOptions(_opts, duration);
  27. const container = document.createElement('div');
  28. const ToastConstructor = (0,external_Vue_namespaceObject.defineComponent)({
  29. props: {
  30. content: {
  31. type: [String, Object],
  32. default: ''
  33. },
  34. type: {
  35. type: String,
  36. validator: value => toastTypes.includes(value),
  37. default: 'info'
  38. },
  39. closable: {
  40. type: Boolean,
  41. default: null
  42. },
  43. duration: {
  44. type: Number,
  45. default: 3000
  46. }
  47. },
  48.  
  49. setup(props, context) {
  50. const {
  51. expose
  52. } = context;
  53. const state = (0,external_Vue_namespaceObject.reactive)({
  54. closable: props.duration === 0 && props.closable == null ? true : props.closable,
  55. // 0 时 closable 默认打开
  56. visible: false
  57. });
  58. (0,external_Vue_namespaceObject.onMounted)(() => {
  59. state.visible = true;
  60.  
  61. if (props.duration > 0) {
  62. setTimeout(close, props.duration);
  63. }
  64. });
  65.  
  66. const close = () => {
  67. state.visible = false;
  68. };
  69.  
  70. const onAfterLeave = () => {
  71. // 销毁
  72. (0,external_Vue_namespaceObject.render)(null, container);
  73. container.remove();
  74. };
  75.  
  76. expose({
  77. close
  78. });
  79. return () => (0,external_Vue_namespaceObject.createVNode)(external_Vue_namespaceObject.Transition, {
  80. "name": "inject-toast-slide-fade",
  81. "appear": true,
  82. "onAfterLeave": onAfterLeave
  83. }, {
  84. default: () => [state.visible && (0,external_Vue_namespaceObject.createVNode)("div", {
  85. "class": "inject-toast"
  86. }, [(0,external_Vue_namespaceObject.createVNode)("div", {
  87. "class": ['inject-toast-content', `inject-toast-content--${props.type}`]
  88. }, [(0,external_Vue_namespaceObject.createVNode)("div", {
  89. "class": "inject-toast-content-text"
  90. }, [props.content]), state.closable && (0,external_Vue_namespaceObject.createVNode)("button", {
  91. "class": "inject-toast-content-close",
  92. "onClick": close
  93. }, [(0,external_Vue_namespaceObject.createTextVNode)("\xD7")])])])]
  94. });
  95. }
  96.  
  97. }); // toast
  98.  
  99. const vm = (0,external_Vue_namespaceObject.createVNode)(ToastConstructor, options);
  100. (0,external_Vue_namespaceObject.render)(vm, container);
  101. insertElementInContainer(container);
  102. return {
  103. close: vm.component?.exposed?.close
  104. };
  105. };
  106.  
  107. toastTypes.forEach(type => {
  108. Toast[type] = function (_opts, duration) {
  109. const options = { ...normalizeOptions(_opts, duration),
  110. type
  111. };
  112. return Toast(options, duration);
  113. };
  114. });
  115. window.Toast = Toast;
  116.  
  117. function safeAppendElement(cb) {
  118. document.body ? cb() : window.addEventListener('DOMContentLoaded', cb);
  119. }
  120.  
  121. function insertElementInContainer(elememnt) {
  122. function getContainer() {
  123. const classname = 'inject-toast-container';
  124. let containerEl = document.querySelector('.' + classname);
  125.  
  126. if (containerEl == null) {
  127. containerEl = document.createElement('div');
  128. containerEl.classList.add(classname);
  129. document.body.appendChild(containerEl);
  130. }
  131.  
  132. return containerEl;
  133. }
  134.  
  135. safeAppendElement(() => {
  136. getContainer().appendChild(elememnt);
  137. });
  138. }
  139.  
  140. (function addStyle() {
  141. const styleEl = document.createElement('style');
  142. styleEl.appendChild(document.createTextNode(`
  143. .inject-toast-container {
  144. position: fixed;
  145. z-index: 99999;
  146. top: 80px;
  147. right: 0;
  148. left: 0;
  149. pointer-events: none;
  150. text-align: center;
  151. }
  152. .inject-toast {
  153. contain: content;
  154. max-height: 100vh;
  155. transition: all .3s ease-in-out;
  156. }
  157. |> {
  158. pointer-events: auto;
  159. display: inline-flex;
  160. justify-content: center;
  161. margin-bottom: 10px;
  162. padding: 8px 16px;
  163. max-width: 90vw;
  164. font-size: 14px;
  165. line-height: 1.5em;
  166. border: 1px solid;
  167. box-shadow: 0 2px 3px rgba(0,0,0,.1);
  168. }
  169. |>--info {
  170. color: #2e8bf0;
  171. background: #f0faff;
  172. border-color: #d4eeff;
  173. }
  174. |>--success {
  175. color: #19bf6c;
  176. background: #edfff3;
  177. border-color: #bbf2cf;
  178. }
  179. |>--warning {
  180. color: #f90;
  181. background: #fff9e6;
  182. border-color: #ffe7a3;
  183. }
  184. |>--error {
  185. color: #ed3f13;
  186. background: #ffefe6;
  187. border-color: #ffcfb8;
  188. }
  189. |>-text {
  190. flex: auto;
  191. }
  192. |>-close {
  193. flex: none;
  194. width: 20px;
  195. margin: 0 -8px 0 10px;
  196. padding: 0;
  197. font-size: 16px;
  198. color: #ababab;
  199. border: none;
  200. background: transparent;
  201. cursor: pointer;
  202. }
  203.  
  204. /* 动画 */
  205. .inject-toast-slide-fade-enter-active,
  206. .inject-toast-slide-fade-leave-active {
  207. transition: all .3s;
  208. }
  209. .inject-toast-slide-fade-enter-from {
  210. transform: translateY(-50%);
  211. opacity: 0;
  212. }
  213. .inject-toast-slide-fade-leave-to {
  214. transform: translateY(50%);
  215. max-height: 0;
  216. padding: 0;
  217. opacity: 0;
  218. }
  219. `.replace(/\|>/g, '.inject-toast-content'))); // fix: tampermonkey 偶尔会获取不到 head
  220.  
  221. safeAppendElement(() => {
  222. document.head.appendChild(styleEl);
  223. });
  224. })();
  225. /******/ })()
  226. ;