Added a wikilinks option to property mappings section

WIP, known bug: the checkboxes don't reflect the state when obsidian restarts
This commit is contained in:
ltctceplrm 2025-12-09 20:30:40 +01:00
parent 4c7ae08f91
commit 95035e5eac
4 changed files with 85 additions and 21 deletions

View file

@ -35,14 +35,24 @@ export class PropertyMapper {
for (const [key, value] of Object.entries(obj)) {
for (const propertyMapping of propertyMappings) {
if (propertyMapping.property === key) {
let finalValue = value;
if (propertyMapping.wikilink) {
if (typeof value === 'string') {
finalValue = `[[${value}]]`;
} else if (Array.isArray(value)) {
finalValue = value.map(v =>
typeof v === 'string' ? `[[${v}]]` : v
);
}
}
if (propertyMapping.mapping === PropertyMappingOption.Map) {
// @ts-ignore
newObj[propertyMapping.newProperty] = value;
newObj[propertyMapping.newProperty] = finalValue;
} else if (propertyMapping.mapping === PropertyMappingOption.Remove) {
// do nothing
} else if (propertyMapping.mapping === PropertyMappingOption.Default) {
// @ts-ignore
newObj[key] = value;
newObj[key] = finalValue;
}
break;
}

View file

@ -75,7 +75,7 @@ export class PropertyMappingModel {
copy(): PropertyMappingModel {
const copy = new PropertyMappingModel(this.type);
for (const property of this.properties) {
const propertyCopy = new PropertyMapping(property.property, property.newProperty, property.mapping, property.locked);
const propertyCopy = new PropertyMapping(property.property, property.newProperty, property.mapping, property.locked, property.wikilink);
copy.properties.push(propertyCopy);
}
return copy;
@ -87,12 +87,14 @@ export class PropertyMapping {
newProperty: string;
locked: boolean;
mapping: PropertyMappingOption;
wikilink: boolean;
constructor(property: string, newProperty: string, mapping: PropertyMappingOption, locked?: boolean) {
constructor(property: string, newProperty: string, mapping: PropertyMappingOption, locked?: boolean, wikilink?: boolean) {
this.property = property;
this.newProperty = newProperty;
this.mapping = mapping;
this.locked = locked ?? false;
this.wikilink = wikilink ?? false;
}
validate(): { res: boolean; err?: Error } {

View file

@ -27,20 +27,27 @@
{#if property.locked}
<div class="media-db-plugin-property-binding-text">property cannot be remapped</div>
{:else}
<select class="dropdown" bind:value={property.mapping}>
{#each propertyMappingOptions as remappingOption}
<option value={remappingOption}>
{remappingOption}
</option>
{/each}
</select>
{#if property.mapping === PropertyMappingOption.Map}
<Icon iconName="arrow-right" />
<div class="media-db-plugin-property-mapping-to">
<input type="text" spellcheck="false" bind:value={property.newProperty} />
</div>
{/if}
<div style="display: flex; align-items: center; gap: 8px;">
<select class="dropdown" bind:value={property.mapping}>
{#each propertyMappingOptions as remappingOption}
<option value={remappingOption}>
{remappingOption}
</option>
{/each}
</select>
{#if property.mapping === PropertyMappingOption.Map}
<Icon iconName="arrow-right" />
<div class="media-db-plugin-property-mapping-to">
<input type="text" spellcheck="false" bind:value={property.newProperty} />
</div>
{/if}
<!-- Wikilink checkbox -->
<label class="media-db-plugin-property-mapping-wikilink-label" title="Convert value to wikilink ([[value]])">
<input type="checkbox" bind:checked={property.wikilink} />
<Icon iconName="link" />
<span>Wikilink</span>
</label>
</div>
{/if}
</div>
{/each}
@ -64,4 +71,11 @@
</div>
<style>
.media-db-plugin-property-mapping-wikilink-label {
display: flex;
align-items: center;
gap: 4px;
font-size: 0.95em;
cursor: pointer;
}
</style>

View file

@ -155,16 +155,54 @@ export function getDefaultSettings(plugin: MediaDbPlugin): MediaDbPluginSettings
const propertyMappingModel: PropertyMappingModel = new PropertyMappingModel(mediaType);
for (const key of Object.keys(metadataObj)) {
propertyMappingModel.properties.push(new PropertyMapping(key, '', PropertyMappingOption.Default, lockedPropertyMappings.contains(key)));
propertyMappingModel.properties.push(
new PropertyMapping(
key,
'',
PropertyMappingOption.Default,
lockedPropertyMappings.contains(key),
false, // wikilink default
)
);
}
propertyMappingModels.push(propertyMappingModel);
}
// MIGRATION: Ensure all property mappings have wikilink defined (for settings loaded from disk)
if (defaultSettings.propertyMappingModels && Array.isArray(defaultSettings.propertyMappingModels)) {
for (const model of defaultSettings.propertyMappingModels) {
if (model.properties && Array.isArray(model.properties)) {
for (const prop of model.properties) {
if (typeof prop.wikilink === 'undefined') {
prop.wikilink = false;
}
}
}
}
}
defaultSettings.propertyMappingModels = propertyMappingModels;
return defaultSettings;
}
/**
* Ensures all property mappings in loaded settings have the wikilink property defined.
*/
export function ensureWikilinkOnPropertyMappings(settings: MediaDbPluginSettings): void {
if (settings.propertyMappingModels && Array.isArray(settings.propertyMappingModels)) {
for (const model of settings.propertyMappingModels) {
if (model.properties && Array.isArray(model.properties)) {
for (const prop of model.properties) {
if (typeof prop.wikilink === 'undefined') {
prop.wikilink = false;
}
}
}
}
}
}
export class MediaDbSettingTab extends PluginSettingTab {
plugin: MediaDbPlugin;
@ -779,8 +817,8 @@ export class MediaDbSettingTab extends PluginSettingTab {
cb.setPlaceholder(`Example: ${DEFAULT_SETTINGS.boardgameFileNameTemplate}`)
.setValue(this.plugin.settings.boardgameFileNameTemplate)
.onChange(data => {
this.plugin.settings.boardgameFileNameTemplate = data;
void this.plugin.saveSettings();
this.plugin.settings.boardgameFileNameTemplate = data;
void this.plugin.saveSettings();
});
});