7419171665cb34f61c11823243644ad73819b647ab368393e41a9102e6f573e83331f59e60e074e76e403260273f33257d6a7100efc27c48089e9afa6d5e16 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { isVNode, render, markRaw, createVNode } from 'vue';
  2. import MessageBoxConstructor from './index.mjs';
  3. import { isClient } from '@vueuse/core';
  4. import { isString, isObject, hasOwn, isFunction } from '@vue/shared';
  5. import { isUndefined, isElement } from '../../../utils/types.mjs';
  6. const messageInstance = /* @__PURE__ */ new Map();
  7. const getAppendToElement = (props) => {
  8. let appendTo = document.body;
  9. if (props.appendTo) {
  10. if (isString(props.appendTo)) {
  11. appendTo = document.querySelector(props.appendTo);
  12. }
  13. if (isElement(props.appendTo)) {
  14. appendTo = props.appendTo;
  15. }
  16. if (!isElement(appendTo)) {
  17. appendTo = document.body;
  18. }
  19. }
  20. return appendTo;
  21. };
  22. const initInstance = (props, container, appContext = null) => {
  23. const vnode = createVNode(MessageBoxConstructor, props, isFunction(props.message) || isVNode(props.message) ? {
  24. default: isFunction(props.message) ? props.message : () => props.message
  25. } : null);
  26. vnode.appContext = appContext;
  27. render(vnode, container);
  28. getAppendToElement(props).appendChild(container.firstElementChild);
  29. return vnode.component;
  30. };
  31. const genContainer = () => {
  32. return document.createElement("div");
  33. };
  34. const showMessage = (options, appContext) => {
  35. const container = genContainer();
  36. options.onVanish = () => {
  37. render(null, container);
  38. messageInstance.delete(vm);
  39. };
  40. options.onAction = (action) => {
  41. const currentMsg = messageInstance.get(vm);
  42. let resolve;
  43. if (options.showInput) {
  44. resolve = { value: vm.inputValue, action };
  45. } else {
  46. resolve = action;
  47. }
  48. if (options.callback) {
  49. options.callback(resolve, instance.proxy);
  50. } else {
  51. if (action === "cancel" || action === "close") {
  52. if (options.distinguishCancelAndClose && action !== "cancel") {
  53. currentMsg.reject("close");
  54. } else {
  55. currentMsg.reject("cancel");
  56. }
  57. } else {
  58. currentMsg.resolve(resolve);
  59. }
  60. }
  61. };
  62. const instance = initInstance(options, container, appContext);
  63. const vm = instance.proxy;
  64. for (const prop in options) {
  65. if (hasOwn(options, prop) && !hasOwn(vm.$props, prop)) {
  66. if (prop === "closeIcon" && isObject(options[prop])) {
  67. vm[prop] = markRaw(options[prop]);
  68. } else {
  69. vm[prop] = options[prop];
  70. }
  71. }
  72. }
  73. vm.visible = true;
  74. return vm;
  75. };
  76. function MessageBox(options, appContext = null) {
  77. if (!isClient)
  78. return Promise.reject();
  79. let callback;
  80. if (isString(options) || isVNode(options)) {
  81. options = {
  82. message: options
  83. };
  84. } else {
  85. callback = options.callback;
  86. }
  87. return new Promise((resolve, reject) => {
  88. const vm = showMessage(options, appContext != null ? appContext : MessageBox._context);
  89. messageInstance.set(vm, {
  90. options,
  91. callback,
  92. resolve,
  93. reject
  94. });
  95. });
  96. }
  97. const MESSAGE_BOX_VARIANTS = ["alert", "confirm", "prompt"];
  98. const MESSAGE_BOX_DEFAULT_OPTS = {
  99. alert: { closeOnPressEscape: false, closeOnClickModal: false },
  100. confirm: { showCancelButton: true },
  101. prompt: { showCancelButton: true, showInput: true }
  102. };
  103. MESSAGE_BOX_VARIANTS.forEach((boxType) => {
  104. MessageBox[boxType] = messageBoxFactory(boxType);
  105. });
  106. function messageBoxFactory(boxType) {
  107. return (message, title, options, appContext) => {
  108. let titleOrOpts = "";
  109. if (isObject(title)) {
  110. options = title;
  111. titleOrOpts = "";
  112. } else if (isUndefined(title)) {
  113. titleOrOpts = "";
  114. } else {
  115. titleOrOpts = title;
  116. }
  117. return MessageBox(Object.assign({
  118. title: titleOrOpts,
  119. message,
  120. type: "",
  121. ...MESSAGE_BOX_DEFAULT_OPTS[boxType]
  122. }, options, {
  123. boxType
  124. }), appContext);
  125. };
  126. }
  127. MessageBox.close = () => {
  128. messageInstance.forEach((_, vm) => {
  129. vm.doClose();
  130. });
  131. messageInstance.clear();
  132. };
  133. MessageBox._context = null;
  134. export { MessageBox as default };
  135. //# sourceMappingURL=messageBox.mjs.map