Skip to content

Commit f0e0088

Browse files
Merge pull request #23 from AuTa/master
👩🏽‍💻Change command can remove tags
2 parents f8d5510 + 67c8e4f commit f0e0088

File tree

1 file changed

+41
-37
lines changed

1 file changed

+41
-37
lines changed

src/plugin/main.ts

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Command,
55
setIcon,
66
debounce,
7+
Editor,
78
MarkdownView,
89
SliderComponent,
910
ToggleComponent,
@@ -123,46 +124,67 @@ export default class cMenuPlugin extends Plugin {
123124

124125
generateCommands() {
125126
const applyCommand = (
126-
prefix: string,
127-
selectedText: string,
128-
suffix: string
127+
command: commandPlot,
128+
editor: Editor,
129129
) => {
130-
suffix = suffix || prefix;
131-
return `${prefix}${selectedText}${suffix}`;
130+
const selectedText = editor.getSelection();
131+
const curserStart = editor.getCursor("from");
132+
const curserEnd = editor.getCursor("to");
133+
const prefix = command.prefix;
134+
const suffix = command.suffix|| prefix;
135+
const setCursor = (mode: number) => {
136+
editor.setCursor(curserStart.line + command.line * mode, curserEnd.ch + command.char * mode);
137+
};
138+
const preStart = { line: curserStart.line-command.line, ch: curserStart.ch - prefix.length };
139+
const pre = editor.getRange(preStart, curserStart);
140+
141+
if (pre == prefix.trimStart()) {
142+
const sufEnd ={ line: curserStart.line+command.line, ch: curserEnd.ch + suffix.length };
143+
const suf = editor.getRange(curserEnd, sufEnd);
144+
if (suf == suffix.trimEnd()) {
145+
editor.replaceRange(selectedText, preStart,sufEnd); // codeblock leave blank lines
146+
return setCursor(-1);
147+
}
148+
}
149+
editor.replaceSelection(`${prefix}${selectedText}${suffix}`);
150+
return setCursor(1);
151+
};
152+
153+
type commandPlot = {
154+
char: number;
155+
line: number;
156+
prefix: string;
157+
suffix: string;
132158
};
133159

134160
type commandsPlot = {
135-
[key: string]: {
136-
replacement: (selectedText: string) => string;
137-
char: number;
138-
line: number;
139-
};
161+
[key: string]: commandPlot;
140162
};
141163

142164
const commandsMap: commandsPlot = {
143165
underline: {
144-
replacement: (selectedText) =>
145-
applyCommand("<u>", selectedText, "</u>"),
146166
char: 3,
147167
line: 0,
168+
prefix: "<u>",
169+
suffix: "</u>",
148170
},
149171
superscript: {
150-
replacement: (selectedText) =>
151-
applyCommand("<sup>", selectedText, "</sup>"),
152172
char: 5,
153173
line: 0,
174+
prefix: "<sup>",
175+
suffix: "</sup>",
154176
},
155177
subscript: {
156-
replacement: (selectedText) =>
157-
applyCommand("<sub>", selectedText, "</sub>"),
158178
char: 5,
159179
line: 0,
180+
prefix: "<sub>",
181+
suffix: "</sub>",
160182
},
161183
codeblock: {
162-
replacement: (selectedText) =>
163-
applyCommand("\n```\n", selectedText, "\n```\n"),
164184
char: 5,
165185
line: 1,
186+
prefix: "\n```\n",
187+
suffix: "\n```\n",
166188
},
167189
};
168190

@@ -177,25 +199,7 @@ export default class cMenuPlugin extends Plugin {
177199
if (activeLeaf) {
178200
const view = activeLeaf;
179201
const editor = view.editor;
180-
const selection = editor.getSelection();
181-
const curserStart = editor.getCursor("from");
182-
const curserEnd = editor.getCursor("to");
183-
if (selection) {
184-
editor.replaceSelection(commandsMap[type].replacement(selection));
185-
editor.setCursor(
186-
curserStart.line + commandsMap[type].line,
187-
curserEnd.ch + commandsMap[type].char
188-
);
189-
} else {
190-
editor.replaceRange(
191-
commandsMap[type].replacement(selection),
192-
curserStart
193-
);
194-
editor.setCursor(
195-
curserStart.line + commandsMap[type].line,
196-
curserEnd.ch + commandsMap[type].char
197-
);
198-
}
202+
applyCommand(commandsMap[type], editor);
199203
await wait(10);
200204
//@ts-ignore
201205
this.app.commands.executeCommandById("editor:focus");

0 commit comments

Comments
 (0)