Skip to content

Commit

Permalink
Fixed:
Browse files Browse the repository at this point in the history
- Nested tags are now displayed correctly.
- Even if multiple tags are simplified to one level, they can be displayed correctly too.

New feature:
- We can keep empty folders, even if they can be simplified.
  • Loading branch information
vrtmrz committed Nov 22, 2022
1 parent 28ace3c commit 3b44baf
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
19 changes: 14 additions & 5 deletions TreeItemComponent.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<script lang="ts">
import { currentFile, maxDepth, tagInfo } from "./store";
import { currentFile, maxDepth, tagInfo, tagFolderSetting } from "./store";
import {
TreeItem,
TagFolderItem,
SUBTREE_MARK_REGEX,
SUBTREE_MARK,
TagFolderSettings,
DEFAULT_SETTINGS,
} from "./types";
import { isAutoExpandTree, omittedTags, renderSpecialTag } from "./util";
import type { TagInfoDict } from "./types";
Expand Down Expand Up @@ -45,6 +47,12 @@
.split("/").length;
let _maxDepth = currentDepth + 1;
let setting: TagFolderSettings = JSON.parse(
JSON.stringify(DEFAULT_SETTINGS)
);
tagFolderSetting.subscribe((newSetting) => {
setting = newSetting;
});
function toggleFolder(evt: MouseEvent, entry: TagFolderItem) {
if (
evt.target instanceof HTMLElement &&
Expand Down Expand Up @@ -136,8 +144,8 @@
ellipsisMark = "";
omitTags = [];
if ("tag" in entry) {
showOnlyChildren = isAutoExpandTree(entry);
const omitTag = omittedTags(entry);
showOnlyChildren = isAutoExpandTree(entry, setting);
const omitTag = omittedTags(entry, setting);
if (omitTag !== false) {
omitTags = [
...omitTag.map((e) =>
Expand All @@ -147,7 +155,8 @@
.join("/")
),
];
ellipsisMark = "/" + omitTags.join("/");
ellipsisMark =
" " + omitTags.join(" ").replace(/\//g, SUBTREE_MARK);
}
}
}
Expand All @@ -158,7 +167,7 @@
? `${
skippedTag
? `${skippedTag}${
entry.tag.startsWith(SUBTREE_MARK) ? " " : "/"
entry.tag.startsWith(SUBTREE_MARK) ? " " : " "
}`
: ""
}${tagMark}${convertedTag}`
Expand Down
23 changes: 19 additions & 4 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
DEFAULT_SETTINGS,
VIEW_TYPE_SCROLL
} from "types";
import { treeRoot, currentFile, maxDepth, tagInfo } from "store";
import { treeRoot, currentFile, maxDepth, tagInfo, tagFolderSetting } from "store";
import { ancestorToLongestTag, ancestorToTags, doEvents, isAutoExpandTree, isSpecialTag, omittedTags, renderSpecialTag, secondsToFreshness } from "./util";
import { ScrollView } from "./ScrollView";

Expand Down Expand Up @@ -249,7 +249,7 @@ class TagFolderView extends ItemView {
const entryPath = "tag" in entry ? [...ancestorToTags(entry.ancestors)] : ['root', ...entry.tags];

if ("tag" in entry) {
const oTags = omittedTags(entry);
const oTags = omittedTags(entry, this.plugin.settings);
if (oTags != false) {
entryPath.push(...oTags);
}
Expand Down Expand Up @@ -823,7 +823,7 @@ export default class TagFolderPlugin extends Plugin {
}
if ("tag" in entry) {
if (path.indexOf(entry.tag) !== -1) return;
if (omittedTags(entry)) return;
if (omittedTags(entry, this.settings)) return;
const key = entry.ancestors.join("/");
for (const tags of this.expandedFolders) {
const tagPrefixToOpen = [];
Expand All @@ -844,7 +844,7 @@ export default class TagFolderPlugin extends Plugin {

for (const child of entry.children) {
if ("tag" in child) {
const autoExp = isAutoExpandTree(child);
const autoExp = isAutoExpandTree(child, this.settings);
const nextDepth = autoExp ? maxDepth : maxDepth - 1;
if (path.indexOf(child.tag) == -1) {
await this.expandLastExpandedFolders(child, false, [...path, entry.tag], openedTags, nextDepth);
Expand Down Expand Up @@ -1481,13 +1481,15 @@ ${this.tagInfoBody}`;
await this.loadData()
);
await this.loadTagInfo();
tagFolderSetting.set(this.settings);
this.compareItems = getCompareMethodItems(this.settings);
this.compareTags = getCompareMethodTags(this.settings);
}

async saveSettings() {
await this.saveData(this.settings);
await this.saveTagInfo();
tagFolderSetting.set(this.settings);
this.compareItems = getCompareMethodItems(this.settings);
this.compareTags = getCompareMethodTags(this.settings);
}
Expand Down Expand Up @@ -1605,6 +1607,19 @@ class TagFolderSettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName("Do not simplify empty folders")
.setDesc(
"Keep empty folders, even if they can be simplified."
)
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.doNotSimplifyTags)
.onChange(async (value) => {
this.plugin.settings.doNotSimplifyTags = value;
await this.plugin.saveSettings();
});
});
const setOrderMethod = async (key: string, order: string) => {
const oldSetting = this.plugin.settings.sortType.split("_");
if (!key) key = oldSetting[0];
Expand Down
3 changes: 2 additions & 1 deletion store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { writable } from "svelte/store";
import { TagInfoDict, TreeItem } from "types";
import { DEFAULT_SETTINGS, TagFolderSettings, TagInfoDict, TreeItem } from "types";

export const treeRoot = writable<TreeItem>();
export const currentFile = writable<string>("");
Expand All @@ -9,3 +9,4 @@ export const maxDepth = writable<number>(0);
export const filterString = writable<string>("");
export const tagInfo = writable<TagInfoDict>({});

export const tagFolderSetting = writable<TagFolderSettings>(DEFAULT_SETTINGS);
2 changes: 2 additions & 0 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface TagFolderSettings {
tagInfo: string;
mergeRedundantCombination: boolean;
useVirtualTag: boolean;
doNotSimplifyTags: boolean;
}

export const DEFAULT_SETTINGS: TagFolderSettings = {
Expand All @@ -88,6 +89,7 @@ export const DEFAULT_SETTINGS: TagFolderSettings = {
tagInfo: "pininfo.md",
mergeRedundantCombination: false,
useVirtualTag: false,
doNotSimplifyTags: false,
};

export const VIEW_TYPE_SCROLL = "tagfolder-view-scroll";
Expand Down
10 changes: 6 additions & 4 deletions util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SUBTREE_MARK, TreeItem, ViewItem, TagFolderItem, EPOCH_DAY, EPOCH_HOUR, FRESHNESS_1, FRESHNESS_2, FRESHNESS_3, FRESHNESS_4, FRESHNESS_5, tagDispDict } from "types";
import { SUBTREE_MARK, TreeItem, ViewItem, TagFolderItem, EPOCH_DAY, EPOCH_HOUR, FRESHNESS_1, FRESHNESS_2, FRESHNESS_3, FRESHNESS_4, FRESHNESS_5, tagDispDict, TagFolderSettings } from "types";

export function unique<T>(items: T[]) {
return [...new Set<T>([...items])];
Expand All @@ -8,7 +8,8 @@ export function allTags(entry: TagFolderItem): string[] {
return unique([...(entry?.descendants ?? []).flatMap(e => e.tags), ...entry.children.flatMap(e => "tag" in e ? allTags(e) : e.tags).filter(e => e)]);
}

export function isAutoExpandTree(entry: TreeItem) {
export function isAutoExpandTree(entry: TreeItem, setting: TagFolderSettings) {
if (setting.doNotSimplifyTags) return false;
if ("tag" in entry) {

const childrenTags = entry.children.filter(
Expand Down Expand Up @@ -39,7 +40,8 @@ export function isAutoExpandTree(entry: TreeItem) {
return false;
}

export function omittedTags(entry: TreeItem): false | string[] {
export function omittedTags(entry: TreeItem, setting: TagFolderSettings): false | string[] {
if (setting.doNotSimplifyTags) return false;
const childrenTags = entry.children.filter(
(e) => "tag" in e
) as TreeItem[];
Expand Down Expand Up @@ -94,7 +96,7 @@ export function renderSpecialTag(tagSrc: string) {
const tag = tagSrc.startsWith(SUBTREE_MARK)
? tagSrc.substring(SUBTREE_MARK.length)
: tagSrc;
return tag in tagDispDict ? tagDispDict[tag] : tag;
return tag in tagDispDict ? tagDispDict[tag] : tagSrc;

}

Expand Down

0 comments on commit 3b44baf

Please sign in to comment.