Skip to content

Commit

Permalink
feat: add settings to enable/disable import of certain types
Browse files Browse the repository at this point in the history
Closes: #16
  • Loading branch information
eXaminator committed Nov 18, 2020
1 parent ac710a5 commit 2ac2edb
Show file tree
Hide file tree
Showing 19 changed files with 113 additions and 53 deletions.
5 changes: 3 additions & 2 deletions src/kanka/Ability.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import EntityType from '../types/EntityType';
import { AbilityData } from '../types/kanka';
import KankaEntity from './KankaEntity';

export default class Ability extends KankaEntity<AbilityData> {
get entityType(): string {
return 'ability';
get entityType(): EntityType {
return EntityType.ability;
}

public get type(): string | undefined {
Expand Down
5 changes: 3 additions & 2 deletions src/kanka/Campaign.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import EntityType from '../types/EntityType';
import { CampaignData } from '../types/kanka';
import Ability from './Ability';
import Character from './Character';
Expand Down Expand Up @@ -26,8 +27,8 @@ export default class Campaign extends KankaEntity<CampaignData> {
#journals = new KankaEntityCollection(this.api.withPath('journals'), Journal);
#quests = new KankaEntityCollection(this.api.withPath('quests'), Quest);

get entityType(): string {
return 'campaign';
get entityType(): EntityType {
return EntityType.campaign;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
5 changes: 3 additions & 2 deletions src/kanka/Character.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import EntityType from '../types/EntityType';
import { CharacterData } from '../types/kanka';
import { MetaDataType } from '../types/KankaSettings';
import KankaEntity from './KankaEntity';

export default class Character extends KankaEntity<CharacterData> {
get entityType(): string {
return 'character';
get entityType(): EntityType {
return EntityType.character;
}

public get type(): string | undefined {
Expand Down
5 changes: 3 additions & 2 deletions src/kanka/Event.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import EntityType from '../types/EntityType';
import { EventData } from '../types/kanka';
import KankaEntity from './KankaEntity';

export default class Event extends KankaEntity<EventData> {
get entityType(): string {
return 'event';
get entityType(): EntityType {
return EntityType.event;
}

public get type(): string | undefined {
Expand Down
5 changes: 3 additions & 2 deletions src/kanka/Family.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import EntityType from '../types/EntityType';
import { FamilyData } from '../types/kanka';
import KankaEntity from './KankaEntity';

export default class Family extends KankaEntity<FamilyData> {
get entityType(): string {
return 'family';
get entityType(): EntityType {
return EntityType.family;
}

public get type(): string | undefined {
Expand Down
5 changes: 3 additions & 2 deletions src/kanka/Item.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import EntityType from '../types/EntityType';
import { ItemData } from '../types/kanka';
import KankaEntity from './KankaEntity';

export default class Item extends KankaEntity<ItemData> {
get entityType(): string {
return 'item';
get entityType(): EntityType {
return EntityType.item;
}

public get type(): string | undefined {
Expand Down
5 changes: 3 additions & 2 deletions src/kanka/Journal.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import EntityType from '../types/EntityType';
import { JournalData } from '../types/kanka';
import KankaEntity from './KankaEntity';

export default class Journal extends KankaEntity<JournalData> {
get entityType(): string {
return 'journal';
get entityType(): EntityType {
return EntityType.journal;
}

public get type(): string | undefined {
Expand Down
3 changes: 2 additions & 1 deletion src/kanka/KankaEntity.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import EntityType from '../types/EntityType';
import { KankaEntityData } from '../types/kanka';
import { MetaDataType } from '../types/KankaSettings';
import EntityAttribute from './EntityAttribute';
Expand All @@ -17,7 +18,7 @@ export default abstract class KankaEntity<T extends KankaEntityData = KankaEntit
return this.data.id;
}

abstract get entityType(): string;
abstract get entityType(): EntityType;

public get attributes(): EntityAttribute[] {
return this.#attributes;
Expand Down
5 changes: 3 additions & 2 deletions src/kanka/Location.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import EntityType from '../types/EntityType';
import { LocationData } from '../types/kanka';
import KankaEntity from './KankaEntity';

export default class Location extends KankaEntity<LocationData> {
get entityType(): string {
return 'location';
get entityType(): EntityType {
return EntityType.location;
}

public get type(): string | undefined {
Expand Down
5 changes: 3 additions & 2 deletions src/kanka/Note.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import EntityType from '../types/EntityType';
import { NoteData } from '../types/kanka';
import KankaEntity from './KankaEntity';

export default class Note extends KankaEntity<NoteData> {
get entityType(): string {
return 'note';
get entityType(): EntityType {
return EntityType.note;
}
}
5 changes: 3 additions & 2 deletions src/kanka/Organisation.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import EntityType from '../types/EntityType';
import { OrganisationData } from '../types/kanka';
import KankaEntity from './KankaEntity';

export default class Organisation extends KankaEntity<OrganisationData> {
get entityType(): string {
return 'organisation';
get entityType(): EntityType {
return EntityType.organisation;
}

public get type(): string | undefined {
Expand Down
5 changes: 3 additions & 2 deletions src/kanka/Quest.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import EntityType from '../types/EntityType';
import { QuestData } from '../types/kanka';
import KankaEntity from './KankaEntity';

export default class Quest extends KankaEntity<QuestData> {
get entityType(): string {
return 'journal';
get entityType(): EntityType {
return EntityType.journal;
}

public get type(): string | undefined {
Expand Down
5 changes: 3 additions & 2 deletions src/kanka/Race.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import EntityType from '../types/EntityType';
import { RaceData } from '../types/kanka';
import KankaEntity from './KankaEntity';

export default class Race extends KankaEntity<RaceData> {
get entityType(): string {
return 'race';
get entityType(): EntityType {
return EntityType.race;
}

public get type(): string | undefined {
Expand Down
1 change: 1 addition & 0 deletions src/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"KANKA.SettingsMetaDataCharacterTraitVisibility.value.none": "No character traits",
"KANKA.SettingsImageInText.label": "Include image in text",
"KANKA.SettingsImageInText.hint": "Should the image be include next to the text in addition to the regular journal entry image?",
"KANKA.SettingsEntityTypeVisibility.hint": "Should this type of entity be loaded? Reducing the types of entity also reduces the number of requests made to Kanka and thus improves the performance.",
"KANKA.BrowserLinkFolder": "Link all",
"KANKA.BrowserRefreshFolder": "Refresh linked",
"KANKA.BrowserNotificationSyncedFolder": "Refreshed all Kanka entities of type {type}.",
Expand Down
31 changes: 17 additions & 14 deletions src/module/KankaBrowser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Campaign from '../kanka/Campaign';
import KankaEntity from '../kanka/KankaEntity';
import moduleConfig from '../module.json';
import { KankaSettings } from '../types/KankaSettings';
import EntityType from '../types/EntityType';
import { kankaImportTypeSetting, KankaSettings } from '../types/KankaSettings';
import getSetting from './getSettings';
import { ensureJournalFolder, findEntriesByType, findEntryByEntity, findEntryByEntityId, writeJournalEntry } from './journal';

Expand All @@ -16,38 +17,38 @@ interface TemplateData {
data: Record<string, EntityList>;
}

const entityTypes = {
ability: {
const entityTypes: Partial<Record<EntityType, { icon: string }>> = {
[EntityType.ability]: {
icon: 'fa-fire',
},
character: {
[EntityType.character]: {
icon: 'fa-user',
},
event: {
[EntityType.event]: {
icon: 'fa-bolt',
},
family: {
[EntityType.family]: {
icon: 'fa-users',
},
item: {
[EntityType.item]: {
icon: 'fa-crown',
},
journal: {
[EntityType.journal]: {
icon: 'fa-feather-alt',
},
location: {
[EntityType.location]: {
icon: 'fa-chess-rook',
},
note: {
[EntityType.note]: {
icon: 'fa-book-open',
},
organisation: {
[EntityType.organisation]: {
icon: 'fa-theater-masks',
},
quest: {
[EntityType.quest]: {
icon: 'fa-map-signs',
},
race: {
[EntityType.race]: {
icon: 'fa-dragon',
},
};
Expand Down Expand Up @@ -125,7 +126,9 @@ export default class KankaBrowser extends Application {
(...parts: unknown[]) => parts.filter(p => typeof p !== 'object').join('.'),
);

const types = Object.keys(entityTypes);
const types = Object
.keys(entityTypes)
.filter(type => getSetting(kankaImportTypeSetting(type as EntityType))) as EntityType[];
const lists = await Promise.all(types.map(type => campaign.getByType(type)?.all(true)));
const data = {};
const allowPrivate = getSetting(KankaSettings.importPrivateEntities) as boolean;
Expand Down
45 changes: 32 additions & 13 deletions src/module/configureSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import CampaignRepository from '../kanka/CampaignRepository';
import KankaApi from '../kanka/KankaApi';
import { logError } from '../logger';
import moduleConfig from '../module.json';
import EntityType from '../types/EntityType';
import {
MetaDataBasicVisibility,
KankaSettings,
MetaDataAttributeVisibility,
MetaDataCharacterTraitVisibility,
MetaDataCharacterTraitVisibility, kankaImportTypeSetting,
} from '../types/KankaSettings';
import getSettings from './getSettings';
import KankaBrowser from './KankaBrowser';
Expand Down Expand Up @@ -106,6 +107,19 @@ export async function registerSettings(): Promise<void> {
},
);

game.settings.register(
moduleConfig.name,
KankaSettings.imageInText,
{
name: game.i18n.localize('KANKA.SettingsImageInText.label'),
hint: game.i18n.localize('KANKA.SettingsImageInText.hint'),
scope: 'world',
config: true,
type: Boolean,
default: false,
},
);

game.settings.register(
moduleConfig.name,
KankaSettings.importPrivateEntities,
Expand Down Expand Up @@ -175,18 +189,23 @@ export async function registerSettings(): Promise<void> {
},
);

game.settings.register(
moduleConfig.name,
KankaSettings.imageInText,
{
name: game.i18n.localize('KANKA.SettingsImageInText.label'),
hint: game.i18n.localize('KANKA.SettingsImageInText.hint'),
scope: 'world',
config: true,
type: Boolean,
default: false,
},
);
Object
.values(EntityType)
.filter(type => type !== EntityType.campaign)
.forEach((type: EntityType) => {
game.settings.register(
moduleConfig.name,
kankaImportTypeSetting(type),
{
name: game.i18n.localize(`KANKA.EntityType.${type}`),
hint: game.i18n.localize('KANKA.SettingsEntityTypeVisibility.hint'),
scope: 'world',
config: true,
type: Boolean,
default: true,
},
);
});

game.settings.sheet.render(); // update sheet if it already visible
}
2 changes: 1 addition & 1 deletion src/module/journal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function ensureJournalFolder(type: string): Promise<Folder | undefi
let folder = findFolderByType(type);

if (!folder) {
const nameKey = `KANKA.EntityType.${type}`;
const nameKey = `KANKA.string.${type}`;
const name = game.i18n.localize(nameKey);

folder = await Folder.create({
Expand Down
16 changes: 16 additions & 0 deletions src/types/EntityType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
enum EntityType {
ability = 'ability',
campaign = 'campaign',
character = 'character',
event = 'event',
family = 'family',
item = 'item',
journal = 'journal',
location = 'location',
note = 'note',
organisation = 'organisation',
quest = 'quest',
race = 'race',
}

export default EntityType;
8 changes: 8 additions & 0 deletions src/types/KankaSettings.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import EntityType from './EntityType';

export enum KankaSettings {
accessToken = 'access_token',
campaign = 'campaign',
Expand All @@ -8,6 +10,12 @@ export enum KankaSettings {
imageInText = 'imageInText',
}

export function kankaImportTypeSetting(type: EntityType): KankaSettings {
// This pretends to be a KankaSettings entry and should be usable everywhere regular KankaSettings
// are usable
return `importType_${type}` as KankaSettings;
}

export enum MetaDataType {
basic = 'basic',
attribute = 'attribute',
Expand Down

0 comments on commit 2ac2edb

Please sign in to comment.