generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 18
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
33ac698
commit 2377ad7
Showing
2 changed files
with
88 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<script lang="ts"> | ||
import { FormDefinition } from "src/core/formDefinition"; | ||
import { makeModel, type TemplateBuilderModel } from "./TemplateBuilder"; | ||
export let definition: FormDefinition; | ||
export let model: TemplateBuilderModel = makeModel(definition); | ||
$: fields = model.fields; | ||
</script> | ||
|
||
<div class="flex gap-2"> | ||
<div> | ||
<h3>Fields to include in frontmatter</h3> | ||
{#each $fields as field} | ||
<div> | ||
<label>{field.name}</label> | ||
<input | ||
type="checkbox" | ||
on:input={(v) => | ||
model.setField(field.name, { onFrontmatter: v.currentTarget.checked })} | ||
/> | ||
</div> | ||
{/each} | ||
</div> | ||
<div> | ||
<pre> | ||
code here | ||
</pre> | ||
</div> | ||
</div> |
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,58 @@ | ||
import { A, pipe } from "@std"; | ||
import { FormDefinition } from "src/core/formDefinition"; | ||
import { writable } from "svelte/store"; | ||
|
||
// type FieldDefinition = FormDefinition["fields"][number]; | ||
|
||
interface FieldOption { | ||
name: string; | ||
onFrontmatter: boolean; | ||
onBody: boolean; | ||
omit: false; | ||
} | ||
|
||
interface OmitedFieldOption { | ||
name: string; | ||
omit: true; | ||
// field: FieldDefinition; | ||
} | ||
|
||
type Field = FieldOption | OmitedFieldOption; | ||
|
||
const Field = (name: string): FieldOption => ({ | ||
name, | ||
onFrontmatter: false, | ||
onBody: false, | ||
omit: false, | ||
}); | ||
|
||
export const makeModel = (formDefinition: FormDefinition) => { | ||
const fields = writable( | ||
formDefinition.fields.reduce((acc, { name }) => [...acc, Field(name)], [] as Field[]), | ||
); | ||
|
||
// function setField(name: string, newValues: Partial<FieldOption>) { | ||
// fields.update((f) => { | ||
// const field = f[name] ?? { name, onFrontmatter: false, onBody: false }; | ||
// f[name] = { ...field, ...newValues }; | ||
// return f; | ||
// }); | ||
// } | ||
|
||
function setField(name: string, newValues: Partial<FieldOption>) { | ||
fields.update(($fields) => | ||
pipe( | ||
$fields, | ||
A.updateFirst( | ||
(f) => f.name === name, | ||
(f) => { | ||
return { ...f, ...newValues } as Field; | ||
}, | ||
), | ||
), | ||
); | ||
} | ||
return { fields, setField }; | ||
}; | ||
|
||
export type TemplateBuilderModel = ReturnType<typeof makeModel>; |