class HarmonyOSPopupExtension {
constructor() { this.lastResult = null; }
getInfo() {
return {
id: 'HarmonyOSTC',
name: 'Morgan的HarmonyOS弹窗',
color1: 0x5ca05c,
color2: 0xa5d296,
blocks: [
{ opcode: 'showInputDialog', blockType: Scratch.BlockType.COMMAND, text: '输入框提示 标题 [TITLE] 提示 [HINT] 默认值 [DEFAULT] 积极按钮 [POSITIVE] 消极按钮 [NEGATIVE]', arguments: { TITLE: { type: Scratch.ArgumentType.STRING, defaultValue: '请输入' }, HINT: { type: Scratch.ArgumentType.STRING, defaultValue: '请输入内容' }, DEFAULT: { type: Scratch.ArgumentType.STRING, defaultValue: '' }, POSITIVE: { type: Scratch.ArgumentType.STRING, defaultValue: '确定' }, NEGATIVE: { type: Scratch.ArgumentType.STRING, defaultValue: '取消' } } },
{ opcode: 'getInputResult', blockType: Scratch.BlockType.REPORTER, text: '输入结果' },
{ opcode: 'showTextDialog', blockType: Scratch.BlockType.COMMAND, text: '文字提示 标题 [TITLE] 内容 [CONTENT] 按钮 [BUTTON]', arguments: { TITLE: { type: Scratch.ArgumentType.STRING, defaultValue: '提示' }, CONTENT: { type: Scratch.ArgumentType.STRING, defaultValue: '操作完成' }, BUTTON: { type: Scratch.ArgumentType.STRING, defaultValue: '确定' } } },
{ opcode: 'showListDialog', blockType: Scratch.BlockType.COMMAND, text: '列表选择 标题 [TITLE] 选项 [OPTIONS] 积极按钮 [POSITIVE] 消极按钮 [NEGATIVE]', arguments: { TITLE: { type: Scratch.ArgumentType.STRING, defaultValue: '请选择' }, OPTIONS: { type: Scratch.ArgumentType.STRING, defaultValue: '选项1,选项2,选项3' }, POSITIVE: { type: Scratch.ArgumentType.STRING, defaultValue: '确定' }, NEGATIVE: { type: Scratch.ArgumentType.STRING, defaultValue: '取消' } } },
{ opcode: 'getListResult', blockType: Scratch.BlockType.REPORTER, text: '选择结果' },
{ opcode: 'showDateDialog', blockType: Scratch.BlockType.COMMAND, text: '日期选择 标题 [TITLE] 默认日期 [DEFAULT] 积极按钮 [POSITIVE] 消极按钮 [NEGATIVE]', arguments: { TITLE: { type: Scratch.ArgumentType.STRING, defaultValue: '选择日期' }, DEFAULT: { type: Scratch.ArgumentType.STRING, defaultValue: '2024-01-01' }, POSITIVE: { type: Scratch.ArgumentType.STRING, defaultValue: '确定' }, NEGATIVE: { type: Scratch.ArgumentType.STRING, defaultValue: '取消' } } },
{ opcode: 'getDateResult', blockType: Scratch.BlockType.REPORTER, text: '日期结果' },
{ opcode: 'showColorDialog', blockType: Scratch.BlockType.COMMAND, text: '颜色选择 标题 [TITLE] 默认颜色 [DEFAULT] 积极按钮 [POSITIVE] 消极按钮 [NEGATIVE]', arguments: { TITLE: { type: Scratch.ArgumentType.STRING, defaultValue: '选择颜色' }, DEFAULT: { type: Scratch.ArgumentType.COLOR, defaultValue: '#007DFF' }, POSITIVE: { type: Scratch.ArgumentType.STRING, defaultValue: '确定' }, NEGATIVE: { type: Scratch.ArgumentType.STRING, defaultValue: '取消' } } },
{ opcode: 'getColorResult', blockType: Scratch.BlockType.REPORTER, text: '颜色结果' }
]
};
}
showInputDialog(e){return new Promise(t=>{this.createModal({title:e.TITLE,content:this.createInputContent(e.HINT,e.DEFAULT),positiveText:e.POSITIVE,negativeText:e.NEGATIVE,onPositive:(e,o)=>{this.lastResult=o?o.value:'';this.removeModal(e);t(this.lastResult)},onNegative:e=>{this.lastResult='';this.removeModal(e);t('')}})})}
getInputResult(){return this.lastResult||''}
showTextDialog(e){return new Promise(t=>{this.createModal({title:e.TITLE,content:this.createTextContent(e.CONTENT),positiveText:e.BUTTON,onPositive:e=>{this.lastResult='confirmed';this.removeModal(e);t('confirmed')}})})}
showListDialog(e){return new Promise(t=>{let o=e.OPTIONS.split(',').map(e=>e.trim());this.createModal({title:e.TITLE,content:this.createListContent(o),positiveText:e.POSITIVE,negativeText:e.NEGATIVE,onPositive:(e,o)=>{let n=o?o.querySelector('.harmony-item.selected'):null;this.lastResult=n?n.textContent:'';this.removeModal(e);t(this.lastResult)},onNegative:e=>{this.lastResult='';this.removeModal(e);t('')}})})}
getListResult(){return this.lastResult||''}
showDateDialog(e){return new Promise(t=>{this.createModal({title:e.TITLE,content:this.createDateContent(e.DEFAULT),positiveText:e.POSITIVE,negativeText:e.NEGATIVE,onPositive:(e,o)=>{this.lastResult=o?o.value:'';this.removeModal(e);t(this.lastResult)},onNegative:e=>{this.lastResult='';this.removeModal(e);t('')}})})}
getDateResult(){return this.lastResult||''}
showColorDialog(e){return new Promise(t=>{this.createModal({title:e.TITLE,content:this.createColorContent(e.DEFAULT),positiveText:e.POSITIVE,negativeText:e.NEGATIVE,onPositive:(e,o)=>{this.lastResult=o?o.value:'';this.removeModal(e);t(this.lastResult)},onNegative:e=>{this.lastResult='';this.removeModal(e);t('')}})})}
getColorResult(){return this.lastResult||''}
createModal(e){let t=document.getElementById('harmony-popup-modal');t&&document.body.removeChild(t);let o=document.createElement('div');o.id='harmony-popup-modal';o.style.cssText='position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.4);display:flex;justify-content:center;align-items:center;z-index:9999;font-family:system-ui;backdrop-filter:blur(8px);';let n=document.createElement('div');n.style.cssText='background:#FFFFFF;border-radius:16px;padding:0;max-width:400px;width:80%;box-shadow:0 20px 60px rgba(0,0,0,0.15);overflow:hidden;animation:harmony-appear 0.3s ease-out;';if(!document.getElementById('harmony-styles')){let t=document.createElement('style');t.id='harmony-styles';t.textContent='@keyframes harmony-appear{from{opacity:0;transform:scale(0.9) translateY(20px);}to{opacity:1;transform:scale(1) translateY(0);}}.harmony-item{padding:16px 20px;border-bottom:1px solid #F0F0F0;cursor:pointer;transition:background 0.2s;font-size:16px;color:#182431;}.harmony-item:hover{background:#F7F7F7;}.harmony-item.selected{background:#F0F7FF;color:#007DFF;}.harmony-item:last-child{border-bottom:none;}';document.head.appendChild(t)}e.title&&(let r=document.createElement('div');r.textContent=e.title;r.style.cssText='padding:24px 24px 16px;font-size:18px;font-weight:600;color:#182431;text-align:center;';n.appendChild(r));e.content&&n.appendChild(e.content);let i=document.createElement('div');i.style.cssText='display:flex;padding:16px 24px 24px;gap:12px;';e.negativeText&&(let s=document.createElement('button');s.textContent=e.negativeText;s.style.cssText='flex:1;background:#8F8F8F;color:white;border:none;padding:14px;border-radius:12px;font-size:16px;font-weight:500;cursor:pointer;transition:all 0.2s;';s.onmouseover=()=>s.style.transform='translateY(-1px)';s.onmouseout=()=>s.style.transform='translateY(0)';s.onclick=()=>e.onNegative(o,e.content.querySelector('input')||e.content.querySelector('.harmony-item.selected'));i.appendChild(s));e.positiveText&&(let d=document.createElement('button');d.textContent=e.positiveText;d.style.cssText='flex:1;background:#007DFF;color:white;border:none;padding:14px;border-radius:12px;font-size:16px;font-weight:500;cursor:pointer;transition:all 0.2s;';d.onmouseover=()=>d.style.transform='translateY(-1px)';d.onmouseout=()=>d.style.transform='translateY(0)';d.onclick=()=>e.onPositive(o,e.content.querySelector('input')||e.content.querySelector('.harmony-item.selected'));i.appendChild(d));n.appendChild(i);o.appendChild(n);document.body.appendChild(o);o.onclick=t=>{t.target===o&&e.onNegative&&e.onNegative(o)}}
createInputContent(e,t){let o=document.createElement('div');o.style.cssText='padding:0 24px 16px;';let n=document.createElement('input');n.type='text';n.placeholder=e;n.value=t;n.style.cssText='width:100%;padding:16px;border:2px solid #F0F0F0;border-radius:12px;font-size:16px;background:#FAFAFA;transition:all 0.2s;box-sizing:border-box;';n.onfocus=()=>{n.style.borderColor='#007DFF';n.style.background='#FFFFFF'};n.onblur=()=>{n.style.borderColor='#F0F0F0';n.style.background='#FAFAFA'};o.appendChild(n);return o}
createTextContent(e){let t=document.createElement('div');t.style.cssText='padding:8px 24px 16px;font-size:16px;color:#666;line-height:1.5;text-align:center;';t.textContent=e;return t}
createListContent(e){let t=document.createElement('div');t.style.cssText='max-height:300px;overflow-y:auto;margin:0 24px 16px;border:2px solid #F0F0F0;border-radius:12px;background:#FAFAFA;';e.forEach(e=>{let o=document.createElement('div');o.className='harmony-item';o.textContent=e;o.onclick=()=>{t.querySelectorAll('.harmony-item').forEach(e=>e.classList.remove('selected'));o.classList.add('selected')};t.appendChild(o)});return t}
createDateContent(e){let t=document.createElement('div');t.style.cssText='padding:0 24px 16px;';let o=document.createElement('input');o.type='date';o.value=e;o.style.cssText='width:100%;padding:16px;border:2px solid #F0F0F0;border-radius:12px;font-size:16px;background:#FAFAFA;';t.appendChild(o);return t}
createColorContent(e){let t=document.createElement('div');t.style.cssText='padding:0 24px 16px;';let o=document.createElement('input');o.type='color';o.value=e;o.style.cssText='width:100%;height:60px;border:2px solid #F0F0F0;border-radius:12px;background:#FAFAFA;cursor:pointer;';t.appendChild(o);return t}
removeModal(e){e&&e.parentNode&&(e.style.animation='harmony-appear 0.3s ease-out reverse',setTimeout(()=>{e.parentNode&&document.body.removeChild(e)},200))}
}
Scratch.extensions.register(new HarmonyOSPopupExtension());