Repository: excalidraw/excalidraw
File: packages/excalidraw/components/App.tsx (TypeScript)

Code before:
      },
    );
  };

  /**
   * use during async image initialization,
   * when the placeholder image could have been modified in the meantime,
   * and when you don't want to loose those modifications
   */
  private getLatestInitializedImageElement = (
    imagePlaceholder: ExcalidrawImageElement,
    fileId: FileId,
  ) => {
    const sceneImageElement = this.scene.getElement(imagePlaceholder.id);
    const latestImageElement =
      sceneImageElement && sceneImageElement.version > imagePlaceholder.version
        ? sceneImageElement
        : imagePlaceholder;

    return newElementWith(
      latestImageElement as InitializedExcalidrawImageElement,
      {
        fileId,
      },
    );
  };

  /**
   * inserts image into elements array and rerenders
   */
  private insertImageElement = async (
    imageElement: ExcalidrawImageElement,
    imageFile: File,
  ) => {
    // we should be handling all cases upstream, but in case we forget to handle
    // a future case, let's throw here
    if (!this.isToolSupported("image")) {
      this.setState({ errorMessage: t("errors.imageToolNotSupported") });
      return;
    }

    this.scene.insertElement(imageElement);

    try {
      const initializedImageElement = await this.initializeImage(
        imageElement,
        imageFile,
      );

      const nextElements = this.scene
        .getElementsIncludingDeleted()

Review comment (suggestion):
this can be simplified to always take the scene element as it should never be older than the initial placeholder element AFAIK

(or if it could be older, then it'd mean we're updating the placeholder somewhere with some presumably meaningful data, and we would need to reconcile the scene & placeholder element to not lose either updates)

Code after:
      },
    );
  };

  /**
   * use during async image initialization,
   * when the placeholder image could have been modified in the meantime,
   * and when you don't want to loose those modifications
   */
  private getLatestInitializedImageElement = (
    imagePlaceholder: ExcalidrawImageElement,
    fileId: FileId,
  ) => {
    const latestImageElement =
      this.scene.getElement(imagePlaceholder.id) ?? imagePlaceholder;

    return newElementWith(
      latestImageElement as InitializedExcalidrawImageElement,
      {
        fileId,
      },
    );
  };

  /**
   * inserts image into elements array and rerenders
   */
  private insertImageElement = async (
    placeholderImageElement: ExcalidrawImageElement,
    imageFile: File,
  ) => {
    // we should be handling all cases upstream, but in case we forget to handle
    // a future case, let's throw here
    if (!this.isToolSupported("image")) {
      this.setState({ errorMessage: t("errors.imageToolNotSupported") });
      return;
    }

    this.scene.insertElement(placeholderImageElement);

    try {
      const initializedImageElement = await this.initializeImage(
        placeholderImageElement,
        imageFile,
      );

      const nextElements = this.scene
        .getElementsIncludingDeleted()
        .map((element) => {
          if (element.id === initializedImageElement.id) {
            return initializedImageElement;