Skip to content

Commit

Permalink
Merge pull request #5 from oeN/fix/use-new-suggest-api
Browse files Browse the repository at this point in the history
Support the Live Preview editor
  • Loading branch information
oeN authored Feb 16, 2022
2 parents b81d021 + 0d90772 commit 6d35adb
Show file tree
Hide file tree
Showing 7 changed files with 488 additions and 437 deletions.
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"id": "liquid-templates",
"name": "Liquid Templates",
"version": "0.2.0",
"minAppVersion": "0.12.3",
"version": "0.3.0",
"minAppVersion": "0.13.10",
"description": "Empower your template with LiquidJS tags",
"author": "diomedet",
"authorUrl": "https://diomedet.com/",
Expand Down
82 changes: 41 additions & 41 deletions src/engine/filters/date.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
import { addDays, format, subDays } from 'date-fns';

import { BaseFilter, BaseFilterProps } from './base_filter';

export default class DateFiler extends BaseFilter {
originalFilter: Function;

constructor(props: BaseFilterProps) {
super({ ...props, filterName: 'date' });
this.originalFilter = this.engine.filters.get('date');
}

handler = (givenValue: number | string, dateFormat?: string): string | number => {
const dateToFormat = (typeof givenValue === 'number')
? givenValue
: this.parseDate(givenValue)
const formatToUse = dateFormat || this.plugin.settings.dateFormat;

// TODO: improve or remove me: keep backwards compatibility with the current 0.1.5 version
if (formatToUse.includes('%')) {
// TODO: send a notification when using this function
return this.originalFilter(givenValue, dateFormat);
}

try {
return format(dateToFormat, formatToUse)
} catch (e) {
if (!(e instanceof RangeError)) throw e;
}

return givenValue;
}

parseDate = (stringToParse: string): Date => {
if(['now', 'today'].includes(stringToParse)) return new Date;
if(stringToParse === 'yesterday') return subDays(Date.now(), 1);
if(stringToParse === 'tomorrow') return addDays(Date.now(), 1);
// try to parse the given string
return new Date(Date.parse(stringToParse));
}
}
import { addDays, format, subDays } from 'date-fns';

import { BaseFilter, BaseFilterProps } from './base_filter';

export default class DateFiler extends BaseFilter {
originalFilter: Function;

constructor(props: BaseFilterProps) {
super({ ...props, filterName: 'date' });
this.originalFilter = this.engine.filters.get('date');
}

handler = (givenValue: number | string, dateFormat?: string): string | number => {
const dateToFormat = (typeof givenValue === 'number')
? givenValue
: this.parseDate(givenValue)
const formatToUse = dateFormat || this.plugin.settings.dateFormat;

// TODO: improve or remove me: keep backwards compatibility with the current 0.1.5 version
if (formatToUse.includes('%')) {
// TODO: send a notification when using this function
return this.originalFilter(givenValue, dateFormat);
}

try {
return format(dateToFormat, formatToUse)
} catch (e) {
if (!(e instanceof RangeError)) throw e;
}

return givenValue;
}

parseDate = (stringToParse: string): Date => {
if(['now', 'today'].includes(stringToParse)) return new Date;
if(stringToParse === 'yesterday') return subDays(Date.now(), 1);
if(stringToParse === 'tomorrow') return addDays(Date.now(), 1);
// try to parse the given string
return new Date(Date.parse(stringToParse));
}
}
22 changes: 11 additions & 11 deletions src/engine/filters/days_after.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { addDays } from 'date-fns';

import { BaseFilter, BaseFilterProps } from './base_filter';

export default class DaysAfter extends BaseFilter {
constructor(props: BaseFilterProps) {
super({ ...props, filterName: 'days_after' });
}

handler = (daysToSub: number): Date => addDays(Date.now(), daysToSub);
}
import { addDays } from 'date-fns';

import { BaseFilter, BaseFilterProps } from './base_filter';

export default class DaysAfter extends BaseFilter {
constructor(props: BaseFilterProps) {
super({ ...props, filterName: 'days_after' });
}

handler = (daysToAdd: number): Date => addDays(Date.now(), daysToAdd);
}
25 changes: 3 additions & 22 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,29 @@ import { LiquidTemplatesSettings, DEFAULT_SETTINGS, LiquidTemplatesSettingsTab }
import TemplateSuggest from "./suggest/template-suggest";

export default class LiquidTemplates extends Plugin {
private autosuggest: TemplateSuggest;
public settings: LiquidTemplatesSettings;

async onload(): Promise<void> {
console.log('loading liquid templates plugin');
await this.loadSettings();

this.setupAutosuggest();
this.addSettingTab(new LiquidTemplatesSettingsTab(this.app, this));

this.addSettingTab(new LiquidTemplatesSettingsTab(this.app, this))
this.registerEditorSuggest(new TemplateSuggest(this.app, this));
}

onunload(): void {
console.log('unloading liquid templates plugin');
// remove the autosuggest handler when unloading the plugin
this.app.workspace.iterateCodeMirrors((cm: CodeMirror.Editor) => {
cm.off("change", this.autosuggestHandler);
})
}

setupAutosuggest(): void {
this.autosuggest = new TemplateSuggest(this.app, this);

this.registerCodeMirror((cm: CodeMirror.Editor) => {
cm.on("change", this.autosuggestHandler);
});
}

autosuggestHandler = (
cmEditor: CodeMirror.Editor,
changeObj: CodeMirror.EditorChange
): boolean => {
return this.autosuggest?.update(cmEditor, changeObj);
};

async loadSettings(): Promise<void> {
this.settings = this.settingsWithDefault(await this.loadData());
}

async saveSettings(): Promise<void> {
// the templates folder can change
this.setupAutosuggest();
// this.setupAutosuggest();
await this.saveData(this.settings);
}

Expand Down
Loading

0 comments on commit 6d35adb

Please sign in to comment.