Skip to content

Commit

Permalink
Added possibility to not include chess Hanayama Huzzle puzzles
Browse files Browse the repository at this point in the history
  • Loading branch information
revolter committed Feb 25, 2024
1 parent 4172761 commit 1d83881
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export default class TrackALotPlugin extends Plugin {
const command = commandFactory.command(HanayamaHuzzlesRecipe.NAME, new HanayamaHuzzlesRecipe(
markdownTableFactory,
markdownTableConverter,
trackablesUpdater
trackablesUpdater,
settingsManager.settings.hanayamaHuzzles
));

this.addCommand(command);
Expand Down
15 changes: 11 additions & 4 deletions src/recipes/hanayama_huzzles/HanayamaHuzzlesRecipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import { RecipeListUpdater } from '../helpers/RecipeListUpdater';
import { RecipeMarkdownListUpdater } from '../helpers/RecipeMarkdownListUpdater';
import { RecipeMarker } from '../helpers/RecipeMarker';
import { HanayamaHuzzle } from './HanayamaHuzzle';
import { HanayamaHuzzlesRecipeSettings } from './settings/HanayamaHuzzlesRecipeSettings';

export class HanayamaHuzzlesRecipe implements Recipe {
static readonly NAME = 'Hanayama Huzzles';
static readonly WEBPAGE = 'https://hanayama-toys.com/product-category/puzzles/huzzle';
static readonly CHESS_PUZZLES_WEBPAGE = 'https://hanayama-toys.com/product-category/puzzles/huzzle/chess-puzzle';

static readonly #HEADERS: readonly string[] = ['Level', 'Index', 'Name', 'Picture', 'Status'];
static readonly #SCRAPE_URLS: readonly string[] = [
Expand All @@ -20,16 +22,16 @@ export class HanayamaHuzzlesRecipe implements Recipe {
'https://hanayama-toys.com/product-category/puzzles/huzzle/level-3-normal',
'https://hanayama-toys.com/product-category/puzzles/huzzle/level-4-hard',
'https://hanayama-toys.com/product-category/puzzles/huzzle/level-5-expert',
'https://hanayama-toys.com/product-category/puzzles/huzzle/level-6-grand-master',
'https://hanayama-toys.com/product-category/puzzles/huzzle/chess-puzzle'
'https://hanayama-toys.com/product-category/puzzles/huzzle/level-6-grand-master'
];

#marker = new RecipeMarker(HanayamaHuzzlesRecipe.NAME);

constructor(
private markdownTableFactory: MarkdownTableFactory,
private markdownTableConverter: MarkdownTableConverter,
private trackablesUpdater: TrackablesUpdater
private trackablesUpdater: TrackablesUpdater,
private settings: HanayamaHuzzlesRecipeSettings
) {}

async updatedListInContent(content: string): Promise<string> {
Expand All @@ -51,7 +53,12 @@ export class HanayamaHuzzlesRecipe implements Recipe {

async #scrapeHuzzles(): Promise<HanayamaHuzzle[]> {
const metadataRegex = new RegExp(/\w+[ ](?<level>\d+)-(?<index>\d+)[ ](?<name>.+)/); // https://regex101.com/r/1vGzHd/2
const scraper = new WebsiteScraper(HanayamaHuzzlesRecipe.#SCRAPE_URLS);

const urls = [...HanayamaHuzzlesRecipe.#SCRAPE_URLS];
if (this.settings.includeChessPuzzles) {
urls.push(HanayamaHuzzlesRecipe.CHESS_PUZZLES_WEBPAGE);
}
const scraper = new WebsiteScraper(urls);

return await scraper.scrape(
content => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Setting } from 'obsidian';
import { RecipeExtraSettingsAdder } from 'src/settings/RecipeExtraSettingsAdder';
import { SettingsAdder } from 'src/settings/SettingsAdder';
import { HanayamaHuzzlesRecipe } from '../HanayamaHuzzlesRecipe';
import { HanayamaHuzzlesRecipeSettings } from './HanayamaHuzzlesRecipeSettings';

export class HanayamaHuzzlesRecipeExtraSettingsAdder implements RecipeExtraSettingsAdder {
private includeChessPuzzlesSetting: Setting;

constructor(
private root: HTMLElement,
private settingsAdder: SettingsAdder,
private settings: HanayamaHuzzlesRecipeSettings
) {}

add(): Setting[] {
this.includeChessPuzzlesSetting = this.settingsAdder.add(
'Include chess puzzles',
this.root.createFragment(
this.root.createText('span', 'Whether to include the chess puzzles ('),
this.root.createLink(HanayamaHuzzlesRecipe.CHESS_PUZZLES_WEBPAGE),
this.root.createText('span', ') or not.')
)
);

return [this.includeChessPuzzlesSetting];
}

activate(onChange: () => Promise<void>) {
this.includeChessPuzzlesSetting.addToggle(toggle => {
return toggle
.setValue(this.settings.includeChessPuzzles)
.onChange(async (value) => {
this.settings.includeChessPuzzles = value;

await onChange();
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import { RecipeSettingsToggleable } from 'src/settings/data/RecipeSettingsToggle

export class HanayamaHuzzlesRecipeSettings implements RecipeSettingsToggleable {
isActive: boolean;
includeChessPuzzles = true;
}
6 changes: 6 additions & 0 deletions src/settings/RecipeExtraSettingsAdder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Setting } from 'obsidian';

export interface RecipeExtraSettingsAdder {
add(): Setting[];
activate(onChange: () => Promise<void>): void;
}
16 changes: 14 additions & 2 deletions src/settings/RecipesSettingsTab.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { App, Plugin, PluginSettingTab } from 'obsidian';
import 'src/html/HTMLElementExtensions';
import { HanayamaHuzzlesRecipe } from 'src/recipes/hanayama_huzzles/HanayamaHuzzlesRecipe';
import { HanayamaHuzzlesRecipeSettings } from 'src/recipes/hanayama_huzzles/HanayamaHuzzlesRecipeSettings';
import { HanayamaHuzzlesRecipeExtraSettingsAdder } from 'src/recipes/hanayama_huzzles/settings/HanayamaHuzzlesRecipeExtraSettingsAdder';
import { HanayamaHuzzlesRecipeSettings } from 'src/recipes/hanayama_huzzles/settings/HanayamaHuzzlesRecipeSettings';
import { IQPuzzlesRecipe } from 'src/recipes/iq_puzzles/IQPuzzlesRecipe';
import { IQPuzzlesRecipeSettings } from 'src/recipes/iq_puzzles/IQPuzzlesRecipeSettings';
import { IQPuzzlesRecipeSettings } from 'src/recipes/iq_puzzles/settings/IQPuzzlesRecipeSettings';
import { RecipeSettingsAdder } from './RecipeSettingsAdder';
import { SettingsAdder } from './SettingsAdder';
import { SettingsManager } from './SettingsManager';
Expand Down Expand Up @@ -40,13 +41,24 @@ export class RecipesSettingsTab extends PluginSettingTab {
#addHanayamaHuzzlesSettings(settings: HanayamaHuzzlesRecipeSettings, settingsAdder: SettingsAdder) {
const hanayamaHuzzlesRecipeSettingsAdder = new RecipeSettingsAdder(this.containerEl, settingsAdder);
hanayamaHuzzlesRecipeSettingsAdder.add(HanayamaHuzzlesRecipe.NAME, HanayamaHuzzlesRecipe.WEBPAGE);

const extraSettingsAdder = new HanayamaHuzzlesRecipeExtraSettingsAdder(this.containerEl, settingsAdder, settings);
const extraSettings = extraSettingsAdder.add();

hanayamaHuzzlesRecipeSettingsAdder.activate(
() => { return settings.isActive; },
async value => {
settings.isActive = value;

extraSettings.forEach(setting => {
setting.setDisabled(!value);
});

await this.settingsManager.saveSettings();
}
);

extraSettingsAdder.activate(async () => { await this.settingsManager.saveSettings(); });
}

#addIQPuzzlesSettings(settings: IQPuzzlesRecipeSettings, settingsAdder: SettingsAdder) {
Expand Down
4 changes: 2 additions & 2 deletions src/settings/SettingsManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Plugin } from 'obsidian';
import { HanayamaHuzzlesRecipeSettings } from 'src/recipes/hanayama_huzzles/HanayamaHuzzlesRecipeSettings';
import { IQPuzzlesRecipeSettings } from 'src/recipes/iq_puzzles/IQPuzzlesRecipeSettings';
import { HanayamaHuzzlesRecipeSettings } from 'src/recipes/hanayama_huzzles/settings/HanayamaHuzzlesRecipeSettings';
import { IQPuzzlesRecipeSettings } from 'src/recipes/iq_puzzles/settings/IQPuzzlesRecipeSettings';
import { RecipesPluginSettings } from './data/RecipesPluginSettings';

export class SettingsManager {
Expand Down
4 changes: 2 additions & 2 deletions src/settings/data/RecipesPluginSettings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HanayamaHuzzlesRecipeSettings } from 'src/recipes/hanayama_huzzles/HanayamaHuzzlesRecipeSettings';
import { IQPuzzlesRecipeSettings } from 'src/recipes/iq_puzzles/IQPuzzlesRecipeSettings';
import { HanayamaHuzzlesRecipeSettings } from 'src/recipes/hanayama_huzzles/settings/HanayamaHuzzlesRecipeSettings';
import { IQPuzzlesRecipeSettings } from 'src/recipes/iq_puzzles/settings/IQPuzzlesRecipeSettings';

export interface RecipesPluginSettings {
hanayamaHuzzles: HanayamaHuzzlesRecipeSettings;
Expand Down
5 changes: 5 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@
display: inline-block;
margin-block-end: var(--p-spacing);
}

.is-disabled {
pointer-events: none;
opacity: 0.2;
}

0 comments on commit 1d83881

Please sign in to comment.