mirror of
https://github.com/devonthesofa/obsidian-note-status.git
synced 2026-07-22 05:45:04 +00:00
refactor: reuse color logic
This commit is contained in:
parent
da2572398d
commit
a13adb265d
3 changed files with 72 additions and 34 deletions
|
|
@ -2,6 +2,11 @@ import { StatusIconPreview } from "@/components/atoms/StatusIconPreview";
|
|||
import { GroupedStatuses } from "@/types/noteStatus";
|
||||
import React, { FC, memo } from "react";
|
||||
import { StatusIcon } from "@/components/atoms/StatusIcon";
|
||||
import {
|
||||
getPrimaryStatus,
|
||||
getUnknownStatusColor,
|
||||
resolveStatusColor,
|
||||
} from "@/utils/statusColor";
|
||||
|
||||
type Props = {
|
||||
statuses: GroupedStatuses;
|
||||
|
|
@ -35,15 +40,12 @@ export const FileExplorerIcon: FC<Props> = memo(
|
|||
|
||||
const useStatusColors = iconColorMode === "status";
|
||||
|
||||
const getStatusColor = (color?: string) =>
|
||||
(color && color.trim()) || "var(--text-accent)";
|
||||
|
||||
if (totalStatuses === 0) {
|
||||
if (hideUnknownStatus) return null;
|
||||
|
||||
const icon = unknownStatusConfig?.icon || "❓";
|
||||
const color = useStatusColors
|
||||
? unknownStatusConfig?.color?.trim() || "#8b949e"
|
||||
? unknownStatusConfig?.color?.trim() || getUnknownStatusColor()
|
||||
: undefined;
|
||||
|
||||
return (
|
||||
|
|
@ -64,10 +66,10 @@ export const FileExplorerIcon: FC<Props> = memo(
|
|||
);
|
||||
}
|
||||
|
||||
const primaryStatus = statusEntries[0]?.[1]?.[0];
|
||||
const primaryStatus = getPrimaryStatus(statuses);
|
||||
if (!primaryStatus) return null;
|
||||
const iconColor = useStatusColors
|
||||
? getStatusColor(primaryStatus.color)
|
||||
? resolveStatusColor(primaryStatus)
|
||||
: undefined;
|
||||
|
||||
return (
|
||||
|
|
|
|||
|
|
@ -6,7 +6,12 @@ import {
|
|||
} from "@/core/lazyElementObserver";
|
||||
import { NoteStatusService } from "@/core/noteStatusService";
|
||||
import settingsService from "@/core/settingsService";
|
||||
import { GroupedStatuses, NoteStatus } from "@/types/noteStatus";
|
||||
import { GroupedStatuses } from "@/types/noteStatus";
|
||||
import {
|
||||
getPrimaryStatus,
|
||||
getUnknownStatusColor,
|
||||
resolveStatusColor,
|
||||
} from "@/utils/statusColor";
|
||||
import { Plugin, TFile, View } from "obsidian";
|
||||
import { createRoot } from "react-dom/client";
|
||||
import { StatusesInfoPopup } from "../popups/statusesInfoPopupIntegration";
|
||||
|
|
@ -119,11 +124,10 @@ export class FileExplorerIntegration implements IElementProcessor {
|
|||
}
|
||||
|
||||
private getUnknownStatusConfig() {
|
||||
const settings = settingsService.settings;
|
||||
return {
|
||||
icon: settings.unknownStatusIcon || "❓",
|
||||
lucideIcon: settings.unknownStatusLucideIcon || "",
|
||||
color: settings.unknownStatusColor || "#8b949e",
|
||||
icon: settingsService.settings.unknownStatusIcon || "❓",
|
||||
lucideIcon: settingsService.settings.unknownStatusLucideIcon || "",
|
||||
color: getUnknownStatusColor(),
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -293,13 +297,16 @@ export class FileExplorerIntegration implements IElementProcessor {
|
|||
return;
|
||||
}
|
||||
|
||||
const primaryStatus = this.getPrimaryStatus(statuses);
|
||||
const primaryStatus = getPrimaryStatus(statuses);
|
||||
if (!primaryStatus) {
|
||||
this.clearFileNameColor(element);
|
||||
return;
|
||||
}
|
||||
|
||||
const color = primaryStatus.color?.trim() || this.getFallbackColor();
|
||||
const color = resolveStatusColor(
|
||||
primaryStatus,
|
||||
getUnknownStatusColor(),
|
||||
);
|
||||
if (!color) {
|
||||
this.clearFileNameColor(element);
|
||||
return;
|
||||
|
|
@ -314,27 +321,6 @@ export class FileExplorerIntegration implements IElementProcessor {
|
|||
element.style.color = color;
|
||||
}
|
||||
|
||||
private getPrimaryStatus(
|
||||
statuses?: GroupedStatuses | null,
|
||||
): NoteStatus | null {
|
||||
if (!statuses) {
|
||||
return null;
|
||||
}
|
||||
for (const list of Object.values(statuses)) {
|
||||
if (list.length) {
|
||||
return list[0];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private getFallbackColor(): string {
|
||||
return (
|
||||
settingsService.settings.unknownStatusColor?.trim() ||
|
||||
"var(--text-accent)"
|
||||
);
|
||||
}
|
||||
|
||||
private clearFileNameColor(element: HTMLElement): void {
|
||||
if (!element.dataset[this.FILE_NAME_COLORIZED_ATTR]) {
|
||||
return;
|
||||
|
|
|
|||
50
utils/statusColor.ts
Normal file
50
utils/statusColor.ts
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
import settingsService from "@/core/settingsService";
|
||||
import { GroupedStatuses, NoteStatus } from "@/types/noteStatus";
|
||||
|
||||
export const DEFAULT_STATUS_ACCENT_COLOR = "var(--text-accent)";
|
||||
const DEFAULT_UNKNOWN_STATUS_COLOR = "#8b949e";
|
||||
|
||||
export function getPrimaryStatus(
|
||||
statuses?: GroupedStatuses | null,
|
||||
): NoteStatus | null {
|
||||
if (!statuses) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (const list of Object.values(statuses)) {
|
||||
if (list.length) {
|
||||
return list[0];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export function getUnknownStatusColor(): string {
|
||||
return (
|
||||
settingsService.settings.unknownStatusColor?.trim() ||
|
||||
DEFAULT_UNKNOWN_STATUS_COLOR
|
||||
);
|
||||
}
|
||||
|
||||
export function resolveStatusColor(
|
||||
status?: NoteStatus | null,
|
||||
fallbackColor?: string,
|
||||
defaultColor: string | null = DEFAULT_STATUS_ACCENT_COLOR,
|
||||
): string | undefined {
|
||||
const statusColor = status?.color?.trim();
|
||||
if (statusColor) {
|
||||
return statusColor;
|
||||
}
|
||||
|
||||
const fallback = fallbackColor?.trim();
|
||||
if (fallback) {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
if (defaultColor === null) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return defaultColor || undefined;
|
||||
}
|
||||
Loading…
Reference in a new issue