Skip to content

Commit

Permalink
Use empty values as default values
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmostellar committed Mar 29, 2024
1 parent 701c5d4 commit 0427919
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 31 deletions.
56 changes: 48 additions & 8 deletions src/obsidian/settings/settingTab.ts
Expand Up @@ -2,6 +2,8 @@ import { debounce, Notice, PluginSettingTab, Setting } from "obsidian";

import { getLocale, LOCALE_CATEGORY } from "@src/lang/lang";

import { FALLBACK_SETTINGS } from "./settingTypes";

import type { App } from "obsidian";
import type FormattoPlugin from "@src/main";

Expand All @@ -27,6 +29,10 @@ export class FormattoSettingTab extends PluginSettingTab {
return value !== "0" && value !== "1" && parseFloat(value) % 1 !== 0;
}

private putDefaultIndicator(value: string): string {
return `${value} (Default)`;
}

display(): void {
const { containerEl } = this;
containerEl.empty();
Expand Down Expand Up @@ -69,7 +75,11 @@ export class FormattoSettingTab extends PluginSettingTab {
)
.addText((text) =>
text
.setPlaceholder("3")
.setPlaceholder(
this.putDefaultIndicator(
FALLBACK_SETTINGS.headingGaps.beforeTopLevelHeadings
)
)
.setValue(
this.plugin.settings.headingGaps.beforeTopLevelHeadings
)
Expand All @@ -96,7 +106,11 @@ export class FormattoSettingTab extends PluginSettingTab {
)
.addText((text) =>
text
.setPlaceholder("1")
.setPlaceholder(
this.putDefaultIndicator(
FALLBACK_SETTINGS.headingGaps.beforeFirstSubHeading
)
)
.setValue(
this.plugin.settings.headingGaps.beforeFirstSubHeading
)
Expand All @@ -120,7 +134,11 @@ export class FormattoSettingTab extends PluginSettingTab {
)
.addText((text) =>
text
.setPlaceholder("2")
.setPlaceholder(
this.putDefaultIndicator(
FALLBACK_SETTINGS.headingGaps.beforeSubHeadings
)
)
.setValue(
this.plugin.settings.headingGaps.beforeSubHeadings
)
Expand All @@ -147,7 +165,11 @@ export class FormattoSettingTab extends PluginSettingTab {
)
.addText((text) =>
text
.setPlaceholder("2")
.setPlaceholder(
this.putDefaultIndicator(
FALLBACK_SETTINGS.otherGaps.afterProperties
)
)
.setValue(this.plugin.settings.otherGaps.afterProperties)
.onChange(async (value) => {
debounceMsg(value);
Expand All @@ -166,7 +188,11 @@ export class FormattoSettingTab extends PluginSettingTab {
)
.addText((text) =>
text
.setPlaceholder("0")
.setPlaceholder(
this.putDefaultIndicator(
FALLBACK_SETTINGS.otherGaps.beforeContents
)
)
.setValue(this.plugin.settings.otherGaps.beforeContents)
.onChange(async (value) => {
debounceMsg(value);
Expand All @@ -190,7 +216,12 @@ export class FormattoSettingTab extends PluginSettingTab {
)
.addText((text) =>
text
.setPlaceholder("1")
.setPlaceholder(
this.putDefaultIndicator(
FALLBACK_SETTINGS.otherGaps
.beforeContentsAfterCodeBlocks
)
)
.setValue(
this.plugin.settings.otherGaps
.beforeContentsAfterCodeBlocks
Expand All @@ -215,7 +246,11 @@ export class FormattoSettingTab extends PluginSettingTab {
)
.addText((text) =>
text
.setPlaceholder("1")
.setPlaceholder(
this.putDefaultIndicator(
FALLBACK_SETTINGS.otherGaps.beforeCodeBlocks
)
)
.setValue(this.plugin.settings.otherGaps.beforeCodeBlocks)
.onChange(async (value) => {
debounceMsg(value);
Expand All @@ -239,7 +274,12 @@ export class FormattoSettingTab extends PluginSettingTab {
)
.addText((text) =>
text
.setPlaceholder("0")
.setPlaceholder(
this.putDefaultIndicator(
FALLBACK_SETTINGS.otherGaps
.beforeCodeBlocksAfterHeadings
)
)
.setValue(
this.plugin.settings.otherGaps
.beforeCodeBlocksAfterHeadings
Expand Down
51 changes: 40 additions & 11 deletions src/obsidian/settings/settingTypes.ts
@@ -1,3 +1,7 @@
/*
Type Declarations
*/

export interface HeadingGaps {
/** Decides gaps before top level of headings. */
beforeTopLevelHeadings: string;
Expand Down Expand Up @@ -39,36 +43,61 @@ export interface FormattoPluginSettings {
otherOptions: Partial<OtherOptions>;
}

// `Partial<Type>` is a TypeScript utility that returns a type with all properties of Type set to optional.
// It enables type checking while letting you only define the properties you want to provide defaults for.
// Source : https://docs.obsidian.md/Plugins/User+interface/Settings#Provide+default+values
/*
Fallback Option Values
*/

export const DEFAULT_HEADING_GAPS: Partial<HeadingGaps> = {
export const FALLBACK_HEADING_GAPS: Partial<HeadingGaps> = {
beforeTopLevelHeadings: "3",
beforeFirstSubHeading: "1",
beforeSubHeadings: "2",
};

export const DEFAULT_OTHER_GAPS: Partial<OtherGaps> = {
export const FALLBACK_OTHER_GAPS: Partial<OtherGaps> = {
afterProperties: "2",
beforeContents: "0",
beforeContentsAfterCodeBlocks: "1",
beforeCodeBlocks: "1",
beforeCodeBlocksAfterHeadings: "0",
};

export const DEFAULT_FORMAT_OPTIONS: Partial<FormatOptions> = {
export const FALLBACK_FORMAT_OPTIONS: Partial<FormatOptions> = {
insertNewline: true,
};

export const DEFAULT_OTHER_OPTIONS: Partial<OtherOptions> = {
export const FALLBACK_OTHER_OPTIONS: Partial<OtherOptions> = {
notifyWhenUnchanged: true,
showMoreDetailedErrorMessages: false,
};

export const FALLBACK_SETTINGS: FormattoPluginSettings = {
headingGaps: FALLBACK_HEADING_GAPS,
otherGaps: FALLBACK_OTHER_GAPS,
formatOptions: FALLBACK_FORMAT_OPTIONS,
otherOptions: FALLBACK_OTHER_OPTIONS,
};

/*
Default Option Values
*/

export const EMPTY_HEADING_GAPS: Partial<HeadingGaps> = {
beforeTopLevelHeadings: "",
beforeFirstSubHeading: "",
beforeSubHeadings: "",
};

export const EMPTY_OTHER_GAPS: Partial<OtherGaps> = {
afterProperties: "",
beforeContents: "",
beforeContentsAfterCodeBlocks: "",
beforeCodeBlocks: "",
beforeCodeBlocksAfterHeadings: "",
};

export const DEFAULT_SETTINGS: FormattoPluginSettings = {
headingGaps: DEFAULT_HEADING_GAPS,
otherGaps: DEFAULT_OTHER_GAPS,
formatOptions: DEFAULT_FORMAT_OPTIONS,
otherOptions: DEFAULT_OTHER_OPTIONS,
headingGaps: EMPTY_HEADING_GAPS,
otherGaps: EMPTY_OTHER_GAPS,
formatOptions: FALLBACK_FORMAT_OPTIONS,
otherOptions: FALLBACK_OTHER_OPTIONS,
};
58 changes: 46 additions & 12 deletions src/obsidian/utils.ts
@@ -1,41 +1,56 @@
import { Editor, Notice } from "obsidian";
import { Editor, EditorPosition, Notice } from "obsidian";

import { getLocale, getWasmLocale, LOCALE_CATEGORY } from "@src/lang/lang";
import FormattoPlugin from "@src/main";

import { format_document } from "../../wasm/pkg/formatto_wasm";
import {
FALLBACK_SETTINGS,
FormattoPluginSettings,
} from "./settings/settingTypes";

export class FormattoUtils {
private plugin: FormattoPlugin;
private cursorPosition: EditorPosition;
private originalDocument: string;
private formattedDocument: string;

constructor(plugin: FormattoPlugin) {
this.plugin = plugin;
}

formatDocument(editor: Editor) {
const cursorPosition = editor.getCursor();
const originalDocument = editor.getValue();
const copiedSettings = JSON.parse(JSON.stringify(this.plugin.settings));
this.handleEmptyOptions(copiedSettings);

this.cursorPosition = editor.getCursor();
this.originalDocument = editor.getValue();

let formattedDocument: string;
try {
formattedDocument = format_document(
originalDocument,
this.plugin.settings,
this.formattedDocument = format_document(
this.originalDocument,
copiedSettings,
JSON.stringify(getWasmLocale())
);
} catch (error) {
new Notice(error);
}
if (!formattedDocument) return;

if (formattedDocument !== originalDocument) {
editor.setValue(formattedDocument);
editor.setSelection(cursorPosition, cursorPosition);
this.displayMessage();

if (!this.formattedDocument) return;
if (this.formattedDocument !== this.originalDocument) {
editor.setValue(this.formattedDocument);
editor.setSelection(this.cursorPosition, this.cursorPosition);
}

this.clearVariables();
}

private displayMessage() {
if (
this.plugin.settings.otherOptions.notifyWhenUnchanged &&
formattedDocument === originalDocument
this.formattedDocument === this.originalDocument
) {
new Notice(
getLocale(
Expand All @@ -52,4 +67,23 @@ export class FormattoUtils {
);
}
}

private handleEmptyOptions(copiedSettings: FormattoPluginSettings) {
for (const optionSection of Object.keys(copiedSettings)) {
for (const optionKey of Object.keys(
copiedSettings[optionSection]
)) {
if (copiedSettings[optionSection][optionKey] === "") {
copiedSettings[optionSection][optionKey] =
FALLBACK_SETTINGS[optionSection][optionKey];
}
}
}
}

private clearVariables() {
this.cursorPosition = undefined;
this.originalDocument = undefined;
this.formattedDocument = undefined;
}
}

0 comments on commit 0427919

Please sign in to comment.