From 7f0838156b9d4a02f93d745fca520a04796cb8ea Mon Sep 17 00:00:00 2001 From: Brian Hursey Date: Wed, 2 Dec 2026 03:30:57 +0000 Subject: [PATCH] Write ratings, keywords, hierarchy and IPTC to XMP Sidecars and JPEG exports now carry xmp:Rating, xmp:Label, dc:subject and lr:hierarchicalSubject plus the IPTC core, and importing a sidecar applies its cataloguing fields independently of its develop settings. Hierarchical keywords keep leaf terms in the flat list, so search and filters are unchanged. --- README.md | 27 ++++++ src/App.tsx | 66 +++++++++++-- src/components/Develop.tsx | 4 + src/components/Panels.tsx | 110 ++++++++++++++++++++- src/lib/edits.ts | 32 +++++++ src/lib/export.ts | 25 ++--- src/lib/keywords.test.ts | 99 +++++++++++++++++++ src/lib/keywords.ts | 181 +++++++++++++++++++++++++++++++++++ src/lib/xmp.test.ts | 77 ++++++++++++++- src/lib/xmp.ts | 190 +++++++++++++++++++++++++++++++++++-- src/types.ts | 24 +++++ 11 files changed, 808 insertions(+), 27 deletions(-) create mode 100644 src/lib/keywords.test.ts create mode 100644 src/lib/keywords.ts diff --git a/README.md b/README.md index 7f5c3f8..3ada590 100644 --- a/README.md +++ b/README.md @@ -994,6 +994,33 @@ listed in [`THIRD_PARTY_NOTICES.md`](THIRD_PARTY_NOTICES.md). --- +## Metadata portability + +The catalog is not the only copy of what you typed. Ratings, colour labels, keywords and +IPTC fields are written into XMP in both directions, so a library can leave. + +- **Two keyword lists, on purpose.** `photo.keywords` holds the leaf terms and is what search + and filters read; `photo.keywordPaths` holds the full `A > B > C` chains. Typing + `Nature > Birds > Robin` records `Nature`, `Nature > Birds` and the full path, and adds + `Robin` to the flat list, so taggers never have to think about the tree and search never + has to. +- **The pair Lightroom writes.** `dc:subject` carries the leaves, `lr:hierarchicalSubject` + the paths, and `xmp:Rating` / `xmp:Label` the rating and colour label. Another editor + sees the same thing Luminary does; a library can be rebuilt from sidecars instead of + from this database. +- **IPTC core.** Headline, credit, source, city, state, country and sublocation are edited + in the Metadata panel and written as `photoshop:` and `Iptc4xmpCore:` properties. +- **Import reads both halves.** Importing a sidecar applies its develop settings and its + cataloguing fields independently: a metadata-only file from a DAM imports without a + `crs:` grade, and a grade imports without keywords. Nothing the file omits is cleared. +- **Exported files carry it too.** A JPEG export embeds the same packet as the sidecar, so a + delivered file keeps its rating, keywords, hierarchy and IPTC. + +`src/lib/keywords.ts` owns the path rules (separators, ancestors, pruning a subtree and +leaving no orphan leaves); `src/lib/xmp.ts` owns the mapping to and from XMP. + +--- + ## Lens profiles Lens correction uses the calibration data of the diff --git a/src/App.tsx b/src/App.tsx index abd9968..e5b4661 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -71,7 +71,7 @@ import type { MergeResult } from './lib/merge'; import { evictBitmap } from './lib/images'; import { importFiles, uid } from './lib/import'; import { PRESETS } from './lib/presets'; -import { downloadXmp, readXmpFile, xmpToEdits } from './lib/xmp'; +import { downloadXmp, readXmpFile, xmpToEdits, xmpToMetadata } from './lib/xmp'; import type { Edits, EventRecord, @@ -2099,12 +2099,63 @@ export default function App() { try { const xml = await readXmpFile(); if (!xml) return; - const result = xmpToEdits(xml, target.edits); - updatePhoto(target.id, { edits: result.edits }, 'Import XMP sidecar'); + + // A sidecar can carry a grade, cataloguing fields, or both. Read the two + // halves independently: a metadata-only file from a DAM must not fail for + // lacking crs: settings, and a grade must not be rejected for lacking + // keywords. Whichever half is present is applied; neither wipes the other. + let edits = target.edits; + let settingsNote = ''; + try { + const result = xmpToEdits(xml, target.edits); + edits = result.edits; + settingsNote = + result.source === 'luminary' + ? 'Luminary settings' + : `${result.applied.length} Lightroom setting${result.applied.length === 1 ? '' : 's'}`; + } catch { + settingsNote = ''; + } + + const meta = xmpToMetadata(xml); + const patch: Partial = {}; + const fields: string[] = []; + if (edits !== target.edits) patch.edits = edits; + if (meta.rating !== undefined) { + patch.rating = meta.rating; + fields.push('rating'); + } + if (meta.label) { + patch.color = meta.label; + fields.push('label'); + } + const keywords = new Set(target.keywords); + for (const word of meta.keywords) keywords.add(word); + if (keywords.size !== target.keywords.length) { + patch.keywords = [...keywords]; + fields.push('keywords'); + } + const paths = new Set(target.keywordPaths ?? []); + for (const path of meta.keywordPaths) paths.add(path); + if (paths.size !== (target.keywordPaths?.length ?? 0)) { + patch.keywordPaths = [...paths]; + fields.push('keyword paths'); + } + if (Object.keys(meta.iptc).length > 0) { + patch.iptc = { ...target.iptc, ...meta.iptc }; + fields.push('IPTC'); + } + if (meta.title && !target.title) patch.title = meta.title; + if (meta.caption) patch.caption = meta.caption; + if (meta.creator) patch.creator = meta.creator; + if (meta.copyright) patch.copyright = meta.copyright; + + if (!patch.edits && fields.length === 0) { + throw new Error('That sidecar has no settings or metadata to import'); + } + updatePhoto(target.id, patch, 'Import XMP sidecar'); setToast( - result.source === 'luminary' - ? 'Imported Luminary settings from the sidecar' - : `Imported ${result.applied.length} Lightroom setting${result.applied.length === 1 ? '' : 's'}`, + [settingsNote, fields.join(', ')].filter(Boolean).join(' · ') || 'Imported the sidecar', ); } catch (err) { setToast(err instanceof Error ? err.message : 'Could not import the sidecar'); @@ -2516,6 +2567,9 @@ export default function App() { onVirtualCopy={() => void createVirtualCopy(activePhoto.id, true)} metadataPresets={metadataPresets} keywordLibrary={[...new Set(photos.flatMap((photo) => photo.keywords))].sort()} + keywordPathLibrary={[ + ...new Set(photos.flatMap((photo) => photo.keywordPaths ?? [])), + ].sort()} onSaveSnapshot={(name) => saveSnapshot(activePhoto.id, name)} onRestoreSnapshot={(snapshot) => restoreSnapshot(activePhoto.id, snapshot)} onDeleteSnapshot={(snapshot) => deleteSnapshot(activePhoto.id, snapshot.id)} diff --git a/src/components/Develop.tsx b/src/components/Develop.tsx index 9968907..dff8c72 100644 --- a/src/components/Develop.tsx +++ b/src/components/Develop.tsx @@ -157,6 +157,8 @@ interface Props { onVirtualCopy: () => void; metadataPresets: MetadataPreset[]; keywordLibrary: string[]; + /** Hierarchical paths already in the library, for metadata suggestions. */ + keywordPathLibrary?: string[]; onSaveSnapshot: (name: string) => void; onRestoreSnapshot: (snapshot: Snapshot) => void; onDeleteSnapshot: (snapshot: Snapshot) => void; @@ -191,6 +193,7 @@ export default function Develop({ onVirtualCopy, metadataPresets, keywordLibrary, + keywordPathLibrary = [], onSaveSnapshot, onRestoreSnapshot, onDeleteSnapshot, @@ -1773,6 +1776,7 @@ export default function Develop({ onUpdate={(patch) => onUpdate(photo.id, patch)} presets={metadataPresets} keywordLibrary={keywordLibrary} + keywordPathLibrary={keywordPathLibrary} onSavePreset={onSaveMetadataPreset} onDeletePreset={onDeleteMetadataPreset} /> diff --git a/src/components/Panels.tsx b/src/components/Panels.tsx index ea1cc8b..76470ef 100644 --- a/src/components/Panels.tsx +++ b/src/components/Panels.tsx @@ -11,6 +11,7 @@ import { } from '../lib/denoiseModels'; import type { DenoiseProgress } from '../lib/api'; import { clamp } from '../lib/curves'; +import { addKeywordPath, removeKeywordPath } from '../lib/keywords'; import type { CurveChannel, Edits, @@ -2662,6 +2663,7 @@ export function MetadataPanel({ onUpdate, presets = [], keywordLibrary = [], + keywordPathLibrary = [], onSavePreset, onDeletePreset, }: { @@ -2670,6 +2672,8 @@ export function MetadataPanel({ onUpdate?: (patch: Partial) => void; presets?: MetadataPreset[]; keywordLibrary?: string[]; + /** Existing hierarchical paths in the library, offered as suggestions. */ + keywordPathLibrary?: string[]; onSavePreset?: (preset: Omit) => void; onDeletePreset?: (id: string) => void; }) { @@ -2705,11 +2709,21 @@ export function MetadataPanel({ const patch = (next: Partial) => onUpdate?.(next); + /** + * One entry point for both kinds of tag: a plain word stays flat, and + * `Nature > Birds` records the path and its ancestors. The leaf is added to + * `keywords` either way, so search and filters keep working on flat terms. + */ const addKeyword = (raw?: string) => { const value = (raw ?? keyword).trim().replace(/,$/, ''); if (!value) return; - const exists = photo.keywords.some((word) => word.toLowerCase() === value.toLowerCase()); - if (!exists) patch({ keywords: [...photo.keywords, value.slice(0, 48)] }); + patch( + addKeywordPath( + { keywords: photo.keywords, keywordPaths: photo.keywordPaths ?? [] }, + value, + /[>/|]/.test(value), + ), + ); setKeyword(''); }; @@ -2717,6 +2731,19 @@ export function MetadataPanel({ patch({ keywords: photo.keywords.filter((item) => item !== word) }); }; + const removePath = (path: string) => { + patch( + removeKeywordPath( + { keywords: photo.keywords, keywordPaths: photo.keywordPaths ?? [] }, + path, + ), + ); + }; + + const patchIptc = (field: keyof NonNullable, value: string) => { + patch({ iptc: { ...photo.iptc, [field]: value } }); + }; + const applyPreset = (id: string) => { setPresetId(id); const preset = presets.find((item) => item.id === id); @@ -2795,11 +2822,32 @@ export function MetadataPanel({ ))} + {photo.keywordPaths?.length ? ( + <> +
+ Keyword paths + {photo.keywordPaths.length} +
+
+ {photo.keywordPaths.map((path) => ( + + ))} +
+ + ) : null} +
setKeyword(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ',') { @@ -2816,9 +2864,65 @@ export function MetadataPanel({ {keywordLibrary.map((word) => (
+
+ IPTC +
+ +
+ + +
+
+ + +
+