Skip to content

Commit fa52da4

Browse files
authored
Linting & Minor Cleanup
1 parent f6b23a7 commit fa52da4

3 files changed

Lines changed: 66 additions & 54 deletions

File tree

‎src/graphInjector.ts‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,34 @@ export function propertyFromId(id: string): string {
3030
}
3131
}
3232

33+
/** Safely coerce a primitive-ish value to a trimmed string without risking a bare `[object Object]`. */
34+
function safeToString(value: string | number | boolean): string {
35+
return String(value).trim();
36+
}
37+
3338
function extractPropertyValues(value: unknown): string[] {
3439
if (Array.isArray(value)) {
35-
return value.flatMap(v => extractPropertyValues(v));
40+
const items = value as unknown[];
41+
return items.flatMap(v => extractPropertyValues(v));
3642
}
3743
if (value === null || value === undefined) return [];
3844
if (typeof value === 'object') {
3945
// Obsidian properties can contain objects for dates/links in some builds.
4046
const maybePath = (value as { path?: unknown }).path;
41-
if (maybePath) return [String(maybePath).trim()];
47+
if (typeof maybePath === 'string' || typeof maybePath === 'number' || typeof maybePath === 'boolean') {
48+
return [safeToString(maybePath)];
49+
}
4250
try {
4351
return [JSON.stringify(value)];
4452
} catch {
45-
return [String(value).trim()];
53+
return [];
4654
}
4755
}
48-
const text = String(value).trim();
49-
return text ? [text] : [];
56+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
57+
const text = safeToString(value);
58+
return text ? [text] : [];
59+
}
60+
return [];
5061
}
5162

5263
function resolveFileForNode(app: App, nodeId: string): TFile | null {
@@ -106,7 +117,9 @@ export function injectPropertyNodes(
106117
const file = resolveFileForNode(app, nodeId);
107118
if (!file) continue;
108119
const cache = app.metadataCache.getFileCache(file);
109-
const frontmatter = cache && cache.frontmatter;
120+
const rawFrontmatter: unknown = cache?.frontmatter;
121+
const frontmatter =
122+
rawFrontmatter && typeof rawFrontmatter === 'object' ? (rawFrontmatter as Record<string, unknown>) : undefined;
110123
if (!frontmatter) continue;
111124

112125
for (const { property } of configs) {

‎src/main.ts‎

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Notice, Plugin } from 'obsidian';
2-
import { DEFAULT_SETTINGS, migrateSettings, PropertiesToGraphSettings } from './settings';
2+
import { DEFAULT_SETTINGS, LegacyPropertiesToGraphSettings, migrateSettings, PropertiesToGraphSettings } from './settings';
33
import { Hierarchy } from './hierarchy';
44
import { PropertiesToGraphSettingTab } from './settingsTab';
55
import { GraphData, GraphRenderer, GraphView } from './graphTypes';
@@ -16,7 +16,7 @@ export default class PropertiesToGraphPlugin extends Plugin {
1616
allNodeIds = new Set<string>();
1717
hierarchy = new Hierarchy();
1818

19-
private refreshTimer: ReturnType<typeof setTimeout> | null = null;
19+
private refreshTimer: number | null = null;
2020
private originalOpenLinkText: typeof this.app.workspace.openLinkText | null = null;
2121
private shiftHeld = false;
2222

@@ -80,7 +80,7 @@ export default class PropertiesToGraphPlugin extends Plugin {
8080
window.removeEventListener('keydown', this.onKeyDown, true);
8181
window.removeEventListener('keyup', this.onKeyUp, true);
8282
window.removeEventListener('blur', this.onBlur, true);
83-
if (this.refreshTimer) clearTimeout(this.refreshTimer);
83+
if (this.refreshTimer) window.clearTimeout(this.refreshTimer);
8484

8585
for (const leaf of this.app.workspace.getLeavesOfType('graph')) {
8686
const view = leaf.view as unknown as GraphView;
@@ -95,7 +95,8 @@ export default class PropertiesToGraphPlugin extends Plugin {
9595
}
9696

9797
async loadSettings(): Promise<void> {
98-
const stored = await this.loadData();
98+
const raw: unknown = await this.loadData();
99+
const stored = raw as (Partial<PropertiesToGraphSettings> & LegacyPropertiesToGraphSettings) | null | undefined;
99100
const { settings, migrated } = migrateSettings(stored);
100101
this.settings = settings;
101102
if (migrated) await this.saveSettings();
@@ -106,8 +107,8 @@ export default class PropertiesToGraphPlugin extends Plugin {
106107
}
107108

108109
scheduleRefresh(): void {
109-
if (this.refreshTimer) clearTimeout(this.refreshTimer);
110-
this.refreshTimer = setTimeout(() => {
110+
if (this.refreshTimer) window.clearTimeout(this.refreshTimer);
111+
this.refreshTimer = window.setTimeout(() => {
111112
this.refreshTimer = null;
112113
this.refreshGraphLeaves();
113114
}, 350);
@@ -190,7 +191,7 @@ export default class PropertiesToGraphPlugin extends Plugin {
190191
const entry = this.settings.properties.find(p => p.property === property);
191192
if (!entry) return;
192193
entry.visible = visible;
193-
this.saveSettings().then(() => this.refreshGraphLeaves());
194+
void this.saveSettings().then(() => this.refreshGraphLeaves());
194195
}
195196
});
196197
} catch (e) {
@@ -200,31 +201,35 @@ export default class PropertiesToGraphPlugin extends Plugin {
200201
}
201202

202203
private installInjector(renderer: GraphRenderer): void {
203-
if (!renderer.__p2gOriginalSetData) renderer.__p2gOriginalSetData = renderer.setData;
204-
const plugin = this;
204+
if (!renderer.__p2gOriginalSetData) renderer.__p2gOriginalSetData = renderer.setData.bind(renderer);
205+
const injectAndPatch = this.handleSetData.bind(this);
205206
renderer.setData = function (this: GraphRenderer, data: GraphData) {
206-
if (!this.__p2gOriginalSetData) return;
207-
try {
208-
const injected = injectPropertyNodes(plugin.app, plugin.settings, plugin.hierarchy, data);
209-
plugin.propertyNodeIds = injected.propertyNodeIds;
210-
plugin.allNodeIds = injected.allNodeIds;
211-
// Mutate the existing Maps in place so the patched prototype's
212-
// captured `deps.nodeLabels`/`deps.nodeProperty` references
213-
// (bound once, on first patch) stay live instead of pointing
214-
// at a stale snapshot from the first render.
215-
plugin.nodeLabels.clear();
216-
injected.nodeLabels.forEach((label, id) => plugin.nodeLabels.set(id, label));
217-
plugin.nodeProperty.clear();
218-
injected.nodeProperty.forEach((property, id) => plugin.nodeProperty.set(id, property));
219-
} catch (e) {
220-
console.error('Properties to Graph: injection failed', e);
221-
}
222-
const setDataResult = this.__p2gOriginalSetData.call(this, data);
223-
plugin.patchNodePrototypeFor(this);
224-
return setDataResult;
207+
return injectAndPatch(this, data);
225208
};
226209
}
227210

211+
private handleSetData(renderer: GraphRenderer, data: GraphData): unknown {
212+
if (!renderer.__p2gOriginalSetData) return undefined;
213+
try {
214+
const injected = injectPropertyNodes(this.app, this.settings, this.hierarchy, data);
215+
this.propertyNodeIds = injected.propertyNodeIds;
216+
this.allNodeIds = injected.allNodeIds;
217+
// Mutate the existing Maps in place so the patched prototype's
218+
// captured `deps.nodeLabels`/`deps.nodeProperty` references
219+
// (bound once, on first patch) stay live instead of pointing
220+
// at a stale snapshot from the first render.
221+
this.nodeLabels.clear();
222+
injected.nodeLabels.forEach((label, id) => this.nodeLabels.set(id, label));
223+
this.nodeProperty.clear();
224+
injected.nodeProperty.forEach((property, id) => this.nodeProperty.set(id, property));
225+
} catch (e) {
226+
console.error('Properties to Graph: injection failed', e);
227+
}
228+
const setDataResult = renderer.__p2gOriginalSetData(data);
229+
this.patchNodePrototypeFor(renderer);
230+
return setDataResult;
231+
}
232+
228233
private patchNodePrototypeFor(renderer: GraphRenderer): void {
229234
patchNodePrototype(renderer, {
230235
getColorForProperty: (property: string) => this.getColorForProperty(property),
@@ -240,30 +245,24 @@ export default class PropertiesToGraphPlugin extends Plugin {
240245

241246
const workspace = this.app.workspace;
242247
this.originalOpenLinkText = workspace.openLinkText.bind(workspace);
243-
const plugin = this;
244248

245-
workspace.openLinkText = function (
246-
linktext: string,
247-
sourcePath: string,
248-
newLeaf?: boolean,
249-
openViewState?: unknown
250-
) {
251-
const activeIsGraph = plugin.app.workspace.getMostRecentLeaf()?.view?.getViewType?.() === 'graph';
249+
workspace.openLinkText = (linktext: string, sourcePath: string, newLeaf?: boolean, openViewState?: unknown) => {
250+
const activeIsGraph = this.app.workspace.getMostRecentLeaf()?.view?.getViewType?.() === 'graph';
252251

253252
// Shift+click on a graph node folds/unfolds its structural subtree.
254-
if (plugin.shiftHeld && activeIsGraph && plugin.allNodeIds.has(linktext)) {
255-
plugin.handleFoldToggle(linktext);
253+
if (this.shiftHeld && activeIsGraph && this.allNodeIds.has(linktext)) {
254+
this.handleFoldToggle(linktext);
256255
return Promise.resolve();
257256
}
258257

259258
// Property nodes are virtual graph nodes. Clicking them behaves
260259
// like clicking a native tag: filter the current graph.
261-
if (plugin.propertyNodeIds.has(linktext)) {
262-
plugin.applyPropertyFilter(linktext);
260+
if (this.propertyNodeIds.has(linktext)) {
261+
this.applyPropertyFilter(linktext);
263262
return Promise.resolve();
264263
}
265264

266-
return plugin.originalOpenLinkText!(linktext, sourcePath, newLeaf, openViewState as never);
265+
return this.originalOpenLinkText!(linktext, sourcePath, newLeaf, openViewState as never);
267266
};
268267
}
269268

@@ -309,7 +308,7 @@ export default class PropertiesToGraphPlugin extends Plugin {
309308
}
310309

311310
try {
312-
view && view.showSearch && view.showSearch();
311+
view?.showSearch?.();
313312
} catch {
314313
// Non-fatal: some Obsidian versions may not expose showSearch().
315314
}
@@ -326,11 +325,11 @@ export default class PropertiesToGraphPlugin extends Plugin {
326325
} else {
327326
for (const id of descendants) this.settings.hiddenNodes[id] = true;
328327
}
329-
this.saveSettings().then(() => this.refreshGraphLeaves());
328+
void this.saveSettings().then(() => this.refreshGraphLeaves());
330329
}
331330

332331
unfoldAll(): void {
333332
this.settings.hiddenNodes = {};
334-
this.saveSettings().then(() => this.refreshGraphLeaves());
333+
void this.saveSettings().then(() => this.refreshGraphLeaves());
335334
}
336335
}

‎src/settingsTab.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ export class PropertiesToGraphSettingTab extends PluginSettingTab {
1313
display(): void {
1414
const { containerEl } = this;
1515
containerEl.empty();
16-
containerEl.createEl('h2', { text: 'Properties to Graph' });
16+
new Setting(containerEl).setName('Properties to Graph').setHeading();
1717
containerEl.createEl('p', {
1818
text: 'Choose one or more frontmatter properties. Each unique value of each property becomes a virtual, colour-coded node in Obsidian\'s graph.'
1919
});
2020

2121
const availableProperties = this.plugin.getAvailableProperties();
2222
const usedProperties = new Set(this.plugin.settings.properties.map(p => p.property));
2323

24-
containerEl.createEl('h3', { text: 'Properties' });
24+
new Setting(containerEl).setName('Properties').setHeading();
2525

2626
if (!this.plugin.settings.properties.length) {
2727
containerEl.createEl('p', {
@@ -95,7 +95,7 @@ export class PropertiesToGraphSettingTab extends PluginSettingTab {
9595
);
9696

9797
setting.settingEl.addClass('p2g-property-row');
98-
const textInput = setting.controlEl.querySelector('input[type="text"]') as HTMLInputElement | null;
98+
const textInput = setting.controlEl.querySelector('input[type="text"]');
9999
if (textInput) textInput.addClass('p2g-property-label-input');
100100
});
101101

@@ -120,7 +120,7 @@ export class PropertiesToGraphSettingTab extends PluginSettingTab {
120120
})
121121
);
122122

123-
containerEl.createEl('h3', { text: 'General' });
123+
new Setting(containerEl).setName('General').setHeading();
124124

125125
new Setting(containerEl)
126126
.setName('Show property nodes')

0 commit comments

Comments
 (0)