create-weapp-vite 2.0.58 → 2.0.60
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +1 -1
- package/dist/index.js +1 -1
- package/dist/{src-hBW5b5RM.js → src-CXCuh90o.js} +69 -11
- package/package.json +1 -1
- package/templates/default/project.private.config.json +1 -1
- package/templates/lib/package.json +1 -1
- package/templates/lib/project.private.config.json +1 -1
- package/templates/tailwindcss/project.private.config.json +1 -1
- package/templates/tdesign/project.private.config.json +1 -1
- package/templates/vant/project.private.config.json +1 -1
- package/templates/wevu/project.private.config.json +1 -1
- package/templates/wevu-tdesign/package.json +1 -1
- package/templates/wevu-tdesign/project.private.config.json +19 -1
- package/templates/wevu-tdesign/src/app.vue +2 -0
- package/templates/wevu-tdesign/src/hooks/useDialog.ts +165 -34
- package/templates/wevu-tdesign/src/hooks/useLayoutFeedbackBridge.ts +17 -0
- package/templates/wevu-tdesign/src/hooks/useToast.ts +48 -9
- package/templates/wevu-tdesign/src/layouts/admin.vue +6 -0
- package/templates/wevu-tdesign/src/layouts/default.vue +6 -0
- package/templates/wevu-tdesign/src/pages/ability/index.vue +0 -2
- package/templates/wevu-tdesign/src/pages/form/index.vue +0 -1
- package/templates/wevu-tdesign/src/pages/index/index.vue +67 -6
- package/templates/wevu-tdesign/src/pages/layout-feedback/components/FeedbackCallerCard.vue +79 -0
- package/templates/wevu-tdesign/src/pages/layout-feedback/index.vue +211 -0
- package/templates/wevu-tdesign/src/pages/layout-store/index.vue +127 -0
- package/templates/wevu-tdesign/src/pages/layouts/index.vue +25 -1
- package/templates/wevu-tdesign/src/pages/list/index.vue +0 -1
- package/templates/wevu-tdesign/src/stores/layoutInteractionDemo.ts +129 -0
package/dist/cli.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as TemplateName, t as createProject } from "./src-
|
|
1
|
+
import { n as TemplateName, t as createProject } from "./src-CXCuh90o.js";
|
|
2
2
|
export { TemplateName, createProject };
|
|
@@ -4,10 +4,10 @@ import fs from "fs-extra";
|
|
|
4
4
|
import path from "pathe";
|
|
5
5
|
import https from "node:https";
|
|
6
6
|
//#region ../weapp-vite/package.json
|
|
7
|
-
var version$1 = "6.11.
|
|
7
|
+
var version$1 = "6.11.5";
|
|
8
8
|
//#endregion
|
|
9
9
|
//#region ../wevu/package.json
|
|
10
|
-
var version = "6.11.
|
|
10
|
+
var version = "6.11.5";
|
|
11
11
|
//#endregion
|
|
12
12
|
//#region src/enums.ts
|
|
13
13
|
let TemplateName = /* @__PURE__ */ function(TemplateName) {
|
|
@@ -72,8 +72,8 @@ const TEMPLATE_CATALOG = {
|
|
|
72
72
|
sass: "^1.98.0",
|
|
73
73
|
tailwindcss: "^4.2.2",
|
|
74
74
|
tslib: "^2.8.1",
|
|
75
|
-
typescript: "^
|
|
76
|
-
vite: "8.0.
|
|
75
|
+
typescript: "^6.0.2",
|
|
76
|
+
vite: "8.0.2",
|
|
77
77
|
vue: "^3.5.30",
|
|
78
78
|
zod: "^4.3.6"
|
|
79
79
|
};
|
|
@@ -82,7 +82,7 @@ const TEMPLATE_NAMED_CATALOG = {
|
|
|
82
82
|
"weapp-tailwindcss-fixed": { "weapp-tailwindcss": "4.10.3" },
|
|
83
83
|
latest: {
|
|
84
84
|
"miniprogram-api-typings": "^5.1.2",
|
|
85
|
-
typescript: "
|
|
85
|
+
typescript: "^6.0.2"
|
|
86
86
|
},
|
|
87
87
|
tailwind3: { tailwindcss: "3.4.19" },
|
|
88
88
|
tailwind4: { tailwindcss: "^4.2.2" }
|
|
@@ -169,9 +169,9 @@ dist-web
|
|
|
169
169
|
dist/web
|
|
170
170
|
vite.config.ts.timestamp-*.mjs
|
|
171
171
|
.weapp-vite/`;
|
|
172
|
-
const CRLF_RE = /\r\n/g;
|
|
172
|
+
const CRLF_RE$1 = /\r\n/g;
|
|
173
173
|
function normalizeLineEndings(value) {
|
|
174
|
-
return value.replace(CRLF_RE, "\n");
|
|
174
|
+
return value.replace(CRLF_RE$1, "\n");
|
|
175
175
|
}
|
|
176
176
|
function trimTrailingBlankLines(lines) {
|
|
177
177
|
let end = lines.length;
|
|
@@ -266,9 +266,61 @@ async function updateGitIgnore(options) {
|
|
|
266
266
|
//#endregion
|
|
267
267
|
//#region src/createProject.ts
|
|
268
268
|
const DIGIT_RE = /\d/;
|
|
269
|
+
const CRLF_RE = /\r\n/g;
|
|
269
270
|
const moduleDir = path.dirname(fileURLToPath(import.meta.url));
|
|
271
|
+
const TEMPLATE_DIR_MAP = {
|
|
272
|
+
[TemplateName.default]: "weapp-vite-template",
|
|
273
|
+
[TemplateName.lib]: "weapp-vite-lib-template",
|
|
274
|
+
[TemplateName.wevu]: "weapp-vite-wevu-template",
|
|
275
|
+
[TemplateName.wevuTdesign]: "weapp-vite-wevu-tailwindcss-tdesign-template",
|
|
276
|
+
[TemplateName.tailwindcss]: "weapp-vite-tailwindcss-template",
|
|
277
|
+
[TemplateName.tdesign]: "weapp-vite-tailwindcss-tdesign-template",
|
|
278
|
+
[TemplateName.vant]: "weapp-vite-tailwindcss-vant-template"
|
|
279
|
+
};
|
|
270
280
|
const templateCatalogMap = { ...TEMPLATE_CATALOG };
|
|
271
281
|
const templateNamedCatalogMap = Object.fromEntries(Object.entries(TEMPLATE_NAMED_CATALOG).map(([name, deps]) => [name, { ...deps }]));
|
|
282
|
+
function resolveWorkspaceTemplateDir(templateName) {
|
|
283
|
+
const templateDirName = TEMPLATE_DIR_MAP[templateName];
|
|
284
|
+
return templateDirName ? path.resolve(moduleDir, "../../../templates", templateDirName) : path.resolve(moduleDir, "../../../templates", templateName);
|
|
285
|
+
}
|
|
286
|
+
async function resolveTemplateDirs(templateName) {
|
|
287
|
+
const packagedTemplateDir = path.resolve(moduleDir, "../templates", templateName);
|
|
288
|
+
const workspaceTemplateDir = resolveWorkspaceTemplateDir(templateName);
|
|
289
|
+
return {
|
|
290
|
+
packagedTemplateDir,
|
|
291
|
+
workspaceTemplateDir,
|
|
292
|
+
preferredTemplateDir: await fs.pathExists(packagedTemplateDir) ? packagedTemplateDir : workspaceTemplateDir
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
function shouldSkipTemplateFile(filePath) {
|
|
296
|
+
return filePath.includes("node_modules") || filePath.includes(`${path.sep}.weapp-vite${path.sep}`) || filePath.includes("vite.config.ts.timestamp") || filePath.includes(`${path.sep}dist${path.sep}`) || filePath.endsWith(`${path.sep}CHANGELOG.md`) || filePath.includes(`${path.sep}.turbo${path.sep}`) || filePath.endsWith(`${path.sep}.DS_Store`);
|
|
297
|
+
}
|
|
298
|
+
function normalizeLines(value) {
|
|
299
|
+
return value.replace(CRLF_RE, "\n").split("\n");
|
|
300
|
+
}
|
|
301
|
+
function mergeGitignoreSource(existing, template) {
|
|
302
|
+
const merged = normalizeLines(existing);
|
|
303
|
+
const seen = new Set(merged);
|
|
304
|
+
for (const line of normalizeLines(template)) {
|
|
305
|
+
if (seen.has(line)) continue;
|
|
306
|
+
merged.push(line);
|
|
307
|
+
seen.add(line);
|
|
308
|
+
}
|
|
309
|
+
while (merged.length > 0 && merged.at(-1) === "") merged.pop();
|
|
310
|
+
return `${merged.join("\n")}\n`;
|
|
311
|
+
}
|
|
312
|
+
async function copyTemplateDir(sourceDir, fallbackDir, targetDir) {
|
|
313
|
+
const copyOptions = { filter(src) {
|
|
314
|
+
return !shouldSkipTemplateFile(src);
|
|
315
|
+
} };
|
|
316
|
+
try {
|
|
317
|
+
await fs.copy(sourceDir, targetDir, copyOptions);
|
|
318
|
+
} catch (error) {
|
|
319
|
+
const errorCode = error?.code;
|
|
320
|
+
if (sourceDir === fallbackDir || errorCode !== "ENOENT") throw error;
|
|
321
|
+
await fs.copy(fallbackDir, targetDir, copyOptions);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
272
324
|
async function ensureDotGitignore(root) {
|
|
273
325
|
const gitignorePath = path.resolve(root, "gitignore");
|
|
274
326
|
const dotGitignorePath = path.resolve(root, ".gitignore");
|
|
@@ -342,15 +394,21 @@ function normalizeTemplateDependencySpecs(pkgJson) {
|
|
|
342
394
|
* @description 根据模板创建项目
|
|
343
395
|
*/
|
|
344
396
|
async function createProject(targetDir = "", templateName = TemplateName.default) {
|
|
345
|
-
const
|
|
346
|
-
|
|
397
|
+
const { preferredTemplateDir, workspaceTemplateDir } = await resolveTemplateDirs(templateName);
|
|
398
|
+
const dotGitignorePath = path.resolve(targetDir, ".gitignore");
|
|
399
|
+
const existingGitignore = await fs.pathExists(dotGitignorePath) ? await fs.readFile(dotGitignorePath, "utf8") : null;
|
|
400
|
+
if (!await fs.pathExists(preferredTemplateDir) && !await fs.pathExists(workspaceTemplateDir)) {
|
|
347
401
|
logger.warn(`没有找到 ${templateName} 模板!`);
|
|
348
402
|
return;
|
|
349
403
|
}
|
|
350
|
-
await
|
|
351
|
-
const templatePackagePath = path.resolve(
|
|
404
|
+
await copyTemplateDir(preferredTemplateDir, workspaceTemplateDir, targetDir);
|
|
405
|
+
const templatePackagePath = path.resolve(preferredTemplateDir, "package.json");
|
|
352
406
|
const packageJsonPath = path.resolve(targetDir, "package.json");
|
|
353
407
|
await ensureDotGitignore(targetDir);
|
|
408
|
+
if (existingGitignore !== null && await fs.pathExists(dotGitignorePath)) {
|
|
409
|
+
const currentGitignore = await fs.readFile(dotGitignorePath, "utf8");
|
|
410
|
+
await fs.writeFile(dotGitignorePath, mergeGitignoreSource(existingGitignore, currentGitignore));
|
|
411
|
+
}
|
|
354
412
|
const pkgJson = await fs.pathExists(templatePackagePath) ? await fs.readJSON(templatePackagePath) : createEmptyPackageJson();
|
|
355
413
|
normalizeTemplateDependencySpecs(pkgJson);
|
|
356
414
|
if (!pkgJson.devDependencies) pkgJson.devDependencies = {};
|
package/package.json
CHANGED
|
@@ -13,6 +13,24 @@
|
|
|
13
13
|
"query": "",
|
|
14
14
|
"scene": null
|
|
15
15
|
},
|
|
16
|
+
{
|
|
17
|
+
"name": "布局页",
|
|
18
|
+
"pathName": "pages/layouts/index",
|
|
19
|
+
"query": "",
|
|
20
|
+
"scene": null
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"name": "Layout 通信演示",
|
|
24
|
+
"pathName": "pages/layout-feedback/index",
|
|
25
|
+
"query": "",
|
|
26
|
+
"scene": null
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
"name": "Store 调用 Layout",
|
|
30
|
+
"pathName": "pages/layout-store/index",
|
|
31
|
+
"query": "",
|
|
32
|
+
"scene": null
|
|
33
|
+
},
|
|
16
34
|
{
|
|
17
35
|
"name": "数据页",
|
|
18
36
|
"pathName": "pages/data/index",
|
|
@@ -58,5 +76,5 @@
|
|
|
58
76
|
]
|
|
59
77
|
}
|
|
60
78
|
},
|
|
61
|
-
"libVersion": "3.
|
|
79
|
+
"libVersion": "3.15.0"
|
|
62
80
|
}
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import Dialog from 'tdesign-miniprogram/dialog/index'
|
|
2
|
-
import { getCurrentInstance } from 'wevu'
|
|
2
|
+
import { getCurrentInstance, resolveLayoutBridge, resolveLayoutHost } from 'wevu'
|
|
3
|
+
import { LAYOUT_DIALOG_BRIDGE_KEY } from '@/hooks/useLayoutFeedbackBridge'
|
|
3
4
|
|
|
4
5
|
export interface DialogOptions {
|
|
6
|
+
bridgeKey?: string
|
|
5
7
|
context?: any
|
|
6
8
|
selector?: string
|
|
7
9
|
}
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
interface BaseDialogPayload {
|
|
12
|
+
bridgeKey?: string
|
|
10
13
|
confirmBtn?: string
|
|
11
14
|
content: string
|
|
12
15
|
context?: any
|
|
@@ -14,61 +17,189 @@ export interface AlertOptions {
|
|
|
14
17
|
title: string
|
|
15
18
|
}
|
|
16
19
|
|
|
17
|
-
export interface
|
|
20
|
+
export interface AlertOptions extends BaseDialogPayload {}
|
|
21
|
+
|
|
22
|
+
export interface ConfirmOptions extends BaseDialogPayload {
|
|
18
23
|
cancelBtn?: string
|
|
19
|
-
confirmBtn?: string
|
|
20
|
-
content: string
|
|
21
|
-
context?: any
|
|
22
|
-
selector?: string
|
|
23
|
-
title: string
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
interface HostDialogInstance {
|
|
27
|
+
_onCancel?: (reason?: unknown) => void
|
|
28
|
+
_onConfirm?: (value?: unknown) => void
|
|
29
|
+
close?: () => void
|
|
30
|
+
setData?: (payload: Record<string, unknown>) => void
|
|
28
31
|
}
|
|
29
32
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
+
type DialogMode = 'alert' | 'confirm'
|
|
34
|
+
|
|
35
|
+
type ResolvedDialogPayload<T extends BaseDialogPayload> = Omit<T, 'bridgeKey' | 'context' | 'selector'>
|
|
36
|
+
|
|
37
|
+
function resolveDialogContext(options: DialogOptions) {
|
|
38
|
+
return options.bridgeKey
|
|
39
|
+
? resolveLayoutBridge(options.bridgeKey, options.context ?? getCurrentInstance())
|
|
40
|
+
: options.context ?? getCurrentInstance()
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function resolveDialogHost(options: DialogOptions) {
|
|
44
|
+
const bridgeKey = options.bridgeKey ?? LAYOUT_DIALOG_BRIDGE_KEY
|
|
45
|
+
const context = resolveDialogContext({
|
|
46
|
+
bridgeKey,
|
|
47
|
+
context: options.context,
|
|
48
|
+
})
|
|
49
|
+
const host = bridgeKey
|
|
50
|
+
? resolveLayoutHost<HostDialogInstance>(bridgeKey, { context })
|
|
51
|
+
: options.selector
|
|
52
|
+
? context?.selectComponent?.(options.selector) ?? null
|
|
53
|
+
: null
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
context,
|
|
57
|
+
host,
|
|
58
|
+
selector: options.selector,
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function closeDialogHost(host: HostDialogInstance) {
|
|
63
|
+
if (typeof host.close === 'function') {
|
|
64
|
+
host.close()
|
|
33
65
|
return
|
|
34
66
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
67
|
+
if (typeof host.setData === 'function') {
|
|
68
|
+
host.setData({ visible: false })
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function attachHostDialogHandlers(
|
|
73
|
+
host: HostDialogInstance,
|
|
74
|
+
handlers: {
|
|
75
|
+
onCancel?: (reason?: unknown) => void
|
|
76
|
+
onConfirm?: (value?: unknown) => void
|
|
77
|
+
},
|
|
78
|
+
) {
|
|
79
|
+
const originalConfirm = typeof host._onConfirm === 'function' ? host._onConfirm : undefined
|
|
80
|
+
const originalCancel = typeof host._onCancel === 'function' ? host._onCancel : undefined
|
|
81
|
+
|
|
82
|
+
host._onConfirm = (value?: unknown) => {
|
|
83
|
+
host._onConfirm = originalConfirm
|
|
84
|
+
host._onCancel = originalCancel
|
|
85
|
+
|
|
86
|
+
if (originalConfirm) {
|
|
87
|
+
originalConfirm.call(host, value)
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
closeDialogHost(host)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
handlers.onConfirm?.(value)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
host._onCancel = (reason?: unknown) => {
|
|
97
|
+
host._onConfirm = originalConfirm
|
|
98
|
+
host._onCancel = originalCancel
|
|
99
|
+
|
|
100
|
+
if (originalCancel) {
|
|
101
|
+
originalCancel.call(host, reason)
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
closeDialogHost(host)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
handlers.onCancel?.(reason)
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function normalizeDialogPayload<T extends BaseDialogPayload>(
|
|
112
|
+
mode: DialogMode,
|
|
113
|
+
payload: T,
|
|
114
|
+
): ResolvedDialogPayload<T> {
|
|
115
|
+
const { bridgeKey: _bridgeKey, context: _context, selector: _selector, ...rest } = payload
|
|
116
|
+
|
|
117
|
+
if (mode === 'alert') {
|
|
118
|
+
return {
|
|
119
|
+
...rest,
|
|
120
|
+
cancelBtn: null,
|
|
121
|
+
} as ResolvedDialogPayload<T>
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return rest as ResolvedDialogPayload<T>
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function openDialogWithHost<T extends BaseDialogPayload>(
|
|
128
|
+
mode: DialogMode,
|
|
129
|
+
host: HostDialogInstance,
|
|
130
|
+
payload: ResolvedDialogPayload<T>,
|
|
131
|
+
) {
|
|
132
|
+
return new Promise((resolve, reject) => {
|
|
133
|
+
host.setData?.({
|
|
134
|
+
...payload,
|
|
135
|
+
visible: true,
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
attachHostDialogHandlers(host, {
|
|
139
|
+
onConfirm: resolve,
|
|
140
|
+
...(mode === 'confirm' ? { onCancel: reject } : {}),
|
|
141
|
+
})
|
|
40
142
|
})
|
|
41
143
|
}
|
|
42
144
|
|
|
43
|
-
|
|
44
|
-
const
|
|
145
|
+
function openDialog<T extends BaseDialogPayload>(mode: DialogMode, payload: T) {
|
|
146
|
+
const bridgeKey = payload.bridgeKey ?? LAYOUT_DIALOG_BRIDGE_KEY
|
|
147
|
+
const { context, host, selector } = resolveDialogHost({
|
|
148
|
+
bridgeKey,
|
|
149
|
+
context: payload.context,
|
|
150
|
+
selector: payload.selector,
|
|
151
|
+
})
|
|
152
|
+
|
|
45
153
|
if (!context) {
|
|
46
|
-
return
|
|
154
|
+
return Promise.resolve()
|
|
47
155
|
}
|
|
48
|
-
|
|
49
|
-
|
|
156
|
+
|
|
157
|
+
const normalizedPayload = normalizeDialogPayload(mode, payload)
|
|
158
|
+
|
|
159
|
+
if (host && typeof host.setData === 'function') {
|
|
160
|
+
return openDialogWithHost(mode, host, normalizedPayload)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (!selector) {
|
|
164
|
+
return Promise.resolve()
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const open = mode === 'alert' ? Dialog.alert : Dialog.confirm
|
|
168
|
+
|
|
169
|
+
return open({
|
|
50
170
|
selector,
|
|
51
171
|
context: context as any,
|
|
52
|
-
...
|
|
172
|
+
...normalizedPayload,
|
|
53
173
|
})
|
|
54
174
|
}
|
|
55
175
|
|
|
176
|
+
export function alertDialog(payload: AlertOptions) {
|
|
177
|
+
return openDialog('alert', payload)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function confirmDialog(payload: ConfirmOptions) {
|
|
181
|
+
return openDialog('confirm', payload)
|
|
182
|
+
}
|
|
183
|
+
|
|
56
184
|
export function useDialog(options: DialogOptions = {}) {
|
|
185
|
+
const bridgeKey = options.bridgeKey ?? LAYOUT_DIALOG_BRIDGE_KEY
|
|
57
186
|
const context = options.context ?? getCurrentInstance()
|
|
187
|
+
|
|
188
|
+
function withDefaults<T extends BaseDialogPayload>(payload: T): T {
|
|
189
|
+
return {
|
|
190
|
+
...payload,
|
|
191
|
+
bridgeKey,
|
|
192
|
+
context: payload.context ?? context,
|
|
193
|
+
selector: payload.selector ?? options.selector,
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
58
197
|
return {
|
|
59
198
|
alert(payload: AlertOptions) {
|
|
60
|
-
return alertDialog(
|
|
61
|
-
...payload,
|
|
62
|
-
context: payload.context ?? context,
|
|
63
|
-
selector: payload.selector ?? options.selector,
|
|
64
|
-
})
|
|
199
|
+
return alertDialog(withDefaults(payload))
|
|
65
200
|
},
|
|
66
201
|
confirm(payload: ConfirmOptions) {
|
|
67
|
-
return confirmDialog(
|
|
68
|
-
...payload,
|
|
69
|
-
context: payload.context ?? context,
|
|
70
|
-
selector: payload.selector ?? options.selector,
|
|
71
|
-
})
|
|
202
|
+
return confirmDialog(withDefaults(payload))
|
|
72
203
|
},
|
|
73
204
|
}
|
|
74
205
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export const LAYOUT_TOAST_BRIDGE_KEY = 'layout-toast'
|
|
2
|
+
export const LAYOUT_DIALOG_BRIDGE_KEY = 'layout-dialog'
|
|
3
|
+
|
|
4
|
+
export interface ToastHostInstance {
|
|
5
|
+
hide?: () => void
|
|
6
|
+
show: (options: Record<string, unknown>) => void
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface DialogHostInstance {
|
|
10
|
+
_onCancel?: (reason?: unknown) => void
|
|
11
|
+
_onConfirm?: (value?: unknown) => void
|
|
12
|
+
close?: () => void
|
|
13
|
+
onCancel?: (reason?: unknown) => void
|
|
14
|
+
onConfirm?: (value?: unknown) => void
|
|
15
|
+
properties?: Record<string, unknown>
|
|
16
|
+
setData: (payload: Record<string, unknown>) => void
|
|
17
|
+
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import Toast from 'tdesign-miniprogram/toast/index'
|
|
2
|
-
import { getCurrentInstance } from 'wevu'
|
|
2
|
+
import { getCurrentInstance, resolveLayoutBridge, resolveLayoutHost } from 'wevu'
|
|
3
|
+
import { LAYOUT_TOAST_BRIDGE_KEY } from '@/hooks/useLayoutFeedbackBridge'
|
|
3
4
|
|
|
4
5
|
export type ToastTheme = 'success' | 'warning' | 'error' | 'default' | 'loading'
|
|
5
6
|
|
|
6
7
|
export interface ShowToastPayload {
|
|
8
|
+
bridgeKey?: string
|
|
7
9
|
context?: any
|
|
8
10
|
duration?: number
|
|
9
11
|
icon?: string
|
|
@@ -15,41 +17,78 @@ export interface ShowToastPayload {
|
|
|
15
17
|
}
|
|
16
18
|
|
|
17
19
|
export interface ToastOptions {
|
|
20
|
+
bridgeKey?: string
|
|
18
21
|
selector?: string
|
|
19
22
|
duration?: number
|
|
20
23
|
theme?: ToastTheme
|
|
21
24
|
}
|
|
22
25
|
|
|
26
|
+
function resolveToastContext(options: { bridgeKey?: string, context?: any }) {
|
|
27
|
+
return options.bridgeKey
|
|
28
|
+
? resolveLayoutBridge(options.bridgeKey, options.context ?? getCurrentInstance())
|
|
29
|
+
: options.context ?? getCurrentInstance()
|
|
30
|
+
}
|
|
31
|
+
|
|
23
32
|
export function showToast(payload: string | ShowToastPayload, theme?: ToastTheme) {
|
|
24
33
|
const mpContext = getCurrentInstance()
|
|
25
34
|
const normalized = typeof payload === 'string'
|
|
26
35
|
? { message: payload, theme }
|
|
27
36
|
: payload
|
|
28
|
-
|
|
29
|
-
const
|
|
37
|
+
const bridgeKey = normalized.bridgeKey ?? LAYOUT_TOAST_BRIDGE_KEY
|
|
38
|
+
const selector = normalized.selector
|
|
39
|
+
const {
|
|
40
|
+
bridgeKey: _bridgeKey,
|
|
41
|
+
context: _context,
|
|
42
|
+
selector: _selector,
|
|
43
|
+
theme: nextTheme,
|
|
44
|
+
title,
|
|
45
|
+
message,
|
|
46
|
+
...rest
|
|
47
|
+
} = normalized
|
|
48
|
+
const options = {
|
|
49
|
+
message: message ?? title ?? '',
|
|
50
|
+
...rest,
|
|
51
|
+
...(nextTheme && nextTheme !== 'default' ? { theme: nextTheme } : {}),
|
|
52
|
+
}
|
|
53
|
+
const context = resolveToastContext({
|
|
54
|
+
bridgeKey,
|
|
55
|
+
context: normalized.context ?? mpContext,
|
|
56
|
+
})
|
|
57
|
+
const host = bridgeKey
|
|
58
|
+
? resolveLayoutHost<{
|
|
59
|
+
show?: (payload: typeof options) => void
|
|
60
|
+
}>(bridgeKey, { context })
|
|
61
|
+
: selector
|
|
62
|
+
? context?.selectComponent?.(selector) ?? null
|
|
63
|
+
: null
|
|
30
64
|
if (!context) {
|
|
31
65
|
return
|
|
32
66
|
}
|
|
33
|
-
|
|
34
|
-
|
|
67
|
+
if (host && typeof host.show === 'function') {
|
|
68
|
+
host.show(options)
|
|
69
|
+
return
|
|
70
|
+
}
|
|
71
|
+
if (!selector) {
|
|
72
|
+
return
|
|
73
|
+
}
|
|
35
74
|
Toast({
|
|
36
75
|
selector,
|
|
37
76
|
context: context as any,
|
|
38
|
-
|
|
39
|
-
...rest,
|
|
40
|
-
...(nextTheme && nextTheme !== 'default' ? { theme: nextTheme } : {}),
|
|
77
|
+
...options,
|
|
41
78
|
} as any)
|
|
42
79
|
}
|
|
43
80
|
|
|
44
81
|
export function useToast(options: ToastOptions = {}) {
|
|
45
82
|
const context = getCurrentInstance()
|
|
46
|
-
const
|
|
83
|
+
const bridgeKey = options.bridgeKey ?? LAYOUT_TOAST_BRIDGE_KEY
|
|
84
|
+
const selector = options.selector
|
|
47
85
|
const duration = options.duration ?? 1200
|
|
48
86
|
const defaultTheme = options.theme ?? 'success'
|
|
49
87
|
|
|
50
88
|
return {
|
|
51
89
|
showToast(message: string, theme: ToastTheme = defaultTheme) {
|
|
52
90
|
return showToast({
|
|
91
|
+
bridgeKey,
|
|
53
92
|
context,
|
|
54
93
|
selector,
|
|
55
94
|
message,
|
|
@@ -6,6 +6,10 @@ const props = defineProps<{
|
|
|
6
6
|
|
|
7
7
|
defineComponentJson({
|
|
8
8
|
component: true,
|
|
9
|
+
usingComponents: {
|
|
10
|
+
't-dialog': 'tdesign-miniprogram/dialog/dialog',
|
|
11
|
+
't-toast': 'tdesign-miniprogram/toast/toast',
|
|
12
|
+
},
|
|
9
13
|
})
|
|
10
14
|
</script>
|
|
11
15
|
|
|
@@ -25,6 +29,8 @@ defineComponentJson({
|
|
|
25
29
|
<view class="pb-[32rpx]">
|
|
26
30
|
<slot />
|
|
27
31
|
</view>
|
|
32
|
+
<t-toast layout-host="layout-toast" />
|
|
33
|
+
<t-dialog layout-host="layout-dialog" />
|
|
28
34
|
</view>
|
|
29
35
|
</template>
|
|
30
36
|
|
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
2
|
defineComponentJson({
|
|
3
3
|
component: true,
|
|
4
|
+
usingComponents: {
|
|
5
|
+
't-dialog': 'tdesign-miniprogram/dialog/dialog',
|
|
6
|
+
't-toast': 'tdesign-miniprogram/toast/toast',
|
|
7
|
+
},
|
|
4
8
|
})
|
|
5
9
|
</script>
|
|
6
10
|
|
|
7
11
|
<template>
|
|
8
12
|
<view class="layout-default">
|
|
9
13
|
<slot />
|
|
14
|
+
<t-toast layout-host="layout-toast" />
|
|
15
|
+
<t-dialog layout-host="layout-dialog" />
|
|
10
16
|
</view>
|
|
11
17
|
</template>
|
|
12
18
|
|