mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-16 17:31:24 +01:00
feat(obsidian): persist FMG-to-note association with fmg-id in frontmatter
When a user selects or creates a note for a burg/marker, the system now automatically links them together by adding/updating the note's frontmatter. **How it works:** 1. **When saving a note**: - Adds `fmg-id: burg123` to frontmatter - Updates `x:` and `y:` coordinates - Preserves existing frontmatter fields 2. **When creating a new note**: - Template includes fmg-id from the start - Uses the proper elementId (e.g., "burg123") 3. **Next time you edit**: - System finds note instantly by fmg-id - No need to search by coordinates again - Permanent two-way link established **Changes:** - obsidian-notes-editor.js: - Pass elementId and coordinates to showMarkdownEditor() - updateFrontmatterWithFmgData() injects fmg-id on save - Automatically updates coordinates to keep in sync - obsidian-bridge.js: - generateNoteTemplate() accepts elementId parameter - Uses elementId for fmg-id instead of element.id Users no longer need to manually match notes - once linked, the association is permanent in the note's frontmatter.
This commit is contained in:
parent
28cf8db82d
commit
154145a518
3 changed files with 49 additions and 10 deletions
|
|
@ -8281,8 +8281,8 @@
|
|||
<script defer src="modules/io/load.js?v=1.108.13"></script>
|
||||
<script defer src="modules/io/cloud.js?v=1.106.0"></script>
|
||||
<script defer src="modules/io/export.js?v=1.108.11"></script>
|
||||
<script defer src="modules/io/obsidian-bridge.js?v=1.108.13.2"></script>
|
||||
<script defer src="modules/ui/obsidian-notes-editor.js?v=1.108.13.2"></script>
|
||||
<script defer src="modules/io/obsidian-bridge.js?v=1.108.13.3"></script>
|
||||
<script defer src="modules/ui/obsidian-notes-editor.js?v=1.108.13.3"></script>
|
||||
<script defer src="modules/ui/obsidian-config.js?v=1.108.13"></script>
|
||||
|
||||
<script defer src="modules/renderers/draw-features.js?v=1.108.2"></script>
|
||||
|
|
|
|||
|
|
@ -343,13 +343,13 @@ const ObsidianBridge = (() => {
|
|||
}
|
||||
|
||||
// Generate note template for FMG element
|
||||
function generateNoteTemplate(element, type) {
|
||||
function generateNoteTemplate(element, type, elementId) {
|
||||
const {x, y} = element;
|
||||
const lat = pack.cells.lat?.[element.cell] || 0;
|
||||
const lon = pack.cells.lon?.[element.cell] || 0;
|
||||
|
||||
const frontmatter = {
|
||||
"fmg-id": element.id || `${type}${element.i}`,
|
||||
"fmg-id": elementId || element.id || `${type}${element.i}`,
|
||||
"fmg-type": type,
|
||||
coordinates: {x, y, lat, lon},
|
||||
tags: [type],
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ function editObsidianNote(elementId, elementType, coordinates) {
|
|||
// Try to find note by FMG ID first, then by coordinates
|
||||
findOrCreateNote(elementId, elementType, coordinates)
|
||||
.then(noteData => {
|
||||
showMarkdownEditor(noteData, elementType);
|
||||
showMarkdownEditor(noteData, elementType, elementId, coordinates);
|
||||
})
|
||||
.catch(error => {
|
||||
ERROR && console.error("Failed to load note:", error);
|
||||
|
|
@ -211,7 +211,7 @@ async function promptCreateNewNote(elementId, elementType, coordinates) {
|
|||
$(this).dialog("close");
|
||||
|
||||
try {
|
||||
const template = ObsidianBridge.generateNoteTemplate(element, elementType);
|
||||
const template = ObsidianBridge.generateNoteTemplate(element, elementType, elementId);
|
||||
await ObsidianBridge.createNote(notePath, template);
|
||||
|
||||
const {frontmatter} = ObsidianBridge.parseFrontmatter(template);
|
||||
|
|
@ -404,7 +404,7 @@ function getElementData(elementId, elementType) {
|
|||
}
|
||||
}
|
||||
|
||||
function showMarkdownEditor(noteData, elementType) {
|
||||
function showMarkdownEditor(noteData, elementType, elementId, coordinates) {
|
||||
const {path, name, content, frontmatter, isNew} = noteData;
|
||||
|
||||
// Extract frontmatter and body
|
||||
|
|
@ -416,9 +416,12 @@ function showMarkdownEditor(noteData, elementType) {
|
|||
byId("obsidianMarkdownEditor").value = content;
|
||||
byId("obsidianMarkdownPreview").innerHTML = renderMarkdown(bodyContent);
|
||||
|
||||
// Store current note data
|
||||
// Store current note data and FMG element info
|
||||
showMarkdownEditor.currentNote = noteData;
|
||||
showMarkdownEditor.originalContent = content;
|
||||
showMarkdownEditor.elementId = elementId;
|
||||
showMarkdownEditor.elementType = elementType;
|
||||
showMarkdownEditor.coordinates = coordinates;
|
||||
|
||||
$("#obsidianNotesEditor").dialog({
|
||||
title: `Obsidian Note: ${name}`,
|
||||
|
|
@ -491,19 +494,55 @@ async function saveObsidianNote() {
|
|||
return;
|
||||
}
|
||||
|
||||
const content = byId("obsidianMarkdownEditor").value;
|
||||
let content = byId("obsidianMarkdownEditor").value;
|
||||
const {path} = showMarkdownEditor.currentNote;
|
||||
const elementId = showMarkdownEditor.elementId;
|
||||
const coordinates = showMarkdownEditor.coordinates;
|
||||
|
||||
// Update/add frontmatter with FMG ID and coordinates
|
||||
if (elementId && coordinates) {
|
||||
content = updateFrontmatterWithFmgData(content, elementId, coordinates);
|
||||
}
|
||||
|
||||
try {
|
||||
await ObsidianBridge.updateNote(path, content);
|
||||
showMarkdownEditor.originalContent = content;
|
||||
tip("Note saved to Obsidian vault", true, "success", 2000);
|
||||
// Update the editor to show the new frontmatter
|
||||
byId("obsidianMarkdownEditor").value = content;
|
||||
tip("Note saved to Obsidian vault (linked to FMG element)", true, "success", 3000);
|
||||
} catch (error) {
|
||||
ERROR && console.error("Failed to save note:", error);
|
||||
tip("Failed to save note: " + error.message, true, "error", 5000);
|
||||
}
|
||||
}
|
||||
|
||||
function updateFrontmatterWithFmgData(content, elementId, coordinates) {
|
||||
const {x, y} = coordinates;
|
||||
const {frontmatter, content: bodyContent} = ObsidianBridge.parseFrontmatter(content);
|
||||
|
||||
// Update frontmatter with FMG data
|
||||
frontmatter["fmg-id"] = elementId;
|
||||
frontmatter["x"] = Math.round(x * 100) / 100;
|
||||
frontmatter["y"] = Math.round(y * 100) / 100;
|
||||
|
||||
// Rebuild frontmatter
|
||||
let frontmatterLines = ["---"];
|
||||
for (const [key, value] of Object.entries(frontmatter)) {
|
||||
if (typeof value === "object" && value !== null) {
|
||||
// Handle nested objects
|
||||
frontmatterLines.push(`${key}:`);
|
||||
for (const [nestedKey, nestedValue] of Object.entries(value)) {
|
||||
frontmatterLines.push(` ${nestedKey}: ${nestedValue}`);
|
||||
}
|
||||
} else {
|
||||
frontmatterLines.push(`${key}: ${value}`);
|
||||
}
|
||||
}
|
||||
frontmatterLines.push("---");
|
||||
|
||||
return frontmatterLines.join("\n") + "\n" + bodyContent;
|
||||
}
|
||||
|
||||
function openInObsidian() {
|
||||
if (!showMarkdownEditor.currentNote) return;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue