Skip to content

Commit

Permalink
Add sort by filename feature, #33
Browse files Browse the repository at this point in the history
  • Loading branch information
jensmtg committed Oct 20, 2022
1 parent 4f33bb9 commit bdbdfd2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
40 changes: 28 additions & 12 deletions src/apiAdapter.tsx
Expand Up @@ -52,7 +52,7 @@ export class ApiAdapter {
const div = document.createElement('div');
await MarkdownRenderer.renderMarkdown(markdown, div, '/', null)
// @ts-ignore
div.innerHTML = div.innerHTML.replaceAll('type="checkbox"', 'type="checkbox" disabled="true"')
div.innerHTML = div.innerHTML.replaceAll('type="checkbox"', 'type="checkbox" disabled="true"')
return div
}

Expand All @@ -71,7 +71,7 @@ export class ApiAdapter {


/** For a given file, should Influx component be shown on it's page? */
getShowStatus(file: TFile) : boolean {
getShowStatus(file: TFile): boolean {
const settings: Partial<ObsidianInfluxSettings> = this.getSettings()
const patterns = settings.showBehaviour === 'OPT_IN' ? settings.inclusionPattern : settings.exclusionPattern
const matched = this.patternMatchingFn(file.path, patterns)
Expand All @@ -82,7 +82,7 @@ export class ApiAdapter {
}


isIncludableSource(path: string) : boolean {
isIncludableSource(path: string): boolean {
const settings: Partial<ObsidianInfluxSettings> = this.getSettings()
const patterns = settings.sourceBehaviour === 'OPT_IN' ? settings.sourceInclusionPattern : settings.sourceExclusionPattern
const matched = this.patternMatchingFn(path, patterns)
Expand All @@ -94,14 +94,14 @@ export class ApiAdapter {


/** For a given file, should Influx component be shown as collapsed on it's page? */
getCollapsedStatus(file: TFile) : boolean {
getCollapsedStatus(file: TFile): boolean {
const settings: Partial<ObsidianInfluxSettings> = this.getSettings()
const matched = this.patternMatchingFn(file.path, settings.collapsedPattern)
return matched
}


patternMatchingFn = (path: string, _patterns: string[]) : boolean => {
patternMatchingFn = (path: string, _patterns: string[]): boolean => {
const patterns = _patterns.filter((_path: string) => _path.length > 0)
const pathMatchesRegex = (pattern: string): boolean => {
try {
Expand All @@ -119,18 +119,34 @@ export class ApiAdapter {
/** A sort function to order notes correctly, based on settings. */
makeComparisonFn(): (a: InlinkingFile, b: InlinkingFile) => 0 | 1 | -1 {
const settings: Partial<ObsidianInfluxSettings> = this.getSettings()
const sortingAttr = settings.sortingAttribute === 'ctime' || settings.sortingAttribute === 'mtime' ? settings.sortingAttribute : 'ctime'

const flip = settings.sortingPrinciple === 'OLDEST_FIRST'

return function compareDatesFn(a: InlinkingFile, b: InlinkingFile) {
if (a.file.stat[sortingAttr] < b.file.stat[sortingAttr]) return flip ? -1 : 1
else if (a.file.stat[sortingAttr] > b.file.stat[sortingAttr]) return flip ? 1 : -1
else return 0
if (settings.sortingAttribute === 'FILENAME') {
return function compareDatesFn(a: InlinkingFile, b: InlinkingFile) {
if (a.file.basename < b.file.basename) return flip ? -1 : 1
else if (a.file.basename > b.file.basename) return flip ? 1 : -1
else return 0
}
}

else {

const sortingAttr = settings.sortingAttribute === 'ctime' || settings.sortingAttribute === 'mtime' ? settings.sortingAttribute : 'ctime'

return function compareDatesFn(a: InlinkingFile, b: InlinkingFile) {
if (a.file.stat[sortingAttr] < b.file.stat[sortingAttr]) return flip ? -1 : 1
else if (a.file.stat[sortingAttr] > b.file.stat[sortingAttr]) return flip ? 1 : -1
else return 0
}

}


}


async renderAllMarkdownBlocks(inlinkingsFiles: InlinkingFile[]) : Promise<ExtendedInlinkingFile[]> {
async renderAllMarkdownBlocks(inlinkingsFiles: InlinkingFile[]): Promise<ExtendedInlinkingFile[]> {
const settings: Partial<ObsidianInfluxSettings> = this.getSettings()
const comparator = this.makeComparisonFn()
const components = await Promise.all(inlinkingsFiles
Expand Down Expand Up @@ -167,6 +183,6 @@ export class ApiAdapter {
return false
}



}
2 changes: 1 addition & 1 deletion src/main.tsx
Expand Up @@ -12,7 +12,7 @@ import { createStyleSheet, StyleSheetType } from './createStyleSheet';
export interface ObsidianInfluxSettings {
liveUpdate: boolean;
sortingPrinciple: 'NEWEST_FIRST' | 'OLDEST_FIRST';
sortingAttribute: 'ctime' | 'mtime'; // created or modified.
sortingAttribute: 'ctime' | 'mtime' | 'FILENAME'; // created or modified.
showBehaviour: 'OPT_OUT' | 'OPT_IN';
exclusionPattern: string[];
inclusionPattern: string[];
Expand Down
3 changes: 2 additions & 1 deletion src/settings.tsx
Expand Up @@ -61,9 +61,10 @@ export class ObsidianInfluxSettingsTab extends PluginSettingTab {
dropdown
.addOption('ctime', 'By date created')
.addOption('mtime', 'By date last modified')
.addOption('FILENAME', 'By filename')
.setValue(this.plugin.data.settings.sortingAttribute)
.onChange(async (value) => {
if (value === 'ctime' || value === 'mtime') {
if (value === 'ctime' || value === 'mtime' || value === 'FILENAME') {
this.plugin.data.settings.sortingAttribute = value;
await this.saveSettings()
}
Expand Down

0 comments on commit bdbdfd2

Please sign in to comment.