xdan / jodit

Jodit - Best WYSIWYG Editor for You
https://xdsoft.net/jodit/
MIT License
1.61k stars 341 forks source link

Paste value with set limitChars #1140

Open pluderes opened 1 month ago

pluderes commented 1 month ago

Jodit Version: 1.3.39

Browser: Chrome/IE/Safary/FF OS: Windows/Mac/Linux Is React App: True Reproduced on xdsoft.net:

Hello, thanks for created Jodit. I have an issue when i use event paste. I set limitChars = 2000. when i have 1998 chars in text editor and i want paste a string with length > 2, jodit will remove it and keep prev value

Expected behavior: Accept paste value to fit limitChars Actual behavior: Remove value want to paste, keep prev value

xdan commented 4 weeks ago

I think this is a difficult task to accomplish. Imagine that when you insert, you will have a table of ten cells with one character in each cell. Limit 8. How will Jodit decide what to delete? Cells? Entire row or table? Maybe the whole column.

pluderes commented 4 weeks ago

In my case. I create a func in event onChange. In this func, I use for loop to check each char input value and a few var like this:

This is my func:

function truncateContent(s, maxLength) { let isTag = false; let result = ''; let charCount = 0; let isSpecialChar = false;

for (let i = 0; i < s.length; i++) {
  if (s[i] === '<') {
    isTag = true;
  }

  if (s[i] === '&' && s[i + 5] === ';') {
    isSpecialChar = true;
  }

  if (s[i] === '>') {
    isTag = false;
  }

  if (!isTag && !isSpecialChar && s[i] !== ' ' && s[i] !== " " && s[i] !== ">") charCount++;

  if (s[i] === ';' && s[i - 5] === '&') {
    isSpecialChar = false;
  }

  if (charCount <= maxLength) result += s[i];

  if (charCount > maxLength) break;
}

if (isTag) result += '>';
return result

}