Skip to content

Commit

Permalink
Formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
tmayoff committed Jul 1, 2024
1 parent cd896a7 commit 6da4be4
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 103 deletions.
3 changes: 1 addition & 2 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
"style": {
"useSingleVarDeclarator": "off",
"noParameterAssign": "off",
"noNonNullAssertion": "off",
"useNamingConvention": "snake_case"
"noNonNullAssertion": "off"
},
"suspicious": {
"noExplicitAny": "off"
Expand Down
20 changes: 12 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,19 @@ export default class MealPlugin extends Plugin {
});

this.registerEvent(
this.app.workspace.on("file-menu", (e, t) => {
if (t instanceof TFile && t.path.contains(get(this.ctx.settings).recipe_directory)) {
e.addItem((e) => {
return e.setTitle("Add to shopping list").setIcon("shopping-basket").onClick(() => {
add_file_to_shopping_list(this.ctx, t);
this.app.workspace.on('file-menu', (e, t) => {
if (t instanceof TFile && t.path.contains(get(this.ctx.settings).recipe_directory)) {
e.addItem((e) => {
return e
.setTitle('Add to shopping list')
.setIcon('shopping-basket')
.onClick(() => {
add_file_to_shopping_list(this.ctx, t);
});
});
});
}
}));
}
}),
);

console.log('tmayoff-meals loaded');
}
Expand Down
9 changes: 4 additions & 5 deletions src/meal_plan/shopping_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export async function add_meal_plan_to_shopping_list(ctx: Context) {
}

export async function add_file_to_shopping_list(ctx: Context, recipe_file: TFile) {

const shopping_list_file_path = append_markdown_ext(get(ctx.settings).shopping_list_note);
let file = ctx.app.vault.getFileByPath(shopping_list_file_path);
if (file == null) {
Expand All @@ -83,7 +82,6 @@ export async function add_file_to_shopping_list(ctx: Context, recipe_file: TFile

return data;
});

}

function get_meal_plan_ingredients(ctx: Context, file: TFile) {
Expand Down Expand Up @@ -158,9 +156,10 @@ function get_ingredients_recipe(ctx: Context, recipe_note: TFile) {
const ignore_list = get(ctx.settings).shopping_list_ignore;

return r.ingredients.filter((i) => {
let found = ignore_list.find((ignored) => {
return i.description.toLowerCase() !== ignored.toLowerCase();
}) === undefined;
let found =
ignore_list.find((ignored) => {
return i.description.toLowerCase() !== ignored.toLowerCase();
}) === undefined;
return !found;
});
}
84 changes: 42 additions & 42 deletions src/recipe/RecipeButton.svelte
Original file line number Diff line number Diff line change
@@ -1,69 +1,69 @@
<script lang="ts">
import { SuggestModal } from "obsidian";
import type { Context } from "../context";
import type { Recipe } from "./recipe";
import { createEventDispatcher, onMount } from "svelte";
import { open_note_file } from "../utils/filesystem";
import { add_recipe_to_meal_plan } from "../meal_plan/plan";
import { DAYS_OF_WEEK } from "../constants";
import { SuggestModal } from 'obsidian';
import type { Context } from '../context';
import type { Recipe } from './recipe';
import { createEventDispatcher, onMount } from 'svelte';
import { open_note_file } from '../utils/filesystem';
import { add_recipe_to_meal_plan } from '../meal_plan/plan';
import { DAYS_OF_WEEK } from '../constants';
export let ctx: Context;
export let recipe: Recipe;
export let ctx: Context;
export let recipe: Recipe;
let open = false;
let open = false;
let dispatch = createEventDispatcher();
let dispatch = createEventDispatcher();
type Callback = () => Promise<void>;
type Callback = () => Promise<void>;
class ButtonTarget {
name: string = "";
class ButtonTarget {
name: string = '';
cb: Callback | undefined;
}
}
let button_targets: Array<ButtonTarget> = [
let button_targets: Array<ButtonTarget> = [
{
name: "Go to recipe",
cb: async () => {
await open_note_file(ctx.app, recipe.path);
dispatch("close_modal");
},
name: 'Go to recipe',
cb: async () => {
await open_note_file(ctx.app, recipe.path);
dispatch('close_modal');
},
},
];
];
for (const d of DAYS_OF_WEEK) {
for (const d of DAYS_OF_WEEK) {
button_targets.push({
name: d,
cb: async () => {
await add_recipe_to_meal_plan(ctx, recipe, d);
},
name: d,
cb: async () => {
await add_recipe_to_meal_plan(ctx, recipe, d);
},
});
}
}
class ButtonModal extends SuggestModal<ButtonTarget> {
class ButtonModal extends SuggestModal<ButtonTarget> {
getSuggestions(_query: string): ButtonTarget[] | Promise<ButtonTarget[]> {
// TODO search actions
return button_targets;
// TODO search actions
return button_targets;
}
onChooseSuggestion(item: ButtonTarget, _evt: KeyboardEvent | MouseEvent) {
if (item.cb) {
item.cb();
}
this.close();
if (item.cb) {
item.cb();
}
this.close();
}
renderSuggestion(item: ButtonTarget, el: HTMLElement): void {
el.createEl("div", { text: item.name });
el.createEl('div', { text: item.name });
}
}
}
let modal: ButtonModal;
let open_recipe_dropdown = function () {
let modal: ButtonModal;
let open_recipe_dropdown = function () {
modal.open();
};
};
onMount(() => {
onMount(() => {
modal = new ButtonModal(ctx.app);
});
});
</script>

<div class="realtive inline-block text-left">
Expand Down
83 changes: 39 additions & 44 deletions src/recipe/SearchRecipe.svelte
Original file line number Diff line number Diff line change
@@ -1,70 +1,65 @@
<script lang="ts">
import { derived, writable } from "svelte/store";
import { IngredientSuggestionModal } from "../suggester/IngredientSuggest";
import { TextComponent } from "obsidian";
import { onMount } from "svelte";
import RecipeButton from "./RecipeButton.svelte";
import type { Context } from "../context";
import { derived, writable } from 'svelte/store';
import { IngredientSuggestionModal } from '../suggester/IngredientSuggest';
import { TextComponent } from 'obsidian';
import { onMount } from 'svelte';
import RecipeButton from './RecipeButton.svelte';
import type { Context } from '../context';
export let ctx: Context;
export let ctx: Context;
let ingredients = ctx.ingredients;
let ingredients = ctx.ingredients;
let search_operation = writable("any of");
let search_operation = writable('any of');
const search_ingredients = writable(new Set<string>());
const search_ingredients = writable(new Set<string>());
function add_ingredient(ingredient: string) {
function add_ingredient(ingredient: string) {
search_ingredients.update((items) => {
items.add(ingredient);
return items;
items.add(ingredient);
return items;
});
}
}
const found_recipes = derived(
[search_ingredients, search_operation, ctx.recipes],
([$search_ingredients, $search_operation, $recipes]) => {
return $recipes.filter((recipe) => {
const found_recipes = derived([search_ingredients, search_operation, ctx.recipes], ([$search_ingredients, $search_operation, $recipes]) => {
return $recipes.filter((recipe) => {
let descs = recipe.ingredients.map((i) => {
if (i == undefined || i.description === undefined) {
return "";
}
if (i == undefined || i.description === undefined) {
return '';
}
return i.description.toLocaleLowerCase();
return i.description.toLocaleLowerCase();
});
if ($search_operation == "all of") {
return [...$search_ingredients].every((i) => {
return descs.contains(i);
});
} else if ($search_operation == "any of") {
return [...$search_ingredients].some((i) => {
return descs.contains(i);
});
if ($search_operation == 'all of') {
return [...$search_ingredients].every((i) => {
return descs.contains(i);
});
} else if ($search_operation == 'any of') {
return [...$search_ingredients].some((i) => {
return descs.contains(i);
});
}
});
},
);
});
});
let suggester_parent: HTMLElement;
onMount(() => {
let suggester_parent: HTMLElement;
onMount(() => {
let suggester_text = new TextComponent(suggester_parent);
suggester_text.inputEl.addClass("w-full");
suggester_text.inputEl.addClass('w-full');
let suggester = new IngredientSuggestionModal(ctx.app, suggester_text, [
...$ingredients,
]);
let suggester = new IngredientSuggestionModal(ctx.app, suggester_text, [...$ingredients]);
suggester_text.onChange(() => {
suggester.shouldNotOpen = false;
suggester.open();
suggester.shouldNotOpen = false;
suggester.open();
});
suggester.onClose = () => {
add_ingredient(suggester.text.getValue());
suggester.text.setValue("");
add_ingredient(suggester.text.getValue());
suggester.text.setValue('');
};
});
});
</script>

<div>
Expand Down
4 changes: 2 additions & 2 deletions src/utils/Dropdown.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
export let text: string;
export let text: string;
let open = false;
let open = false;
</script>

<div class="realtive inline-block text-left">
Expand Down

0 comments on commit 6da4be4

Please sign in to comment.