Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 83270be

Browse files
committed
DEV: Follow-up small fixes to diff-streamer
1 parent 11e90f5 commit 83270be

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

assets/javascripts/discourse/components/modal/diff-modal.gjs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,21 @@ import concatClass from "discourse/helpers/concat-class";
1212
import { ajax } from "discourse/lib/ajax";
1313
import { popupAjaxError } from "discourse/lib/ajax-error";
1414
import { bind } from "discourse/lib/decorators";
15+
import { escapeExpression } from "discourse/lib/utilities";
1516
import { i18n } from "discourse-i18n";
1617
import DiffStreamer from "../../lib/diff-streamer";
1718
import SmoothStreamer from "../../lib/smooth-streamer";
1819
import AiIndicatorWave from "../ai-indicator-wave";
1920

2021
export default class ModalDiffModal extends Component {
2122
@service currentUser;
23+
@service siteSettings;
2224
@service messageBus;
2325

2426
@tracked loading = false;
2527
@tracked finalResult = "";
26-
@tracked diffStreamer = new DiffStreamer(this.args.model.selectedText);
28+
@tracked selectedText = escapeExpression(this.args.model.selectedText);
29+
@tracked diffStreamer = new DiffStreamer(this.selectedText);
2730
@tracked suggestion = "";
2831
@tracked
2932
smoothStreamer = new SmoothStreamer(
@@ -36,6 +39,16 @@ export default class ModalDiffModal extends Component {
3639
this.suggestChanges();
3740
}
3841

42+
get diffResult() {
43+
if (this.diffStreamer.diff?.length > 0) {
44+
return this.diffStreamer.diff;
45+
}
46+
47+
// Prevents flash by showing the
48+
// original text when the diff is empty
49+
return this.selectedText;
50+
}
51+
3952
get isStreaming() {
4053
return this.diffStreamer.isStreaming || this.smoothStreamer.isStreaming;
4154
}
@@ -93,7 +106,7 @@ export default class ModalDiffModal extends Component {
93106
data: {
94107
location: "composer",
95108
mode: this.args.model.mode,
96-
text: this.args.model.selectedText,
109+
text: this.selectedText,
97110
custom_prompt: this.args.model.customPromptValue,
98111
force_default_locale: true,
99112
},
@@ -109,7 +122,7 @@ export default class ModalDiffModal extends Component {
109122

110123
if (this.suggestion) {
111124
this.args.model.toolbarEvent.replaceText(
112-
this.args.model.selectedText,
125+
this.selectedText,
113126
this.suggestion
114127
);
115128
}
@@ -119,21 +132,23 @@ export default class ModalDiffModal extends Component {
119132
? this.finalResult
120133
: this.diffStreamer.suggestion;
121134
if (this.args.model.showResultAsDiff && finalResult) {
122-
this.args.model.toolbarEvent.replaceText(
123-
this.args.model.selectedText,
124-
finalResult
125-
);
135+
this.args.model.toolbarEvent.replaceText(this.selectedText, finalResult);
126136
}
127137
}
128138

129139
<template>
140+
{{log this.prefersRichEditor}}
130141
<DModal
131142
class="composer-ai-helper-modal"
132143
@title={{i18n "discourse_ai.ai_helper.context_menu.changes"}}
133144
@closeModal={{@closeModal}}
134145
>
135146
<:body>
136-
<div {{didInsert this.subscribe}} {{willDestroy this.unsubscribe}}>
147+
<div
148+
{{didInsert this.subscribe}}
149+
{{willDestroy this.unsubscribe}}
150+
class="text-preview"
151+
>
137152
{{#if this.loading}}
138153
<div class="composer-ai-helper-modal__loading">
139154
{{~@model.selectedText~}}
@@ -149,9 +164,7 @@ export default class ModalDiffModal extends Component {
149164
}}
150165
>
151166
{{~#if @model.showResultAsDiff~}}
152-
<span class="diff-inner">{{htmlSafe
153-
this.diffStreamer.diff
154-
}}</span>
167+
<span class="diff-inner">{{htmlSafe this.diffResult}}</span>
155168
{{else}}
156169
{{#if this.smoothStreamer.isStreaming}}
157170
<CookText

assets/javascripts/discourse/lib/diff-streamer.gjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default class DiffStreamer {
1111
@tracked isStreaming = false;
1212
@tracked words = [];
1313
@tracked lastResultText = "";
14-
@tracked diff = "";
14+
@tracked diff = this.selectedText;
1515
@tracked suggestion = "";
1616
@tracked isDone = false;
1717
@tracked isThinking = false;

assets/stylesheets/modules/ai-helper/common/ai-helper.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
.composer-ai-helper-modal {
22
.text-preview,
33
.inline-diff {
4+
font-family: var(--d-font-family--monospace);
5+
font-variant-ligatures: none;
6+
47
ins {
58
background-color: var(--success-low);
69
text-decoration: none;

0 commit comments

Comments
 (0)