generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a93b890
commit dca00ed
Showing
15 changed files
with
2,315 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.