From 2f7251224723d6e789f0074d75984ef117a88b63 Mon Sep 17 00:00:00 2001 From: chris-doucette-stack Date: Wed, 23 Jul 2025 15:19:25 -0400 Subject: [PATCH 1/3] Fixing bug thats preventing snippets from rerendering on run button click --- .../stack-snippets/src/snippet-view.ts | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/plugins/official/stack-snippets/src/snippet-view.ts b/plugins/official/stack-snippets/src/snippet-view.ts index b6f10136..fba7fa54 100644 --- a/plugins/official/stack-snippets/src/snippet-view.ts +++ b/plugins/official/stack-snippets/src/snippet-view.ts @@ -131,13 +131,7 @@ export class StackSnippetView implements NodeView { //Update the reference used by buttons, etc. for the most up-to-date node reference this.node = node; - //Check to see if the metadata has changed - const updatedMeta = getSnippetMetadata(node); - const metaChanged = - JSON.stringify(updatedMeta) !== - JSON.stringify(this.snippetMetadata); - this.snippetMetadata = updatedMeta; - + this.snippetMetadata = getSnippetMetadata(node); if (this.snippetMetadata.hide === "true") { // Update the visibility of the snippet-code div and toggle link const snippetCode = this.contentDOM; @@ -159,7 +153,6 @@ export class StackSnippetView implements NodeView { : "svg-icon-bg iconArrowRightSm"; } - // Update the result container if metadata has changed const content = this.contentNode; //Show the results, if the node meta allows it @@ -231,7 +224,7 @@ export class StackSnippetView implements NodeView { } //Re-run execution the snippet if something has changed, or we don't yet have a result - if (content && (metaChanged || this.resultContainer.innerHTML === "")) { + if (content && (this.hasContentNodeChanged() || this.resultContainer.innerHTML === "")) { this.hideButton.classList.remove("d-none"); //Clear the node this.resultContainer.innerHTML = ""; @@ -256,6 +249,7 @@ export class StackSnippetView implements NodeView { private readonly getPos: () => number; private snippetMetadata: SnippetMetadata; private contentNode: Node; + private contentNodeSnapshot: string; private showButton: HTMLButtonElement; private hideButton: HTMLButtonElement; private fullscreenButton: HTMLButtonElement; @@ -275,6 +269,19 @@ export class StackSnippetView implements NodeView { "h-screen", ]; + private hasContentNodeChanged(): boolean { + if (this.contentNode?.nodeType !== Node.DOCUMENT_NODE) { + return false; + } + + const documentInnerHtml = (this.contentNode as Document).documentElement.innerHTML; + var hasChanged = documentInnerHtml !== this.contentNodeSnapshot; + if (hasChanged) { + this.contentNodeSnapshot = documentInnerHtml; + } + return hasChanged; + } + private buildRunButton(container: HTMLDivElement): void { const runCodeButton = document.createElement("button"); runCodeButton.type = "button"; From 1a3d7f7430d0002a5624efc68f103efaf121a951 Mon Sep 17 00:00:00 2001 From: chris-doucette-stack Date: Wed, 23 Jul 2025 15:57:17 -0400 Subject: [PATCH 2/3] npm format --- plugins/official/stack-snippets/src/snippet-view.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/plugins/official/stack-snippets/src/snippet-view.ts b/plugins/official/stack-snippets/src/snippet-view.ts index fba7fa54..742c2739 100644 --- a/plugins/official/stack-snippets/src/snippet-view.ts +++ b/plugins/official/stack-snippets/src/snippet-view.ts @@ -224,7 +224,11 @@ export class StackSnippetView implements NodeView { } //Re-run execution the snippet if something has changed, or we don't yet have a result - if (content && (this.hasContentNodeChanged() || this.resultContainer.innerHTML === "")) { + if ( + content && + (this.hasContentNodeChanged() || + this.resultContainer.innerHTML === "") + ) { this.hideButton.classList.remove("d-none"); //Clear the node this.resultContainer.innerHTML = ""; @@ -274,8 +278,9 @@ export class StackSnippetView implements NodeView { return false; } - const documentInnerHtml = (this.contentNode as Document).documentElement.innerHTML; - var hasChanged = documentInnerHtml !== this.contentNodeSnapshot; + const documentInnerHtml = (this.contentNode as Document).documentElement + .innerHTML; + const hasChanged = documentInnerHtml !== this.contentNodeSnapshot; if (hasChanged) { this.contentNodeSnapshot = documentInnerHtml; } From a1d09cba9b48f3fda073195df4b35d4d165f9623 Mon Sep 17 00:00:00 2001 From: Chris Doucette Date: Wed, 23 Jul 2025 16:18:57 -0400 Subject: [PATCH 3/3] Create changeset file --- .changeset/few-points-clean.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/few-points-clean.md diff --git a/.changeset/few-points-clean.md b/.changeset/few-points-clean.md new file mode 100644 index 00000000..e9576fde --- /dev/null +++ b/.changeset/few-points-clean.md @@ -0,0 +1,5 @@ +--- +"@stackoverflow/stacks-editor": patch +--- + +Fixes bug thats prevents snippets from rerendering on run button click