From 2f325cfe266057e1cfb2d6d0a343ac0f68ebb6f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Sun, 5 May 2024 13:01:23 +0800 Subject: [PATCH] feat:webui-2 --- static/components/NapCat.ts | 13 +---- static/components/SettingButton.ts | 3 ++ static/components/SettingItem.ts | 15 ++++++ static/components/SettingList.ts | 14 +++++ static/components/SettingOption.ts | 3 ++ static/components/SettingSelect.ts | 84 ++++++++++++++++++++++++++++++ static/components/SettingSwitch.ts | 8 +++ 7 files changed, 128 insertions(+), 12 deletions(-) create mode 100644 static/components/SettingButton.ts create mode 100644 static/components/SettingItem.ts create mode 100644 static/components/SettingList.ts create mode 100644 static/components/SettingOption.ts create mode 100644 static/components/SettingSelect.ts create mode 100644 static/components/SettingSwitch.ts diff --git a/static/components/NapCat.ts b/static/components/NapCat.ts index 56927283..4d715a4b 100644 --- a/static/components/NapCat.ts +++ b/static/components/NapCat.ts @@ -1,20 +1,9 @@ -/// -import { CheckVersion } from '../common/types' -import { SettingButton, SettingItem, SettingList, SettingSwitch, SettingSelect } from './components' -// @ts-ignore + import StyleRaw from './style.css?raw' -import { iconSvg } from './icon' // 打开设置界面时触发 function aprilFoolsEgg(node: Element) { - let today = new Date() - if (today.getDate() === 1) { - console.log('超时空猫猫!!!') - node.querySelector('.name').innerHTML = 'ChronoCat' - } -} - function initSideBar() { document.querySelectorAll('.nav-item.liteloader').forEach((node) => { if (node.textContent.startsWith('LLOneBot')) { diff --git a/static/components/SettingButton.ts b/static/components/SettingButton.ts new file mode 100644 index 00000000..201ffc88 --- /dev/null +++ b/static/components/SettingButton.ts @@ -0,0 +1,3 @@ +export const SettingButton = (text: string, id?: string, type: string = 'secondary') => { + return `${text}` + } \ No newline at end of file diff --git a/static/components/SettingItem.ts b/static/components/SettingItem.ts new file mode 100644 index 00000000..fe2208f7 --- /dev/null +++ b/static/components/SettingItem.ts @@ -0,0 +1,15 @@ +export const SettingItem = ( + title: string, + subtitle?: string, + action?: string, + id?: string, + visible: boolean = true, + ) => { + return ` +
+ ${title} + ${subtitle ? `${subtitle}` : ''} +
+ ${action ? `
${action}
` : ''} +
` + } \ No newline at end of file diff --git a/static/components/SettingList.ts b/static/components/SettingList.ts new file mode 100644 index 00000000..35c09664 --- /dev/null +++ b/static/components/SettingList.ts @@ -0,0 +1,14 @@ +export const SettingList = ( + items: string[], + title?: string, + isCollapsible: boolean = false, + direction: string = 'column', + ) => { + return ` + + + ${items.join('')} + + + ` + } \ No newline at end of file diff --git a/static/components/SettingOption.ts b/static/components/SettingOption.ts new file mode 100644 index 00000000..a5a6fc03 --- /dev/null +++ b/static/components/SettingOption.ts @@ -0,0 +1,3 @@ +export const SettingOption = (text: string, value?: string, isSelected: boolean = false) => { + return `${text}` + } \ No newline at end of file diff --git a/static/components/SettingSelect.ts b/static/components/SettingSelect.ts new file mode 100644 index 00000000..e5627517 --- /dev/null +++ b/static/components/SettingSelect.ts @@ -0,0 +1,84 @@ +import { SettingOption } from './option' + +interface MouseEventExtend extends MouseEvent { + target: HTMLElement +} + +// +const SelectTemplate = document.createElement('template') +SelectTemplate.innerHTML = ` +
+
+ + + + +
+ +
` + +window.customElements.define( + 'ob-setting-select', + class extends HTMLElement { + readonly _button: HTMLDivElement + readonly _text: HTMLInputElement + readonly _context: HTMLUListElement + + constructor() { + super() + + this.attachShadow({ mode: 'open' }) + this.shadowRoot.append(SelectTemplate.content.cloneNode(true)) + + this._button = this.shadowRoot.querySelector('div[part="button"]') + this._text = this.shadowRoot.querySelector('input[part="current-text"]') + this._context = this.shadowRoot.querySelector('ul[part="option-list"]') + + const buttonClick = () => { + const isHidden = this._context.classList.toggle('hidden') + window[`${isHidden ? 'remove' : 'add'}EventListener`]('pointerdown', windowPointerDown) + } + + const windowPointerDown = ({ target }) => { + if (!this.contains(target)) buttonClick() + } + + this._button.addEventListener('click', buttonClick) + this._context.addEventListener('click', ({ target }: MouseEventExtend) => { + if (target.tagName !== 'SETTING-OPTION') return + buttonClick() + + if (target.hasAttribute('is-selected')) return + + this.querySelectorAll('setting-option[is-selected]').forEach((dom) => dom.toggleAttribute('is-selected')) + target.toggleAttribute('is-selected') + + this._text.value = target.textContent + this.dispatchEvent( + new CustomEvent('selected', { + bubbles: true, + composed: true, + detail: { + name: target.textContent, + value: target.dataset.value, + }, + }), + ) + }) + + this._text.value = this.querySelector('setting-option[is-selected]').textContent + } + }, +) + +export const SettingSelect = (items: Array<{ text: string; value: string }>, configKey?: string, configValue?: any) => { + return ` + ${items + .map((e, i) => { + return SettingOption(e.text, e.value, configKey && configValue ? configValue === e.value : i === 0) + }) + .join('')} +` +} \ No newline at end of file diff --git a/static/components/SettingSwitch.ts b/static/components/SettingSwitch.ts new file mode 100644 index 00000000..35936d0f --- /dev/null +++ b/static/components/SettingSwitch.ts @@ -0,0 +1,8 @@ +export const SettingSwitch = (configKey?: string, isActive: boolean = false, extraData?: Record) => { + return ` `data-${key}="${extraData[key]}"`) : ''} + > + ` + } \ No newline at end of file