From 2377ad7cfe90964192801b684caaf6280e0f3a6d Mon Sep 17 00:00:00 2001 From: Danielo Rodriguez Date: Thu, 30 May 2024 10:56:01 +0200 Subject: [PATCH] WIP: template builder V0 --- src/views/components/TemplateBuilder.svelte | 30 +++++++++++ src/views/components/TemplateBuilder.ts | 58 +++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 src/views/components/TemplateBuilder.svelte create mode 100644 src/views/components/TemplateBuilder.ts diff --git a/src/views/components/TemplateBuilder.svelte b/src/views/components/TemplateBuilder.svelte new file mode 100644 index 00000000..16e053e9 --- /dev/null +++ b/src/views/components/TemplateBuilder.svelte @@ -0,0 +1,30 @@ + + +
+
+

Fields to include in frontmatter

+ {#each $fields as field} +
+ + + model.setField(field.name, { onFrontmatter: v.currentTarget.checked })} + /> +
+ {/each} +
+
+
 
+            code here
+        
+
+
diff --git a/src/views/components/TemplateBuilder.ts b/src/views/components/TemplateBuilder.ts new file mode 100644 index 00000000..adbd13b8 --- /dev/null +++ b/src/views/components/TemplateBuilder.ts @@ -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) { + // fields.update((f) => { + // const field = f[name] ?? { name, onFrontmatter: false, onBody: false }; + // f[name] = { ...field, ...newValues }; + // return f; + // }); + // } + + function setField(name: string, newValues: Partial) { + fields.update(($fields) => + pipe( + $fields, + A.updateFirst( + (f) => f.name === name, + (f) => { + return { ...f, ...newValues } as Field; + }, + ), + ), + ); + } + return { fields, setField }; +}; + +export type TemplateBuilderModel = ReturnType;