mirror of
https://github.com/SupertigerDev/waybar-online.git
synced 2026-03-24 16:06:37 +00:00
Add date-fns dependency and enhance Label and Clock modules for date formatting
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import type { Module } from "./createModule";
|
||||
import { clamp, replaceTextWithUnicode } from "./utils";
|
||||
import { format as formatDate } from "date-fns";
|
||||
|
||||
interface LabelOpts {
|
||||
config?: {
|
||||
@@ -8,12 +9,15 @@ interface LabelOpts {
|
||||
states?: Record<string, number>;
|
||||
"format-icons"?: string[];
|
||||
};
|
||||
clickable?: boolean;
|
||||
defaultFormat?: string;
|
||||
module: Module;
|
||||
interval?: number;
|
||||
update: () => void;
|
||||
}
|
||||
export const createLabel = (opts: LabelOpts) => {
|
||||
let format = "";
|
||||
let alt = false;
|
||||
let format = opts.defaultFormat || "";
|
||||
|
||||
const getFormat = () => {
|
||||
if (!opts.config) return undefined;
|
||||
@@ -36,8 +40,28 @@ export const createLabel = (opts: LabelOpts) => {
|
||||
console.error("No format found");
|
||||
return "N/A";
|
||||
}
|
||||
const result = format.replace(/\{(\w+)\}/g, (match, key) => {
|
||||
return data[key] !== undefined ? data[key] : match;
|
||||
|
||||
const regex = /\{(\w+)\}|{:(.*?)}/g;
|
||||
const result = format.replace(regex, (match, key, dateFmt) => {
|
||||
if (key !== undefined) {
|
||||
return data[key] !== undefined ? data[key] : match;
|
||||
}
|
||||
|
||||
if (dateFmt !== undefined) {
|
||||
const now = new Date();
|
||||
|
||||
const convertedFormat = dateFmt
|
||||
.replace(/%Y/g, "yyyy")
|
||||
.replace(/%m/g, "MM")
|
||||
.replace(/%d/g, "dd")
|
||||
.replace(/%H/g, "HH")
|
||||
.replace(/%M/g, "mm")
|
||||
.replace(/%S/g, "ss");
|
||||
|
||||
return formatDate(now, convertedFormat);
|
||||
}
|
||||
|
||||
return match;
|
||||
});
|
||||
|
||||
element.innerHTML = replaceTextWithUnicode(result);
|
||||
@@ -83,6 +107,20 @@ export const createLabel = (opts: LabelOpts) => {
|
||||
return valid_state;
|
||||
};
|
||||
|
||||
if (opts.clickable) {
|
||||
element.style.cursor = "pointer";
|
||||
|
||||
element.onclick = () => {
|
||||
alt = !alt;
|
||||
if (alt && opts.config?.["format-alt"]) {
|
||||
setFormat(opts.config?.["format-alt"]);
|
||||
} else {
|
||||
setFormat(opts.config?.format || opts.defaultFormat || "");
|
||||
}
|
||||
opts.update();
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
element,
|
||||
set,
|
||||
|
||||
@@ -1,16 +1,23 @@
|
||||
import type { WaybarConfig } from "../configParser";
|
||||
import type { Module } from "../createModule";
|
||||
import { createLabel } from "../Label";
|
||||
|
||||
export const createClockModule = (
|
||||
module: Module,
|
||||
config: WaybarConfig["clock"]
|
||||
) => {
|
||||
const update = () => {
|
||||
const date = new Date();
|
||||
const label = createLabel({
|
||||
config,
|
||||
module,
|
||||
clickable: true,
|
||||
update: () => update(),
|
||||
defaultFormat: "{:%H:%M}",
|
||||
interval: 1000,
|
||||
});
|
||||
|
||||
const hours = date.getHours().toString().padStart(2, "0");
|
||||
const minutes = date.getMinutes().toString().padStart(2, "0");
|
||||
module.element.innerHTML = `${hours}:${minutes}`;
|
||||
module.element.appendChild(label.element);
|
||||
const update = () => {
|
||||
label.set({});
|
||||
};
|
||||
|
||||
update();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#waybar {
|
||||
display: flex;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.modules {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { parseConfig } from "./configParser";
|
||||
import { createModule } from "./createModule";
|
||||
import { parseConfig, type WaybarConfig } from "./configParser";
|
||||
import { createModule, type Module } from "./createModule";
|
||||
import { modules, type ModuleArray, type Modules } from "./modules";
|
||||
|
||||
import "./waybar.css";
|
||||
@@ -29,25 +29,30 @@ export const createWaybarPage = async () => {
|
||||
const moduleCenter = (config["modules-center"] as ModuleArray) || [];
|
||||
const moduleRight = (config["modules-right"] as ModuleArray) || [];
|
||||
|
||||
const loadModule = async (
|
||||
name: string,
|
||||
createModuleBase: (name: string) => any,
|
||||
loadIntoElement: HTMLElement,
|
||||
config: WaybarConfig
|
||||
) => {
|
||||
if (!modules[name as Modules]) {
|
||||
console.error(`Module ${name} not found`);
|
||||
return;
|
||||
}
|
||||
const module = createModuleBase(name);
|
||||
loadIntoElement.appendChild(module.element);
|
||||
const createModule = await modules[name as Modules]?.();
|
||||
|
||||
createModule(module, (config as any)[name]);
|
||||
console.log(`Loaded module ${name}`);
|
||||
};
|
||||
|
||||
const loadModules = async (
|
||||
enabledModules: ModuleArray,
|
||||
modulesElement: HTMLElement
|
||||
) => {
|
||||
enabledModules.forEach(async (moduleName) => {
|
||||
if (moduleName === "battery") {
|
||||
console.log(moduleName);
|
||||
const module = createModule("battery");
|
||||
modulesElement.appendChild(module.element);
|
||||
const createBatteryModule = await modules.battery();
|
||||
createBatteryModule(module, config.battery);
|
||||
}
|
||||
if (moduleName === "clock") {
|
||||
console.log(moduleName);
|
||||
const module = createModule("clock");
|
||||
modulesElement.appendChild(module.element);
|
||||
const createClockModule = await modules.clock();
|
||||
createClockModule(module, config.clock);
|
||||
}
|
||||
loadModule(moduleName, createModule, modulesElement, config);
|
||||
});
|
||||
};
|
||||
loadModules(moduleLeft, modulesLeftElement);
|
||||
|
||||
Reference in New Issue
Block a user