Skip to content

Commit

Permalink
WIP: compile to template
Browse files Browse the repository at this point in the history
  • Loading branch information
danielo515 committed May 30, 2024
1 parent e376cd0 commit 7f1604f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/views/components/TemplateBuilder.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
export let model: TemplateBuilderModel;
$: fields = model.fields;
$: code = model.code;
</script>

<div class="modal-form flex gap-2">
Expand All @@ -22,7 +23,7 @@
</div>
<div>
<pre>
code here
{$code}
</pre>
</div>
</div>
29 changes: 20 additions & 9 deletions src/views/components/TemplateBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { A, pipe } from "@std";
import { FormDefinition } from "src/core/formDefinition";
import { writable } from "svelte/store";
import { derived, writable } from "svelte/store";

// type FieldDefinition = FormDefinition["fields"][number];

Expand All @@ -26,18 +26,29 @@ const Field = (name: string): FieldOption => ({
omit: false,
});

function compileTemplaterTemplate(fields: Field[]) {
const fieldsToInclude = fields.filter((field): field is FieldOption => !field.omit);
const frontmatterFields = fieldsToInclude.filter((field) => field.onFrontmatter);
const frontmatterCode =
frontmatterFields.length === fieldsToInclude.length
? `tR + = result.asFrontmatterString();`
: `tR + = result.asFrontmatterString({${frontmatterFields
.map((field) => field.name)
.join(", ")}});`;

return `
<% "--- "%>
${frontmatterCode}
<% "--- "%>
`;
}

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;
// });
// }
const code = derived(fields, compileTemplaterTemplate);

function setField(name: string, newValues: Partial<FieldOption>) {
fields.update(($fields) =>
Expand All @@ -52,7 +63,7 @@ export const makeModel = (formDefinition: FormDefinition) => {
),
);
}
return { fields, setField };
return { fields, setField, code };
};

export type TemplateBuilderModel = ReturnType<typeof makeModel>;

0 comments on commit 7f1604f

Please sign in to comment.