Skip to content

Commit e264572

Browse files
authored
FIX: Closing AI menu stops post audio from playing (#1369)
## 🔍 Overview When you have a post with audio being played and you open and close the AI post helper menu, it re-renders the entire post DOM, causing the audio to be interrupted and stop playing. The reason for this is because we highlight the selected text when opening the AI post helper menu and we replace the entire post back with the original post HTML when closing the menu. This fix ensures that we do not re-render the entire post DOM and instead only remove the highlighted section that was added. ## 🔗 Context https://meta.discourse.org/t/ai-helper-interrupting-uploaded-mp3-audio-stream/366817?u=keegan
1 parent 70b0db2 commit e264572

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

assets/javascripts/discourse/connectors/post-text-buttons/ai-post-helper-trigger.gjs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export default class AiPostHelperTrigger extends Component {
1919
@tracked menuState = this.MENU_STATES.triggers;
2020
@tracked showMainButtons = true;
2121
@tracked showAiButtons = true;
22-
@tracked originalPostHTML = null;
2322
@tracked postHighlighted = false;
2423
@tracked currentMenu = this.menu.getByIdentifier(
2524
"post-text-selection-toolbar"
@@ -45,7 +44,6 @@ export default class AiPostHelperTrigger extends Component {
4544
return;
4645
}
4746

48-
this.originalPostHTML = postElement.innerHTML;
4947
this.selectedText = this.args.outletArgs.data.quoteState.buffer;
5048

5149
const selection = window.getSelection();
@@ -121,7 +119,15 @@ export default class AiPostHelperTrigger extends Component {
121119
return;
122120
}
123121

124-
postElement.innerHTML = this.originalPostHTML;
122+
const highlightedSpans = postElement.querySelectorAll(
123+
"span.ai-helper-highlighted-selection"
124+
);
125+
126+
highlightedSpans.forEach((span) => {
127+
const textNode = document.createTextNode(span.textContent);
128+
span.parentNode.replaceChild(textNode, span);
129+
});
130+
125131
this.postHighlighted = false;
126132
}
127133

0 commit comments

Comments
 (0)