diff --git a/Zframework/clipboard.lua b/Zframework/clipboard.lua index 21553f06..93c3c99e 100644 --- a/Zframework/clipboard.lua +++ b/Zframework/clipboard.lua @@ -16,41 +16,70 @@ local function _sanitize(content) return content end --- if SYSTEM~='Web' then --- local get = love.system.getClipboardText --- local set = love.system.setClipboardText --- return { --- get=function() return get() or '' end, --- set=function(content) set(_sanitize(content)) end, --- _update=NULL, --- } --- end +if SYSTEM~='Web' then + local get = love.system.getClipboardText + local set = love.system.setClipboardText + return { + get=function() return get() or '' end, + set=function(content) set(_sanitize(content)) end, + _update=NULL, + } +end + +if WEB_COMPAT_MODE then + local _clipboardBuffer='' + return { + get=function () + JS.newPromiseRequest( + JS.stringFunc( + [[ + window.navigator.clipboard + .readText() + .then((text) => _$_(text)) + .catch((e) => { + console.warn(e); + _$_(''); + }); + ]] + ), + function(data) _clipboardBuffer=data end, + function(id, error) print(id, error) end, + 3, + 'getClipboardText' + ) + return _clipboardBuffer + end, + set=function (str) + JS.callJS(JS.stringFunc( + [[ + window.navigator.clipboard + .writeText('%s') + .then(() => console.log('Copied to clipboard')) + .catch((e) => console.warn(e)); + ]], + _sanitize(str) + )) + end, + _update=NULL, + } +end local clipboard_thread=love.thread.newThread('Zframework/clipboard_thread.lua') local getCHN=love.thread.newChannel() local setCHN=love.thread.newChannel() local triggerCHN=love.thread.newChannel() -print(clipboard_thread:start(getCHN,setCHN,triggerCHN)) +clipboard_thread:start(getCHN,setCHN,triggerCHN) -local clipboard={} - -function clipboard.get() - return getCHN:peek() or '' -end - -function clipboard.set(content) - setCHN:push(_sanitize(content)) -end - -function clipboard._update() - triggerCHN:push(0) - local error = clipboard_thread:getError() - if error then - MES.new('error',error) - MES.traceback() - clipboard._update=NULL - end -end - -return clipboard \ No newline at end of file +return{ + get=function() return getCHN:peek() or '' end, + set=function(content) setCHN:push(_sanitize(content)) end, + _update=function() + triggerCHN:push(0) + local error = clipboard_thread:getError() + if error then + MES.new('error',error) + MES.traceback() + end + end, +} diff --git a/Zframework/clipboard_thread.lua b/Zframework/clipboard_thread.lua index a6380b9f..eace600d 100644 --- a/Zframework/clipboard_thread.lua +++ b/Zframework/clipboard_thread.lua @@ -3,9 +3,7 @@ local getCHN,setCHN,triggerCHN=... local CHN_demand,CHN_getCount=triggerCHN.demand,triggerCHN.getCount local CHN_push,CHN_pop=triggerCHN.push,triggerCHN.pop -print("2"); JS=require'Zframework.js' -print("3"); local yield=coroutine.yield local setThread=coroutine.wrap(function()