@real1ty-obsidian-plugins/utils 2.5.0 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/dist/core/evaluator/filter.d.ts.map +1 -1
  2. package/dist/core/evaluator/filter.js.map +1 -1
  3. package/dist/core/frontmatter-value.d.ts +143 -0
  4. package/dist/core/frontmatter-value.d.ts.map +1 -0
  5. package/dist/core/frontmatter-value.js +408 -0
  6. package/dist/core/frontmatter-value.js.map +1 -0
  7. package/dist/core/index.d.ts +1 -0
  8. package/dist/core/index.d.ts.map +1 -1
  9. package/dist/core/index.js +1 -0
  10. package/dist/core/index.js.map +1 -1
  11. package/dist/file/index.d.ts +1 -0
  12. package/dist/file/index.d.ts.map +1 -1
  13. package/dist/file/index.js +1 -0
  14. package/dist/file/index.js.map +1 -1
  15. package/dist/file/link-parser.d.ts +26 -0
  16. package/dist/file/link-parser.d.ts.map +1 -1
  17. package/dist/file/link-parser.js +59 -0
  18. package/dist/file/link-parser.js.map +1 -1
  19. package/dist/file/property-utils.d.ts +55 -0
  20. package/dist/file/property-utils.d.ts.map +1 -0
  21. package/dist/file/property-utils.js +90 -0
  22. package/dist/file/property-utils.js.map +1 -0
  23. package/dist/index.d.ts +1 -0
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/index.js +2 -0
  26. package/dist/index.js.map +1 -1
  27. package/dist/inputs/index.d.ts +2 -0
  28. package/dist/inputs/index.d.ts.map +1 -0
  29. package/dist/inputs/index.js +2 -0
  30. package/dist/inputs/index.js.map +1 -0
  31. package/dist/inputs/input-filter-manager.d.ts +72 -0
  32. package/dist/inputs/input-filter-manager.d.ts.map +1 -0
  33. package/dist/inputs/input-filter-manager.js +140 -0
  34. package/dist/inputs/input-filter-manager.js.map +1 -0
  35. package/package.json +2 -1
  36. package/src/core/evaluator/filter.ts +0 -2
  37. package/src/core/frontmatter-value.ts +528 -0
  38. package/src/core/index.ts +1 -0
  39. package/src/file/index.ts +1 -0
  40. package/src/file/link-parser.ts +73 -0
  41. package/src/file/property-utils.ts +114 -0
  42. package/src/index.ts +2 -0
  43. package/src/inputs/index.ts +1 -0
  44. package/src/inputs/input-filter-manager.ts +194 -0
@@ -0,0 +1,114 @@
1
+ import { normalizePath } from "obsidian";
2
+
3
+ import { formatWikiLink, parsePropertyLinks } from "./link-parser";
4
+
5
+ /**
6
+ * Adds a link to a property, avoiding duplicates using normalized path comparison.
7
+ * Prevents cycles and duplicate relationships by comparing normalized paths.
8
+ *
9
+ * **Important**: linkPath should be WITHOUT .md extension (wikilink format).
10
+ *
11
+ * @param currentValue - The current property value (can be string, string[], or undefined)
12
+ * @param linkPath - The file path to add (without .md extension, e.g., "folder/file")
13
+ * @returns New array with link added, or same array if link already exists
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * addLinkToProperty(undefined, "MyNote") // ["[[MyNote]]"]
18
+ * addLinkToProperty("[[Note1]]", "Note2") // ["[[Note1]]", "[[Note2]]"]
19
+ * addLinkToProperty(["[[Note1]]"], "Note2") // ["[[Note1]]", "[[Note2]]"]
20
+ * addLinkToProperty(["[[Note1]]"], "Note1") // ["[[Note1]]"] (no change - duplicate prevented)
21
+ * addLinkToProperty(["[[Folder/Note]]"], "folder/note") // ["[[Folder/Note]]", "[[folder/note|note]]"] (case-sensitive, different entry)
22
+ * ```
23
+ */
24
+ export function addLinkToProperty(
25
+ currentValue: string | string[] | undefined,
26
+ linkPath: string
27
+ ): string[] {
28
+ // Handle undefined or null
29
+ if (currentValue === undefined || currentValue === null) {
30
+ return [formatWikiLink(linkPath)];
31
+ }
32
+
33
+ // Normalize to array
34
+ const currentArray = Array.isArray(currentValue) ? currentValue : [currentValue];
35
+
36
+ const existingPaths = parsePropertyLinks(currentArray);
37
+
38
+ // Normalize paths for comparison to prevent duplicates with different casing or separators
39
+ const normalizedLinkPath = normalizePath(linkPath);
40
+
41
+ const normalizedExistingPaths = existingPaths.map((p) => normalizePath(p));
42
+
43
+ // Only add if not already present (using normalized path comparison)
44
+ if (!normalizedExistingPaths.includes(normalizedLinkPath)) {
45
+ return [...currentArray, formatWikiLink(linkPath)];
46
+ }
47
+
48
+ return currentArray;
49
+ }
50
+
51
+ /**
52
+ * Removes a link from a property using normalized path comparison.
53
+ *
54
+ * @param currentValue - The current property value (can be string, string[], or undefined)
55
+ * @param linkPath - The file path to remove (without .md extension)
56
+ * @returns New array with link removed (can be empty)
57
+ *
58
+ * @example
59
+ * ```ts
60
+ * removeLinkFromProperty(["[[Note1]]", "[[Note2]]"], "Note1") // ["[[Note2]]"]
61
+ * removeLinkFromProperty(["[[Note1]]"], "Note1") // []
62
+ * removeLinkFromProperty("[[Note1]]", "Note1") // []
63
+ * removeLinkFromProperty(undefined, "Note1") // []
64
+ * removeLinkFromProperty(["[[Folder/Note]]"], "Folder/Note") // [] (case-sensitive removal)
65
+ * ```
66
+ */
67
+ export function removeLinkFromProperty(
68
+ currentValue: string | string[] | undefined,
69
+ linkPath: string
70
+ ): string[] {
71
+ if (currentValue === undefined || currentValue === null) {
72
+ return [];
73
+ }
74
+
75
+ // Normalize to array
76
+ const currentArray = Array.isArray(currentValue) ? currentValue : [currentValue];
77
+
78
+ const normalizedLinkPath = normalizePath(linkPath);
79
+
80
+ return currentArray.filter((item) => {
81
+ const parsed = parsePropertyLinks([item])[0];
82
+
83
+ if (!parsed) return true; // Keep invalid entries
84
+
85
+ return normalizePath(parsed) !== normalizedLinkPath;
86
+ });
87
+ }
88
+
89
+ /**
90
+ * Checks if a link exists in a property using normalized path comparison.
91
+ *
92
+ * @param currentValue - The current property value (can be string, string[], or undefined)
93
+ * @param linkPath - The file path to check (without .md extension)
94
+ * @returns True if the link exists
95
+ *
96
+ * @example
97
+ * ```ts
98
+ * hasLinkInProperty(["[[Note1]]", "[[Note2]]"], "Note1") // true
99
+ * hasLinkInProperty("[[Note1]]", "Note1") // true
100
+ * hasLinkInProperty([], "Note1") // false
101
+ * hasLinkInProperty(undefined, "Note1") // false
102
+ * hasLinkInProperty(["[[Folder/Note]]"], "Folder/Note") // true (case-sensitive match)
103
+ * ```
104
+ */
105
+ export function hasLinkInProperty(
106
+ currentValue: string | string[] | undefined,
107
+ linkPath: string
108
+ ): boolean {
109
+ const existingPaths = parsePropertyLinks(currentValue);
110
+
111
+ const normalizedLinkPath = normalizePath(linkPath);
112
+
113
+ return existingPaths.some((path) => normalizePath(path) === normalizedLinkPath);
114
+ }
package/src/index.ts CHANGED
@@ -9,6 +9,8 @@ export * from "./core";
9
9
  export * from "./date";
10
10
  // File operations
11
11
  export * from "./file";
12
+ // Input utilities
13
+ export * from "./inputs";
12
14
  export * from "./settings";
13
15
  // String utilities
14
16
  export * from "./string";
@@ -0,0 +1 @@
1
+ export * from "./input-filter-manager";
@@ -0,0 +1,194 @@
1
+ export type FilterChangeCallback = (filterValue: string) => void;
2
+
3
+ const DEFAULT_DEBOUNCE_MS = 150;
4
+
5
+ export interface InputFilterManagerOptions {
6
+ placeholder: string;
7
+ cssClass: string;
8
+ cssPrefix: string;
9
+ onFilterChange: FilterChangeCallback;
10
+ initiallyVisible?: boolean;
11
+ onHide?: () => void;
12
+ debounceMs?: number;
13
+ }
14
+
15
+ /**
16
+ * Abstract base class for managing input-based filters with debouncing.
17
+ * Provides a reusable pattern for filter inputs with show/hide functionality,
18
+ * keyboard shortcuts, and debounced updates.
19
+ *
20
+ * @template T - The type of data being filtered (optional, defaults to unknown)
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * class MyFilterManager extends InputFilterManager<MyDataType> {
25
+ * shouldInclude(data: MyDataType): boolean {
26
+ * const filter = this.getCurrentValue().toLowerCase();
27
+ * return data.name.toLowerCase().includes(filter);
28
+ * }
29
+ * }
30
+ *
31
+ * const manager = new MyFilterManager(
32
+ * containerEl,
33
+ * {
34
+ * placeholder: "Filter items...",
35
+ * cssClass: "my-filter-input",
36
+ * onFilterChange: (value) => console.log("Filter changed:", value),
37
+ * initiallyVisible: false,
38
+ * }
39
+ * );
40
+ * ```
41
+ */
42
+ export abstract class InputFilterManager<T> {
43
+ protected containerEl: HTMLElement;
44
+ protected inputEl: HTMLInputElement | null = null;
45
+ protected debounceTimer: number | null = null;
46
+ protected currentValue = "";
47
+ protected persistentlyVisible = false;
48
+ protected onHide?: () => void;
49
+ protected readonly debounceMs: number;
50
+ protected readonly placeholder: string;
51
+ protected readonly cssClass: string;
52
+ protected readonly cssPrefix: string;
53
+ protected readonly onFilterChange: FilterChangeCallback;
54
+
55
+ constructor(
56
+ protected parentEl: HTMLElement,
57
+ options: InputFilterManagerOptions
58
+ ) {
59
+ const {
60
+ placeholder,
61
+ cssClass,
62
+ cssPrefix,
63
+ onFilterChange,
64
+ initiallyVisible = false,
65
+ onHide,
66
+ debounceMs = DEFAULT_DEBOUNCE_MS,
67
+ } = options;
68
+
69
+ this.debounceMs = debounceMs;
70
+ this.placeholder = placeholder;
71
+ this.cssClass = cssClass;
72
+ this.cssPrefix = cssPrefix;
73
+ this.onFilterChange = onFilterChange;
74
+ this.onHide = onHide;
75
+
76
+ const classes = `${cssClass}-container${initiallyVisible ? "" : ` ${cssPrefix}-hidden`}`;
77
+ this.containerEl = this.parentEl.createEl("div", {
78
+ cls: classes,
79
+ });
80
+
81
+ this.render();
82
+ }
83
+
84
+ private render(): void {
85
+ this.inputEl = this.containerEl.createEl("input", {
86
+ type: "text",
87
+ cls: this.cssClass,
88
+ placeholder: this.placeholder,
89
+ });
90
+
91
+ this.inputEl.addEventListener("input", () => {
92
+ this.handleInputChange();
93
+ });
94
+
95
+ this.inputEl.addEventListener("keydown", (evt) => {
96
+ if (evt.key === "Escape") {
97
+ // Only allow hiding if not persistently visible
98
+ if (!this.persistentlyVisible) {
99
+ this.hide();
100
+ } else {
101
+ // Just blur the input if persistently visible
102
+ this.inputEl?.blur();
103
+ }
104
+ } else if (evt.key === "Enter") {
105
+ this.applyFilterImmediately();
106
+ }
107
+ });
108
+ }
109
+
110
+ private handleInputChange(): void {
111
+ if (this.debounceTimer !== null) {
112
+ window.clearTimeout(this.debounceTimer);
113
+ }
114
+
115
+ this.debounceTimer = window.setTimeout(() => {
116
+ this.applyFilterImmediately();
117
+ }, this.debounceMs);
118
+ }
119
+
120
+ private applyFilterImmediately(): void {
121
+ const newValue = this.inputEl?.value.trim() ?? "";
122
+
123
+ if (newValue !== this.currentValue) {
124
+ this.updateFilterValue(newValue);
125
+ }
126
+ }
127
+
128
+ protected updateFilterValue(value: string): void {
129
+ this.currentValue = value;
130
+ this.onFilterChange(value);
131
+ }
132
+
133
+ getCurrentValue(): string {
134
+ return this.currentValue;
135
+ }
136
+
137
+ show(): void {
138
+ this.containerEl.removeClass(`${this.cssPrefix}-hidden`);
139
+ this.inputEl?.focus();
140
+ }
141
+
142
+ hide(): void {
143
+ // Don't allow hiding if persistently visible
144
+ if (this.persistentlyVisible) {
145
+ return;
146
+ }
147
+
148
+ this.containerEl.addClass(`${this.cssPrefix}-hidden`);
149
+
150
+ if (this.inputEl) {
151
+ this.inputEl.value = "";
152
+ }
153
+
154
+ this.updateFilterValue("");
155
+ this.onHide?.();
156
+ }
157
+
158
+ focus(): void {
159
+ this.inputEl?.focus();
160
+ }
161
+
162
+ isVisible(): boolean {
163
+ return !this.containerEl.hasClass(`${this.cssPrefix}-hidden`);
164
+ }
165
+
166
+ setPersistentlyVisible(value: boolean): void {
167
+ this.persistentlyVisible = value;
168
+
169
+ if (value) {
170
+ this.show();
171
+ } else {
172
+ this.hide();
173
+ }
174
+ }
175
+
176
+ destroy(): void {
177
+ if (this.debounceTimer !== null) {
178
+ window.clearTimeout(this.debounceTimer);
179
+ this.debounceTimer = null;
180
+ }
181
+
182
+ this.containerEl.remove();
183
+ this.inputEl = null;
184
+ }
185
+
186
+ /**
187
+ * Abstract method that subclasses must implement to determine
188
+ * whether a data item should be included based on the current filter.
189
+ *
190
+ * @param data - The data item to check
191
+ * @returns True if the item should be included, false otherwise
192
+ */
193
+ abstract shouldInclude(data: T): boolean;
194
+ }