Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/text editor/link editor: tweak UI
Adorian Doran505d96613b97
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 92eb16de54..416d8f9159 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -67,7 +67,7 @@"button_edit_link": "Save link","button_fix_link": "Fix link","web_link": "Web link",- "url": "URL",+ "url": "Address (URL)","mirror_title": "Mirror the note's title","mirror_title_description": "The link shows the note's current title, and changes when the note is renamed.","display_as": "Display as",diff --git a/apps/client/src/widgets/dialogs/add_link.spec.tsx b/apps/client/src/widgets/dialogs/add_link.spec.tsxindex 01b418c212..971e6e6c55 100644--- a/apps/client/src/widgets/dialogs/add_link.spec.tsx+++ b/apps/client/src/widgets/dialogs/add_link.spec.tsx@@ -144,6 +144,8 @@ describe("AddLinkDialog", () => {expect(segmented?.nextElementSibling?.classList.contains("tn-card")).toBe(true);expect(choice().map((button) => button.textContent?.trim())).toStrictEqual([ "add_link.web_link", "add_link.note" ]);+ expect(choice()[1]?.querySelector(".tn-svg-icon svg")).not.toBeNull();+ expect(choice()[1]?.querySelector(".bx")).toBeNull();expect(current()).toBe("add_link.note");expect(container.querySelector(".note-autocomplete")).not.toBeNull();@@ -160,7 +162,8 @@ describe("AddLinkDialog", () => {await open();await act(async () => choice()[0]?.click());await type(field("url"), "https://trilium.cc");- expect(nested("text")).toBe(true);+ expect(nested("text")).toBe(false);+ expect(container.querySelector(".add-link-url + .add-link-text")).not.toBeNull();expect(stacked()).toStrictEqual([]);await type(field("text"), "Trilium");await submit();diff --git a/apps/client/src/widgets/dialogs/add_link.tsx b/apps/client/src/widgets/dialogs/add_link.tsxindex 4a9655b9a8..0e67cbd53f 100644--- a/apps/client/src/widgets/dialogs/add_link.tsx+++ b/apps/client/src/widgets/dialogs/add_link.tsx@@ -1,6 +1,7 @@import "./add_link.css";import type { LinkDisplayMode } from "@triliumnext/ckeditor5";+import internalLinkIcon from "@triliumnext/ckeditor5/src/icons/trilium.svg?raw";import { ALLOWED_PROTOCOLS, isHttpUrl } from "@triliumnext/commons";import { useEffect, useRef, useState } from "preact/hooks";@@ -460,7 +461,7 @@ function useAddLinkForm(opts: AddLinkOpts | undefined) {className="add-link-target"options={[{ value: "url", label: t("add_link.web_link"), icon: "bx-globe" },- { value: "note", label: t("add_link.note"), icon: "bx-note" }+ { value: "note", label: t("add_link.note"), iconSvg: internalLinkIcon }]}currentValue={target}onChange={(next) => changeTarget(next)}@@ -472,8 +473,6 @@ function useAddLinkForm(opts: AddLinkOpts | undefined) {<><OptionCardSectionclassName="add-link-url" name="url" label={t("add_link.url")}- subSectionsVisible={showsWebLinkTitle}- subSections={titleSection}><FormTextBoxinputRef={webLinkRef}@@ -487,6 +486,8 @@ function useAddLinkForm(opts: AddLinkOpts | undefined) {/></OptionCardSection>+ {showsWebLinkTitle && titleSection}+{showsDisplay && (<OptionCardSectionclassName="add-link-display" name="display"diff --git a/apps/client/src/widgets/react/Button.tsx b/apps/client/src/widgets/react/Button.tsxindex a3b85aa1a3..35f25e28f0 100644--- a/apps/client/src/widgets/react/Button.tsx+++ b/apps/client/src/widgets/react/Button.tsx@@ -10,7 +10,7 @@ import { isDesktop, isMobile } from "../../services/utils";import ActionButton from "./ActionButton";import Dropdown from "./Dropdown";import { useStaticTooltip } from "./hooks";-import Icon from "./Icon";+import Icon, { SvgIcon } from "./Icon";import { useCommandTrigger } from "./react_utils";import { renderShortcutKbds } from "./shortcut_kbd";@@ -24,6 +24,8 @@ export interface ButtonProps extends Pick<ButtonHTMLAttributes<HTMLButtonElementtext: string | ComponentChildren;className?: string;icon?: string;+ /** The SVG of an icon drawn in place of {@link icon}, such as a CKEditor toolbar item's. */+ iconSvg?: string;keyboardShortcut?: string;/*** Called when the button is clicked. If not set, the button will submit the form (if any).@@ -45,15 +47,16 @@ export interface ButtonProps extends Pick<ButtonHTMLAttributes<HTMLButtonElement"aria-pressed"?: boolean;}-function Button({ name, buttonRef, className, text, onClick, keyboardShortcut, icon, kind, disabled, disabledTooltip, size, style, triggerCommand, ...restProps }: ButtonProps) {+function Button({ name, buttonRef, className, text, onClick, keyboardShortcut, icon, iconSvg, kind, disabled, disabledTooltip, size, style, triggerCommand, ...restProps }: ButtonProps) {const triggerOwnCommand = useCommandTrigger(triggerCommand);+ const hasIcon = !!icon || !!iconSvg;// Memoize classes array to prevent recreationconst classes = useMemo(() => {const classList: string[] = ["btn"];// Said here because a stylesheet cannot see it: the label is a text node, which no selector// reaches, so the spacing meant to part an icon from it has no way of knowing there is none.- if (icon && (text === undefined || text === null || text === "")) {+ if (hasIcon && (text === undefined || text === null || text === "")) {classList.push("tn-icon-only");}@@ -78,7 +81,7 @@ function Button({ name, buttonRef, className, text, onClick, keyboardShortcut, iclassList.push("btn-micro");}return classList.join(" ");- }, [kind, className, size, icon, text]);+ }, [kind, className, size, hasIcon, text]);// Memoize keyboard shortcut renderingconst shortcutElements = useMemo(() => {@@ -97,7 +100,7 @@ function Button({ name, buttonRef, className, text, onClick, keyboardShortcut, istyle={style}{...restProps}>- {icon && <Icon icon={`bx ${icon}`} />}+ {iconSvg ? <SvgIcon svg={iconSvg} /> : icon && <Icon icon={`bx ${icon}`} />}{text} {shortcutElements}</button>);diff --git a/apps/client/src/widgets/react/SegmentedChoice.spec.tsx b/apps/client/src/widgets/react/SegmentedChoice.spec.tsxindex d41886cb2f..bba6f911f5 100644--- a/apps/client/src/widgets/react/SegmentedChoice.spec.tsx+++ b/apps/client/src/widgets/react/SegmentedChoice.spec.tsx@@ -108,6 +108,26 @@ describe("SegmentedChoice", () => {expect(onChange).not.toHaveBeenCalled();});+ it("draws an icon from its SVG source, with or without a label", () => {+ const svg = "<svg viewBox=\"0 0 1 1\"></svg>";+ render(+ <SegmentedChoice+ options={[+ { value: "named" as const, label: "Named", iconSvg: svg },+ { value: "bare" as const, title: "Bare", iconSvg: svg }+ ]}+ currentValue="named" onChange={() => {}}+ />, container+ );++ const buttons = [ ...container.querySelectorAll("button") ];+ expect(buttons.map((button) => !!button.querySelector(".tn-svg-icon svg")))+ .toStrictEqual([ true, true ]);+ expect(container.querySelector(".bx")).toBeNull();+ expect(buttons.map((button) => button.classList.contains("tn-icon-only")))+ .toStrictEqual([ false, true ]);+ });+describe("collapsed onto a narrow screen", () => {const iconOptions = [{ value: "light" as const, label: "Light", icon: "bx-sun" },diff --git a/apps/client/src/widgets/react/SegmentedChoice.tsx b/apps/client/src/widgets/react/SegmentedChoice.tsxindex 747506a745..bfe966dec9 100644--- a/apps/client/src/widgets/react/SegmentedChoice.tsx+++ b/apps/client/src/widgets/react/SegmentedChoice.tsx@@ -14,6 +14,8 @@ export interface SegmentedChoiceOption<T extends string> {label?: string;/** An icon class without its `bx` prefix, e.g. `bx-square`. */icon?: string;+ /** The SVG of an icon drawn in place of {@link icon}, such as a CKEditor toolbar item's. */+ iconSvg?: string;/** What the option is called, for an option that shows as an icon alone. */title?: string;/**@@ -68,7 +70,7 @@ export default function SegmentedChoice<T extends string>({ options, currentValureturn (<ButtonGroup size="sm" className={clsx("tn-segmented-choice", className)}>- {options.map(({ value, label, icon, title, glyph, count }) => (+ {options.map(({ value, label, icon, iconSvg, title, glyph, count }) => (<Buttonkey={value}// Name and count share one inline box, rather than being two children of the@@ -83,6 +85,7 @@ export default function SegmentedChoice<T extends string>({ options, currentValu</span>) : (label ?? ""))}icon={icon}+ iconSvg={iconSvg}title={title}size="small"disabled={disabled}