Project

General

Profile

Actions

Defect #43662

closed

Cursor may move to incorrect position when pasting inline images from clipboard

Added by Yasu Saku 21 days ago. Updated 5 days ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Text formatting
Target version:
Start date:
Due date:
% Done:

0%

Estimated time:
Resolution:
Fixed
Affected version:

Description

Issue

In the addInlineAttachmentMarkup() function in attachments.js, there is a race condition where the cursor is moved before the image markup from getInlineAttachmentMarkup() is actually inserted into the textarea.

Root Cause

The getInlineAttachmentMarkup() function returns a Promise. However, the cursor movement code is placed outside the .then() callback, causing it to execute immediately without waiting for the promise to resolve:

getInlineAttachmentMarkup(file)
  .then(imageMarkup => {
    $textarea.val(...); // Asynchronous operation
  })

// This executes BEFORE the above promise resolves
cursorPosition = $textarea.prop('selectionStart');
$textarea.prop({
  'selectionStart': cursorPosition + 1,
  'selectionEnd': cursorPosition + 1
});

Impact

  • The cursor is positioned before the image markup is inserted
  • The cursor position is calculated from the unchanged textarea value
  • Image insertion appears broken or cursor behavior is inconsistent

Solution

Move the cursor movement code inside the .then() callback and calculate the correct position based on the inserted markup length:

getInlineAttachmentMarkup(file)
  .then(imageMarkup => {
    $textarea.val(
      description.substring(0, cursorPosition)
      + (newLineBefore ? '\n' : '')
      + imageMarkup
      + (newLineAfter ? '\n' : '')
      + description.substring(cursorPosition, description.length)
    );

    // Move cursor after the inserted markup
    var newCursorPosition = cursorPosition + (newLineBefore ? 1 : 0) + imageMarkup.length;
    $textarea.prop({
      'selectionStart': newCursorPosition + 1,
      'selectionEnd': newCursorPosition + 1
    });
  })

I attached a patch for the latest commit to the trunk (source:/trunk@24291).


Files


Related issues

Related to Redmine - Feature #38504: Display pasted images in appropriate size on HiDPI displaysClosedGo MAEDA

Actions
Actions #1

Updated by Go MAEDA 20 days ago

  • Related to Feature #38504: Display pasted images in appropriate size on HiDPI displays added
Actions #2

Updated by Go MAEDA 20 days ago

  • Category changed from Wiki to Text formatting
  • Target version set to 6.1.2

This issue was introduced by #38504. Setting the target version to 6.1.2.

Actions #3

Updated by Go MAEDA 8 days ago

  • Subject changed from Race condition in addInlineAttachmentMarkup causes cursor movement before image markup is inserted to Cursor may move to incorrect position when pasting inline images from clipboard
  • Status changed from New to Resolved
  • Assignee set to Go MAEDA
  • Resolution set to Fixed

Committed the patch in r24352. Thank you.

Actions #4

Updated by Go MAEDA 5 days ago

  • Status changed from Resolved to Closed

Merged the fix into 6.1-stable in r24372.

Actions #5

Updated by Yasu Saku 5 days ago

Go MAEDA wrote in #note-4:

Merged the fix into 6.1-stable in r24372.

Thank you so match.

Actions

Also available in: Atom PDF