generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d0b680
commit dc95fcf
Showing
13 changed files
with
192 additions
and
655 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
.../core/src/fields/inputFields/fields/ImageListSuggester/ImageListSuggesterComponent.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<script lang="ts"> | ||
import Icon from '../../../../utils/components/Icon.svelte'; | ||
import Button from '../../../../utils/components/Button.svelte'; | ||
import { IPlugin } from '../../../../IPlugin'; | ||
import { ContextMenuItemDefinition } from 'packages/core/src/utils/IContextMenu'; | ||
import { ButtonStyleType } from 'packages/core/src/config/ButtonConfig'; | ||
export let plugin: IPlugin; | ||
export let value: string[]; | ||
export let showSuggester: () => void; | ||
export let onValueChange: (value: string[]) => void; | ||
export function setValue(v: string[]): void { | ||
value = v; | ||
} | ||
export function addValue(v: string): void { | ||
value.push(v); | ||
value = value; | ||
} | ||
function remove(i: number) { | ||
value.splice(i, 1); | ||
// call with copy of array | ||
onValueChange(value); | ||
// tell svelte to update | ||
value = value; | ||
} | ||
function suggestKey(event: KeyboardEvent): void { | ||
if (event.key === ' ') { | ||
showSuggester(); | ||
} | ||
} | ||
function openContextMenuForElement(e: MouseEvent, index: number) { | ||
const menuActions: ContextMenuItemDefinition[] = []; | ||
if (index > 0) { | ||
menuActions.push({ | ||
name: 'Move left', | ||
icon: 'arrow-left', | ||
onclick: () => { | ||
const temp = value[index - 1]; | ||
value[index - 1] = value[index]; | ||
value[index] = temp; | ||
onValueChange(value); | ||
}, | ||
}); | ||
} | ||
if (index < value.length - 1) { | ||
menuActions.push({ | ||
name: 'Move right', | ||
icon: 'arrow-right', | ||
onclick: () => { | ||
const temp = value[index + 1]; | ||
value[index + 1] = value[index]; | ||
value[index] = temp; | ||
onValueChange(value); | ||
}, | ||
}); | ||
} | ||
menuActions.push({ | ||
name: 'Remove', | ||
icon: 'x', | ||
warning: true, | ||
onclick: () => remove(index), | ||
}); | ||
plugin.internal.createContextMenu(menuActions).showWithEvent(e); | ||
} | ||
</script> | ||
|
||
<div class="mb-image-card-grid"> | ||
{#each value as image, i} | ||
<div class="mb-image-card"> | ||
<img class="mb-image-card-image" src={plugin.internal.imagePathToUri(image)} alt={image} /> | ||
<div class="mb-image-card-footer"> | ||
<span>{image}</span> | ||
<Button variant={ButtonStyleType.PLAIN} on:click={e => openContextMenuForElement(e, i)}> | ||
<Icon iconName="more-vertical" plugin={plugin} /> | ||
</Button> | ||
</div> | ||
</div> | ||
{/each} | ||
</div> | ||
<div class="mb-list-input"> | ||
<Button variant={ButtonStyleType.DEFAULT} on:click={() => showSuggester()}>Add new image</Button> | ||
</div> |
41 changes: 41 additions & 0 deletions
41
packages/core/src/fields/inputFields/fields/ImageListSuggester/ImageListSuggesterIPF.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { AbstractInputField } from 'packages/core/src/fields/inputFields/AbstractInputField'; | ||
import ImageListSuggesterComponent from 'packages/core/src/fields/inputFields/fields/ImageListSuggester/ImageListSuggesterComponent.svelte'; | ||
import { type MBLiteral, parseUnknownToLiteralArray, stringifyLiteral } from 'packages/core/src/utils/Literal'; | ||
import { type SvelteComponent } from 'svelte'; | ||
|
||
export class ImageListSuggesterIPF extends AbstractInputField<MBLiteral[], string[]> { | ||
protected filterValue(value: unknown): MBLiteral[] | undefined { | ||
return parseUnknownToLiteralArray(value); | ||
} | ||
|
||
protected getFallbackDefaultValue(): string[] { | ||
return []; | ||
} | ||
|
||
protected getSvelteComponent(): typeof SvelteComponent { | ||
// @ts-ignore | ||
return ImageListSuggesterComponent; | ||
} | ||
|
||
protected rawMapValue(value: string[]): MBLiteral[] { | ||
return value; | ||
} | ||
|
||
protected rawReverseMapValue(value: MBLiteral[]): string[] | undefined { | ||
return value.map(v => stringifyLiteral(v)).filter(v => v !== undefined); | ||
} | ||
|
||
protected getMountArgs(): Record<string, unknown> { | ||
return { | ||
showSuggester: () => this.openModal(), | ||
}; | ||
} | ||
|
||
openModal(): void { | ||
this.base.plugin.internal.openImageSuggesterModal(this, (selected: string) => { | ||
const value = this.getInternalValue(); | ||
value.push(selected); | ||
this.setInternalValue(value); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/core/src/fields/inputFields/fields/Suggester/SuggesterHelper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.