Skip to content

Commit

Permalink
fix(script): 优化 $modal API ,修复 onClose 执行逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Apr 23, 2023
1 parent e074a95 commit 37cd819
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
32 changes: 30 additions & 2 deletions packages/core/src/elements/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ export class ModalElement extends IElement {
/** 弹窗取消按钮 */
cancelButton?: HTMLButtonElement | null;
/** 弹窗底部输入框 */
modalInput: HTMLInputElement = el('input', { className: 'modal-input' });
modalInput: HTMLInputElement | HTMLTextAreaElement = el('input', { className: 'modal-input' });
modalInputType?: 'input' | 'textarea' = 'input';
/**
* 弹窗类型
* prompt : 询问弹窗,并带有输入框
* confirm : 确认弹窗,带有取消按钮
* alert : 默认弹窗,只有确认按钮
*/
type: 'prompt' | 'alert' | 'confirm' = 'alert';
type: 'prompt' | 'alert' | 'confirm' | 'simple' = 'alert';
/** 弹窗内容 */
content: string | HTMLElement = '';
/** 输入框默认内容 */
Expand All @@ -41,6 +42,10 @@ export class ModalElement extends IElement {
cancelButtonText?: string;
/** 确认按钮的文本 */
confirmButtonText?: string;

/** 弹窗元素样式 */
modalStyle?: Partial<CSSStyleDeclaration> = {};

/** 返回 false 则不会关闭模态框 */
onConfirm?: (value?: string) => boolean | void | Promise<boolean | void>;
/** 点击取消的回调 */
Expand All @@ -49,7 +54,19 @@ export class ModalElement extends IElement {
onClose?: (value?: string) => void;

connectedCallback() {
/**
* 通过 class 来区分弹窗类型
* prompt : 下方带有输入框
* confirm : 输入框将被隐藏
* alert : 取消框和输入框都将被隐藏
*/
this.classList.add(this.type);

/**
* 合并样式
*/
Object.assign(this.style, this.modalStyle || {});

// 弹窗来源
const profile = el('div', {
innerText: this.profile || '弹窗来自: OCS ' + ($gm.getInfos()?.script.version || ''),
Expand All @@ -62,12 +79,17 @@ export class ModalElement extends IElement {
this.body.append(typeof this.content === 'string' ? el('div', { innerHTML: this.content }) : this.content);

// 输入框
if (this.modalInputType === 'textarea') {
this.modalInput = el('textarea', { className: 'modal-input', style: { height: '100px' } });
}
this.modalInput.placeholder = this.placeholder || '';
this.modalInput.value = this.inputDefaultValue || '';

// 底部
this.footer.append(this.modalInput);
// 添加到模态框
this.append(profile, this._title, this.body, this.footer);

this.style.width = (this.width || 400) + 'px';
if (this.cancelButton === undefined) {
this.cancelButton = el('button', { className: 'modal-cancel-button' });
Expand All @@ -92,6 +114,12 @@ export class ModalElement extends IElement {
this.cancelButton !== null && this.footer.append(this.cancelButton);
this.confirmButton !== null && this.footer.append(this.confirmButton);

if (this.type === 'simple') {
this.footer.remove();
} else if (this.type === 'prompt') {
this.modalInput.focus();
}

$.onresize(this.body, (modal) => {
this.body.style.maxHeight = window.innerHeight - 100 + 'px';
this.body.style.maxWidth = window.innerWidth - 50 + 'px';
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/interfaces/cors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class CorsEventEmitter {
* @param args 事件参数
* @param callback 事件回调,可以接收返回值
*/
emit(name: string, args: any[], callback: (returnValue: any, remote: boolean) => void): void {
emit(name: string, args: any[] = [], callback?: (returnValue: any, remote: boolean) => void): void {
$store
.getTab($const.TAB_UID)
.then((uid: string) => {
Expand All @@ -51,7 +51,7 @@ export class CorsEventEmitter {
// 移除此监听器
$store.removeChangeListener(listenerId);
// 执行回调
callback($store.get(this.keyOfReturn(id)), !!remote);
callback?.($store.get(this.keyOfReturn(id)), !!remote);
// 移除冗余的本地临时存储变量
$store.delete(this.keyOfState(id));
$store.delete(this.keyOfReturn(id));
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/render/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export type ModalAttrs = Pick<
| 'confirmButton'
| 'inputDefaultValue'
| 'profile'
| 'modalInputType'
| 'modalStyle'
> & {
/** 取消生成窗口的关闭按钮 */
disableWrapperCloseable?: boolean;
Expand Down Expand Up @@ -484,6 +486,7 @@ export function $modal(type: ModalElement['type'], attrs: ModalAttrs) {
} else {
attrs.onCancel?.();
}
attrs.onClose?.(args);
});
}
}
Expand Down

0 comments on commit 37cd819

Please sign in to comment.