Skip to content

Commit ea17250

Browse files
committed
Improve console modal
1 parent 17a43c3 commit ea17250

File tree

5 files changed

+56
-29
lines changed

5 files changed

+56
-29
lines changed

packages/duoyun-ui/src/patterns/form.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { adoptedStyle, customElement, memo, property, shadow } from '@mantou/gem
22
import { createRef, createState, css, GemElement, html, TemplateResult } from '@mantou/gem/lib/element';
33
import { history } from '@mantou/gem/lib/history';
44
import type { StyleObject } from '@mantou/gem/lib/utils';
5-
import { GemError, styleMap } from '@mantou/gem/lib/utils';
5+
import { GemError, QueryString, styleMap } from '@mantou/gem/lib/utils';
66

77
import { DuoyunDatePickerElement } from '../elements/date-picker';
88
import { DuoyunDateRangePickerElement } from '../elements/date-range-picker';
@@ -19,7 +19,7 @@ import { icons } from '../lib/icons';
1919
import { locale } from '../lib/locale';
2020
import { blockContainer, focusStyle } from '../lib/styles';
2121
import { theme } from '../lib/theme';
22-
import { readProp } from '../lib/utils';
22+
import { getStringFromTemplate, readProp } from '../lib/utils';
2323

2424
import '../elements/button';
2525
import '../elements/form';
@@ -555,11 +555,7 @@ type CreateFormOptions<T> = {
555555
export function createForm<T = Record<string, unknown>>(options: CreateFormOptions<T>) {
556556
const containerType = options.type === 'modal' ? Modal : Drawer;
557557
const { query } = history.getParams();
558-
if (options.query) {
559-
query.setAny(options.query[0], options.query[1]);
560-
history.replace({ query });
561-
}
562-
return containerType
558+
let modalPromise = containerType
563559
.open<DyPatFormElement<T>>({
564560
header: options.header,
565561
body: html`
@@ -577,14 +573,26 @@ export function createForm<T = Record<string, unknown>>(options: CreateFormOptio
577573
await DuoyunWaitElement.instance?.removed;
578574
},
579575
})
580-
.then((ele) => ele.state.data)
581-
.catch((ele) => {
582-
throw ele.state.data;
583-
})
584-
.finally(() => {
585-
if (options.query) {
586-
query.delete(options.query[0]);
587-
history.replace({ query });
588-
}
576+
.catch((ele) => ele);
577+
modalPromise.modal.addEventListener('close', (evt: CustomEvent) => {
578+
if (evt.detail !== modalPromise) modalPromise.finally(() => history.back());
579+
});
580+
if (options.query) {
581+
query.delete(options.query[0]);
582+
history.replace({ query });
583+
584+
const newQuery = new QueryString(query);
585+
newQuery.setAny(options.query[0], options.query[1]);
586+
587+
history.push({
588+
query: newQuery,
589+
title: getStringFromTemplate(options.header || ''),
590+
close: () => modalPromise.modal.parentElement && modalPromise.modal.close(modalPromise),
591+
open: async () => {
592+
const { data } = (await modalPromise).state;
593+
modalPromise = createForm({ ...options, data, query: undefined });
594+
},
589595
});
596+
}
597+
return modalPromise;
590598
}

packages/gem-examples/src/console/users.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { customElement } from '@mantou/gem/lib/decorators';
1+
import { customElement, type Emitter, effect, emitter, mounted } from '@mantou/gem/lib/decorators';
22
import { createState, GemElement, html } from '@mantou/gem/lib/element';
33
import type { ContextMenuItem } from 'duoyun-ui/elements/contextmenu';
44
import { ContextMenu } from 'duoyun-ui/elements/contextmenu';
@@ -16,6 +16,8 @@ import { fetchItemsWithArgs } from './api';
1616
import 'duoyun-ui/patterns/table';
1717
import 'duoyun-ui/elements/button';
1818

19+
import { history } from '@mantou/gem/lib/history';
20+
1921
const pagination = createPaginationStore<Item>({
2022
storageKey: 'users',
2123
cacheItems: true,
@@ -38,6 +40,8 @@ type NewItem = Modify<typeof initItem, { social?: string[] }>;
3840

3941
@customElement('console-page-users')
4042
export class ConsolePageItemElement extends GemElement {
43+
@emitter loaded: Emitter;
44+
4145
#state = createState({
4246
pagination: pagination,
4347
paginationMap: new Map([['', pagination]]),
@@ -220,13 +224,14 @@ export class ConsolePageItemElement extends GemElement {
220224
header: `Edit: ${r.id}`,
221225
query: ['id', r.id],
222226
formItems: this.getFormItems(true),
227+
prepareClose: (data) => {
228+
console.log(data);
229+
},
223230
prepareOk: async (data) => {
224231
await sleep(1000);
225232
console.log(data);
226233
this.#state.pagination.updateItem(data);
227234
},
228-
}).catch((data) => {
229-
console.log(data);
230235
});
231236
};
232237

@@ -237,22 +242,37 @@ export class ConsolePageItemElement extends GemElement {
237242
header: `Create`,
238243
query: ['new', true],
239244
formItems: this.getFormItems(),
245+
prepareClose: (data) => {
246+
console.log(data);
247+
},
240248
prepareOk: async (data) => {
241249
await sleep(1000);
242250
console.log(data);
243251
throw new Error('No implement!');
244252
},
245-
}).catch((data) => {
246-
console.log(data);
247253
});
248254
};
249255

256+
@mounted()
257+
#init = async () => {
258+
const { query } = history.getParams();
259+
if (query.get('new')) {
260+
this.onCreate();
261+
}
262+
const id = query.get('id');
263+
if (id) {
264+
await new Promise((res) => this.addEventListener('loaded', res, { once: true }));
265+
const item = this.#state.pagination.store.items[id];
266+
item && this.onUpdate(item);
267+
}
268+
};
269+
250270
/**
251271
* 将搜索/过滤结果储存进内存 `PaginationStore`
252272
* 被根据 `searchAndFilterKey` 进行缓存
253273
* 这里使用页面级缓存,切换页面后将被清除
254274
*/
255-
#onFetch = ({ detail }: CustomEvent<FetchEventDetail>) => {
275+
#onFetch = async ({ detail }: CustomEvent<FetchEventDetail>) => {
256276
let newPagination = this.#state.paginationMap.get(detail.pageKey);
257277
if (!newPagination) {
258278
newPagination = createPaginationStore<Item>({
@@ -262,7 +282,8 @@ export class ConsolePageItemElement extends GemElement {
262282
this.#state.paginationMap.set(detail.pageKey, newPagination);
263283
}
264284
this.#state({ pagination: newPagination });
265-
newPagination.updatePage(fetchItemsWithArgs, detail);
285+
await newPagination.updatePage(fetchItemsWithArgs, detail);
286+
this.loaded();
266287
};
267288

268289
render = () => {

packages/gem/src/elements/base/title.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ const setTitle = (documentTitle: string) => {
2727
function setDocumentTitle(defaultTitle?: string | null, prefix = '', suffix = '') {
2828
const title = titleStore.title || defaultTitle;
2929
if (title && title !== titleStore.defaultTitle) {
30-
GemTitleElement.title = title;
31-
setTitle(prefix + GemTitleElement.title + suffix);
30+
setTitle(prefix + (GemTitleElement.title = title) + suffix);
3231
} else {
33-
GemTitleElement.title = titleStore.defaultTitle;
34-
setTitle(GemTitleElement.title);
32+
setTitle((GemTitleElement.title = titleStore.defaultTitle));
3533
}
3634
}
3735

packages/gem/src/lib/history.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ if (!globalThis._GEMHISTORY) {
271271
path: getInternalPath(pathname),
272272
query: new QueryString(search),
273273
hash: decodeURIComponent(hash),
274-
title: newState.$title, // document.title 是导航前的
274+
title: gemTitleStore.title || newState.$title, // document.title 是导航前的
275275
data: newState,
276276
});
277277
}

packages/vscode-gem-plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"private": true,
33
"name": "vscode-plugin-gem",
44
"displayName": "Gem",
5-
"description": "Gem plugin for VS Code",
5+
"description": "Language Support for Gem",
66
"icon": "logo.png",
77
"version": "1.0.10",
88
"engines": {

0 commit comments

Comments
 (0)