Skip to content

Build your personal knowledge base with Trilium Notes

fix(ai-assistant): open as a prompt and settle the layout when the run is asked for

Elian Doranc1ee147d4b73
diff --git a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_form.spec.ts b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_form.spec.ts
index 2c83e1f2da..832a0cc5e3 100644
--- a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_form.spec.ts
+++ b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_form.spec.ts
@@ -20,18 +20,57 @@ afterEach(() => {
});
describe("AiAssistantFormView", () => {
- it("starts in the prompt phase with the strip on show but inert", () => {
+ /** Whether a row of the form is on show, by the class the template hides it with. */
+ function rowShown(selector: string): boolean {
+ const row = form.element?.querySelector(selector);
+ return !!row && !row.classList.contains("ck-hidden");
+ }
+
+ it("starts as a prompt and nothing else", () => {
const form = makeForm();
expect(form.phase).toBe("prompt");
- // The strip holds its place from the start — appearing with the response would shift
- // every row beneath it — so nothing in it is hidden, only disabled.
- for (const view of [form.resultToggleView, form.changesToggleView, form.tryAgainButtonView]) {
+ // Nothing to review yet, so the review's rows are not there to be reviewed with.
+ expect(rowShown(".ck-ai-assistant-form__preview-toolbar")).toBe(false);
+ expect(rowShown(".ck-ai-assistant-form__actions")).toBe(false);
+ expect(rowShown(".ck-ai-assistant-form__prompt-row")).toBe(true);
+
+ // Without the reset exemption, the body wrapper's `.ck-reset_all` nowraps every paragraph.
+ expect(form.previewView.element?.classList.contains("ck-reset_all-excluded")).toBe(true);
+ });
+
+ it("settles its layout when the run is asked for, not when it lands", () => {
+ const form = makeForm();
+ form.beginStreaming();
+
+ // Everything the review needs arrives at once, with the keystroke that asked for it, and
+ // is inert until there is something to use it on. Nothing may appear later: the wait for
+ // the first chunk, and the moment it lands, both have to leave the rows where they are.
+ expect(rowShown(".ck-ai-assistant-form__preview-toolbar")).toBe(true);
+ expect(rowShown(".ck-ai-assistant-form__actions")).toBe(true);
+
+ const reviewViews = [
+ form.resultToggleView,
+ form.changesToggleView,
+ form.tryAgainButtonView,
+ form.insertBelowButtonView,
+ form.replaceButtonView
+ ];
+ for (const view of reviewViews) {
expect(view.isVisible).toBe(true);
expect(view.isEnabled).toBe(false);
}
- // Without the reset exemption, the body wrapper's `.ck-reset_all` nowraps every paragraph.
- expect(form.previewView.element?.classList.contains("ck-reset_all-excluded")).toBe(true);
+
+ form.setPreview("<p>new</p>");
+ form.enterReview({ hasContent: true });
+
+ expect(rowShown(".ck-ai-assistant-form__preview-toolbar")).toBe(true);
+ expect(rowShown(".ck-ai-assistant-form__actions")).toBe(true);
+ for (const view of reviewViews) {
+ expect(view.isVisible).toBe(true);
+ }
+ expect(form.insertBelowButtonView.isEnabled).toBe(true);
+ expect(form.replaceButtonView.isEnabled).toBe(true);
});
it("streams into the preview and always shows the raw result while streaming", () => {
diff --git a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_form.ts b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_form.ts
index c1e0747aed..4105a20a35 100644
--- a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_form.ts
+++ b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_form.ts
@@ -114,7 +114,7 @@ export default class AiAssistantFormView extends View {
// Takes Send's place in the prompt row while a run is in flight: the control that acts on
// the run belongs where the eye already is, and one icon replacing another keeps the row
// from resizing as the phase turns over.
- this.stopButtonView = this._createActionButton(locale, t("Stop"), "streaming", "stop");
+ this.stopButtonView = this._createActionButton(locale, t("Stop"), "streaming", "stop", "isVisible");
this.stopButtonView.set({ icon: IconStop, withText: false, tooltip: true });
this.stopButtonView.class = "ck-button-action ck-ai-assistant-form__icon-action";
this.replaceButtonView = this._createActionButton(locale, t("Replace"), "review", "replace");
@@ -152,8 +152,18 @@ export default class AiAssistantFormView extends View {
{
// The preview's own strip: the Result/Changes toggle on the left, "Try again"
// pushed to the right. The feature's title belongs to the dialog's header.
+ //
+ // Absent until a run is asked for — before that the form is a prompt and
+ // nothing else — and present unchanged from then on, so the only thing that
+ // moves once the response is on its way is the preview filling up.
tag: "div",
- attributes: { class: ["ck", "ck-ai-assistant-form__preview-toolbar"] },
+ attributes: {
+ class: [
+ "ck",
+ "ck-ai-assistant-form__preview-toolbar",
+ bind.if("phase", "ck-hidden", (phase) => phase === "prompt")
+ ]
+ },
children: [
{
// The two modes are one choice, so they share a container the host can
@@ -223,8 +233,17 @@ export default class AiAssistantFormView extends View {
children: [this.promptInputView, this.sendButtonView, this.stopButtonView]
},
{
+ // Reserved from the moment a run is asked for, its buttons inert until there is
+ // something to commit: appearing at the end of a run would drop the dialog's
+ // last row in under the reader mid-read.
tag: "div",
- attributes: { class: ["ck", "ck-ai-assistant-form__actions"] },
+ attributes: {
+ class: [
+ "ck",
+ "ck-ai-assistant-form__actions",
+ bind.if("phase", "ck-hidden", (phase) => phase === "prompt")
+ ]
+ },
children: [
// The row is pushed to the end, so "Replace" comes last to land where the
// eye finishes and the primary action is expected.
@@ -408,10 +427,21 @@ export default class AiAssistantFormView extends View {
}
/** A text action button that is only visible in the given phase and re-emits `eventName`. */
- private _createActionButton(locale: Locale, label: string, phase: AiAssistantFormPhase, eventName: string) {
+ /**
+ * A text button that fires `eventName` when the form reaches `phase`. Gated by enablement, so
+ * the row it sits in keeps its height across the phase it belongs to — `isVisible` is for a
+ * button that shares its slot with another, which only "Stop" does.
+ */
+ private _createActionButton(
+ locale: Locale,
+ label: string,
+ phase: AiAssistantFormPhase,
+ eventName: string,
+ gate: "isVisible" | "isEnabled" = "isEnabled"
+ ) {
const button = new ButtonView(locale);
button.set({ label, withText: true });
- button.bind("isVisible").to(this, "phase", (currentPhase) => currentPhase === phase);
+ button.bind(gate).to(this, "phase", (currentPhase) => currentPhase === phase);
button.on("execute", () => this.fire(eventName));
return button;
}
diff --git a/packages/ckeditor5/src/plugins/ai_assistant/theme/ai_assistant.css b/packages/ckeditor5/src/plugins/ai_assistant/theme/ai_assistant.css
index 035ba3f0f0..ca74599693 100644
--- a/packages/ckeditor5/src/plugins/ai_assistant/theme/ai_assistant.css
+++ b/packages/ckeditor5/src/plugins/ai_assistant/theme/ai_assistant.css
@@ -134,11 +134,6 @@
white-space: nowrap;
}
-/* An actions row with every button hidden should not reserve a gap's worth of height. */
-.ck.ck-ai-assistant-form .ck-ai-assistant-form__actions:not(:has(.ck-button:not(.ck-hidden))) {
- display: none;
-}
-
/* The inline diff shown by the "Changes" view (htmldiff-js `ins`/`del` markup, the same
convention the revisions dialog styles). Colors are self-contained rather than borrowing the
client's Bootstrap variables, so the plugin package stays host-agnostic. */