Skip to content

Commit

Permalink
feat(core): 添加跳过版本的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Sep 12, 2023
1 parent e6a80d5 commit 63913b2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 42 deletions.
65 changes: 36 additions & 29 deletions packages/core/src/elements/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export class ModalElement extends IElement {
/** 弹窗主体 */
body: HTMLDivElement = el('div', { className: 'modal-body' });
/** 弹窗底部 */
footer: HTMLDivElement = el('div', { className: 'modal-footer' });
footerContainer: HTMLDivElement = el('div', { className: 'modal-footer' });
footer: HTMLDivElement = el('div');
/** 弹窗确认按钮 */
confirmButton?: HTMLButtonElement | null;
/** 弹窗取消按钮 */
Expand Down Expand Up @@ -85,39 +86,45 @@ export class ModalElement extends IElement {
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.append(profile, this._title, this.body, this.footerContainer);
// 设置模态框宽度
this.style.width = (this.width || 400) + 'px';
if (this.cancelButton === undefined) {
this.cancelButton = el('button', { className: 'modal-cancel-button' });
this.cancelButton.innerText = this.cancelButtonText || '取消';
this.cancelButton.addEventListener('click', () => {
this.onCancel?.();
this.onClose?.();
this.remove();
});
}
if (this.confirmButton === undefined) {
this.confirmButton = el('button', { className: 'modal-confirm-button' });
this.confirmButton.innerText = this.confirmButtonText || '确定';
this.confirmButton.addEventListener('click', async () => {
if ((await this.onConfirm?.(this.modalInput.value)) !== false) {
// 底部

// 如果没有自定义底部,则按照类型来添加
if (this.footer === undefined) {
this.footerContainer.append(this.modalInput);
if (this.footer === undefined && this.cancelButton === undefined) {
this.cancelButton = el('button', { className: 'modal-cancel-button' });
this.cancelButton.innerText = this.cancelButtonText || '取消';
this.cancelButton.addEventListener('click', () => {
this.onCancel?.();
this.onClose?.();
this.remove();
this.onClose?.(this.modalInput.value);
}
});
}
});
}
if (this.footer === undefined && this.confirmButton === undefined) {
this.confirmButton = el('button', { className: 'modal-confirm-button' });
this.confirmButton.innerText = this.confirmButtonText || '确定';
this.confirmButton.addEventListener('click', async () => {
if ((await this.onConfirm?.(this.modalInput.value)) !== false) {
this.remove();
this.onClose?.(this.modalInput.value);
}
});
}

this.cancelButton !== null && this.footer.append(this.cancelButton);
this.confirmButton !== null && this.footer.append(this.confirmButton);
this.cancelButton && this.footerContainer.append(this.cancelButton);
this.confirmButton && this.footerContainer.append(this.confirmButton);

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

$.onresize(this.body, (modal) => {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/render/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type ModalAttrs = Pick<
/** 消息显示时间(秒) */
duration?: number;
};
footer?: HTMLDivElement;
};

const minimizeSvg =
Expand Down
39 changes: 26 additions & 13 deletions packages/scripts/src/projects/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ export const BackgroundProject = Project.create({
configs: {
notToday: {
defaultValue: -1
},
ignoreVersions: {
defaultValue: [] as string[]
}
},
oncomplete() {
Expand All @@ -336,29 +339,39 @@ export const BackgroundProject = Project.create({
method: 'get',
type: 'GM_xmlhttpRequest'
});
if (gt(version['last-version'], infos.script.version)) {

if (
// 跳过主动忽略的版本
this.cfg.ignoreVersions.includes(version['last-version']) === false &&
// 版本比较
gt(version['last-version'], infos.script.version)
) {
const modal = $modal('confirm', {
width: 600,
content: $creator.notes([
`检测到新版本发布 ${version['last-version']} :`,
[...(version.notes || [])]
]),
cancelButton: el(
'button',
{ className: 'base-style-button-secondary', innerText: '今日不再提示' },
(btn) => {
footer: el('div', [
el('button', { className: 'base-style-button-secondary', innerText: '跳过此版本' }, (btn) => {
btn.onclick = () => {
this.cfg.ignoreVersions = [...this.cfg.ignoreVersions, version['last-version']];
modal?.remove();
};
}),
el('button', { className: 'base-style-button-secondary', innerText: '今日不再提示' }, (btn) => {
btn.onclick = () => {
this.cfg.notToday = new Date().getDate();
modal?.remove();
};
}
),
confirmButton: el('button', { className: 'base-style-button', innerText: '前往更新' }, (btn) => {
btn.onclick = () => {
window.open(version.resource[infos.scriptHandler], '_blank');
modal?.remove();
};
})
}),
el('button', { className: 'base-style-button', innerText: '前往更新' }, (btn) => {
btn.onclick = () => {
window.open(version.resource[infos.scriptHandler], '_blank');
modal?.remove();
};
})
])
});
}
}, 5 * 1000);
Expand Down

0 comments on commit 63913b2

Please sign in to comment.