Skip to content

Commit

Permalink
feat!: remove raw datasource and use frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
waynevanson committed Aug 27, 2023
1 parent 281a18e commit 46cb24a
Show file tree
Hide file tree
Showing 22 changed files with 307 additions and 509 deletions.
24 changes: 13 additions & 11 deletions .vault/.obsidian/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
"type": "split",
"children": [
{
"id": "7ad1d46ce8a5b7ef",
"id": "783a819f9359185b",
"type": "tabs",
"children": [
{
"id": "840752223d316a9b",
"id": "07a69522aff335a3",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "Sample 01.md",
"mode": "preview",
"file": "Sample 02.md",
"mode": "source",
"source": true
}
}
Expand Down Expand Up @@ -85,7 +85,7 @@
"state": {
"type": "backlink",
"state": {
"file": "Sample 01.md",
"file": "Sample 02.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
Expand All @@ -102,7 +102,7 @@
"state": {
"type": "outgoing-link",
"state": {
"file": "Sample 01.md",
"file": "Sample 02.md",
"linksCollapsed": false,
"unlinkedCollapsed": true
}
Expand All @@ -125,7 +125,7 @@
"state": {
"type": "outline",
"state": {
"file": "Sample 01.md"
"file": "Sample 02.md"
}
}
}
Expand All @@ -147,14 +147,16 @@
"command-palette:Open command palette": false
}
},
"active": "840752223d316a9b",
"active": "07a69522aff335a3",
"lastOpenFiles": [
"data.md",
"Sample 01.md",
"Sample 02.md",
"d.md",
"NonObject.md",
"Yello world.md",
"ss/NonObject.md",
"NonObject.md",
"ss",
"data.md",
"d.md",
"Untitled 1.md",
"data.ms",
"data.json.md",
Expand Down
16 changes: 0 additions & 16 deletions .vault/NonObject.md

This file was deleted.

12 changes: 7 additions & 5 deletions .vault/Sample 01.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
datasource:
file:
path: data.md
frontmatter: data
schema:
properties:
name:
type: string
age:
type: integer
inline:
properties:
name:
type: string
age:
type: integer
```
10 changes: 10 additions & 0 deletions .vault/Sample 02.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
```yaml-data-entry
datasource:
file:
path: data.md
frontmatter: data
schema:
file:
path: data.md
frontmatter: schema
```
1 change: 0 additions & 1 deletion .vault/d.md

This file was deleted.

11 changes: 10 additions & 1 deletion .vault/data.md
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
[{},{},{},{"name":"hello"},{},{"name":"My Man"}]
---
schema:
type: object
properties:
name:
type: string
data:
name: Suck me off
age: 444
---
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ It does what it says on the tin but doesn't do what it could do.

Feel free to submit issues and discussions with your desires.

Instructions coming soon, check back next week!

## Preview

<details>
Expand Down
43 changes: 29 additions & 14 deletions packages/data-entry/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,32 @@ import { pipe } from 'fp-ts/lib/function';
import * as decoder from 'io-ts/Decoder';
import { Decoder } from 'io-ts/Decoder';

type GetSet<A> = Record<'get' | 'set', A>;

// remove the whole writing to a file thing, gross
// also allow loading icon to happen somewhere without removing the form.
export type Datasource = Sum<{
file: {
path: string;
// frontmatter: GetSet<string>;
frontmatter: string; // defaults to 'data'
};
folder: string;
}>;

export interface Configuration {
datasource: Datasource;
schema: Sum<{ inline: JsonSchema }>; //register definitions behind "data-entry" or "vault" key
uischema?: Sum<{ inline: UISchemaElement }>; // get set
// todo - register definitions behind "data-entry" or "vault" key
schema: Sum<{
inline: JsonSchema;
file: {
path: string;
frontmatter: string;
}; // defaults to 'schema'
}>;
uischema?: Sum<{
inline: UISchemaElement;
file: {
path: string;
frontmatter: string;
}; // defaults to 'uischema'
}>;
}

export type Sum<T extends Record<string, unknown>> = keyof T extends never
Expand Down Expand Up @@ -58,22 +68,27 @@ const sum = <P extends Record<string, Decoder<unknown, unknown>>>(
),
);

/// schema
const datasource = sum({
file: decoder.struct({
path: decoder.string,
}),
folder: decoder.string,
const file = decoder.struct({
path: decoder.string,
frontmatter: decoder.string,
});
/// schema
const datasource = sum({ file });

export const configuration = pipe(
decoder.struct({
datasource,
schema: decoder.struct({ inline: decoder.UnknownRecord }),
schema: sum({
inline: decoder.UnknownRecord,
file,
}),
}),
decoder.intersect(
decoder.partial({
uischema: decoder.struct({ inline: decoder.UnknownRecord }),
uischema: sum({
inline: decoder.UnknownRecord,
file,
}),
}),
),
);
128 changes: 23 additions & 105 deletions packages/data-entry/src/components/application.tsx
Original file line number Diff line number Diff line change
@@ -1,126 +1,44 @@
import { createDefaultValue } from '@jsonforms/core';
import { Alert, Button, CircularProgress } from '@mui/material';
import { readonlyRecord } from 'fp-ts';
import { Alert } from '@mui/material';
import * as React from 'react';
import { MouseEventHandler, ReactNode, useMemo, useState } from 'react';
import {
Form,
useCursor,
useFile,
useForm,
useForms,
useToggle,
} from '../hooks';
import { ReactNode, useCallback, useState } from 'react';
import { Form } from '../hooks';
import { useFrontmatter } from '../hooks/frontmatter';
import { useApplication } from './context';
import { ControlPanel } from './control-panel';
import { Formed } from './form';

const CallbackCreateNewFile = ({
onClick,
}: {
onClick?: MouseEventHandler<HTMLButtonElement>;
}) => (
<Alert
severity="warning"
action={
<Button color="inherit" onClick={onClick}>
Create File
</Button>
}
>
File does not exist. Would you like to create it?
</Alert>
);

export function Application() {
const application = useApplication();
const file = useFile(application.vault, application.fileName);
const [newMode, newModeToggle] = useToggle(false);
const frontmatter = useFrontmatter(application.app, application.fileName);
const [defaultForm] = useState(
() => createDefaultValue(application.schema) as Form,
);
const [created, createdSet] = useState<Form>(defaultForm);
const jsoned = useMemo(
() => JSON.parse(file.data as never) as Array<Form> | null,
[file.data],
);
const [forms, formsSet] = useForms(jsoned);
const max = file.data != null ? readonlyRecord.size(forms) : null;

// todo - rename to index
const index = useCursor(0, max);
const [form, formSet] = useForm({
newMode,
cursor: index.value,
created: [created, createdSet],
forms: [forms, formsSet],
});
const [form, formSet] = useState<Form>(defaultForm);
// todo - update form contents when the file changes

const [errors, errorsSet] = useState<Array<unknown>>([]);

const handleSubmit = () => {
const array: Array<Form | null> = jsoned ?? [];
if (newMode) {
array.push(form);
} else if (index.value !== null) {
array[index.value] = form;
}
file.modify(JSON.stringify(array, null, 2));
};

const count = max != null ? max : 0;
const page = index.value != null ? index.value + 1 : 0;
if (file.loading)
return (
<Alert action={<CircularProgress color="inherit" />}>
Loading file {application.fileName}
</Alert>
);

if (file.error) {
return <Alert severity="error">{file.error.message}</Alert>;
}

if (file.data == null) {
return (
<CallbackCreateNewFile
onClick={() => file.modify(JSON.stringify('[]', null, 2))}
/>
);
}
const handleSubmit = useCallback(() => {
frontmatter.modify((json) => ({ ...json, data: form as never }));
}, [form, frontmatter]);

return (
<ErrorBoundary>
<ControlPanel
newMode={newMode}
onClear={() => formSet(defaultForm)}
onToggleMode={newModeToggle}
count={count}
page={page}
onPageChange={(_, page) => index.valueSet(page - 1)}
onRemove={() => {
if (index.value == null) return;
const array: Array<Form | null> = jsoned ?? [];
array.splice(index.value, 1);
file.modify(JSON.stringify(array, null, 2));
{frontmatter.error && (
<Alert severity="error">{frontmatter.error.message}</Alert>
)}
<Formed
errors={errors as never}
onSubmit={handleSubmit}
submit="Update"
schema={application.schema ?? undefined}
uischema={application.uischema ?? undefined}
data={form}
onChange={({ data, errors }) => {
formSet(data);
errorsSet(errors ?? []);
}}
/>
{form != null ? (
<Formed
errors={errors as never}
onSubmit={handleSubmit}
submit={newMode ? 'Save as new' : 'Save existing'}
schema={application.schema ?? undefined}
uischema={application.uischema ?? undefined}
data={form}
onChange={({ data, errors: _errors }) => {
formSet(data);
errorsSet(errors);
}}
/>
) : (
<Alert severity="info">There are no items to display.</Alert>
)}
</ErrorBoundary>
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/data-entry/src/components/context.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { JsonSchema, UISchemaElement } from '@jsonforms/core';
import { Vault } from 'obsidian';
import { App } from 'obsidian';
import { createContext, useContext } from 'react';

export interface MainProps {
vault: Vault;
app: App;
schema: JsonSchema;
uischema?: UISchemaElement;
fileName: string;
Expand Down
Loading

0 comments on commit 46cb24a

Please sign in to comment.