Skip to content

Commit

Permalink
global configuration for format on paste #97
Browse files Browse the repository at this point in the history
  • Loading branch information
Benature committed Jun 17, 2024
1 parent 4088717 commit e1a5a83
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
## 🏗️ developed
> to be update in the next version
## 3.2.0-b1
- [feature] Settings: global configuration for format on paste #97

## 3.1.0
- [feature] several commands (e.g. decode URI) support in front matter (metadata)
- [feature] command for open settings tab directly
Expand Down
2 changes: 1 addition & 1 deletion manifest-beta.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-text-format",
"name": "Text Format",
"version": "3.1.0",
"version": "3.2.0-b1",
"minAppVersion": "0.9.7",
"description": "Format text such as lowercase/uppercase/capitalize/titlecase, converting order/bullet list, removing redundant spaces/newline characters.",
"author": "Benature",
Expand Down
8 changes: 6 additions & 2 deletions src/langs/langs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const EN = {
"word-cases-desc": "lowercase / uppercase / title case / capitalize case / cycle case",
"lowercase-before-capitalize": "Lowercase before capitalize/title case",
"lowercase-before-capitalize-desc": "When running the capitalize or title case command, the plugin will lowercase the selection at first.",
"cycle-case-sequence": "Cycle case sequence (one case in a line)",
"cycle-case-sequence": "Cycle case sequence (one case per line)",
"cycle-case-sequence-desc":
"Support cases: `lowerCase`, `upperCase`, `capitalizeWord`, `capitalizeSentence`, `titleCase`. \n" +
"Note that the result of `capitalizeWord` and `titleCase` could be the same in some cases, " +
Expand Down Expand Up @@ -109,7 +109,11 @@ const EN = {
"method-decide-callout-type": "Method to decide callout type",
"method-decide-callout-type-desc": "How to decide the type of new callout block for command `Callout format`? `Fix callout type` use the default callout type always, other methods only use the default type when it fails to find previous callout block.",
"default-callout-type": "Default callout type",
"default-callout-type-desc": "Set the default callout type for command `Callout format`. "
"default-callout-type-desc": "Set the default callout type for command `Callout format`. ",
"format-on-paste": {
"name": "Format on paste",
"desc": "Format the pasted content automatically. One command per line.",
}
}
}

Expand Down
13 changes: 9 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default class TextFormat extends Plugin {
const text = activeElement.textContent;
const replacedText = await this.quickFormat(text, cmd);
await this.app.fileManager.processFrontMatter(file, (fm) => {
fm[metadataKey] = replacedText;
fm[metadataKey] = replacedText;
});
// let keyboardEvent = new KeyboardEvent('keydown', {
// keyCode: 13, code: 'KeyEnter', key: 'Enter'
Expand All @@ -102,14 +102,19 @@ export default class TextFormat extends Plugin {
return;
}
// @ts-ignore
const formatOnPasteCmdList = info.metadataEditor.properties.find(m => m.key === "tfFormatOnPaste")?.value;
let formatOnPasteCmdList = info.metadataEditor.properties.find(m => m.key === "tfFormatOnPaste")?.value;
// console.log(formatOnPasteCmdList)
if (formatOnPasteCmdList === undefined || formatOnPasteCmdList?.length == 0) { return; }
if (formatOnPasteCmdList === undefined) {
if (this.settings.formatOnSaveSettings.enabled) {
formatOnPasteCmdList = this.settings.formatOnSaveSettings.commandsString.split("\n").map((c) => c.trim().replace(/^[ -]*/g, ""));
} else return;
}
if (formatOnPasteCmdList?.length == 0) return;

let clipboard = evt.clipboardData.getData('text/html') || evt.clipboardData.getData('text/plain');
if (!clipboard) { return; }

evt?.preventDefault();
// evt?.stopPropagation();

for (let cmd of formatOnPasteCmdList) {
const formatText = (await this.formatSelection(clipboard, cmd)).editorChange.text;
Expand Down
20 changes: 20 additions & 0 deletions src/settings/settingTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ export class TextFormatSettingTab extends PluginSettingTab {
this.makeCollapsible(headerEl, this.contentEl, true);


new Setting(this.contentEl)
.setName(getString(["setting", "format-on-paste", "name"]))
.setDesc(getString(["setting", "format-on-paste", "desc"]))
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.formatOnSaveSettings.enabled)
.onChange(async (value) => {
this.plugin.settings.formatOnSaveSettings.enabled = value;
await this.plugin.saveSettings();
});
})
.addTextArea((text) =>
text
// .setPlaceholder(getString(["setting", "format-on-paste", "placeholder"]))
.setValue(this.plugin.settings.formatOnSaveSettings.commandsString)
.onChange(async (value) => {
this.plugin.settings.formatOnSaveSettings.commandsString = value;
await this.plugin.saveSettings();
})
);


new Setting(this.contentEl)
Expand Down

0 comments on commit e1a5a83

Please sign in to comment.