merge #100
This commit is contained in:
commit
e416434bf3
8 changed files with 559 additions and 457 deletions
|
|
@ -8,6 +8,7 @@ import { MediaType } from '../../utils/MediaType';
|
||||||
export class MALAPI extends APIModel {
|
export class MALAPI extends APIModel {
|
||||||
plugin: MediaDbPlugin;
|
plugin: MediaDbPlugin;
|
||||||
typeMappings: Map<string, string>;
|
typeMappings: Map<string, string>;
|
||||||
|
apiDateFormat: string = 'YYYY-MM-DDTHH:mm:ssZ'; // ISO
|
||||||
|
|
||||||
constructor(plugin: MediaDbPlugin) {
|
constructor(plugin: MediaDbPlugin) {
|
||||||
super();
|
super();
|
||||||
|
|
@ -115,7 +116,7 @@ export class MALAPI extends APIModel {
|
||||||
image: result.images?.jpg?.image_url ?? '',
|
image: result.images?.jpg?.image_url ?? '',
|
||||||
|
|
||||||
released: true,
|
released: true,
|
||||||
premiere: new Date(result.aired?.from).toLocaleDateString() ?? 'unknown',
|
premiere: this.plugin.dateFormatter.format(result.aired?.from, this.apiDateFormat) ?? 'unknown',
|
||||||
streamingServices: result.streaming?.map((x: any) => x.name) ?? [],
|
streamingServices: result.streaming?.map((x: any) => x.name) ?? [],
|
||||||
|
|
||||||
userData: {
|
userData: {
|
||||||
|
|
@ -146,7 +147,7 @@ export class MALAPI extends APIModel {
|
||||||
image: result.images?.jpg?.image_url ?? '',
|
image: result.images?.jpg?.image_url ?? '',
|
||||||
|
|
||||||
released: true,
|
released: true,
|
||||||
premiere: new Date(result.aired?.from).toLocaleDateString() ?? 'unknown',
|
premiere: this.plugin.dateFormatter.format(result.aired?.from, this.apiDateFormat) ?? 'unknown',
|
||||||
streamingServices: result.streaming?.map((x: any) => x.name) ?? [],
|
streamingServices: result.streaming?.map((x: any) => x.name) ?? [],
|
||||||
|
|
||||||
userData: {
|
userData: {
|
||||||
|
|
@ -176,8 +177,8 @@ export class MALAPI extends APIModel {
|
||||||
image: result.images?.jpg?.image_url ?? '',
|
image: result.images?.jpg?.image_url ?? '',
|
||||||
|
|
||||||
released: true,
|
released: true,
|
||||||
airedFrom: new Date(result.aired?.from).toLocaleDateString() ?? 'unknown',
|
airedFrom: this.plugin.dateFormatter.format(result.aired?.from, this.apiDateFormat) ?? 'unknown',
|
||||||
airedTo: new Date(result.aired?.to).toLocaleDateString() ?? 'unknown',
|
airedTo: this.plugin.dateFormatter.format(result.aired?.to, this.apiDateFormat) ?? 'unknown',
|
||||||
airing: result.airing,
|
airing: result.airing,
|
||||||
|
|
||||||
userData: {
|
userData: {
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import { MediaType } from '../../utils/MediaType';
|
||||||
export class OMDbAPI extends APIModel {
|
export class OMDbAPI extends APIModel {
|
||||||
plugin: MediaDbPlugin;
|
plugin: MediaDbPlugin;
|
||||||
typeMappings: Map<string, string>;
|
typeMappings: Map<string, string>;
|
||||||
|
apiDateFormat: string = 'DD MMM YYYY';
|
||||||
|
|
||||||
constructor(plugin: MediaDbPlugin) {
|
constructor(plugin: MediaDbPlugin) {
|
||||||
super();
|
super();
|
||||||
|
|
@ -142,7 +143,7 @@ export class OMDbAPI extends APIModel {
|
||||||
|
|
||||||
released: true,
|
released: true,
|
||||||
streamingServices: [],
|
streamingServices: [],
|
||||||
premiere: new Date(result.Released).toLocaleDateString() ?? 'unknown',
|
premiere: this.plugin.dateFormatter.format(result.Released, this.apiDateFormat) ?? 'unknown',
|
||||||
|
|
||||||
userData: {
|
userData: {
|
||||||
watched: false,
|
watched: false,
|
||||||
|
|
@ -173,7 +174,7 @@ export class OMDbAPI extends APIModel {
|
||||||
released: true,
|
released: true,
|
||||||
streamingServices: [],
|
streamingServices: [],
|
||||||
airing: false,
|
airing: false,
|
||||||
airedFrom: new Date(result.Released).toLocaleDateString() ?? 'unknown',
|
airedFrom: this.plugin.dateFormatter.format(result.Released, this.apiDateFormat) ?? 'unknown',
|
||||||
airedTo: 'unknown',
|
airedTo: 'unknown',
|
||||||
|
|
||||||
userData: {
|
userData: {
|
||||||
|
|
@ -199,7 +200,7 @@ export class OMDbAPI extends APIModel {
|
||||||
image: result.Poster ?? '',
|
image: result.Poster ?? '',
|
||||||
|
|
||||||
released: true,
|
released: true,
|
||||||
releaseDate: new Date(result.Released).toLocaleDateString() ?? 'unknown',
|
releaseDate: this.plugin.dateFormatter.format(result.Released, this.apiDateFormat) ?? 'unknown',
|
||||||
|
|
||||||
userData: {
|
userData: {
|
||||||
played: false,
|
played: false,
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import { MediaType } from '../../utils/MediaType';
|
||||||
export class SteamAPI extends APIModel {
|
export class SteamAPI extends APIModel {
|
||||||
plugin: MediaDbPlugin;
|
plugin: MediaDbPlugin;
|
||||||
typeMappings: Map<string, string>;
|
typeMappings: Map<string, string>;
|
||||||
|
apiDateFormat: string = 'DD MMM, YYYY';
|
||||||
|
|
||||||
constructor(plugin: MediaDbPlugin) {
|
constructor(plugin: MediaDbPlugin) {
|
||||||
super();
|
super();
|
||||||
|
|
@ -109,7 +110,7 @@ export class SteamAPI extends APIModel {
|
||||||
image: result.header_image ?? '',
|
image: result.header_image ?? '',
|
||||||
|
|
||||||
released: !result.release_date?.comming_soon,
|
released: !result.release_date?.comming_soon,
|
||||||
releaseDate: new Date(result.release_date?.date).toLocaleDateString() ?? 'unknown',
|
releaseDate: this.plugin.dateFormatter.format(result.release_date?.date, this.apiDateFormat) ?? 'unknown',
|
||||||
|
|
||||||
userData: {
|
userData: {
|
||||||
played: false,
|
played: false,
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import { MediaType } from '../../utils/MediaType';
|
||||||
|
|
||||||
export class WikipediaAPI extends APIModel {
|
export class WikipediaAPI extends APIModel {
|
||||||
plugin: MediaDbPlugin;
|
plugin: MediaDbPlugin;
|
||||||
|
apiDateFormat: string = 'YYYY-MM-DDTHH:mm:ssZ'; // ISO
|
||||||
|
|
||||||
constructor(plugin: MediaDbPlugin) {
|
constructor(plugin: MediaDbPlugin) {
|
||||||
super();
|
super();
|
||||||
|
|
@ -72,7 +73,7 @@ export class WikipediaAPI extends APIModel {
|
||||||
id: result.pageid,
|
id: result.pageid,
|
||||||
|
|
||||||
wikiUrl: result.fullurl,
|
wikiUrl: result.fullurl,
|
||||||
lastUpdated: new Date(result.touched).toLocaleDateString() ?? 'unknown',
|
lastUpdated: this.plugin.dateFormatter.format(result.touched, this.apiDateFormat),
|
||||||
length: result.length,
|
length: result.length,
|
||||||
|
|
||||||
userData: {},
|
userData: {},
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ import { YAMLConverter } from './utils/YAMLConverter';
|
||||||
import { MediaDbFolderImportModal } from './modals/MediaDbFolderImportModal';
|
import { MediaDbFolderImportModal } from './modals/MediaDbFolderImportModal';
|
||||||
import { PropertyMapping, PropertyMappingModel } from './settings/PropertyMapping';
|
import { PropertyMapping, PropertyMappingModel } from './settings/PropertyMapping';
|
||||||
import { ModalHelper, ModalResultCode, SearchModalOptions } from './utils/ModalHelper';
|
import { ModalHelper, ModalResultCode, SearchModalOptions } from './utils/ModalHelper';
|
||||||
|
import { DateFormatter } from './utils/DateFormatter';
|
||||||
|
|
||||||
export default class MediaDbPlugin extends Plugin {
|
export default class MediaDbPlugin extends Plugin {
|
||||||
settings: MediaDbPluginSettings;
|
settings: MediaDbPluginSettings;
|
||||||
|
|
@ -22,6 +23,7 @@ export default class MediaDbPlugin extends Plugin {
|
||||||
mediaTypeManager: MediaTypeManager;
|
mediaTypeManager: MediaTypeManager;
|
||||||
modelPropertyMapper: PropertyMapper;
|
modelPropertyMapper: PropertyMapper;
|
||||||
modalHelper: ModalHelper;
|
modalHelper: ModalHelper;
|
||||||
|
dateFormatter: DateFormatter;
|
||||||
|
|
||||||
frontMatterRexExpPattern: string = '^(---)\\n[\\s\\S]*?\\n---';
|
frontMatterRexExpPattern: string = '^(---)\\n[\\s\\S]*?\\n---';
|
||||||
|
|
||||||
|
|
@ -39,6 +41,7 @@ export default class MediaDbPlugin extends Plugin {
|
||||||
this.mediaTypeManager = new MediaTypeManager();
|
this.mediaTypeManager = new MediaTypeManager();
|
||||||
this.modelPropertyMapper = new PropertyMapper(this);
|
this.modelPropertyMapper = new PropertyMapper(this);
|
||||||
this.modalHelper = new ModalHelper(this);
|
this.modalHelper = new ModalHelper(this);
|
||||||
|
this.dateFormatter = new DateFormatter();
|
||||||
|
|
||||||
await this.loadSettings();
|
await this.loadSettings();
|
||||||
// register the settings tab
|
// register the settings tab
|
||||||
|
|
@ -46,6 +49,7 @@ export default class MediaDbPlugin extends Plugin {
|
||||||
|
|
||||||
this.mediaTypeManager.updateTemplates(this.settings);
|
this.mediaTypeManager.updateTemplates(this.settings);
|
||||||
this.mediaTypeManager.updateFolders(this.settings);
|
this.mediaTypeManager.updateFolders(this.settings);
|
||||||
|
this.dateFormatter.setFormat(this.settings.customDateFormat);
|
||||||
|
|
||||||
// add icon to the left ribbon
|
// add icon to the left ribbon
|
||||||
const ribbonIconEl = this.addRibbonIcon('database', 'Add new Media DB entry', () => this.createEntryWithAdvancedSearchModal());
|
const ribbonIconEl = this.addRibbonIcon('database', 'Add new Media DB entry', () => this.createEntryWithAdvancedSearchModal());
|
||||||
|
|
@ -563,6 +567,7 @@ export default class MediaDbPlugin extends Plugin {
|
||||||
async saveSettings(): Promise<void> {
|
async saveSettings(): Promise<void> {
|
||||||
this.mediaTypeManager.updateTemplates(this.settings);
|
this.mediaTypeManager.updateTemplates(this.settings);
|
||||||
this.mediaTypeManager.updateFolders(this.settings);
|
this.mediaTypeManager.updateFolders(this.settings);
|
||||||
|
this.dateFormatter.setFormat(this.settings.customDateFormat);
|
||||||
|
|
||||||
await this.saveData(this.settings);
|
await this.saveData(this.settings);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,12 +7,14 @@ import PropertyMappingModelsComponent from './PropertyMappingModelsComponent.sve
|
||||||
import { PropertyMapping, PropertyMappingModel, PropertyMappingOption } from './PropertyMapping';
|
import { PropertyMapping, PropertyMappingModel, PropertyMappingOption } from './PropertyMapping';
|
||||||
import { MEDIA_TYPES } from '../utils/MediaTypeManager';
|
import { MEDIA_TYPES } from '../utils/MediaTypeManager';
|
||||||
import { MediaTypeModel } from '../models/MediaTypeModel';
|
import { MediaTypeModel } from '../models/MediaTypeModel';
|
||||||
|
import { fragWithHTML } from '../utils/Utils';
|
||||||
|
|
||||||
export interface MediaDbPluginSettings {
|
export interface MediaDbPluginSettings {
|
||||||
OMDbKey: string;
|
OMDbKey: string;
|
||||||
sfwFilter: boolean;
|
sfwFilter: boolean;
|
||||||
useCustomYamlStringifier: boolean;
|
useCustomYamlStringifier: boolean;
|
||||||
templates: boolean;
|
templates: boolean;
|
||||||
|
customDateFormat: string;
|
||||||
|
|
||||||
movieTemplate: string;
|
movieTemplate: string;
|
||||||
seriesTemplate: string;
|
seriesTemplate: string;
|
||||||
|
|
@ -50,6 +52,7 @@ const DEFAULT_SETTINGS: MediaDbPluginSettings = {
|
||||||
sfwFilter: true,
|
sfwFilter: true,
|
||||||
useCustomYamlStringifier: true,
|
useCustomYamlStringifier: true,
|
||||||
templates: true,
|
templates: true,
|
||||||
|
customDateFormat: 'L',
|
||||||
|
|
||||||
movieTemplate: '',
|
movieTemplate: '',
|
||||||
seriesTemplate: '',
|
seriesTemplate: '',
|
||||||
|
|
@ -165,6 +168,23 @@ export class MediaDbSettingTab extends PluginSettingTab {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
new Setting(containerEl)
|
||||||
|
.setName('Date format')
|
||||||
|
.setDesc(fragWithHTML("Your custom date format. Use <em>\'YYYY-MM-DD\'</em> for example.<br>" +
|
||||||
|
"For more syntax, refer to <a href='https://momentjs.com/docs/#/displaying/format/'>format reference</a>.<br>" +
|
||||||
|
"Your current syntax looks like this: <b><a id='media-db-dateformat-preview' style='pointer-events: none; cursor: default; text-decoration: none;'>" +
|
||||||
|
this.plugin.dateFormatter.getPreview() + "</a></b>"))
|
||||||
|
.addText(cb => {
|
||||||
|
cb.setPlaceholder(DEFAULT_SETTINGS.customDateFormat)
|
||||||
|
.setValue(this.plugin.settings.customDateFormat === DEFAULT_SETTINGS.customDateFormat ? '' : this.plugin.settings.customDateFormat)
|
||||||
|
.onChange(data => {
|
||||||
|
const newDateFormat = data ? data : DEFAULT_SETTINGS.customDateFormat;
|
||||||
|
this.plugin.settings.customDateFormat = newDateFormat;
|
||||||
|
document.getElementById('media-db-dateformat-preview').textContent = this.plugin.dateFormatter.getPreview(newDateFormat); // update preview
|
||||||
|
this.plugin.saveSettings();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
containerEl.createEl('h3', { text: 'New File Location' });
|
containerEl.createEl('h3', { text: 'New File Location' });
|
||||||
// region new file location
|
// region new file location
|
||||||
new Setting(containerEl)
|
new Setting(containerEl)
|
||||||
|
|
|
||||||
71
src/utils/DateFormatter.ts
Normal file
71
src/utils/DateFormatter.ts
Normal file
|
|
@ -0,0 +1,71 @@
|
||||||
|
// obsidian already uses moment, so no need to package it twice!
|
||||||
|
// import { moment } from 'obsidian'; // doesn't work for release build
|
||||||
|
// obsidian uses a namespace-style import for moment, which ES6 doesn't allow anymore
|
||||||
|
const obsidian = require('obsidian');
|
||||||
|
const moment = obsidian.moment;
|
||||||
|
|
||||||
|
export class DateFormatter {
|
||||||
|
toFormat: string;
|
||||||
|
locale: string;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.toFormat = 'YYYY-MM-DD';
|
||||||
|
// get locale of this machine (e.g. en, en-gb, de, fr, etc.)
|
||||||
|
this.locale = new Intl.DateTimeFormat().resolvedOptions().locale;
|
||||||
|
}
|
||||||
|
|
||||||
|
setFormat(format: string): void {
|
||||||
|
this.toFormat = format;
|
||||||
|
}
|
||||||
|
|
||||||
|
getPreview(format?: string): string {
|
||||||
|
const today = moment();
|
||||||
|
|
||||||
|
if (!format) {
|
||||||
|
format = this.toFormat;
|
||||||
|
}
|
||||||
|
|
||||||
|
return today.locale(this.locale).format(format);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tries to format a given date string with the currently set date format.
|
||||||
|
* You can set a date format by calling `setFormat()`.
|
||||||
|
*
|
||||||
|
* @param dateString the date string to be formatted
|
||||||
|
* @param dateFormat the current format of `dateString`. When this is `null` and the actual format of the
|
||||||
|
* given date string is not `C2822` or `ISO` format, this function will try to guess the format by using the native `Date` module.
|
||||||
|
* @param locale the locale of `dateString`. This is needed when `dateString` includes a month or day name and its locale format differs
|
||||||
|
* from the locale of this machine.
|
||||||
|
* @returns formatted date string or null if `dateString` is not a valid date
|
||||||
|
*/
|
||||||
|
format(dateString: string, dateFormat?: string, locale: string = 'en'): string | null {
|
||||||
|
if (!dateString) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
let date: moment.Moment;
|
||||||
|
|
||||||
|
if (!dateFormat) {
|
||||||
|
// reading date formats other then C2822 or ISO with moment is deprecated
|
||||||
|
// see https://momentjs.com/docs/#/parsing/string/
|
||||||
|
if (this.hasMomentFormat(dateString)) {
|
||||||
|
// expect C2822 or ISO format
|
||||||
|
date = moment(dateString);
|
||||||
|
} else {
|
||||||
|
// try to read date string with native Date
|
||||||
|
date = moment(new Date(dateString));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
date = moment(dateString, dateFormat, locale);
|
||||||
|
}
|
||||||
|
|
||||||
|
// format date (if it is valid)
|
||||||
|
return date.isValid() ? date.locale(this.locale).format(this.toFormat) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private hasMomentFormat(dateString: string): boolean {
|
||||||
|
const date = moment(dateString, true); // strict mode
|
||||||
|
return date.isValid();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -135,6 +135,8 @@ export function markdownTable(content: string[][]): string {
|
||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const fragWithHTML = (html: string) => createFragment(frag => (frag.createDiv().innerHTML = html));
|
||||||
|
|
||||||
export function dateToString(date: Date): string {
|
export function dateToString(date: Date): string {
|
||||||
return `${date.getMonth() + 1}-${date.getDate()}-${date.getFullYear()}`;
|
return `${date.getMonth() + 1}-${date.getDate()}-${date.getFullYear()}`;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue