// The rules behind the note pins of a map (right click a map, pin one of the campaign notes at that // spot, click the pin to read it). Pins are stored in image pixels, so these helpers only translate // between the image and what the map viewer shows; they hold no state and talk to no API. const PATH_SEPARATOR = ' / '; // Screen pixels (relative to the top left corner of the map viewport) → image pixels, or null when // the point is not over the image: nothing gets pinned outside of it. function ToImagePoint(point, view) { const zoom = Number(view?.zoom); const width = Number(view?.width); const height = Number(view?.height); if (!zoom || !width || !height) return null; const x = (Number(point?.x) - Number(view?.pan?.x ?? 0)) / zoom; const y = (Number(point?.y) - Number(view?.pan?.y ?? 0)) / zoom; if (!Number.isFinite(x) || !Number.isFinite(y)) return null; if (x < 0 || y < 0 || x > width || y > height) return null; return { x: Math.round(x), y: Math.round(y) }; } // A pin (image pixels) → screen pixels of the map viewport. Pins are drawn in screen space instead // of inside the scaled stage, so their marker keeps the same size whatever the zoom is. function ToScreenPoint(pin, view) { const zoom = Number(view?.zoom) || 0; return { x: Number(view?.pan?.x ?? 0) + Number(pin?.x ?? 0) * zoom, y: Number(view?.pan?.y ?? 0) + Number(pin?.y ?? 0) * zoom }; } // The path of the folder a note lives in, so two notes sharing a name can still be told apart. function FolderPath(folderId, folderById) { const names = []; const seen = new Set(); let folder = folderId ? folderById.get(String(folderId)) : null; // Folder trees are acyclic by construction; `seen` only keeps a broken parent chain from // looping forever. while (folder && !seen.has(String(folder._id))) { seen.add(String(folder._id)); if (folder.name) names.unshift(String(folder.name)); folder = folder.parentFolder ? folderById.get(String(folder.parentFolder)) : null; } return names.join(PATH_SEPARATOR); } // 'Folder / Subfolder / Note title' out of the picker option's folder path and title. function OptionLabel(option) { const path = option?.path ? `${option.path}${PATH_SEPARATOR}` : ''; return `${path}${option?.title ?? ''}`; } // The note picker of a campaign: `{ value, title, path, label }` per note, sorted by label. Notes // without a title are left out, they would show as an empty menu entry. function BuildNoteOptions(notes, folders) { const folderById = new Map((folders ?? []).map((folder) => [String(folder._id), folder])); return (notes ?? []) .filter((note) => note?._id && String(note.title ?? '').trim()) .map((note) => { const option = { value: String(note._id), title: String(note.title).trim(), path: FolderPath(note.folder, folderById) }; return { ...option, label: OptionLabel(option) }; }) .sort((a, b) => a.label.localeCompare(b.label)); } export { BuildNoteOptions, OptionLabel, ToImagePoint, ToScreenPoint };