Repository: excalidraw/excalidraw
File: src/element/textElement.ts (TypeScript)

Code before:
  originalLines.forEach((originalLine) => {
    const words = originalLine.split(" ");
    // This means its newline so push it
    if (words.length === 1 && words[0] === "") {
      lines.push(words[0]);
      return; // continue
    }
    let currentLine = "";
    let currentLineWidthTillNow = 0;

    let index = 0;
    while (index < words.length) {
      const currentWordWidth = getLineWidth(words[index], font);

      // Start breaking longer words exceeding max width
      if (currentWordWidth >= maxWidth) {
        // push current line since the current word exceeds the max width
        // so will be appended in next line
        push(currentLine);
        currentLine = "";
        currentLineWidthTillNow = 0;
        while (Array.from(words[index]).length > 0) {
          const currentChar = Array.from(words[index])[0];
          const width = charWidth.calculate(currentChar, font);
          currentLineWidthTillNow += width;
          words[index] = Array.from(words[index]).slice(1).join("");

          if (currentLineWidthTillNow >= maxWidth) {
            // only remove last trailing space which we have added when joining words
            if (currentLine.slice(-1) === " ") {
              currentLine = currentLine.slice(0, -1);
            }
            push(currentLine);
            currentLine = currentChar;
            currentLineWidthTillNow = width;
          } else {
            currentLine += currentChar;
          }
        }
        // push current line if appending space exceeds max width
        if (currentLineWidthTillNow + spaceWidth >= maxWidth) {
          push(currentLine);
          currentLine = "";
          currentLineWidthTillNow = 0;
        } else {
          // space needs to be appended before next word
          // as currentLine contains chars which couldn't be appended
          // to previous line
          currentLine += " ";
          currentLineWidthTillNow += spaceWidth;
        }

Review comment (suggestion):
```suggestion
          words[index] = words[index].slice(currentChar.length);
```

Code after:
  originalLines.forEach((originalLine) => {
    const words = originalLine.split(" ");
    // This means its newline so push it
    if (words.length === 1 && words[0] === "") {
      lines.push(words[0]);
      return; // continue
    }
    let currentLine = "";
    let currentLineWidthTillNow = 0;

    let index = 0;
    while (index < words.length) {
      const currentWordWidth = getLineWidth(words[index], font);

      // Start breaking longer words exceeding max width
      if (currentWordWidth >= maxWidth) {
        // push current line since the current word exceeds the max width
        // so will be appended in next line
        push(currentLine);
        currentLine = "";
        currentLineWidthTillNow = 0;
        while (words[index].length > 0) {
          const currentChar = String.fromCodePoint(
            words[index].codePointAt(0)!,
          );
          const width = charWidth.calculate(currentChar, font);
          currentLineWidthTillNow += width;
          words[index] = words[index].slice(currentChar.length);

          if (currentLineWidthTillNow >= maxWidth) {
            // only remove last trailing space which we have added when joining words
            if (currentLine.slice(-1) === " ") {
              currentLine = currentLine.slice(0, -1);
            }
            push(currentLine);
            currentLine = currentChar;
            currentLineWidthTillNow = width;
          } else {
            currentLine += currentChar;
          }
        }
        // push current line if appending space exceeds max width
        if (currentLineWidthTillNow + spaceWidth >= maxWidth) {
          push(currentLine);
          currentLine = "";
          currentLineWidthTillNow = 0;
        } else {
          // space needs to be appended before next word
          // as currentLine contains chars which couldn't be appended
          // to previous line
          currentLine += " ";