mirror of
https://github.com/ipshing/obsidian-text-transform.git
synced 2026-07-22 08:30:29 +00:00
Adding text transform functions and commands
This commit is contained in:
parent
9b2ab3928f
commit
7af88dbd4c
3 changed files with 72 additions and 1 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -13,6 +13,7 @@ package-lock.json
|
|||
main.js
|
||||
*.js.map
|
||||
/build/*
|
||||
build*
|
||||
|
||||
# Exclude sourcemaps
|
||||
*.map
|
||||
|
|
|
|||
43
src/main.ts
43
src/main.ts
|
|
@ -1,4 +1,5 @@
|
|||
import { Plugin } from "obsidian";
|
||||
import { toUpperCase, toLowerCase, toTitleCase } from "./text";
|
||||
|
||||
interface TextTransformSettings {
|
||||
mySetting: string;
|
||||
|
|
@ -13,9 +14,49 @@ export default class TextTransform extends Plugin {
|
|||
|
||||
async onload() {
|
||||
await this.loadSettings();
|
||||
|
||||
// Add default commands for transforming cases
|
||||
this.addCommand({
|
||||
id: "text-transform-uppercase",
|
||||
name: "Transform to Uppercase",
|
||||
hotkeys: [
|
||||
{
|
||||
modifiers: ["Mod", "Shift"],
|
||||
key: "U",
|
||||
},
|
||||
],
|
||||
editorCallback: toUpperCase,
|
||||
});
|
||||
|
||||
this.addCommand({
|
||||
id: "text-transform-Lowercase",
|
||||
name: "Transform to Lowercase",
|
||||
hotkeys: [
|
||||
{
|
||||
modifiers: ["Mod"],
|
||||
key: "U",
|
||||
},
|
||||
],
|
||||
editorCallback: toLowerCase,
|
||||
});
|
||||
this.addCommand({
|
||||
id: "text-transform-title-case",
|
||||
name: "Transform to Title Case",
|
||||
hotkeys: [
|
||||
{
|
||||
modifiers: ["Mod", "Alt"],
|
||||
key: "U",
|
||||
},
|
||||
],
|
||||
editorCallback: toTitleCase,
|
||||
});
|
||||
|
||||
console.log("Text Transform plugin loaded");
|
||||
}
|
||||
|
||||
onunload() {}
|
||||
async onunload() {
|
||||
console.log("Text Transform plugin unloaded");
|
||||
}
|
||||
|
||||
async loadSettings() {
|
||||
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
|
||||
|
|
|
|||
29
src/text.ts
Normal file
29
src/text.ts
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import { Editor, MarkdownView } from "obsidian";
|
||||
|
||||
export function toUpperCase(editor: Editor, view: MarkdownView) {
|
||||
const from = editor.getCursor("from");
|
||||
const to = editor.getCursor("to");
|
||||
const selection = editor.getSelection();
|
||||
editor.replaceSelection(selection.toUpperCase());
|
||||
editor.setSelection(from, to);
|
||||
}
|
||||
|
||||
export function toLowerCase(editor: Editor, view: MarkdownView) {
|
||||
const from = editor.getCursor("from");
|
||||
const to = editor.getCursor("to");
|
||||
const selection = editor.getSelection();
|
||||
editor.replaceSelection(selection.toLowerCase());
|
||||
editor.setSelection(from, to);
|
||||
}
|
||||
|
||||
export function toTitleCase(editor: Editor, view: MarkdownView) {
|
||||
const from = editor.getCursor("from");
|
||||
const to = editor.getCursor("to");
|
||||
const selection = editor.getSelection();
|
||||
// First put the string into lower case
|
||||
let titleCase = selection.toLowerCase();
|
||||
// Then transform to title case
|
||||
titleCase = titleCase.replace(/(^|\s)\S/g, (l) => l.toUpperCase());
|
||||
editor.replaceSelection(titleCase);
|
||||
editor.setSelection(from, to);
|
||||
}
|
||||
Loading…
Reference in a new issue