@waradu/keyboard
Version:
A keyboard manager compatible with JavaScript, TypeScript and Nuxt
54 lines (52 loc) • 1.78 kB
JavaScript
function merge(...configs) {
const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
return configs.reduceRight(
(out, config) => {
if (!config) return out;
for (const key in config) {
const value = config[key];
if (value === void 0) continue;
out[key] = isObj(out[key]) && isObj(value) ? merge(value, out[key]) : value;
}
return out;
},
{}
);
}
const isEditableElement = (element) => {
if (element instanceof HTMLTextAreaElement) {
return !element.readOnly && !element.disabled;
}
if (element instanceof HTMLInputElement) {
const nonEditableTypes = /* @__PURE__ */ new Set([
"button",
"checkbox",
"color",
"file",
"hidden",
"image",
"radio",
"range",
"reset",
"submit"
]);
return !element.readOnly && !element.disabled && !nonEditableTypes.has(element.type);
}
const contentEditable = element.closest("[contenteditable]");
if (!contentEditable) return false;
const value = contentEditable.getAttribute("contenteditable")?.toLowerCase();
return value === "" || value === "true" || value === "plaintext-only";
};
function detectOsInBrowser() {
if (typeof navigator === "undefined") return "unknown";
const platform = (navigator.platform || "").toLowerCase();
const ua = (navigator.userAgent || "").toLowerCase();
if (platform.includes("mac")) return "macos";
if (platform.includes("win")) return "windows";
if (platform.includes("linux")) return "linux";
if (ua.includes("mac os")) return "macos";
if (ua.includes("windows")) return "windows";
if (ua.includes("linux") || ua.includes("x11")) return "linux";
return "unknown";
}
export { detectOsInBrowser, isEditableElement, merge };