Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only close annotation form on focus loss when empty #4958

Merged
merged 2 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export class AnnotationForm extends watchMixin(ShadowlessLitElement) {
@property({ state: true })
_savedAnnotationTitle: string;
@property({ state: true })
_savedAnnotationSearchInput = "";
@property({ state: true })
saveAnnotation = false;

get savedAnnotationTitle(): string {
Expand All @@ -69,6 +71,15 @@ export class AnnotationForm extends watchMixin(ShadowlessLitElement) {
},
savedAnnotationId: () => {
this._savedAnnotationId = this.savedAnnotationId || "";
},
saveAnnotation: () => {
this.listenForCloseIfEmpty();
},
_annotationText: () => {
this.listenForCloseIfEmpty();
},
_savedAnnotationSearchInput: () => {
this.listenForCloseIfEmpty();
}
};

Expand Down Expand Up @@ -108,13 +119,23 @@ export class AnnotationForm extends watchMixin(ShadowlessLitElement) {
}
}

connectedCallback(): void {
super.connectedCallback();
if (!this.annotationText) {
get isEmpty(): boolean {
return this._annotationText.length === 0 && !this.saveAnnotation && this._savedAnnotationSearchInput.length === 0;
}

listenForCloseIfEmpty(): void {
if (this.isEmpty) {
this.listenForClose();
} else {
this.stopListeningForClose();
}
}

connectedCallback(): void {
super.connectedCallback();
this.listenForCloseIfEmpty();
}

disconnectedCallback(): void {
super.disconnectedCallback();
this.stopListeningForClose();
Expand All @@ -125,15 +146,11 @@ export class AnnotationForm extends watchMixin(ShadowlessLitElement) {
this._annotationText = e.detail.text;
}
this._savedAnnotationId = e.detail.id;
this._savedAnnotationSearchInput = e.detail.title;
}

handleTextInput(): void {
this._annotationText = this.inputRef.value.value;
if (this._annotationText.length > 0) {
this.stopListeningForClose();
} else {
this.listenForClose();
}
}

handleCancel(): void {
Expand Down
27 changes: 27 additions & 0 deletions test/javascript/code_listing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,30 @@ test("click on comment button", async () => {
await userEvent.click(annotationButton);
expect(document.querySelectorAll("d-annotation-form").length).toBe(1);
});

test("empty form should close on click outside", async () => {
codeListing.initAnnotateButtons();
await nextFrame();
expect(document.querySelectorAll("d-annotation-form").length).toBe(0);
const annotationButton: HTMLButtonElement = document.querySelector(".annotation-button .btn");
await userEvent.click(annotationButton);
expect(document.querySelectorAll("d-annotation-form").length).toBe(1);
await userEvent.click(document.body);
expect(document.querySelectorAll("d-annotation-form").length).toBe(0);
});

test("form should not close when it has content", async () => {
codeListing.initAnnotateButtons();
await nextFrame();
expect(document.querySelectorAll("d-annotation-form").length).toBe(0);
const annotationButton: HTMLButtonElement = document.querySelector(".annotation-button .btn");
await userEvent.click(annotationButton);
expect(document.querySelectorAll("d-annotation-form").length).toBe(1);
const textarea: HTMLTextAreaElement = document.querySelector("d-annotation-form textarea");
await userEvent.type(textarea, "This is a test");
await userEvent.click(document.body);
expect(document.querySelectorAll("d-annotation-form").length).toBe(1);
await userEvent.clear(textarea);
await userEvent.click(document.body);
expect(document.querySelectorAll("d-annotation-form").length).toBe(0);
});