Skip to content

Commit

Permalink
WIP: template builder V0
Browse files Browse the repository at this point in the history
  • Loading branch information
danielo515 committed May 30, 2024
1 parent 33ac698 commit 2377ad7
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/views/components/TemplateBuilder.svelte
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>
58 changes: 58 additions & 0 deletions src/views/components/TemplateBuilder.ts
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>;

0 comments on commit 2377ad7

Please sign in to comment.