Add delete line command

This commit is contained in:
ipshing 2024-03-04 13:12:27 -08:00
parent b99c9373c5
commit 1199bf17c0

View file

@ -64,7 +64,7 @@ export default class TextTransform extends Plugin {
// Add command for selecting a word
this.addCommand({
id: "select-word",
name: "Select Word",
name: "Select word",
editorCallback: (editor) => {
if (editor.hasFocus()) {
const info = this.getSelectionInfo(editor);
@ -74,7 +74,7 @@ export default class TextTransform extends Plugin {
});
this.addCommand({
id: "select-word-ignore",
name: "Select Word (Ignore Boundary Characters setting)",
name: "Select word (ignore boundary characters setting)",
editorCallback: (editor) => {
if (editor.hasFocus()) {
const info = this.getSelectionInfo(editor, true);
@ -83,6 +83,32 @@ export default class TextTransform extends Plugin {
},
});
// Text manipulation
this.addCommand({
id: "delete-line",
name: "Delete line",
editorCallback: async (editor, context) => {
if (editor.hasFocus()) {
// Get cursor position for selection
const from = editor.getCursor("from");
const to = editor.getCursor("to");
// Determine new cursor position
const newCursor: EditorPosition = { ch: editor.getCursor("head").ch, line: from.line };
if (editor.getLine(to.line + 1).length < newCursor.ch) {
newCursor.ch = editor.getLine(to.line + 1).length;
}
// Set selection to encompass entire line(s)
from.ch = 0;
to.ch = editor.getLine(to.line).length;
// Replace with empty text then delete the line
editor.replaceRange("", from, to);
editor.exec("deleteLine");
// Set cursor
editor.setCursor(newCursor);
}
},
});
console.log("Text Transform plugin loaded");
}