Skip to content

Commit

Permalink
Show icon on view toolbar field picker (#891)
Browse files Browse the repository at this point in the history
  • Loading branch information
Acylation authored Jun 3, 2024
1 parent d3746dd commit ad41645
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/ui/views/Board/BoardOptions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { Field } from "src/ui/components/Field";
import { SwitchSelect } from "../Table/components/SwitchSelect";
import { i18n } from "src/lib/stores/i18n";
import { fieldToSelectableValue } from "../helpers";
import { fieldIcon, fieldToSelectableValue } from "../helpers";
import { getFieldsByType } from "./board";
import { DataFieldType, type DataField } from "src/lib/dataframe/dataframe";
import { getFieldByName } from "src/ui/app/toolbar/viewOptions/filter/helpers";
Expand Down Expand Up @@ -81,6 +81,7 @@
label={$i18n.t("views.board.include-fields")}
items={fields.map((field) => ({
label: field.name,
icon: fieldIcon(field),
value: field.name,
enabled: includedFields.includes(field.name),
}))}
Expand Down
3 changes: 2 additions & 1 deletion src/ui/views/Gallery/GalleryOptionsProvider.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
ViewToolbar,
} from "src/ui/components/Layout";
import { SwitchSelect } from "../Table/components/SwitchSelect";
import { fieldToSelectableValue } from "../helpers";
import { fieldIcon, fieldToSelectableValue } from "../helpers";
import type { GalleryConfig } from "./types";
export let fields: DataField[];
Expand Down Expand Up @@ -80,6 +80,7 @@
label={$i18n.t("views.gallery.include-fields")}
items={fields.map((field) => ({
label: field.name,
icon: fieldIcon(field),
value: field.name,
enabled: !!config?.includeFields?.includes(field.name),
}))}
Expand Down
7 changes: 7 additions & 0 deletions src/ui/views/Table/TableView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import { CreateFieldModal } from "src/ui/modals/createFieldModal";
import { Icon } from "obsidian-svelte";
import { TextLabel } from "./components/DataGrid/GridCell/GridTextCell";
import { fieldIcon } from "../helpers";
export let project: ProjectDefinition;
export let frame: DataFrame;
Expand All @@ -49,6 +50,11 @@
onConfigChange(cfg);
}
export function getFieldTypeByName(name: string): DataFieldType | undefined {
const field = fields.find((field) => name === field.name);
return field?.type;
}
$: ({ fields, records } = frame);
$: {
Expand Down Expand Up @@ -216,6 +222,7 @@
label={$i18n.t("views.table.hide-fields")}
items={columns.map((column) => ({
label: column.field,
icon: fieldIcon(column),
value: column.field,
enabled: !column.hide,
}))}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
<script lang="ts">
import type { Menu } from "obsidian";
import { Icon, IconButton } from "obsidian-svelte";
import { DataFieldType } from "src/lib/dataframe/dataframe";
import { i18n } from "src/lib/stores/i18n";
import { get } from "svelte/store";
import { fieldIcon } from "src/ui/views/helpers";
import { fieldIcon, fieldDisplayText } from "src/ui/views/helpers";
import type { GridColDef } from "../dataGrid";
import { TextLabel } from "../GridCell";
Expand All @@ -28,20 +25,7 @@
style:width={`${column.width}px`}
class:pinned={column.pinned}
>
{#if column.repeated}
{#if column.field == "tags"}
<Icon name="tags" tooltip={get(i18n).t(`data-types.tags`) ?? ""} />
{:else if column.field == "aliases"}
<Icon name="forward" tooltip={get(i18n).t(`data-types.aliases`) ?? ""} />
{:else}
<Icon name="list" tooltip={get(i18n).t(`data-types.list`) ?? ""} />
{/if}
{:else}
<Icon
name={fieldIcon(column.type ?? DataFieldType.Unknown)}
tooltip={get(i18n).t(`data-types.${column.type}`) ?? ""}
/>
{/if}
<Icon name={fieldIcon(column)} tooltip={fieldDisplayText(column)} />

<TextLabel value={column.field} />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { Menu, MenuItem } from "obsidian-svelte";
interface SwitchItem {
readonly label: string;
readonly icon?: string;
readonly value: string;
readonly enabled: boolean;
}
Expand All @@ -25,8 +26,9 @@
</div>

<Menu anchorEl={ref} open={isOpen} onClose={() => (isOpen = false)}>
{#each items as { label, value, enabled }}
{#each items as { label, icon, value, enabled }}
<MenuItem
{icon}
{label}
checked={enabled}
on:check={({ detail: checked }) => onChange(value, checked)}
Expand Down
39 changes: 37 additions & 2 deletions src/ui/views/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,21 @@ import { DataFieldType, type DataField } from "../../lib/dataframe/dataframe";
import type { ViewProps } from "../app/useView";
import type { Menu } from "obsidian";

export function fieldIcon(field: DataFieldType): string {
switch (field) {
import { i18n } from "src/lib/stores/i18n";
import { get } from "svelte/store";

export function fieldIcon(field: DataField): string {
switch (field.type) {
case DataFieldType.String:
if (field.repeated) {
switch (field.name) {
case "tags":
return "tags";
case "aliases":
return "forward";
}
return "list";
}
return "text";
case DataFieldType.Number:
return "binary";
Expand All @@ -17,6 +29,29 @@ export function fieldIcon(field: DataFieldType): string {
return "file-question";
}

export function fieldDisplayText(field: DataField): string {
switch (field.type) {
case DataFieldType.String:
if (field.repeated) {
switch (field.name) {
case "tags":
return get(i18n).t("data-types.tags");
case "aliases":
return get(i18n).t("data-types.aliases");
}
return get(i18n).t("data-types.list");
}
return get(i18n).t("data-types.string");
case DataFieldType.Number:
return get(i18n).t("data-types.number");
case DataFieldType.Boolean:
return get(i18n).t("data-types.boolean");
case DataFieldType.Date:
return get(i18n).t("data-types.date");
}
return get(i18n).t("data-types.unknown");
}

export function fieldToSelectableValue(field: DataField): {
label: string;
value: string;
Expand Down

0 comments on commit ad41645

Please sign in to comment.