Skip to content

Commit

Permalink
Merge pull request #23 from tmayoff/small_tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
tmayoff committed Feb 4, 2024
2 parents 8b89908 + 1d11929 commit 104d7a5
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 208 deletions.
8 changes: 8 additions & 0 deletions src/main.ts
Expand Up @@ -78,8 +78,16 @@ class RecipeSearch extends Modal {
async onOpen() {
this.recipeView = new SearchRecipe({
target: this.containerEl.children[1].children[2],
props: {
app: this.app,
},
});

this.recipeView.$on('close_modal', () => {
this.close();
});
}

onClose(): void {
this.contentEl.empty();
this.recipeView?.$destroy();
Expand Down
16 changes: 14 additions & 2 deletions src/recipe/RecipeButton.svelte
@@ -1,11 +1,17 @@
<script lang="ts">
import type { App } from "obsidian";
import { DAYS_OF_WEEK } from "../constants";
import { add_recipe_to_meal_plan } from "../meal_plan/plan";
import { open_note_file } from "../utils/filesystem";
import type { Recipe } from "./recipe";
import { createEventDispatcher } from "svelte";
export let app: App;
export let recipe: Recipe;
let open = false;
let dispatch = createEventDispatcher();
</script>

<div class="realtive inline-block text-left">
Expand Down Expand Up @@ -41,7 +47,13 @@
aria-labelledby="menu-button"
tabindex="-1"
>
<button on:click={() => {}}>Go to recipe</button>
<button
on:click={async () => {
await open_note_file(app, recipe.path);
open = false;
dispatch('close_modal');
}}>Go to recipe</button
>
{#each DAYS_OF_WEEK as day}
<button
class="rounded-none"
Expand Down
10 changes: 4 additions & 6 deletions src/recipe/SearchRecipe.svelte
Expand Up @@ -3,12 +3,12 @@
import { ingredients, recipes } from "../store";
import { MinusCircle } from "lucide-svelte";
import { IngredientSuggestionModal } from "../suggester/IngredientSuggest";
import { TextComponent } from "obsidian";
import { App, TextComponent } from "obsidian";
import { createEventDispatcher, onMount } from "svelte";
import { DAYS_OF_WEEK } from "../constants";
import { add_recipe_to_meal_plan } from "../meal_plan/plan";
import RecipeButton from "./RecipeButton.svelte";
export let app: App;
let search_operation = writable("any of");
const search_ingredients = writable(new Set<string>());
Expand Down Expand Up @@ -64,8 +64,6 @@
suggester.text.setValue("");
};
});
let dispatch = createEventDispatcher();
</script>

<div>
Expand Down Expand Up @@ -118,7 +116,7 @@
<h2>Recipes</h2>
<div class="flex flex-col p-3">
{#each $found_recipes as recipe}
<RecipeButton {recipe} />
<RecipeButton on:close_modal {app} {recipe} />
{/each}
</div>
</div>
Expand Down
42 changes: 22 additions & 20 deletions src/recipe/recipe.ts
@@ -1,32 +1,34 @@
import { TFile, type TFolder } from "obsidian";
import type { Ingredient } from "parse-ingredient";
import { get_ingredients } from "./ingredients";
import { TFile, type TFolder } from 'obsidian';
import type { Ingredient } from 'parse-ingredient';
import { get_ingredients } from './ingredients';

export class Recipe {
name: string;
path: TFile;
name: string;
path: TFile;

ingredients: Ingredient[];
ingredients: Ingredient[];

constructor(path: TFile, name: string = path.basename) {
this.path = path;
this.name = name;
this.ingredients = new Array();
}
constructor(path: TFile, name: string = path.basename) {
this.path = path;
this.name = name;
this.ingredients = new Array();
}

public async fill_ingredients() {
this.ingredients = await get_ingredients(this.path);
}
public async fill_ingredients() {
this.ingredients = await get_ingredients(this.path);
}
}

export async function get_recipes(recipe_dir: TFolder) {
const recipes: Recipe[] = new Array();
const recipes: Recipe[] = new Array();

for (const file of recipe_dir.children) {
const recipe = new Recipe(file);
await recipe.fill_ingredients();
recipes.push(recipe);
for (const file of recipe_dir.children) {
if (file instanceof TFile) {
const recipe = new Recipe(file);
await recipe.fill_ingredients();
recipes.push(recipe);
}
}

return recipes;
return recipes;
}
34 changes: 34 additions & 0 deletions src/utils/filesystem.ts
@@ -0,0 +1,34 @@
import { TFile, type App } from 'obsidian';

export async function open_note_file(app: App, file: TFile) {
open_note_path(app, file.path);
}

export async function open_note_path(app: App, file_path: string) {
let found = false;

const file_name = file_path.substring(file_path.lastIndexOf('/') + 1, file_path.lastIndexOf('.'));
app.workspace.iterateAllLeaves((leaf) => {
if (leaf.getDisplayText() === file_name) {
app.workspace.setActiveLeaf(leaf);
found = true;
}
});

if (!found) {
await app.workspace.openLinkText(file_path, '', true);
}
}

export function note_exists(app: App, file_path: string) {
const file = app.vault.getAbstractFileByPath(file_path);
return file != null && file instanceof TFile;
}

export function append_markdown_ext(file_path: string) {
if (!file_path.endsWith('.md')) {
file_path += '.md';
}

return file_path;
}
1 change: 1 addition & 0 deletions test_vault/.obsidian/.gitignore
@@ -0,0 +1 @@
workspace.json
180 changes: 0 additions & 180 deletions test_vault/.obsidian/workspace.json

This file was deleted.

1 change: 1 addition & 0 deletions test_vault/Meal Plan.md
@@ -1,6 +1,7 @@
# Week of February 4th
## Sunday
- [[Creamy Garlic Chicken & Potatoes]]
- [[Creamy Garlic Chicken & Potatoes]]

## Monday
- [[Creamy Garlic Chicken & Potatoes]]
Expand Down

0 comments on commit 104d7a5

Please sign in to comment.