Skip to content

Commit

Permalink
Merge pull request #8 from chhoumann/v1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann authored May 22, 2021
2 parents bd93a82 + 17e4c01 commit 9ce7ec4
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 24 deletions.
9 changes: 9 additions & 0 deletions .autorc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"plugins": [
["obsidian", { "dir": "./dist", "zip": true }]
],
"owner": "chhoumann",
"repo": "MetaEdit",
"name": "Christian B. B. Houmann",
"email": "christian@bagerbach.com"
}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.env

# Intellij
*.iml
.idea
Expand All @@ -7,7 +9,7 @@ node_modules
package-lock.json

# build
main.js
dist/main.js
*.js.map

# obsidian
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "MetaEdit",
"name": "MetaEdit",
"version": "1.2.0",
"version": "1.3.0",
"minAppVersion": "0.12.0",
"description": "MetaEdit helps you manage your metadata.",
"author": "Christian B. B. Houmann",
Expand Down
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "metaedit",
"version": "1.2.0",
"version": "1.3.0",
"description": "MetaEdit helps you manage your metadata.",
"main": "main.js",
"main": "dist/main.js",
"scripts": {
"dev": "rollup --config rollup.config.js -w",
"build": "rollup --config rollup.config.js --environment BUILD:production"
"build": "rollup --config rollup.config.js --environment BUILD:production",
"release": "auto shipit"
},
"keywords": [],
"author": "Christian B. B. Houmann",
Expand All @@ -14,16 +15,18 @@
"@rollup/plugin-commonjs": "^18.0.0",
"@rollup/plugin-node-resolve": "^11.2.1",
"@rollup/plugin-typescript": "^8.2.1",
"@tsconfig/svelte": "1.0.10",
"@types/node": "^14.14.37",
"auto": "10.28.0",
"auto-plugin-obsidian": "0.1.4",
"obsidian": "^0.12.0",
"rollup": "^2.32.1",
"tslib": "^2.2.0",
"typescript": "^4.2.4",
"rollup-plugin-svelte": "^7.1.0",
"svelte": "^3.37.0",
"svelte-check": "^1.3.0",
"svelte-preprocess": "^4.7.0",
"@tsconfig/svelte": "1.0.10"
"tslib": "^2.2.0",
"typescript": "^4.2.4"
},
"dependencies": {
"@popperjs/core": "^2.9.2"
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import autoPreprocess from "svelte-preprocess";
export default {
input: 'src/main.ts',
output: {
dir: '.',
dir: './dist',
format: 'cjs',
exports: 'default',
},
Expand Down
31 changes: 31 additions & 0 deletions src/Modals/LinkMenu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type MetaEdit from "../main";
import {Menu, TFile} from "obsidian";

export class LinkMenu {
private targetFile: TFile;

constructor(private plugin: MetaEdit) {
this.registerEvent();
}

registerEvent(): void {
this.plugin.registerEvent(
this.plugin.app.workspace.on('file-menu', (menu, file, source) => {
if (source === "link-context-menu" && file instanceof TFile) {
this.targetFile = file;
this.addOptions(menu);
}
})
)
}

private addOptions(menu: Menu) {
menu.addItem(item => {
item.setIcon('pencil');
item.setTitle("Edit Meta");
item.onClick(async evt => {
await this.plugin.runMetaEditForFile(this.targetFile);
})
})
}
}
18 changes: 12 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {debounce, Notice, Plugin, TAbstractFile, TFile} from 'obsidian';
import {debounce, Notice, Plugin, TAbstractFile, TFile, TFolder} from 'obsidian';
import {MetaEditSettingsTab} from "./Settings/metaEditSettingsTab";
import MEMainSuggester from "./Modals/metaEditSuggester";
import MetaController from "./metaController";
import type {MetaEditSettings} from "./Settings/metaEditSettings";
import {DEFAULT_SETTINGS} from "./Settings/defaultSettings";
import {LinkMenu} from "./Modals/LinkMenu";

export default class MetaEdit extends Plugin {
public settings: MetaEditSettings;
Expand Down Expand Up @@ -45,17 +46,22 @@ export default class MetaEdit extends Plugin {
callback: async () => {
const file: TFile = this.getCurrentFile();
if (!file) return;
const data = await this.controller.getPropertiesInFile(file);
if (!data) return;

const suggester: MEMainSuggester = new MEMainSuggester(this.app, this, data, file, this.controller);
suggester.open();
await this.runMetaEditForFile(file);
}
});

this.onModifyCallbackToggle(true);

this.addSettingTab(new MetaEditSettingsTab(this.app, this));
new LinkMenu(this);
}

public async runMetaEditForFile(file: TFile) {
const data = await this.controller.getPropertiesInFile(file);
if (!data) return;

const suggester: MEMainSuggester = new MEMainSuggester(this.app, this, data, file, this.controller);
suggester.open();
}

onunload() {
Expand Down
16 changes: 8 additions & 8 deletions src/metaController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ export default class MetaController {
this.plugin = plugin;
}

public async getPropertiesInFile(file: TFile) {
public async getPropertiesInFile(file: TFile): Promise<{}> {
const yaml = await this.parser.parseFrontmatter(file);
const inlineFields = await this.parser.parseInlineFields(file);

return {...yaml, ...inlineFields};
}

public async addYamlProp(propName: string, propValue: string, file: TFile) {
public async addYamlProp(propName: string, propValue: string, file: TFile): Promise<void> {
const fileContent: string = await this.app.vault.read(file);
const frontmatter: FrontMatterCache = this.app.metadataCache.getFileCache(file).frontmatter;
const isYamlEmpty: boolean = (frontmatter === undefined && !fileContent.match(/^-{3}\s*\n*\r*-{3}/));
Expand All @@ -53,7 +53,7 @@ export default class MetaController {
await this.app.vault.modify(file, newFileContent);
}

public async addDataviewField(propName: string, propValue: string, file: TFile) {
public async addDataviewField(propName: string, propValue: string, file: TFile): Promise<void> {
const fileContent: string = await this.app.vault.read(file);
let lines = fileContent.split("\n").reduce((obj: {[key: string]: string}, line: string, idx: number) => {
obj[idx] = !!line ? line : "";
Expand All @@ -72,7 +72,7 @@ export default class MetaController {
await this.app.vault.modify(file, newFileContent);
}

public async editMetaElement(toEdit: string, meta: SuggestData, file: TFile) {
public async editMetaElement(toEdit: string, meta: SuggestData, file: TFile): Promise<void> {
const mode: EditMode = this.plugin.settings.EditMode.mode;

if (mode === EditMode.AllMulti || mode === EditMode.SomeMulti)
Expand All @@ -81,7 +81,7 @@ export default class MetaController {
await this.standardMode(toEdit, file);
}

public async handleProgressProps(meta: SuggestData, file: TFile) {
public async handleProgressProps(meta: SuggestData, file: TFile): Promise<void> {
try {
const {enabled, properties} = this.plugin.settings.ProgressProperties;
if (!enabled) return;
Expand Down Expand Up @@ -265,8 +265,8 @@ export default class MetaController {
return null;
}

public async updatePropertyInFile(property: string, newValue: string, file: TFile) {
const propertyIsYaml = this.propertyIsYaml(property, file);
public async updatePropertyInFile(property: string, newValue: string, file: TFile): Promise<void> {
const propertyIsYaml = await this.propertyIsYaml(property, file);
const fileContent = await this.app.vault.read(file);

const newFileContent = fileContent.split("\n").map(line => {
Expand All @@ -284,7 +284,7 @@ export default class MetaController {
await this.app.vault.modify(file, newFileContent);
}

private async updateMultipleInFile(props: {[key: string]: string}, file: TFile) {
private async updateMultipleInFile(props: {[key: string]: string}, file: TFile): Promise<void> {
const fileContent = (await this.app.vault.read(file)).split("\n");

for (const prop of Object.keys(props)) {
Expand Down
2 changes: 1 addition & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class MetaEditParser {
}

public async parseFrontmatter(file: TFile): Promise<{ [key: string]: any }> {
const frontmatter = this.app.metadataCache.getFileCache(file).frontmatter;
const frontmatter = this.app.metadataCache.getFileCache(file)?.frontmatter;
if (!frontmatter) return {};
const {position: {start, end}} = frontmatter;
const filecontent = await this.app.vault.read(file);
Expand Down

0 comments on commit 9ce7ec4

Please sign in to comment.