Skip to content

Commit

Permalink
Merge pull request #3 from chhoumann/v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann authored May 18, 2021
2 parents b238ef3 + 7664cdc commit a941ab0
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 108 deletions.
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.0.0",
"version": "1.1.0",
"minAppVersion": "0.12.0",
"description": "MetaEdit helps you manage your metadata.",
"author": "Christian B. B. Houmann",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "metaedit",
"version": "1.0.0",
"version": "1.1.0",
"description": "MetaEdit helps you manage your metadata.",
"main": "main.js",
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,4 @@
margin-right: 5px;
font-size: 15px;
}
.not-a-button {
background: none;
color: inherit;
border: none;
padding: 0;
font: inherit;
cursor: pointer;
outline: inherit;
}
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,4 @@
margin-right: 5px;
font-size: 15px;
}
.not-a-button {
background: none;
color: inherit;
border: none;
padding: 0;
font: inherit;
cursor: pointer;
outline: inherit;
}
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,4 @@
margin-right: 5px;
font-size: 15px;
}
.not-a-button {
background: none;
color: inherit;
border: none;
padding: 0;
font: inherit;
cursor: pointer;
outline: inherit;
}
</style>
84 changes: 67 additions & 17 deletions src/Modals/metaEditSuggester.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import {App, FuzzyMatch, FuzzySuggestModal, TFile} from "obsidian";
import type MetaEdit from "../main";
import type MetaController from "../metaController";
import type {SuggestData} from "../Types/suggestData";

export type SuggestData = {[key: string]: string};
const newYaml: string = "New YAML property";
const newDataView: string = "New Dataview field";

export default class MetaEditSuggester extends FuzzySuggestModal<string> {
public app: App;
private readonly file: TFile;
private plugin: MetaEdit;
private readonly data: SuggestData;
private options: SuggestData;
private readonly options: SuggestData;
private controller: MetaController;

constructor(app: App, plugin: MetaEdit, data: SuggestData, file: TFile, controller: MetaController) {
Expand All @@ -19,8 +21,14 @@ export default class MetaEditSuggester extends FuzzySuggestModal<string> {
this.plugin = plugin;
this.data = data;
this.controller = controller;
this.options = {
newYaml, newDataView
}

this.getMetaOptions();
this.setInstructions([
{command: "❌", purpose: "Delete property"},
{command: "🔃", purpose: "Transform to YAML/Dataview"}
])
this.removeIgnored();
}

Expand All @@ -29,6 +37,9 @@ export default class MetaEditSuggester extends FuzzySuggestModal<string> {

if (Object.values(this.options).find(v => v === item.item)) {
el.style.fontWeight = "bold";
} else {
this.createButton(el, item, "❌", this.deleteItem(item));
this.createButton(el, item, "🔃", this.transformProperty(item))
}
}

Expand All @@ -44,26 +55,65 @@ export default class MetaEditSuggester extends FuzzySuggestModal<string> {
}

async onChooseItem(item: string, evt: MouseEvent | KeyboardEvent): Promise<void> {
switch (item) {
case "New YAML property":
await this.controller.addYamlProp(this.file);
break;
case "New Dataview field":
await this.controller.addDataviewField(this.file);
break;
default:
await this.controller.editMetaElement(item, this.data, this.file);
break;
if (item === newYaml) {
const {propName, propValue} = await this.controller.createNewProperty();
await this.controller.addYamlProp(propName, propValue, this.file);
return;
}

if (item === newDataView) {
const {propName, propValue} = await this.controller.createNewProperty();
await this.controller.addDataviewField(propName, propValue, this.file);
return;
}

await this.controller.editMetaElement(item, this.data, this.file);
}

private getMetaOptions(): void {
this.options = {
newYaml: "New YAML property",
newDataView: "New Dataview field"
private deleteItem(item: FuzzyMatch<string>) {
return async (evt: MouseEvent) => {
evt.stopPropagation();
console.log(`Clicked delete for ${item.item}`)
await this.controller.deleteProperty(item.item, this.file);
this.close();
};
}

private transformProperty(item: FuzzyMatch<string>) {
return async (evt: MouseEvent | KeyboardEvent) => {
evt.stopPropagation();

if (this.controller.propertyIsYaml(item.item, this.file)) {
await this.toDataview(item);
} else {
await this.toYaml(item);
}

this.close();
}
}

private async toYaml(item: FuzzyMatch<string>) {
const content: string = this.data[item.item];
await this.controller.deleteProperty(item.item, this.file);
await this.controller.addYamlProp(item.item, content, this.file);
}

private async toDataview(item: FuzzyMatch<string>) {
const content: string = this.data[item.item];
await this.controller.deleteProperty(item.item, this.file);
await this.controller.addDataviewField(item.item, content, this.file);
}

private createButton(el: HTMLElement, item: FuzzyMatch<string>, content: string, callback: (evt: MouseEvent) => void) {
const itemButton = el.createEl("button");
itemButton.textContent = content;
itemButton.classList.add("not-a-button");
itemButton.style.float = "right";
itemButton.style.marginRight = "4px";
itemButton.addEventListener("click", callback);
}

private removeIgnored(): void {
const ignored = this.plugin.settings.IgnoredProperties.properties;
const data = Object.keys(this.data);
Expand Down
10 changes: 0 additions & 10 deletions src/Modals/shared/SingleValueTableEditorContent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,4 @@
margin-right: 5px;
font-size: 15px;
}
.not-a-button {
background: none;
color: inherit;
border: none;
padding: 0;
font: inherit;
cursor: pointer;
outline: inherit;
}
</style>
3 changes: 3 additions & 0 deletions src/Types/metaType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum MetaType {
YAML, Dataview
}
3 changes: 3 additions & 0 deletions src/Types/suggestData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface SuggestData {
[key: string]: string
}
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default class MetaEdit extends Plugin {
id: 'metaEditRun',
name: 'Run MetaEdit',
callback: async () => {
const file = await this.getCurrentFile();
const file: TFile = this.getCurrentFile();
if (!file) return;
const data = await this.controller.getPropertiesInFile(file);
if (!data) return;
Expand All @@ -71,7 +71,7 @@ export default class MetaEdit extends Plugin {
this.toggleOnFileModifyUpdateProgressProperties(false);
}

public async getCurrentFile() {
public getCurrentFile() {
try {
return this.app.workspace.getActiveFile();
}
Expand Down
Loading

0 comments on commit a941ab0

Please sign in to comment.