Skip to content

Commit 084010b

Browse files
committed
feat: add close
1 parent c20d07a commit 084010b

File tree

3 files changed

+54
-26
lines changed

3 files changed

+54
-26
lines changed

src/components/common/Message/instance.ts

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import { createVNode, render } from "vue";
22
import Ins from "./index.vue";
3-
import type { InstanceQueqe, MessageInstance, Options } from "./type";
3+
import type {
4+
InstanceQueqe,
5+
MessageInstance,
6+
Options,
7+
InstanceComponent,
8+
IntanceOptions,
9+
} from "./type";
410

511
const isServer = typeof window === "undefined";
612

713
const Queqe: InstanceQueqe = [];
814
let seed = 5201414;
9-
const Message: MessageInstance = (opts: Options | String) => {
15+
const Message: MessageInstance = (opts: IntanceOptions) => {
1016
if (isServer) return;
1117
let options: Options = {
1218
content: "",
@@ -50,17 +56,33 @@ const Message: MessageInstance = (opts: Options | String) => {
5056
render(vm, container);
5157
Queqe.push(vm);
5258
document.body.appendChild(container.firstElementChild as Element);
59+
return {
60+
close: () => {
61+
(vm.component?.proxy as InstanceComponent).visible = false;
62+
},
63+
};
5364
};
5465

55-
Message.closeAll = () => {};
56-
57-
Message.success = () => {};
58-
59-
Message.warn = () => {};
60-
61-
Message.info = () => {};
66+
Message.closeAll = () => {
67+
for (let i = Queqe.length - 1; i >= 0; i--) {
68+
const instance = Queqe[i].component as any;
69+
instance.ctx.close();
70+
}
71+
};
6272

63-
Message.error = () => {};
73+
(["success", "warn", "info", "error"] as const).forEach(type => {
74+
Message[type] = (options: IntanceOptions) => {
75+
if (typeof options === "string") {
76+
options = {
77+
content: options,
78+
type,
79+
};
80+
} else {
81+
options.type = type;
82+
}
83+
return Message(options);
84+
};
85+
});
6486

6587
const remove = (id: string, userOnclose?: () => void) => {
6688
const index = Queqe.findIndex(vm => {
@@ -76,8 +98,15 @@ const remove = (id: string, userOnclose?: () => void) => {
7698
const len = Queqe.length;
7799
if (len < 1) return;
78100
for (let i = index; i < len; i++) {
79-
console.log("更改 fixed 高度 全部减去 - " + removedHeight, Queqe);
80-
let top = [];
101+
console.log(
102+
"更改队列里全部高度 fixed 高度 全部减去 - " + removedHeight,
103+
Queqe
104+
);
105+
const pos =
106+
parseInt((Queqe[i] as any).el.style["top"], 10) -
107+
removedHeight -
108+
16;
109+
(Queqe[i] as any).component.props.offset = pos;
81110
}
82111
};
83112

src/components/common/Message/type.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import type { VNode } from "vue";
1+
import type { VNode, ComponentPublicInstance } from "vue";
22

33
export type MessageType = "warn" | "error" | "success" | "info";
44

55
export type MessageInstance = {
6-
(opt: Options): void;
7-
warn(opt: Options): void;
8-
error(opt: Options): void;
9-
success(opt: Options): void;
10-
info(opt: Options): void;
6+
(opt: IntanceOptions): void;
7+
warn(opt: IntanceOptions): void;
8+
error(opt: IntanceOptions): void;
9+
success(opt: IntanceOptions): void;
10+
info(opt: IntanceOptions): void;
1111
closeAll(): void;
1212
};
1313

@@ -21,4 +21,8 @@ export type Options = {
2121
id?: string;
2222
};
2323

24+
export type InstanceComponent = ComponentPublicInstance<{ visible: boolean }>;
25+
2426
export type InstanceQueqe = Array<VNode>;
27+
28+
export type IntanceOptions = Options | string;

src/views/home/index.vue

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts" setup="">
22
import { defineProps, onMounted, reactive } from "vue";
33
import { useHead } from "@vueuse/head";
4-
import M from "~/components/common/Message/instance";
4+
import M, { useMessage } from "~/components/common/Message/instance";
55
import { GetArticles } from "~/api/article";
66
import ContentLayout from "~/components/layouts/Content/index.vue";
77
import DefaultLayout from "~/components/layouts/Default/index.vue";
@@ -39,13 +39,8 @@ const getList = (req: API.ARTICLE.GetArticleListRequest) => {
3939
};
4040
4141
onMounted(async () => {
42-
setInterval(() => {
43-
M({
44-
content: "222",
45-
type: "success",
46-
onClose: () => console.log("onclose gogogog"),
47-
});
48-
}, 3000);
42+
const m = useMessage();
43+
m.info("ddd");
4944
await getList(props.query ?? {});
5045
});
5146
</script>

0 commit comments

Comments
 (0)