mirror of
https://github.com/SupertigerDev/waybar-online.git
synced 2026-03-24 08:06:37 +00:00
Refactor Label component for consistency and clarity in format handling
This commit is contained in:
@@ -8,14 +8,14 @@ interface LabelOpts {
|
||||
states: string[];
|
||||
"format-icons"?: string[];
|
||||
};
|
||||
module: Module
|
||||
module: Module;
|
||||
interval?: number;
|
||||
update: () => void;
|
||||
}
|
||||
export const createLabel = (opts: LabelOpts) => {
|
||||
let format = "";
|
||||
|
||||
const getMarkup = () => {
|
||||
const getFormat = () => {
|
||||
if (!opts.config) return undefined;
|
||||
if (!format) {
|
||||
return opts.config?.format || undefined;
|
||||
@@ -29,12 +29,12 @@ export const createLabel = (opts: LabelOpts) => {
|
||||
}
|
||||
|
||||
const set = (data: Record<string, any>) => {
|
||||
const markup = getMarkup() as string | undefined;
|
||||
if (!markup) {
|
||||
const format = getFormat() as string | undefined;
|
||||
if (!format) {
|
||||
console.error("No format found");
|
||||
return "N/A";
|
||||
}
|
||||
const result = markup.replace(/\{(\w+)\}/g, (match, key) => {
|
||||
const result = format.replace(/\{(\w+)\}/g, (match, key) => {
|
||||
return data[key] !== undefined ? data[key] : match;
|
||||
});
|
||||
|
||||
@@ -64,7 +64,6 @@ export const createLabel = (opts: LabelOpts) => {
|
||||
const entries = Object.entries(states) as unknown as [string, number][];
|
||||
|
||||
entries.sort((a, b) => {
|
||||
|
||||
if (lesser) {
|
||||
return a[1] - b[1];
|
||||
}
|
||||
@@ -78,15 +77,14 @@ export const createLabel = (opts: LabelOpts) => {
|
||||
} else {
|
||||
opts.module.element.classList.remove(state[0]);
|
||||
}
|
||||
})
|
||||
});
|
||||
return valid_state;
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
element,
|
||||
set,
|
||||
getIcon,
|
||||
getState
|
||||
getState,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user