generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
60 lines (50 loc) · 1.77 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { Plugin, Editor, MarkdownView, Notice } from 'obsidian';
interface Text2ChatBubbleStylerSettings {
// Define any specific settings you might need; for now, let's keep it simple
exampleSetting: string;
}
const DEFAULT_SETTINGS: Text2ChatBubbleStylerSettings = {
exampleSetting: 'default value'
}
export default class Text2ChatBubbleStyler extends Plugin {
settings: Text2ChatBubbleStylerSettings;
async onload() {
await this.loadSettings();
// Add command to apply chat bubble style
this.addCommand({
id: 'apply-chat-bubble-style',
name: 'Apply Chat Bubble Style',
callback: () => this.applyChatBubbleStyle(),
hotkeys: [{
modifiers: ["Mod"],
key: 'b'
}]
});
}
onunload() {
// Perform cleanup if necessary
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
applyChatBubbleStyle() {
const activeLeaf = this.app.workspace.activeLeaf;
if (activeLeaf?.view instanceof MarkdownView) {
const editor = activeLeaf.view.editor;
const selection = editor.getSelection();
if (selection) {
const chatFormatted = this.formatAsChatBubble(selection);
editor.replaceSelection(chatFormatted);
} else {
new Notice('No text selected!');
}
}
}
formatAsChatBubble(text: string): string {
// A simple formatter that wraps text in div and class names
return `<div class="chat"><div class="msg sent">${text.replace(/\n/g, '<br>')}</div></div>`;
}
}