Skip to content

Commit

Permalink
Version 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
JeppeKlitgaard committed Jun 12, 2021
1 parent a93b890 commit dca00ed
Show file tree
Hide file tree
Showing 15 changed files with 2,315 additions and 157 deletions.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,44 @@ They are all set to <kbd>Blank</kbd> by default.
### Hotkeys
| Recommended hotkey | Action |
| --- | --- |
| <kbd>Alt</kbd> + <kbd>←</kbd> | Move Selection Left |
| <kbd>Alt</kbd> + <kbd>→</kbd> | Move Selection Right |
| <kbd>Blank</kbd> | Toggle Better Bold (asterisks) |
| <kbd>Ctrl</kbd> + <kbd>B</kbd> | Toggle Better Bold (underscores) |
| <kbd>Blank</kbd> | Toggle Better Italics (asterisks) |
| <kbd>Ctrl</kbd> + <kbd>I</kbd> | Toggle Better Italics (underscores) |
| <kbd>Blank</kbd> | Toggle Better Code |
| <kbd>CTRL</kbd> + <kbd>/</kbd> | Toggle Better Comment |
| <kbd>Blank</kbd> | Toggle Better Highlight |
| <kbd>Blank</kbd> | Toggle Better Strikethrough |
| <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>A</kbd> | Select Current Line(s) |
| <kbd>Shift</kbd> + <kbd>Alt</kbd> + <kbd>↑</kbd> | Copy Current Line(s) Up |
| <kbd>Shift</kbd> + <kbd>Alt</kbd> + <kbd>↓</kbd> | Copy Current Line(s) Down |
| <kbd>Shift</kbd> + <kbd>Alt</kbd> + <kbd>←</kbd> | Copy Current Line(s) Left |
| <kbd>Shift</kbd> + <kbd>Alt</kbd> + <kbd>→</kbd> | Copy Current Line(s) Right |
| <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>A</kbd> | Select Current Line(s) |
| <kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>1</kbd> | Toggle Heading - H1 |
| <kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>2</kbd> | Toggle Heading - H2 |
| <kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>3</kbd> | Toggle Heading - H3 |
| <kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>4</kbd> | Toggle Heading - H4 |
| <kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>5</kbd> | Toggle Heading - H5 |
| <kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>6</kbd> | Toggle Heading - H6 |

### Move selection

Move selection does not support going across lines. This seems unnecessary and
is really messy to implement.
### Better Toggles

The default toggles in Obsidian can be quite wonky and do not always
work well. The toggles implemented here should always return to the
same state when toggled twice.

They also do nice word-wrapping for you.

Additionally __Bold__ and _Italics_ are implemented with underscores as well
as asterisks. Simply bind the command you want.

Math toggles are also included.
## Philosophy

- By default no features are enabled. You can enable the features you want.
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-tweaks",
"name": "Obsidian Tweaks",
"version": "0.1.1",
"version": "0.2.0",
"minAppVersion": "0.12.3",
"description": "A small plugin that implements some sorely missed features.",
"author": "Jeppe Klitgaard",
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
{
"name": "obsidian-tweaks",
"version": "0.1.1",
"version": "0.2.0",
"description": "A small plugin that implements some sorely missed features.",
"main": "main.js",
"scripts": {
"dev": "rollup --config rollup.config.js -w",
"build": "rollup --config rollup.config.js --environment BUILD:production"
},
"keywords": ["obsidian"],
"keywords": [
"obsidian"
],
"author": "Jeppe Klitgaard",
"license": "Unlicense",
"devDependencies": {
"@rollup/plugin-commonjs": "^18.0.0",
"@rollup/plugin-node-resolve": "^11.2.1",
"@rollup/plugin-typescript": "^8.2.1",
"@types/node": "^14.14.37",
"auto": "^10.29.3",
"auto-plugin-obsidian": "^0.1.4",
"obsidian": "^0.12.0",
"rollup": "^2.32.1",
"tslib": "^2.2.0",
Expand Down
93 changes: 93 additions & 0 deletions src/BetterFormatting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { App, EditorPosition, MarkdownView, Editor } from "obsidian"
import ObsidianTweaksPlugin from "./main"
import { Direction } from "./Constants"


export class BetterFormatting {
public app: App
private plugin: ObsidianTweaksPlugin

constructor(app: App, plugin: ObsidianTweaksPlugin) {
this.app = app
this.plugin = plugin
}

toggleWrapper(symbolStart: string, symbolEnd: string): void {
// Principle:
// Toggling twice == no-op
// This is not trivial to achieve :(
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView)
if (!activeView) {
return
}

const editor = activeView.editor

let anchor = editor.getCursor("from")
let head = editor.getCursor("to")

let wordStart: EditorPosition
let wordEnd: EditorPosition

wordStart = editor.cm.findWordAt(anchor).anchor
wordEnd = editor.cm.findWordAt(head).head

let textToWrap = editor.getRange(wordStart, wordEnd)

if (textToWrap.trim() === "") {
wordStart = anchor
wordEnd = anchor

let charBefore = editor.getRange(
{line: wordStart.line, ch: wordStart.ch - 1},
{line: wordStart.line, ch: wordStart.ch},
)

// We should've wrapped a whole word more but CM word selection
// is weird.
// Let's fix
if (charBefore.trim() !== "") {
wordStart = editor.cm.findWordAt(
{line: wordStart.line, ch: wordStart.ch - 1})
.anchor

}

// Update textToWrap again
textToWrap = editor.getRange(wordStart, wordEnd)

}

let alreadyWrapped = (
textToWrap.startsWith(symbolStart) &&
textToWrap.endsWith(symbolEnd)
)

let newText: string
if (alreadyWrapped) {
newText = textToWrap.substring(symbolStart.length, textToWrap.length - symbolEnd.length)
} else {
newText = symbolStart + textToWrap + symbolEnd
}

editor.replaceRange(
newText,
{line: wordStart.line, ch: wordStart.ch},
{line: wordEnd.line, ch: wordEnd.ch},
)

if (alreadyWrapped) {
editor.setSelection(
{line: anchor.line, ch: anchor.ch - symbolStart.length},
{line: head.line, ch: head.ch - symbolStart.length}
)
} else {
editor.setSelection(
{line: anchor.line, ch: anchor.ch + symbolStart.length},
{line: head.line, ch: head.ch + symbolStart.length}
)
}

return
}
}
8 changes: 8 additions & 0 deletions src/Constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const DEBUG_HEAD = "Obsidean Tweaks: "

export enum Direction {
Up,
Down,
Left,
Right,
}
72 changes: 34 additions & 38 deletions src/DirectionalCopy.ts
Original file line number Diff line number Diff line change
@@ -1,89 +1,85 @@
import { App, EditableFileView, Editor, MarkdownView } from "obsidian";
import ObsidianTweaksPlugin from "./main";
import { DEBUG_HEAD } from "./constants";
import { App, MarkdownView } from "obsidian"
import ObsidianTweaksPlugin from "main"
import { Direction } from "Constants"

export enum Direction {
Up,
Down,
Left,
Right
}

export class DirectionalCopy {
public app: App;
private plugin: ObsidianTweaksPlugin;
public app: App
private plugin: ObsidianTweaksPlugin

constructor(app: App, plugin: ObsidianTweaksPlugin) {
this.app = app;
this.plugin = plugin;
this.app = app
this.plugin = plugin
}

directionalCopy(direction: Direction): void {
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView)
if (!activeView) {
return;
return
}

let anchor = activeView.editor.getCursor("from");
let head = activeView.editor.getCursor("to");
const editor = activeView.editor

let headLength = activeView.editor.getLine(head.line).length;
let anchor = editor.getCursor("from")
let head = editor.getCursor("to")

let textToCopyVertical = activeView.editor.getRange(
let headLength = editor.getLine(head.line).length

let textToCopyVertical = editor.getRange(
{line: anchor.line, ch: 0},
{line: head.line, ch: headLength}
)

// Copy up
if (direction === Direction.Up) {
activeView.editor.replaceRange(
editor.replaceRange(
textToCopyVertical + '\n',
{line: anchor.line, ch: 0}
);
activeView.editor.setSelection(
)
editor.setSelection(
{line: anchor.line, ch: anchor.ch},
{line: head.line, ch: head.ch},
);
)
}
// Copy down
else if (direction === Direction.Down) {
let addedLines = head.line - anchor.line;
let addedLines = head.line - anchor.line

activeView.editor.replaceRange(
editor.replaceRange(
'\n' + textToCopyVertical,
{line: head.line, ch: headLength}
);
activeView.editor.setSelection(
)
editor.setSelection(
{line: anchor.line + addedLines + 1, ch: anchor.ch},
{line: head.line + addedLines + 1, ch: head.ch},
);
)
}
// Copy left
else if (direction === Direction.Left) {
let textToCopy = activeView.editor.getSelection();
let textToCopy = editor.getSelection()

activeView.editor.replaceRange(
editor.replaceRange(
textToCopy,
{line: anchor.line, ch: anchor.ch}
);
activeView.editor.setSelection(
)
editor.setSelection(
{line: anchor.line, ch: anchor.ch},
{line: head.line, ch: head.ch},
);
)
}
// Copy right
else if (direction === Direction.Right) {
let textToCopy = activeView.editor.getSelection();
let textToCopy = editor.getSelection()

activeView.editor.replaceRange(
editor.replaceRange(
textToCopy,
{line: anchor.line, ch: anchor.ch}
);
)
}
else {
console.log(DEBUG_HEAD + "Something went wrong...");
this.plugin.debug("Something went wrong...")
}

return;
return
}
}
Loading

0 comments on commit dca00ed

Please sign in to comment.