Skip to content

Commit

Permalink
chore: complete the dataview feature
Browse files Browse the repository at this point in the history
  • Loading branch information
danielo515 committed Sep 13, 2023
1 parent 22b8949 commit 18669e3
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ https://github.com/danielo515/obsidian-modal-form/assets/2270425/542974aa-c58b-4
- toggle (true/false)
- free text
- text with autocompletion for note names (from a folder or root)
- text with autocompletion from a dataview query
- text with autocompletion from a dataview query (requires dataview plugin)
- select from a list
- list of fixed values
- list of notes from a folder
Expand Down
16 changes: 14 additions & 2 deletions src/core/formDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ type inputType =
| inputDataviewSource
| inputSelectFixed;

export const FieldTypeReadable: Record<AllFieldTypes, string> = {
"text": "Text",
"number": "Number",
"date": "Date",
"time": "Time",
"datetime": "DateTime",
"toggle": "Toggle",
"note": "Note",
"slider": "Slider",
"select": "Select",
"dataview": "Dataview",
} as const;

function isObject(input: unknown): input is Record<string, unknown> {
return typeof input === "object" && input !== null;
Expand Down Expand Up @@ -69,7 +81,7 @@ export function isInputSelectFixed(input: unknown): input is inputSelectFixed {
})
}

export type AllFieldTypes = FieldType | "note" | "slider" | "select";
export type AllFieldTypes = inputType['type']
export type FieldDefinition = {
name: string;
label?: string;
Expand All @@ -96,7 +108,7 @@ export type FormDefinition = {
// It has all the possible values, and then you need to narrow it down
// to the actual type.
export type EditableInput = {
type: FieldType | "select" | "slider" | "note";
type: AllFieldTypes;
source?: "notes" | "fixed";
folder?: string;
min?: number;
Expand Down
11 changes: 0 additions & 11 deletions src/views/EditFormView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,6 @@ import type { FormDefinition, AllFieldTypes } from "../core/formDefinition";
import FormEditor from './FormBuilder.svelte'

export const EDIT_FORM_VIEW = "modal-form-edit-form-view";
export const FieldTypeReadable: Record<AllFieldTypes, string> = {
"text": "Text",
"number": "Number",
"date": "Date",
"time": "Time",
"datetime": "DateTime",
"toggle": "Toggle",
"note": "Note",
"slider": "Slider",
"select": "Select"
} as const;

function parseState(maybeState: unknown): maybeState is FormDefinition {
if (maybeState === null) {
Expand Down
8 changes: 7 additions & 1 deletion src/views/FormBuilder.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
type FormDefinition,
isValidFormDefinition,
isInputNoteFromFolder,
FieldTypeReadable,
} from "src/core/formDefinition";
import { FolderSuggest } from "src/suggesters/suggestFolder";
import { FieldTypeReadable } from "src/views/EditFormView";
import { setIcon, Setting, App } from "obsidian";
import FormRow from "./components/FormRow.svelte";
import InputBuilderDataview from "./components/inputBuilderDataview.svelte";
export let definition: EditableFormDefinition = {
title: "",
Expand Down Expand Up @@ -307,6 +308,11 @@
>
<label>Source Folder</label>
</div>
{:else if field.input.type === "dataview"}
<InputBuilderDataview
{index}
bind:value={field.input.query}
/>
{/if}
</div>
<div class="flex gap1">
Expand Down
32 changes: 32 additions & 0 deletions src/views/components/inputBuilderDataview.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<script lang="ts">
import FormRow from "./FormRow.svelte";
export let index: number;
export let value: string;
$: id = `dataview_${index}`;
</script>

<FormRow label="Dataview Query" {id}>
<span class="modal-form-hint">
This is a <a
href="https://blacksmithgu.github.io/obsidian-dataview/api/intro/"
>Dataview</a
>
query that will be used to populate the input suggestions. You should provide
a query that returns a list of strings, for example:
<pre class="language-js"><code
>dv.pages('#tag').map(p => p.file.name)</code
></pre>
</span>
<textarea
{id}
bind:value
name="dataview_query"
class="form-control"
rows="3"
placeholder="dv.pages('#tag').map(p => p.file.name)"
/>
</FormRow>

<style>
</style>
8 changes: 8 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@ If your plugin does not need CSS, delete this file.
.modal-form-primary {
color: var(--text-accent);
}

.modal-form-hint {
color: var(--text-muted);
}
/* .modal-form-hint code {
color: var(--text-normal);
background-color: var(--background-secondary);
} */

0 comments on commit 18669e3

Please sign in to comment.