Skip to content

Conversation

@erickzhao
Copy link
Member

@erickzhao erickzhao commented Nov 18, 2025

Motivation

In its current state, Fiddle marks the current document as "edited" as soon as a change is made when the saved state applies. This means there's no going back to a "Saved" state even if you Undo all your changes.

Solution

This PR saves a Map of SHA-1 hashes generated via window.crypto.subtle.digest for each file and derives the isEdited property via a getter function instead:

public get isEdited() {
  if (this.savedHashes.size === 0) {
    return false;
  }

  if (this.savedHashes.size !== this.currentHashes.size) {
    return true;
  }
  for (const [id, hash] of this.currentHashes) {
    if (this.savedHashes.get(id) !== hash) return true;
  }
  return false;
}

This makes it so that:

  • isEdited state is more intuitive to users (prior to this, it was just "was this untouched since the last saved state?")
  • We also have access to the isEdited state for each file individually.

Reviewer notes

The diff here got pretty big, but the core logic just lives in src/renderer/editor-mosaic.ts. Most of the diff follows from that:

  • Because the digest function is asynchronous, anything that sets editor contents is now an async function.
  • I tried converting Enzyme tests to RTL as much as possible and reducing reliance on snapshot testing.

There's just one big ugly hack where the save state gets loaded on a timeout because it's a bit tricky to properly wait for when all content is being properly stored and to figure out when to save the hash:

// HACK: editors should be mounted shortly after we load something.
// We could try waiting for every single `editorDidMount` callback
// to fire, but that gets complicated with recycled editors with changed
// values. This is just easier for now.
await new Promise<void>((resolve) =>
  setTimeout(async () => {
    await this.markAsSaved();
    resolve();
  }, 100),
);

@erickzhao erickzhao requested review from a team and codebytere as code owners November 18, 2025 02:58
@coveralls
Copy link

coveralls commented Nov 18, 2025

Coverage Status

coverage: 79.741% (+0.1%) from 79.596%
when pulling 12ce68d on digest
into 10e6bd4 on main.

Copy link
Member

@dsanders11 dsanders11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave it for a follow-up PR, but we should build on this to fix bugs with the gist uploading. Currently if you just spam the "Update" button you'll upload a bunch of empty revisions to the gist because we unconditionally do updates and GitHub is happy to take them as empty commits it appears.

We'll want to update src/renderer/components/commands-action-button.tsx to only do the initial revision after publishing if the content has changed from the default (edge case), and also only update the gist if the content has changed.

We can also disable the "Update" button depending on the isEdited state so that it's clear when you actually have changes to upload to the gist.

@erickzhao erickzhao merged commit b93357f into main Dec 11, 2025
15 checks passed
@erickzhao erickzhao deleted the digest branch December 11, 2025 00:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants