Skip to content

Commit 1d83881

Browse files
committed
Added possibility to not include chess Hanayama Huzzle puzzles
1 parent 4172761 commit 1d83881

File tree

10 files changed

+83
-11
lines changed

10 files changed

+83
-11
lines changed

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export default class TrackALotPlugin extends Plugin {
2525
const command = commandFactory.command(HanayamaHuzzlesRecipe.NAME, new HanayamaHuzzlesRecipe(
2626
markdownTableFactory,
2727
markdownTableConverter,
28-
trackablesUpdater
28+
trackablesUpdater,
29+
settingsManager.settings.hanayamaHuzzles
2930
));
3031

3132
this.addCommand(command);

src/recipes/hanayama_huzzles/HanayamaHuzzlesRecipe.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import { RecipeListUpdater } from '../helpers/RecipeListUpdater';
88
import { RecipeMarkdownListUpdater } from '../helpers/RecipeMarkdownListUpdater';
99
import { RecipeMarker } from '../helpers/RecipeMarker';
1010
import { HanayamaHuzzle } from './HanayamaHuzzle';
11+
import { HanayamaHuzzlesRecipeSettings } from './settings/HanayamaHuzzlesRecipeSettings';
1112

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

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

2728
#marker = new RecipeMarker(HanayamaHuzzlesRecipe.NAME);
2829

2930
constructor(
3031
private markdownTableFactory: MarkdownTableFactory,
3132
private markdownTableConverter: MarkdownTableConverter,
32-
private trackablesUpdater: TrackablesUpdater
33+
private trackablesUpdater: TrackablesUpdater,
34+
private settings: HanayamaHuzzlesRecipeSettings
3335
) {}
3436

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

5254
async #scrapeHuzzles(): Promise<HanayamaHuzzle[]> {
5355
const metadataRegex = new RegExp(/\w+[ ](?<level>\d+)-(?<index>\d+)[ ](?<name>.+)/); // https://regex101.com/r/1vGzHd/2
54-
const scraper = new WebsiteScraper(HanayamaHuzzlesRecipe.#SCRAPE_URLS);
56+
57+
const urls = [...HanayamaHuzzlesRecipe.#SCRAPE_URLS];
58+
if (this.settings.includeChessPuzzles) {
59+
urls.push(HanayamaHuzzlesRecipe.CHESS_PUZZLES_WEBPAGE);
60+
}
61+
const scraper = new WebsiteScraper(urls);
5562

5663
return await scraper.scrape(
5764
content => {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { Setting } from 'obsidian';
2+
import { RecipeExtraSettingsAdder } from 'src/settings/RecipeExtraSettingsAdder';
3+
import { SettingsAdder } from 'src/settings/SettingsAdder';
4+
import { HanayamaHuzzlesRecipe } from '../HanayamaHuzzlesRecipe';
5+
import { HanayamaHuzzlesRecipeSettings } from './HanayamaHuzzlesRecipeSettings';
6+
7+
export class HanayamaHuzzlesRecipeExtraSettingsAdder implements RecipeExtraSettingsAdder {
8+
private includeChessPuzzlesSetting: Setting;
9+
10+
constructor(
11+
private root: HTMLElement,
12+
private settingsAdder: SettingsAdder,
13+
private settings: HanayamaHuzzlesRecipeSettings
14+
) {}
15+
16+
add(): Setting[] {
17+
this.includeChessPuzzlesSetting = this.settingsAdder.add(
18+
'Include chess puzzles',
19+
this.root.createFragment(
20+
this.root.createText('span', 'Whether to include the chess puzzles ('),
21+
this.root.createLink(HanayamaHuzzlesRecipe.CHESS_PUZZLES_WEBPAGE),
22+
this.root.createText('span', ') or not.')
23+
)
24+
);
25+
26+
return [this.includeChessPuzzlesSetting];
27+
}
28+
29+
activate(onChange: () => Promise<void>) {
30+
this.includeChessPuzzlesSetting.addToggle(toggle => {
31+
return toggle
32+
.setValue(this.settings.includeChessPuzzles)
33+
.onChange(async (value) => {
34+
this.settings.includeChessPuzzles = value;
35+
36+
await onChange();
37+
});
38+
});
39+
}
40+
}

src/recipes/hanayama_huzzles/HanayamaHuzzlesRecipeSettings.ts renamed to src/recipes/hanayama_huzzles/settings/HanayamaHuzzlesRecipeSettings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ import { RecipeSettingsToggleable } from 'src/settings/data/RecipeSettingsToggle
22

33
export class HanayamaHuzzlesRecipeSettings implements RecipeSettingsToggleable {
44
isActive: boolean;
5+
includeChessPuzzles = true;
56
}

src/recipes/iq_puzzles/IQPuzzlesRecipeSettings.ts renamed to src/recipes/iq_puzzles/settings/IQPuzzlesRecipeSettings.ts

File renamed without changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Setting } from 'obsidian';
2+
3+
export interface RecipeExtraSettingsAdder {
4+
add(): Setting[];
5+
activate(onChange: () => Promise<void>): void;
6+
}

src/settings/RecipesSettingsTab.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { App, Plugin, PluginSettingTab } from 'obsidian';
22
import 'src/html/HTMLElementExtensions';
33
import { HanayamaHuzzlesRecipe } from 'src/recipes/hanayama_huzzles/HanayamaHuzzlesRecipe';
4-
import { HanayamaHuzzlesRecipeSettings } from 'src/recipes/hanayama_huzzles/HanayamaHuzzlesRecipeSettings';
4+
import { HanayamaHuzzlesRecipeExtraSettingsAdder } from 'src/recipes/hanayama_huzzles/settings/HanayamaHuzzlesRecipeExtraSettingsAdder';
5+
import { HanayamaHuzzlesRecipeSettings } from 'src/recipes/hanayama_huzzles/settings/HanayamaHuzzlesRecipeSettings';
56
import { IQPuzzlesRecipe } from 'src/recipes/iq_puzzles/IQPuzzlesRecipe';
6-
import { IQPuzzlesRecipeSettings } from 'src/recipes/iq_puzzles/IQPuzzlesRecipeSettings';
7+
import { IQPuzzlesRecipeSettings } from 'src/recipes/iq_puzzles/settings/IQPuzzlesRecipeSettings';
78
import { RecipeSettingsAdder } from './RecipeSettingsAdder';
89
import { SettingsAdder } from './SettingsAdder';
910
import { SettingsManager } from './SettingsManager';
@@ -40,13 +41,24 @@ export class RecipesSettingsTab extends PluginSettingTab {
4041
#addHanayamaHuzzlesSettings(settings: HanayamaHuzzlesRecipeSettings, settingsAdder: SettingsAdder) {
4142
const hanayamaHuzzlesRecipeSettingsAdder = new RecipeSettingsAdder(this.containerEl, settingsAdder);
4243
hanayamaHuzzlesRecipeSettingsAdder.add(HanayamaHuzzlesRecipe.NAME, HanayamaHuzzlesRecipe.WEBPAGE);
44+
45+
const extraSettingsAdder = new HanayamaHuzzlesRecipeExtraSettingsAdder(this.containerEl, settingsAdder, settings);
46+
const extraSettings = extraSettingsAdder.add();
47+
4348
hanayamaHuzzlesRecipeSettingsAdder.activate(
4449
() => { return settings.isActive; },
4550
async value => {
4651
settings.isActive = value;
52+
53+
extraSettings.forEach(setting => {
54+
setting.setDisabled(!value);
55+
});
56+
4757
await this.settingsManager.saveSettings();
4858
}
4959
);
60+
61+
extraSettingsAdder.activate(async () => { await this.settingsManager.saveSettings(); });
5062
}
5163

5264
#addIQPuzzlesSettings(settings: IQPuzzlesRecipeSettings, settingsAdder: SettingsAdder) {

src/settings/SettingsManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Plugin } from 'obsidian';
2-
import { HanayamaHuzzlesRecipeSettings } from 'src/recipes/hanayama_huzzles/HanayamaHuzzlesRecipeSettings';
3-
import { IQPuzzlesRecipeSettings } from 'src/recipes/iq_puzzles/IQPuzzlesRecipeSettings';
2+
import { HanayamaHuzzlesRecipeSettings } from 'src/recipes/hanayama_huzzles/settings/HanayamaHuzzlesRecipeSettings';
3+
import { IQPuzzlesRecipeSettings } from 'src/recipes/iq_puzzles/settings/IQPuzzlesRecipeSettings';
44
import { RecipesPluginSettings } from './data/RecipesPluginSettings';
55

66
export class SettingsManager {

src/settings/data/RecipesPluginSettings.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { HanayamaHuzzlesRecipeSettings } from 'src/recipes/hanayama_huzzles/HanayamaHuzzlesRecipeSettings';
2-
import { IQPuzzlesRecipeSettings } from 'src/recipes/iq_puzzles/IQPuzzlesRecipeSettings';
1+
import { HanayamaHuzzlesRecipeSettings } from 'src/recipes/hanayama_huzzles/settings/HanayamaHuzzlesRecipeSettings';
2+
import { IQPuzzlesRecipeSettings } from 'src/recipes/iq_puzzles/settings/IQPuzzlesRecipeSettings';
33

44
export interface RecipesPluginSettings {
55
hanayamaHuzzles: HanayamaHuzzlesRecipeSettings;

styles.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@
66
display: inline-block;
77
margin-block-end: var(--p-spacing);
88
}
9+
10+
.is-disabled {
11+
pointer-events: none;
12+
opacity: 0.2;
13+
}

0 commit comments

Comments
 (0)