Action

Move Line Down

Posted by Alexander, Last update over 1 year ago

Moves the selected line down while retaining cursor position.

Check out the complementary action Move Line Up.

Steps

  • script

    // Get info on and text of current line
    const [current_line_position] = editor.getSelectedRange()
    const [current_line_start, current_line_length] = editor.getSelectedLineRange()
    const cursor_line_offset = current_line_position - current_line_start
    let cursor_line_start = 0
    let current_line_text = editor.getTextInRange(current_line_start, current_line_length)
    
    // Check if current line == last line of draft
    if (current_line_text.length > 0 && current_line_text.endsWith('\n')) {
      // Select next line
      editor.setSelectedRange(current_line_start + current_line_length, 0)
    
      // Get info on and text of next line
      const [next_line_start, next_line_length] = editor.getSelectedLineRange()
      let next_line_text = editor.getTextInRange(next_line_start, next_line_length)
    
      // If the next line is last line of draft
      if (!next_line_text.endsWith('\n')) {
        // 1. Add line break to it
        next_line_text = next_line_text + '\n'
    
        if (current_line_text.endsWith('\n')) {
          // 2. Remove line break from current line
          current_line_text = current_line_text.substring(0, current_line_text.length - 1)
        }
    
        // Determine where "new" current line starts
        cursor_line_start = current_line_start + next_line_length + 1
      } else {
        cursor_line_start = current_line_start + next_line_length
      }
    
      // Swap content of both lines
      editor.setTextInRange(next_line_start, next_line_length, current_line_text)
      editor.setTextInRange(current_line_start, current_line_length, next_line_text)
    
      // Reset cursor to original position within line
      editor.setSelectedRange(cursor_line_start + cursor_line_offset, 0)
    }

Options

  • After Success Default
    Notification Error
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.