Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/public/scripts/getLongLatFromAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
module.exports = async (params) => {
const {app, obsidian, quickAddApi} = params;

// The note open when the macro starts is the one that gets the location.
// Capture it now: the prompt's Peek lets the user open other notes (for
// example to copy the address) before submitting.
const activeFile = app.workspace.getActiveFile();
if (!activeFile) {
new obsidian.Notice("No active file", 5000);
return;
}

const address = await quickAddApi.inputPrompt("🏠 Address");
const address = await quickAddApi.inputPrompt(`🏠 Address for ${activeFile.path}`);
if (!address) {
new obsidian.Notice("No address given", 5000);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ location: 48.8582599,2.2945006
---
```

If the note already has a `location` property, the script replaces its value. If Nominatim finds no match, you get a notice and the note is left unchanged.
The prompt names the note it will update, for example **🏠 Address for Places/Eiffel Tower.md**. That note gets the location even if you [peek](/docs/ControllingPrompts/#peek) and open another note, say to copy the address, before you submit. If the note already has a `location` property, the script replaces its value. If Nominatim finds no match, you get a notice and the note is left unchanged.

![The Eiffel Tower note's location property, and its pin in Map View](../Images/examples/macro-location-map-view.png)

Expand Down
25 changes: 22 additions & 3 deletions tests/examples/getLongLatFromAddress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,19 @@ function run(options: {
async (_file: unknown, fn: (fm: Record<string, unknown>) => void) =>
fn(frontmatter),
);
const inputPrompt = vi.fn(async () => options.address);
const startFile = { path: "Places/Eiffel Tower.md", basename: "Eiffel Tower" };
let activeFile: typeof startFile | null =
options.activeFile === false ? null : startFile;
const inputPrompt = vi.fn(async (_header: string) => {
// While the prompt is open, Peek lets the user open another note.
activeFile = { path: "Contacts/Eiffel Tower.md", basename: "Eiffel Tower" };
return options.address;
});

const params = {
app: {
workspace: {
getActiveFile: () =>
options.activeFile === false ? null : { path: "Place.md" },
getActiveFile: () => activeFile,
},
fileManager: { processFrontMatter },
},
Expand All @@ -62,6 +68,7 @@ function run(options: {
requestUrl,
processFrontMatter,
inputPrompt,
startFile,
};
}

Expand All @@ -84,6 +91,18 @@ describe("getLongLatFromAddress example script", () => {
expect(ctx.notices).toEqual([]);
});

it("writes to the note open when the macro started, even if another note is active on submit", async () => {
const ctx = run({
address: "Eiffel Tower, Paris",
results: [{ lat: "48.8582599", lon: "2.2945006" }],
});
await ctx.done;

expect(ctx.inputPrompt).toHaveBeenCalledWith("🏠 Address for Places/Eiffel Tower.md");
expect(ctx.processFrontMatter).toHaveBeenCalledTimes(1);
expect(ctx.processFrontMatter.mock.calls[0][0]).toBe(ctx.startFile);
});

it("replaces an existing location", async () => {
const ctx = run({
address: "Colosseum, Rome",
Expand Down
Loading