This is bugging me. This is the second part of a two part action script— the first allows me to select a position in my current draft, this moves selected line(s) to the selected position. I want the moved line to be selected, but no matter what I’m doing, the cursor ends up at the end of the draft. I feel like I might be overlooking something obvious, just not sure what…
if (context.previewValues["selectedLine"] !== undefined) {
let selectedLine = JSON.parse(context.previewValues["selectedLine"]);
if (selectedLine) {
let targetLineIndex = parseInt(selectedLine.lineNumber);
// Get the text content of the draft
let content = draft.content;
let lines = content.split("\n");
// Get the current selection range
let selectionRange = editor.getSelectedRange();
let selectionStart = selectionRange[0];
let selectionEnd = selectionRange[0] + selectionRange[1];
// Find the start and end line indices
let textBeforeStart = content.substring(0, selectionStart);
let textBeforeEnd = content.substring(0, selectionEnd);
let startLineIndex = textBeforeStart.split('\n').length - 1;
let endLineIndex = textBeforeEnd.split('\n').length - 1;
// Only proceed if target is not within the selected range
if (!(targetLineIndex >= startLineIndex && targetLineIndex <= endLineIndex)) {
// save current version
draft.saveVersion()
// Extract the lines to move
let linesToMove = lines.slice(startLineIndex, endLineIndex + 1);
// Remove the lines from their current position
lines.splice(startLineIndex, endLineIndex - startLineIndex + 1);
// Adjust target index if we removed lines before it
let adjustedTargetIndex = targetLineIndex;
if (startLineIndex <= targetLineIndex) {
adjustedTargetIndex -= (endLineIndex - startLineIndex + 1);
}
// Insert the lines at the new position (after the target line)
lines.splice(adjustedTargetIndex + 1, 0, ...linesToMove);
// Update the draft content
draft.content = lines.join("\n");
// Calculate new cursor position (at the start of moved lines)
let linesBeforeMovedLines = lines.slice(0, adjustedTargetIndex + 1);
let newCursorPos = linesBeforeMovedLines.join("\n").length + 1;
// Calculate the length of the moved content for selection
let movedContent = linesToMove.join("\n");
// Select the entire moved block
editor.setSelectedRange(newCursorPos, movedContent.length);
}
} else {
context.fail("No heading was selected or passed correctly.");
}
}
else if (context.previewValues["moveToEnd"] !== undefined) {
let selectionData = JSON.parse(context.previewValues["moveToEnd"]);
let startLineIndex = parseInt(selectionData.startLine);
let endLineIndex = parseInt(selectionData.endLine);
let content = draft.content;
let lines = content.split('\n');
// Validate line indices
if (startLineIndex >= 0 && startLineIndex < lines.length &&
endLineIndex >= 0 && endLineIndex < lines.length &&
startLineIndex <= endLineIndex) {
// Save current version
draft.saveVersion();
// Extract the lines to move
let linesToMove = lines.slice(startLineIndex, endLineIndex + 1);
// Remove the lines from their current position
lines.splice(startLineIndex, endLineIndex - startLineIndex + 1);
// Trim empty lines from the end before appending
while (lines.length > 0 && lines[lines.length - 1].trim() === '') {
lines.pop();
}
// Append the moved lines to the end
lines.push(...linesToMove);
// Update the draft content
draft.content = lines.join('\n');
// Calculate new cursor position (at the start of moved lines at the end)
let linesBeforeMovedLines = lines.slice(0, lines.length - linesToMove.length);
let newCursorPos = linesBeforeMovedLines.length > 0 ?
linesBeforeMovedLines.join('\n').length + 1 : 0;
// Calculate the length of the moved content for selection
let movedContent = linesToMove.join('\n');
// Select the entire moved block
editor.setSelectedRange(newCursorPos, movedContent.length);
} else {
context.fail("Invalid line selection.");
}
}
draft.update();
editor.activate();