Skip to content

Commit

Permalink
refactor: refactor files & cleanning
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed May 26, 2022
1 parent 3b9fc18 commit c3a250c
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 117 deletions.
3 changes: 1 addition & 2 deletions mkdocsPublisher/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Notice, Plugin, TFile } from "obsidian";
import {
MkdocsSettingsTab,
MkdocsPublicationSettings,
DEFAULT_SETTINGS,
} from "./settings";
import { ShareStatusBar } from "./utils/status_bar";
import MkdocsPublish from "./utils/publication";
import { disablePublish, noticeMessage } from "./utils/utils";
import {MkdocsPublicationSettings, DEFAULT_SETTINGS} from './settings/interface'

export default class MkdocsPublication extends Plugin {
settings: MkdocsPublicationSettings;
Expand Down
130 changes: 18 additions & 112 deletions mkdocsPublisher/settings.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,12 @@
import {App, PluginSettingTab, Setting} from 'obsidian'
import MkdocsPublication from './main'

export interface MkdocsPublicationSettings {
githubRepo: string;
githubName: string;
GhToken: string;
shareKey: string;
ExcludedFolder: string;
fileMenu: boolean;
editorMenu: boolean;
downloadedFolder: string;
folderDefaultName: string;
yamlFolderKey: string;
rootFolder: string;
workflowName: string;
transfertEmbeded: boolean;
defaultImageFolder: string;
autoCleanUp: boolean;
}

export const DEFAULT_SETTINGS: MkdocsPublicationSettings = {
githubRepo: '',
githubName: '',
GhToken: '',
shareKey: 'share',
ExcludedFolder: '',
fileMenu: false,
editorMenu: false,
downloadedFolder: 'fixedFolder',
//fixedFolder
//yamlFrontmatter
//obsidianPath
folderDefaultName: '',
yamlFolderKey: '',
rootFolder: '',
workflowName: '',
transfertEmbeded: true,
defaultImageFolder: '',
autoCleanUp: false,
}

function showSettings(containerEl: Setting) {
containerEl.descEl.show();
containerEl.nameEl.show();
containerEl.controlEl.show();
}

function hideSettings(containerEl: Setting) {
containerEl.descEl.hide();
containerEl.nameEl.hide();
containerEl.controlEl.hide();
}
import {
hideSettings,
showSettings,
autoCleanCondition,
yamlFrontmatterSettings,
autoCleanUpSettingsOnCondition
} from "./settings/stylesSettings";

export class MkdocsSettingsTab extends PluginSettingTab {
plugin: MkdocsPublication;
Expand Down Expand Up @@ -126,23 +82,7 @@ export class MkdocsSettingsTab extends PluginSettingTab {
.setValue(this.plugin.settings.downloadedFolder)
.onChange(async(value: string)=>{
this.plugin.settings.downloadedFolder=value;
if (value == 'yamlFrontmatter') {
showSettings(frontmatterKeySettings);
showSettings(rootFolderSettings);
if (this.plugin.settings.rootFolder.length === 0) {
autoCleanSetting.setDisabled(true);
autoCleanSetting.components[0].toggleEl.classList.remove('is-enabled')
} else {
autoCleanSetting.setDisabled(false);
autoCleanSetting.components[0].toggleEl.classList.add('is-enabled')
}
} else {
if (this.plugin.settings.folderDefaultName.length > 0) {
autoCleanSetting.setDisabled(false);
}
hideSettings(frontmatterKeySettings);
hideSettings(rootFolderSettings);
}
await yamlFrontmatterSettings(frontmatterKeySettings, rootFolderSettings, autoCleanSetting, value, this.plugin)
await this.plugin.saveSettings();
});
});
Expand All @@ -156,16 +96,7 @@ export class MkdocsSettingsTab extends PluginSettingTab {
.setValue(this.plugin.settings.folderDefaultName)
.onChange(async (value) => {
this.plugin.settings.folderDefaultName = value.replace(/\/$/, '');
if (value.length === 0) {
this.plugin.settings.autoCleanUp = false;
autoCleanSetting.setDisabled(true);
autoCleanSetting.components[0].toggleEl.classList.remove('is-enabled')
} else {
autoCleanSetting.setDisabled(false);
if (this.plugin.settings.autoCleanUp) {
autoCleanSetting.components[0].toggleEl.classList.add('is-enabled')
}
}
await autoCleanCondition(value, autoCleanSetting, this.plugin)
await this.plugin.saveSettings();
});
});
Expand Down Expand Up @@ -193,28 +124,11 @@ export class MkdocsSettingsTab extends PluginSettingTab {
.setValue(this.plugin.settings.rootFolder)
.onChange(async(value)=>{
this.plugin.settings.rootFolder =value.replace(/\/$/, '');
if (value.length === 0 && this.plugin.settings.downloadedFolder==='yamlFrontmatter') {
this.plugin.settings.autoCleanUp = false;
autoCleanSetting.setDisabled(true);
autoCleanSetting.components[0].toggleEl.classList.remove('is-enabled')
} else {
autoCleanSetting.setDisabled(false);
if (this.plugin.settings.autoCleanUp) {
autoCleanSetting.components[0].toggleEl.classList.add('is-enabled')
}
}
await autoCleanCondition(value, autoCleanSetting, this.plugin);
await this.plugin.saveSettings();
});
});

if (this.plugin.settings.downloadedFolder == 'yamlFrontmatter') {
showSettings(frontmatterKeySettings);
showSettings(rootFolderSettings);
} else {
hideSettings(frontmatterKeySettings);
hideSettings(rootFolderSettings);
}

containerEl.createEl('h5', {text: 'Workflow'})
new Setting(containerEl)
.setName('Github action name')
Expand All @@ -234,7 +148,8 @@ export class MkdocsSettingsTab extends PluginSettingTab {
const condition = (this.plugin.settings.downloadedFolder === "yamlFrontmatter" &&
(this.plugin.settings.rootFolder.length === 0) ||
(this.plugin.settings.folderDefaultName.length === 0));
const autoCleanSetting= new Setting(containerEl)

const autoCleanSetting = new Setting(containerEl)
.setName('Auto clean up')
.setDesc('If the plugin must remove from github the removed' +
' files (stop share or deleted)')
Expand All @@ -247,27 +162,16 @@ export class MkdocsSettingsTab extends PluginSettingTab {
await this.plugin.saveSettings();
});
});
if (condition) {
autoCleanSetting.setDisabled(true);
autoCleanSetting.components[0].toggleEl.classList.remove('is-enabled')
this.plugin.settings.autoCleanUp = false;
this.plugin.saveSettings().then();
} else {
autoCleanSetting.setDisabled(false);
if (this.plugin.settings.autoCleanUp) {
autoCleanSetting.components[0].toggleEl.classList.add('is-enabled')
}
}

containerEl.createEl('h5', {text: 'Embedded files'})
new Setting(containerEl)
.setName('Transfer image')
.setDesc('Send image linked to a file in github')
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.transfertEmbeded)
.setValue(this.plugin.settings.transferEmbedded)
.onChange(async (value) => {
this.plugin.settings.transfertEmbeded = value;
this.plugin.settings.transferEmbedded = value;
value ? showSettings(settingsDefaultImage) : hideSettings(settingsDefaultImage);
await this.plugin.saveSettings();
});
Expand All @@ -286,8 +190,6 @@ export class MkdocsSettingsTab extends PluginSettingTab {
});
});

this.plugin.settings.transfertEmbeded ? showSettings(settingsDefaultImage) : hideSettings(settingsDefaultImage);

containerEl.createEl('h1', { text: 'Plugin Settings' })
new Setting(containerEl)
.setName('Share Key')
Expand Down Expand Up @@ -335,5 +237,9 @@ export class MkdocsSettingsTab extends PluginSettingTab {
await this.plugin.saveSettings()
})
)

autoCleanUpSettingsOnCondition(condition, autoCleanSetting, this.plugin);
yamlFrontmatterSettings(frontmatterKeySettings, rootFolderSettings, autoCleanSetting, this.plugin.settings.downloadedFolder, this.plugin).then();
this.plugin.settings.transferEmbedded ? showSettings(settingsDefaultImage) : hideSettings(settingsDefaultImage);
}
}
38 changes: 38 additions & 0 deletions mkdocsPublisher/settings/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export interface MkdocsPublicationSettings {
githubRepo: string;
githubName: string;
GhToken: string;
shareKey: string;
ExcludedFolder: string;
fileMenu: boolean;
editorMenu: boolean;
downloadedFolder: string;
folderDefaultName: string;
yamlFolderKey: string;
rootFolder: string;
workflowName: string;
transferEmbedded: boolean;
defaultImageFolder: string;
autoCleanUp: boolean;
}

export const DEFAULT_SETTINGS: MkdocsPublicationSettings = {
githubRepo: '',
githubName: '',
GhToken: '',
shareKey: 'share',
ExcludedFolder: '',
fileMenu: false,
editorMenu: false,
downloadedFolder: 'fixedFolder',
//fixedFolder
//yamlFrontmatter
//obsidianPath
folderDefaultName: '',
yamlFolderKey: '',
rootFolder: '',
workflowName: '',
transferEmbedded: true,
defaultImageFolder: '',
autoCleanUp: false,
}
73 changes: 73 additions & 0 deletions mkdocsPublisher/settings/stylesSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Setting } from "obsidian";
import MkdocsPublication from "../main";

export function showSettings(containerEl: Setting) {
containerEl.descEl.show();
containerEl.nameEl.show();
containerEl.controlEl.show();
}

export function hideSettings(containerEl: Setting) {
containerEl.descEl.hide();
containerEl.nameEl.hide();
containerEl.controlEl.hide();
}

export async function autoCleanCondition(value: string, autoCleanSetting: Setting, plugin: MkdocsPublication) {
const settings = plugin.settings;
if (value.length === 0 && settings.downloadedFolder === 'yamlFrontmatter') {
settings.autoCleanUp = false;
await plugin.saveSettings();
autoCleanSetting.setDisabled(true);
// @ts-ignore
autoCleanSetting.components[0].toggleEl.classList.remove('is-enabled')
} else if (value.length === 0 && settings.downloadedFolder !== "yamlFrontmatter") {
settings.autoCleanUp = false;
autoCleanSetting.setDisabled(true);
// @ts-ignore
autoCleanSetting.components[0].toggleEl.classList.remove('is-enabled')
} else {
autoCleanSetting.setDisabled(false);
if (settings.autoCleanUp) {
// @ts-ignore
autoCleanSetting.components[0].toggleEl.classList.add('is-enabled')
}
}
}

export async function yamlFrontmatterSettings(frontmatterKeySettings: Setting, rootFolderSettings: Setting, autoCleanSetting: Setting, value: string, plugin: MkdocsPublication) {
const settings = plugin.settings;
console.log(settings.rootFolder)
if (value == 'yamlFrontmatter') {
showSettings(frontmatterKeySettings);
showSettings(rootFolderSettings);
autoCleanCondition(settings.rootFolder, autoCleanSetting, plugin).then();
} else {
if (settings.folderDefaultName.length > 0) {
autoCleanSetting.setDisabled(false);
if (settings.autoCleanUp) {
// @ts-ignore
autoCleanSetting.components[0].toggleEl.classList.add('is-enabled')
}
}
hideSettings(frontmatterKeySettings);
hideSettings(rootFolderSettings);
}
}

export function autoCleanUpSettingsOnCondition(condition: boolean, autoCleanSetting: Setting,plugin: MkdocsPublication) {
const settings = plugin.settings;
if (condition) {
autoCleanSetting.setDisabled(true);
// @ts-ignore
autoCleanSetting.components[0].toggleEl.classList.remove('is-enabled')
settings.autoCleanUp = false;
plugin.saveSettings().then();
} else {
autoCleanSetting.setDisabled(false);
if (settings.autoCleanUp) {
// @ts-ignore
autoCleanSetting.components[0].toggleEl.classList.add('is-enabled')
}
}
}
4 changes: 2 additions & 2 deletions mkdocsPublisher/utils/publication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Vault,
arrayBufferToBase64,
} from "obsidian";
import { MkdocsPublicationSettings } from "../settings";
import { MkdocsPublicationSettings } from "../settings/interface";
import { Octokit } from "@octokit/core";
import { Base64 } from "js-base64";

Expand Down Expand Up @@ -161,7 +161,7 @@ export default class MkdocsPublish {
path = folderDefault + file.path;
}
await this.uploadText(file.path, text, path, file.name);
if (linkedImage.length > 0 && this.settings.transfertEmbeded) {
if (linkedImage.length > 0 && this.settings.transferEmbedded) {
for (const image of linkedImage) {
await this.uploadImage(image);
}
Expand Down
2 changes: 1 addition & 1 deletion mkdocsPublisher/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
TFile,
Notice
} from 'obsidian'
import { MkdocsPublicationSettings } from '../settings'
import { MkdocsPublicationSettings } from '../settings/interface'
import MkdocsPublish from "./publication";


Expand Down

0 comments on commit c3a250c

Please sign in to comment.