Skip to content

Commit

Permalink
support command in metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Benature committed Jun 14, 2024
1 parent a903c22 commit 63d3e85
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export default class TextFormat extends Plugin {
debounceUpdateCommandRequest = debounce(this.updateCommandRequest, 1000, true);
// memory: TextFormatMemory;

executeCommandById(cmd: string) {
// @ts-ignore
this.app.commands.executeCommandById(cmd);
}

async formatEditorOrTitle(cmd: string) {
if (isActiveTitle()) {
const file = this.app.workspace.getActiveFile();
Expand Down Expand Up @@ -406,28 +411,39 @@ export default class TextFormat extends Plugin {
this.debounceUpdateCommandWrapper();
this.debounceUpdateCommandRequest();
this.debounceUpdateCommandCustomReplace();

this.addCommand({
id: "decodeURI",
name: { en: "Decode URL", zh: "解码 URL", "zh-TW": "解碼 URL" }[lang],
icon: "link",
callback: async () => {
const activeElement = document.activeElement;
if (activeElement.classList.contains("metadata-input-longtext")) {
let metadataKey = activeElement.parentElement.parentElement.getAttribute("data-property-key");
// focus on parent element, so that the new frontmatter can be updated
activeElement.parentElement.parentElement.focus();
const file = this.app.workspace.getActiveFile();
const frontmatter = this.app.metadataCache.getCache(file?.path as string)?.frontmatter;
let formatRes = await this.formatSelection(frontmatter[metadataKey], "decodeURI");
if (file) {
await this.app.fileManager.processFrontMatter(file, (fm) => {
fm[metadataKey] = formatRes.editorChange.text;
});
}
} else {
this.executeCommandById(`obsidian-text-format::editor:decodeURI`);
}
},
});
this.addCommand({
id: ":editor:decodeURI",
name: { en: "Decode URL", zh: "解码 URL", "zh-TW": "解碼 URL" }[lang] + " (Editor)",
icon: "link",
editorCallback: (editor: Editor, view: MarkdownView) => {
this.editorTextFormat(editor, view, "decodeURI");
},
});
// this.addCommand({
// id: "paragraph-double-spaces",
// name: { en: "Add extra double spaces per paragraph for whole file", zh: "全文为每段段末添加双空格", "zh-TW": "全文為每段段末添加雙空格" }[lang],
// editorCallback: (editor: Editor, view: MarkdownView) => {
// extraDoubleSpaces(editor, view);
// },
// });
// this.addCommand({
// id: "add-line-break",
// name: { en: "Add extra line break to paragraph", zh: "在段落末添加额外换行", "zh-TW": "在段落末添加額外換行" }[lang],
// editorCallback: (editor: Editor, view: MarkdownView) => {
// this.editorTextFormat(editor, view, "add-line-break");
// },
// });

this.addCommand({
id: "space-word-symbol",
name: { en: "Format space between words and symbols", zh: "格式化单词与符号之间的空格", "zh-TW": "格式化單詞與符號之間的空格" }[lang],
Expand Down

0 comments on commit 63d3e85

Please sign in to comment.