From b5edabc10433c1dfe444a2c07ad1eaccc71ef188 Mon Sep 17 00:00:00 2001 From: Vick Scarlet Date: Thu, 2 Dec 2021 18:18:46 +0800 Subject: [PATCH] add hint message --- src/i18n/en-us.js | 8 ++++++++ src/i18n/zh-cn.js | 8 ++++++++ src/index.js | 9 +++++---- src/ui/themes/saveload.js | 28 ++++++++++++++++++++-------- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/i18n/en-us.js b/src/i18n/en-us.js index 6c1c4a0..1717287 100644 --- a/src/i18n/en-us.js +++ b/src/i18n/en-us.js @@ -124,6 +124,14 @@ export default ({ UI_Read: 'Copy', UI_Write: 'Paste', + UI_CopySuccess: 'Copy to clipboard success', + UI_CopyFaild: 'Copy to clipboard faild', + + UI_PasteSuccessDecodeSuccess: 'Load Success!', + UI_PasteSuccessDecodeFaild: 'Paste success, but load faild!', + UI_PasteFaildDecodeSuccess: 'Paste faild, use input load success!', + UI_PasteFaildDecodeFaild: 'Paste faild, use input load faild!', + UI_LoadSuccess: 'Load Success!', UI_LoadFaild: 'Load Faild!', diff --git a/src/i18n/zh-cn.js b/src/i18n/zh-cn.js index 39901e2..526dcb4 100644 --- a/src/i18n/zh-cn.js +++ b/src/i18n/zh-cn.js @@ -127,6 +127,14 @@ export default ({ UI_LoadSuccess: '读档成功!', UI_LoadFaild: '读档失败!', + UI_CopySuccess: '成功复制存档到剪贴板', + UI_CopyFaild: '复制存档到剪贴板失败,请手动复制存档内容', + + UI_PasteSuccessDecodeSuccess: '读档成功!', + UI_PasteSuccessDecodeFaild: '粘贴剪贴板存档内容成功,但剪贴板内容读档失败', + UI_PasteFaildDecodeSuccess: '粘贴剪贴板存档内容失败,使用输入的内容读档成功', + UI_PasteFaildDecodeFaild: '粘贴剪贴板存档内容失败,使用输入的内容读档失败', + M_NoRank: '别卷了,没有排行榜', F_RemakeTimes: '已重开{0}次', diff --git a/src/index.js b/src/index.js index 54aae3e..b23fb7f 100644 --- a/src/index.js +++ b/src/index.js @@ -38,15 +38,16 @@ globalThis.$$copy = async text => { const result = await navigator.permissions.query({ name: "clipboard-write" }) if (result.state == "granted" || result.state == "prompt") { navigator.clipboard.writeText(text) - return; + return true; } const input = document.createElement('input'); input.setAttribute('style', 'opacity: 0;'); document.body.appendChild(input); input.value = text; input.select(); - document.execCommand("copy"); + const r = document.execCommand("copy"); document.body.removeChild(input); + return r; } globalThis.$$read = async ()=>{ @@ -58,10 +59,10 @@ globalThis.$$read = async ()=>{ input.setAttribute('style', 'opacity: 0;'); document.body.appendChild(input); input.focus(); - document.execCommand("paste"); + const r = document.execCommand("paste"); const text = input.value; document.body.removeChild(input); - return text; + return r?text:r; }; const core = new Life(); diff --git a/src/ui/themes/saveload.js b/src/ui/themes/saveload.js index c0dcda6..44250a6 100644 --- a/src/ui/themes/saveload.js +++ b/src/ui/themes/saveload.js @@ -5,13 +5,25 @@ export default class SaveLoad extends ui.view.SaveLoadUI { await this.close(); $ui.switchView(UI.pages.MAIN); }); - this.btnRead.on(Laya.Event.CLICK, this,()=>$$copy(this.input.text = this.data)); + this.btnRead.on(Laya.Event.CLICK, this, async ()=>{ + const result = await $$copy(this.input.text = this.data); + $$event('message', [result? 'UI_CopySuccess': 'UI_CopyFaild']); + }); this.btnWrite.on(Laya.Event.CLICK, this, async ()=>{ const text = await $$read(); - if(text) { - this.data = this.input.text = text; + if(text == false) { + this.data = [ + this.input.text, + 'UI_PasteFaildDecodeSuccess', + 'UI_PasteFaildDecodeFaild' + ]; } else { - this.data = this.input.text; + this.input.text = text; + this.data = [ + text, + 'UI_PasteSuccessDecodeSuccess', + 'UI_PasteSuccessDecodeFaild' + ]; } }); @@ -40,7 +52,7 @@ export default class SaveLoad extends ui.view.SaveLoadUI { const file = e.target.files[0]; if(!file) return; const reader = new FileReader(); - reader.onload = () => this.data = reader.result; + reader.onload = () => this.data = [reader.result]; reader.readAsText(file); document.body.removeChild(file); }; @@ -67,17 +79,17 @@ export default class SaveLoad extends ui.view.SaveLoadUI { return JSON.stringify(data); } - set data(v) { + set data([v, success = 'UI_LoadSuccess', faild = 'UI_LoadFaild']) { try { const data = JSON.parse(v); for(const key in data) localStorage.setItem(key, data[key]); - $$event('message', ['UI_LoadSuccess']); + $$event('message', [success]); $ui.theme = $ui.theme; this.btnClose.event(Laya.Event.CLICK); } catch (e) { console.error(e); - $$event('message', ['UI_LoadFaild']); + $$event('message', [faild]); } } } \ No newline at end of file