Skip to content

Commit

Permalink
fix(menu): better submenu manager with shareAll options
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Aug 26, 2023
1 parent a6f0343 commit fbc6015
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
18 changes: 13 additions & 5 deletions src/commands/file_menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function addSubMenuCommandsFolder(plugin: GithubPublisher, item: MenuItem
.setTitle(i18next.t("commands.shareViewFiles.multiple.other"))
.setIcon("folder-symlink")
.onClick(async () => {
new ChooseRepoToRun(plugin.app, plugin, null, branchName, "folder", async (item: Repository) => {
new ChooseRepoToRun(plugin.app, plugin, null, branchName, "folder", null, async (item: Repository) => {
await shareFolderRepo(plugin, folder, branchName, item);
}).open();

Expand All @@ -94,7 +94,7 @@ export function addSubMenuCommandsFolder(plugin: GithubPublisher, item: MenuItem
*/
export function addMenuFile(plugin: GithubPublisher, file: TFile, branchName: string, menu: Menu) {
const frontmatter = file instanceof TFile ? plugin.app.metadataCache.getFileCache(file)!.frontmatter : undefined;
const getSharedKey = getRepoSharedKey(plugin.settings, frontmatter);
let getSharedKey = getRepoSharedKey(plugin.settings, frontmatter);
const allKeysFromFile = multipleSharedKey(frontmatter, plugin.settings);
if (
isShared(frontmatter, plugin.settings, file, getSharedKey) &&
Expand All @@ -105,6 +105,7 @@ export function addMenuFile(plugin: GithubPublisher, file: TFile, branchName: st
/**
* Create a submenu if multiple repo exists in the settings
*/

if (allKeysFromFile.length > 1 || (repoFrontmatter instanceof Array && repoFrontmatter.length > 1)) {
item
.setTitle("Github Publisher")
Expand All @@ -119,11 +120,18 @@ export function addMenuFile(plugin: GithubPublisher, file: TFile, branchName: st
return;
}
const fileName = plugin.getTitleFieldForCommand(file, plugin.app.metadataCache.getFileCache(file)?.frontmatter).replace(".md", "");
const repoName = repoFrontmatter instanceof Array ? repoFrontmatter : [repoFrontmatter];

if (!frontmatter || !frontmatter[plugin.settings.plugin.shareKey]) {
const otherRepo = plugin.settings.github.otherRepo.find((repo) => repo.shareAll?.enable);
if (otherRepo) getSharedKey = otherRepo;
else if (plugin.settings.plugin.shareAll?.enable) getSharedKey = defaultRepo(plugin.settings);
} else if (frontmatter[plugin.settings.plugin.shareKey]) {
getSharedKey = defaultRepo(plugin.settings);
}
item
.setTitle(i18next.t("commands.shareViewFiles.multiple.on", {
doc: fileName,
smartKey: repoName[0].repo || getSharedKey?.smartKey.toUpperCase() || i18next.t("common.default").toUpperCase()
smartKey: getSharedKey?.smartKey.toUpperCase() || i18next.t("common.default").toUpperCase()
}))
.setIcon("file-up")
.onClick(async () => {
Expand Down Expand Up @@ -230,7 +238,7 @@ export function subMenuCommandsFile(plugin: GithubPublisher, item: MenuItem, fil
.setTitle(i18next.t("commands.shareViewFiles.multiple.other"))
.setIcon("file-input")
.onClick(async () => {
new ChooseRepoToRun(plugin.app, plugin, repo?.shareKey, branchName, "file", async (item: Repository) => {
new ChooseRepoToRun(plugin.app, plugin, repo?.shareKey, branchName, "file", file.basename, async (item: Repository) => {
await shareOneNote(
branchName,
await plugin.reloadOctokit(),
Expand Down
8 changes: 5 additions & 3 deletions src/commands/suggest_other_repo_commands_modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,24 @@ export class ChooseRepoToRun extends FuzzySuggestModal<Repository> {
branchName: string;
keyToFind: string | null;
type: "folder" | "file";
fileName: string | null;
onSubmit: (item: Repository) => void;

constructor(app: App, plugin: GithubPublisherPlugin, keyToFind: null|string = null, branchName: string, type:"folder"|"file", onSubmit: (item: Repository) => void) {
constructor(app: App, plugin: GithubPublisherPlugin, keyToFind: null|string = null, branchName: string, type:"folder"|"file", fileName: string | null, onSubmit: (item: Repository) => void) {
super(app);
this.plugin = plugin;
this.branchName = branchName;
this.keyToFind = keyToFind;
this.onSubmit = onSubmit;
this.fileName = fileName;
this.type = type;
}

getItems(): Repository[] {
let repoFound: Repository[] = [];
const defRepo = defaultRepo(this.plugin.settings);
if (this.type === "file") {
if (this.plugin.settings.plugin.shareAll?.enable) {
if (this.plugin.settings.plugin.shareAll?.enable && !this.fileName?.startsWith(this.plugin.settings.plugin.shareAll?.excludedFileName)) {
repoFound.push(defRepo);
}
if (this.keyToFind) {
Expand All @@ -82,7 +84,7 @@ export class ChooseRepoToRun extends FuzzySuggestModal<Repository> {
}
}
}
repoFound=repoFound.concat(this.plugin.settings.github.otherRepo.filter((repo: Repository) => repo.shareAll?.enable));
repoFound=repoFound.concat(this.plugin.settings.github.otherRepo.filter((repo: Repository) => repo.shareAll?.enable && !this.fileName?.startsWith(repo.shareAll?.excludedFileName)));
repoFound.push(defRepo);
repoFound=[...new Set(repoFound)];
if (repoFound.length === 0)
Expand Down
10 changes: 6 additions & 4 deletions src/utils/data_validation_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ export function isShared(
if (!file || file.extension !== "md") {
return false;
}
if (!settings.plugin.shareAll || !settings.plugin.shareAll?.enable) {
const otherRepoWithShareAll = settings.github.otherRepo.filter((repo) => repo.shareAll?.enable);
if (!settings.plugin.shareAll?.enable && otherRepoWithShareAll.length === 0) {
const shareKey = otherRepo ? otherRepo.shareKey : settings.plugin.shareKey;
if ( meta == null || meta[shareKey] === undefined || isExcludedPath(settings, file)) {
return false;
} return meta[shareKey];
} else if (settings.plugin.shareAll!.enable) {
const excludedFileName = settings.plugin.shareAll!.excludedFileName;
if (!file.basename.startsWith(excludedFileName) && !isExcludedPath(settings, file)) {
} else if (settings.plugin.shareAll?.enable || otherRepoWithShareAll.length > 0) {
const allExcludedFileName = otherRepoWithShareAll.map((repo) => repo.shareAll!.excludedFileName);
allExcludedFileName.push(settings.plugin.shareAll!.excludedFileName);
if (allExcludedFileName.some(prefix => !file.basename.startsWith(prefix)) && !isExcludedPath(settings, file)) {
return true;
}
}
Expand Down

0 comments on commit fbc6015

Please sign in to comment.