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:
parent
4c7ae08f91
commit
95035e5eac
4 changed files with 85 additions and 21 deletions
|
|
@ -35,14 +35,24 @@ export class PropertyMapper {
|
||||||
for (const [key, value] of Object.entries(obj)) {
|
for (const [key, value] of Object.entries(obj)) {
|
||||||
for (const propertyMapping of propertyMappings) {
|
for (const propertyMapping of propertyMappings) {
|
||||||
if (propertyMapping.property === key) {
|
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) {
|
if (propertyMapping.mapping === PropertyMappingOption.Map) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
newObj[propertyMapping.newProperty] = value;
|
newObj[propertyMapping.newProperty] = finalValue;
|
||||||
} else if (propertyMapping.mapping === PropertyMappingOption.Remove) {
|
} else if (propertyMapping.mapping === PropertyMappingOption.Remove) {
|
||||||
// do nothing
|
// do nothing
|
||||||
} else if (propertyMapping.mapping === PropertyMappingOption.Default) {
|
} else if (propertyMapping.mapping === PropertyMappingOption.Default) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
newObj[key] = value;
|
newObj[key] = finalValue;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -75,7 +75,7 @@ export class PropertyMappingModel {
|
||||||
copy(): PropertyMappingModel {
|
copy(): PropertyMappingModel {
|
||||||
const copy = new PropertyMappingModel(this.type);
|
const copy = new PropertyMappingModel(this.type);
|
||||||
for (const property of this.properties) {
|
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);
|
copy.properties.push(propertyCopy);
|
||||||
}
|
}
|
||||||
return copy;
|
return copy;
|
||||||
|
|
@ -87,12 +87,14 @@ export class PropertyMapping {
|
||||||
newProperty: string;
|
newProperty: string;
|
||||||
locked: boolean;
|
locked: boolean;
|
||||||
mapping: PropertyMappingOption;
|
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.property = property;
|
||||||
this.newProperty = newProperty;
|
this.newProperty = newProperty;
|
||||||
this.mapping = mapping;
|
this.mapping = mapping;
|
||||||
this.locked = locked ?? false;
|
this.locked = locked ?? false;
|
||||||
|
this.wikilink = wikilink ?? false;
|
||||||
}
|
}
|
||||||
|
|
||||||
validate(): { res: boolean; err?: Error } {
|
validate(): { res: boolean; err?: Error } {
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@
|
||||||
{#if property.locked}
|
{#if property.locked}
|
||||||
<div class="media-db-plugin-property-binding-text">property cannot be remapped</div>
|
<div class="media-db-plugin-property-binding-text">property cannot be remapped</div>
|
||||||
{:else}
|
{:else}
|
||||||
|
<div style="display: flex; align-items: center; gap: 8px;">
|
||||||
<select class="dropdown" bind:value={property.mapping}>
|
<select class="dropdown" bind:value={property.mapping}>
|
||||||
{#each propertyMappingOptions as remappingOption}
|
{#each propertyMappingOptions as remappingOption}
|
||||||
<option value={remappingOption}>
|
<option value={remappingOption}>
|
||||||
|
|
@ -34,13 +35,19 @@
|
||||||
</option>
|
</option>
|
||||||
{/each}
|
{/each}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
{#if property.mapping === PropertyMappingOption.Map}
|
{#if property.mapping === PropertyMappingOption.Map}
|
||||||
<Icon iconName="arrow-right" />
|
<Icon iconName="arrow-right" />
|
||||||
<div class="media-db-plugin-property-mapping-to">
|
<div class="media-db-plugin-property-mapping-to">
|
||||||
<input type="text" spellcheck="false" bind:value={property.newProperty} />
|
<input type="text" spellcheck="false" bind:value={property.newProperty} />
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/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}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
{/each}
|
{/each}
|
||||||
|
|
@ -64,4 +71,11 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
|
.media-db-plugin-property-mapping-wikilink-label {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 4px;
|
||||||
|
font-size: 0.95em;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
|
||||||
|
|
@ -155,16 +155,54 @@ export function getDefaultSettings(plugin: MediaDbPlugin): MediaDbPluginSettings
|
||||||
const propertyMappingModel: PropertyMappingModel = new PropertyMappingModel(mediaType);
|
const propertyMappingModel: PropertyMappingModel = new PropertyMappingModel(mediaType);
|
||||||
|
|
||||||
for (const key of Object.keys(metadataObj)) {
|
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);
|
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;
|
defaultSettings.propertyMappingModels = propertyMappingModels;
|
||||||
return defaultSettings;
|
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 {
|
export class MediaDbSettingTab extends PluginSettingTab {
|
||||||
plugin: MediaDbPlugin;
|
plugin: MediaDbPlugin;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue