Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/link editor: enable the submit button only when a link can be created

Adorian Doran73d772f69f09
diff --git a/apps/client/src/widgets/dialogs/edit_link.spec.tsx b/apps/client/src/widgets/dialogs/edit_link.spec.tsx
index 5a77d3d446..eed48811ef 100644
--- a/apps/client/src/widgets/dialogs/edit_link.spec.tsx
+++ b/apps/client/src/widgets/dialogs/edit_link.spec.tsx
@@ -326,6 +326,29 @@ describe("EditLinkDialog", () => {
expect(field("url")?.value).toBe("https://pasted.example");
});
+ it("enables its button only once the side shown has a note, an address or an anchor", async () => {
+ const button = () => container.querySelector<HTMLButtonElement>(".modal-footer button");
+ await open({ anchors: [ "intro" ] });
+ expect(button()?.disabled).toBe(true);
+ const note = buildNote({ title: "Note" });
+ await act(async () => autocomplete.onChange?.({ notePath: `root/${note.noteId}` }));
+ expect(button()?.disabled).toBe(false);
+ await act(async () => autocomplete.onChange?.(null));
+ expect(button()?.disabled).toBe(true);
+
+ await act(async () => choice()[0]?.click());
+ expect(button()?.disabled).toBe(true);
+ await type(field("url"), "example.com");
+ expect(button()?.disabled).toBe(false);
+ await type(field("url"), " ");
+ expect(button()?.disabled).toBe(true);
+
+ await act(async () => choice()[2]?.click());
+ expect(button()?.disabled).toBe(true);
+ await act(async () => anchorRows()[0]?.click());
+ expect(button()?.disabled).toBe(false);
+ });
+
it("focuses the field of the side shown, except on mobile, where it waits for a tap", async () => {
async function openAndShow() {
await open();
@@ -735,10 +758,13 @@ describe("EditLinkBalloon", () => {
expect(url).not.toBeNull();
expect(document.activeElement).toBe(url);
+ const button = form.querySelector<HTMLButtonElement>(".edit-link-actions button");
+ expect(button?.disabled).toBe(true);
await submit(form);
expect(onClose).not.toHaveBeenCalled();
await type(url, "example.com");
+ expect(button?.disabled).toBe(false);
await submit(form);
expect(onClose).toHaveBeenCalledOnce();
expect(addLink)
diff --git a/apps/client/src/widgets/dialogs/edit_link.tsx b/apps/client/src/widgets/dialogs/edit_link.tsx
index af387daef2..c3b3b49aa4 100644
--- a/apps/client/src/widgets/dialogs/edit_link.tsx
+++ b/apps/client/src/widgets/dialogs/edit_link.tsx
@@ -142,7 +142,9 @@ export default function EditLinkDialog() {
size="lg"
title={form.title}
helpPageId="QEAPj01N5f7w"
- footer={<Button text={form.buttonText} keyboardShortcut="Enter" />}
+ footer={
+ <Button text={form.buttonText} keyboardShortcut="Enter" disabled={!form.hasLink} />
+ }
onSubmit={onSubmit}
onShown={form.onShown}
onHidden={() => {
@@ -187,7 +189,7 @@ export function EditLinkBalloon({ opts, onClose }: { opts: EditLinkOpts; onClose
>
{form.body}
<div className="edit-link-actions">
- <Button text={form.buttonText} keyboardShortcut="Enter" />
+ <Button text={form.buttonText} keyboardShortcut="Enter" disabled={!form.hasLink} />
</div>
</form>
);
@@ -584,7 +586,10 @@ function useEditLinkForm(opts: EditLinkOpts | undefined) {
return {
title,
buttonText,
- hasLink: target === "anchor" ? !!heading || !!anchor : !!suggestion,
+ /** Whether the side shown has a link to apply: an address, a note, a heading or an anchor. */
+ hasLink: target === "url" ? !!suggestion?.externalLink
+ : target === "note" ? !!suggestion?.notePath
+ : !!heading || !!anchor,
/** Fills and focuses the fields, once the form is on screen. */
onShown: () => setIsShowPending(true),
applyLink,