mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 00:13:13 +09:00
Port packages/web_ui to TypeScript (#508)
* .jsx to .tsx * controller: tsc --build before webpack-cli * Type Annotation * disabled move into the <Input/> and <Checkbox/> * cleanup debug left over * documentation * default ModPack * modPackUpdateHandlers no undefined * PluginInfo changes proposition * PluginInfo changes proposition miss * ModRecord & ModInfo feedback loop at const mods = [ ? * ["connect", "drop", "resume", "close"] as const * .tsx to .ts when possible
This commit is contained in:
1 parent
a4893751e9
commit
838170a9ff
92 files changed
+1104
-707
No files matched your search
@@ -0,0 +1,69 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { EventEmitter } from "events";
|
||||
|
||||
class Permission extends EventEmitter {
|
||||
status!: { state: string };
|
||||
|
||||
constructor(name: PermissionName) {
|
||||
super();
|
||||
navigator.permissions.query({ name }).then(
|
||||
status => {
|
||||
this.status = status;
|
||||
this.emit("change", this.status.state);
|
||||
|
||||
if (status.addEventListener) {
|
||||
status.addEventListener("change", () => {
|
||||
this.emit("change", this.status.state);
|
||||
});
|
||||
}
|
||||
},
|
||||
// Pretend the clipboard will work if this query fails.
|
||||
() => {
|
||||
this.status = { state: "prompt" };
|
||||
this.emit("change", "prompt");
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
get state() {
|
||||
return this.status.state || "prompt";
|
||||
}
|
||||
}
|
||||
|
||||
let readPermission = new Permission("clipboard-read" as PermissionName);
|
||||
let writePermission = new Permission("clipboard-write" as PermissionName);
|
||||
|
||||
// Wrapper for navigator.clipboard suitable for use in React
|
||||
export function useClipboard() {
|
||||
let [readPermissionState, setReadPermissionState] = useState(readPermission.state);
|
||||
let [writePermissionState, setWritePermissionState] = useState(writePermission.state);
|
||||
|
||||
useEffect(() => {
|
||||
readPermission.on("change", setReadPermissionState);
|
||||
writePermission.on("change", setWritePermissionState);
|
||||
return () => {
|
||||
readPermission.off("change", setReadPermissionState);
|
||||
writePermission.off("change", setWritePermissionState);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return {
|
||||
readPermissionState: readPermissionState,
|
||||
read: async () => await navigator.clipboard.read(),
|
||||
readText: async () => await navigator.clipboard.readText(),
|
||||
writePermissionState: writePermissionState,
|
||||
write: async (data: ClipboardItems) => await navigator.clipboard.write(data),
|
||||
writeText: async (text: string) => await navigator.clipboard.writeText(text),
|
||||
|
||||
// Give a possible reason for why the clipboard is not available
|
||||
deniedReason: () => {
|
||||
if (window.isSecureContext === false) {
|
||||
if (window.location.protocol !== "https:") {
|
||||
return "Clipboard interaction is blocked due this page not being served over https.";
|
||||
}
|
||||
return "Clipboard interaction is blocked due this page not being a secure context.";
|
||||
}
|
||||
return "Clipboard interaction is blocked by the browser.";
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user