Skip to content

Build your personal knowledge base with Trilium Notes

feat(export): card-based export dialog with shared SelectableCard

Elian Dorana475090b7207
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index b8167e0d77..4910ca6c8a 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -128,20 +128,25 @@
"export": {
"export_note_title": "Export note",
"close": "Close",
- "export_type_subtree": "This note and all of its descendants",
- "format_html": "HTML - recommended as it keeps all format",
- "format_html_zip": "HTML in ZIP archive - this is recommended since this preserves all the formatting.",
- "format_markdown": "Markdown - this preserves most of the formatting.",
- "format_opml": "OPML - outliner interchange format for text only. Formatting, images and files are not included.",
+ "what_to_export": "What to export",
+ "export_type_subtree": "This note & all descendants",
+ "export_type_single": "Only this note",
+ "format": "Format",
+ "format_html_name": "HTML",
+ "format_html_description": "Keeps all formatting",
+ "format_markdown_name": "Markdown",
+ "format_markdown_description": "Preserves most formatting",
+ "format_opml_name": "OPML",
+ "format_opml_description": "Outline format, text only",
+ "format_share_name": "HTML for web",
+ "format_share_description": "Publishable static site using the shared-notes theme",
"opml_version_1": "OPML v1.0 - plain text only",
"opml_version_2": "OPML v2.0 - allows also HTML",
- "export_type_single": "Only this note without its descendants",
"export": "Export",
"choose_export_type": "Choose export type first please",
"export_in_progress": "Export in progress: {{progressCount}}",
"export_finished_successfully": "Export finished successfully.",
- "format_pdf": "PDF - for printing or sharing purposes.",
- "share-format": "HTML for web publishing - uses the same theme that is used shared notes, but can be published as a static website."
+ "format_pdf": "PDF - for printing or sharing purposes."
},
"help": {
"title": "Cheatsheet",
diff --git a/apps/client/src/widgets/dialogs/export.css b/apps/client/src/widgets/dialogs/export.css
index a0903b06c6..8a784e6d41 100644
--- a/apps/client/src/widgets/dialogs/export.css
+++ b/apps/client/src/widgets/dialogs/export.css
@@ -1,16 +1,23 @@
-.export-dialog form .form-check {
- padding-top: 10px;
- padding-bottom: 10px;
-}
+.modal.export-dialog {
+ .tn-card {
+ margin-bottom: 1rem;
+ }
-.export-dialog form .format-choice {
- padding-inline-start: 40px;
-}
+ .tn-card:last-child {
+ margin-bottom: 0;
+ }
-.export-dialog form .opml-versions {
- padding-inline-start: 60px;
-}
+ /* Two-segment toggle that fills the card width. */
+ .export-type-group {
+ display: flex;
+ width: 100%;
+ }
-.export-dialog form .form-check-label {
- padding: 2px;
-}
\ No newline at end of file
+ .export-type-group .btn {
+ flex: 1;
+ }
+
+ .opml-versions {
+ margin-top: 0.75em;
+ }
+}
diff --git a/apps/client/src/widgets/dialogs/export.tsx b/apps/client/src/widgets/dialogs/export.tsx
index 547ea712b7..c6bfe6ba92 100644
--- a/apps/client/src/widgets/dialogs/export.tsx
+++ b/apps/client/src/widgets/dialogs/export.tsx
@@ -9,10 +9,11 @@ import toastService, { type ToastOptionsWithRequiredId } from "../../services/to
import tree from "../../services/tree";
import utils, { isStandalone } from "../../services/utils";
import ws from "../../services/ws";
-import Button from "../react/Button";
-import FormRadioGroup from "../react/FormRadioGroup";
+import Button, { ButtonGroup } from "../react/Button";
+import { Card, CardSection } from "../react/Card";
import { useTriliumEvent } from "../react/hooks";
import Modal from "../react/Modal";
+import SelectableCard, { SelectableCardGrid } from "../react/SelectableCard";
interface ExportDialogProps {
branchId?: string | null;
@@ -20,9 +21,16 @@ interface ExportDialogProps {
defaultType?: "subtree" | "single";
}
+interface ExportFormat {
+ value: string;
+ name: string;
+ description: string;
+ icon: string;
+}
+
export default function ExportDialog() {
const [ opts, setOpts ] = useState<ExportDialogProps>();
- const [ exportType, setExportType ] = useState<string>(opts?.defaultType ?? "subtree");
+ const [ exportType, setExportType ] = useState<"subtree" | "single">("subtree");
const [ subtreeFormat, setSubtreeFormat ] = useState("html");
const [ singleFormat, setSingleFormat ] = useState("html");
const [ opmlVersion, setOpmlVersion ] = useState("2.0");
@@ -36,6 +44,7 @@ export default function ExportDialog() {
const branchId = await froca.getBranchId(parentNoteId, noteId);
+ setExportType(defaultType ?? "subtree");
setOpts({
noteTitle: noteId && await tree.getNoteTitle(noteId),
defaultType,
@@ -44,6 +53,10 @@ export default function ExportDialog() {
setShown(true);
});
+ const formats = exportType === "subtree" ? subtreeFormats : singleFormats;
+ const selectedFormat = exportType === "subtree" ? subtreeFormat : singleFormat;
+ const setSelectedFormat = exportType === "subtree" ? setSubtreeFormat : setSingleFormat;
+
return (
<Modal
className="export-dialog"
@@ -54,79 +67,63 @@ export default function ExportDialog() {
return;
}
- const format = (exportType === "subtree" ? subtreeFormat : singleFormat);
- const version = (format === "opml" ? opmlVersion : "1.0");
- exportBranch(opts.branchId, exportType, format, version);
+ const version = (selectedFormat === "opml" ? opmlVersion : "1.0");
+ exportBranch(opts.branchId, exportType, selectedFormat, version);
setShown(false);
}}
onHidden={() => setShown(false)}
footer={<Button className="export-button" text={t("export.export")} kind="primary" />}
show={shown}
>
-
- <FormRadioGroup
- name="export-type"
- currentValue={exportType} onChange={setExportType}
- values={[{
- value: "subtree",
- label: t("export.export_type_subtree")
- }]}
- />
-
- { exportType === "subtree" &&
- <div className="export-subtree-formats format-choice">
- <FormRadioGroup
- name="export-subtree-format"
- currentValue={subtreeFormat} onChange={setSubtreeFormat}
- values={[
- { value: "html", label: t("export.format_html_zip") },
- { value: "markdown", label: t("export.format_markdown") },
- !isStandalone && { value: "share", label: t("export.share-format") },
- { value: "opml", label: t("export.format_opml") }
- ]}
- />
-
- { subtreeFormat === "opml" &&
- <div className="opml-versions">
- <FormRadioGroup
- name="opml-version"
- currentValue={opmlVersion} onChange={setOpmlVersion}
- values={[
- { value: "1.0", label: t("export.opml_version_1") },
- { value: "2.0", label: t("export.opml_version_2") }
- ]}
+ <Card heading={t("export.what_to_export")}>
+ <CardSection>
+ <ButtonGroup className="export-type-group">
+ <button type="button" className={`btn btn-secondary ${exportType === "subtree" ? "active" : ""}`} onClick={() => setExportType("subtree")}>
+ {t("export.export_type_subtree")}
+ </button>
+ <button type="button" className={`btn btn-secondary ${exportType === "single" ? "active" : ""}`} onClick={() => setExportType("single")}>
+ {t("export.export_type_single")}
+ </button>
+ </ButtonGroup>
+ </CardSection>
+ </Card>
+
+ <Card heading={t("export.format")}>
+ <CardSection>
+ <SelectableCardGrid columns={2}>
+ {formats.map((format) => (
+ <SelectableCard
+ key={format.value} icon={format.icon} title={format.name} description={format.description}
+ selected={selectedFormat === format.value} onSelect={() => setSelectedFormat(format.value)}
/>
- </div>
- }
- </div>
- }
-
- <FormRadioGroup
- name="export-type"
- currentValue={exportType} onChange={setExportType}
- values={[{
- value: "single",
- label: t("export.export_type_single")
- }]}
- />
-
- { exportType === "single" &&
- <div class="export-single-formats format-choice">
- <FormRadioGroup
- name="export-single-format"
- currentValue={singleFormat} onChange={setSingleFormat}
- values={[
- { value: "html", label: t("export.format_html") },
- { value: "markdown", label: t("export.format_markdown") }
- ]}
- />
- </div>
- }
-
+ ))}
+ </SelectableCardGrid>
+
+ {selectedFormat === "opml" && (
+ <ButtonGroup className="opml-versions" size="sm">
+ <button type="button" className={`btn btn-secondary ${opmlVersion === "1.0" ? "active" : ""}`} onClick={() => setOpmlVersion("1.0")}>
+ {t("export.opml_version_1")}
+ </button>
+ <button type="button" className={`btn btn-secondary ${opmlVersion === "2.0" ? "active" : ""}`} onClick={() => setOpmlVersion("2.0")}>
+ {t("export.opml_version_2")}
+ </button>
+ </ButtonGroup>
+ )}
+ </CardSection>
+ </Card>
</Modal>
);
}
+const HTML_FORMAT: ExportFormat = { value: "html", name: t("export.format_html_name"), description: t("export.format_html_description"), icon: "bx bxl-html5" };
+const MARKDOWN_FORMAT: ExportFormat = { value: "markdown", name: t("export.format_markdown_name"), description: t("export.format_markdown_description"), icon: "bx bxl-markdown" };
+const SHARE_FORMAT: ExportFormat = { value: "share", name: t("export.format_share_name"), description: t("export.format_share_description"), icon: "bx bx-globe" };
+const OPML_FORMAT: ExportFormat = { value: "opml", name: t("export.format_opml_name"), description: t("export.format_opml_description"), icon: "bx bx-list-ul" };
+
+// `share` publishes a static site and only makes sense on a full server, not the standalone build.
+const subtreeFormats: ExportFormat[] = [HTML_FORMAT, MARKDOWN_FORMAT, ...(isStandalone ? [] : [SHARE_FORMAT]), OPML_FORMAT];
+const singleFormats: ExportFormat[] = [HTML_FORMAT, MARKDOWN_FORMAT];
+
function exportBranch(branchId: string, type: string, format: string, version: string) {
const taskId = utils.randomString(10);
const url = open.getUrlForDownload(`api/branches/${branchId}/export/${type}/${format}/${version}/${taskId}`);
diff --git a/apps/client/src/widgets/dialogs/import/import_dialog.css b/apps/client/src/widgets/dialogs/import/import_dialog.css
index 262e755f49..51f470e3c8 100644
--- a/apps/client/src/widgets/dialogs/import/import_dialog.css
+++ b/apps/client/src/widgets/dialogs/import/import_dialog.css
@@ -8,12 +8,6 @@
margin-bottom: 0;
}
- .import-provider-picker {
- display: grid;
- grid-template-columns: repeat(3, 1fr);
- gap: 0.75em;
- }
-
/* Local file import is grouped on its own, full-width, beneath the service grid. */
.import-provider-local {
display: flex;
@@ -47,65 +41,6 @@
margin-top: 0.4em;
}
- .import-provider-card {
- display: flex;
- align-items: center;
- gap: 0.75em;
- width: 100%;
- padding: 0.75em;
- text-align: left;
- border: 1px solid var(--main-border-color);
- border-radius: 8px;
- background: var(--accented-background-color);
- cursor: pointer;
- }
-
- .import-provider-card:hover {
- border-color: var(--main-text-color);
- }
-
- .import-provider-card.selected {
- border-color: var(--main-text-color);
- background: var(--active-item-background-color);
- color: var(--active-item-text-color);
- }
-
- .import-provider-card-icon {
- flex-shrink: 0;
- width: 1.5em;
- height: 1.5em;
- background-color: currentColor;
- -webkit-mask: var(--provider-icon) center / contain no-repeat;
- mask: var(--provider-icon) center / contain no-repeat;
- }
-
- /* Boxicons-based card icon (used by providers that supply `icon` instead of `iconUrl`). */
- .import-provider-card-bxicon {
- flex-shrink: 0;
- font-size: 1.5em;
- line-height: 1;
- }
-
- .import-provider-card-text {
- display: flex;
- flex-direction: column;
- min-width: 0;
- }
-
- .import-provider-card-name {
- font-weight: bold;
- }
-
- .import-provider-card-description {
- color: var(--muted-text-color);
- font-size: 0.85em;
- }
-
- .import-provider-card.selected .import-provider-card-description {
- color: inherit;
- opacity: 0.8;
- }
-
.import-provider-hint {
color: var(--muted-text-color);
text-align: center;
diff --git a/apps/client/src/widgets/dialogs/import/import_dialog.tsx b/apps/client/src/widgets/dialogs/import/import_dialog.tsx
index 0e0d0a3ae3..e51215666e 100644
--- a/apps/client/src/widgets/dialogs/import/import_dialog.tsx
+++ b/apps/client/src/widgets/dialogs/import/import_dialog.tsx
@@ -8,8 +8,8 @@ import tree from "../../../services/tree.js";
import { Card, CardSection } from "../../react/Card.js";
import { useTriliumEvent } from "../../react/hooks.js";
import Modal from "../../react/Modal.js";
+import SelectableCard, { SelectableCardGrid } from "../../react/SelectableCard.js";
import { importProviders } from "./index.js";
-import type { ImportProvider } from "./types.js";
/**
* Unified import dialog. The top shows a picker of registered {@link importProviders} as selectable
@@ -59,16 +59,16 @@ export default function ImportDialog() {
>
<Card heading={t("import.import_from")}>
<CardSection>
- <div className="import-provider-picker">
+ <SelectableCardGrid>
{serviceProviders.map((p) => (
- <ImportProviderCard key={p.id} provider={p} selected={p.id === providerId} onSelect={() => setProviderId(p.id)} />
+ <SelectableCard key={p.id} iconUrl={p.iconUrl} icon={p.icon} title={p.name} description={p.description} selected={p.id === providerId} onSelect={() => setProviderId(p.id)} />
))}
- </div>
+ </SelectableCardGrid>
{localProviders.length > 0 && (
<div className="import-provider-local">
{localProviders.map((p) => (
- <ImportProviderCard key={p.id} provider={p} selected={p.id === providerId} onSelect={() => setProviderId(p.id)} />
+ <SelectableCard key={p.id} iconUrl={p.iconUrl} icon={p.icon} title={p.name} description={p.description} selected={p.id === providerId} onSelect={() => setProviderId(p.id)} />
))}
</div>
)}
@@ -87,17 +87,3 @@ export default function ImportDialog() {
const serviceProviders = importProviders.filter((p) => p.group !== "local");
const localProviders = importProviders.filter((p) => p.group === "local");
const defaultProviderId = (localProviders[0] ?? importProviders[0])?.id;
-
-function ImportProviderCard({ provider, selected, onSelect }: { provider: ImportProvider; selected: boolean; onSelect: () => void }) {
- return (
- <button type="button" className={`import-provider-card ${selected ? "selected" : ""}`} onClick={onSelect}>
- {provider.iconUrl
- ? <span className="import-provider-card-icon" style={{ "--provider-icon": `url("${provider.iconUrl}")` }} />
- : <span className={`import-provider-card-bxicon ${provider.icon ?? ""}`} />}
- <span className="import-provider-card-text">
- <span className="import-provider-card-name">{provider.name}</span>
- <span className="import-provider-card-description">{provider.description}</span>
- </span>
- </button>
- );
-}
diff --git a/apps/client/src/widgets/react/SelectableCard.css b/apps/client/src/widgets/react/SelectableCard.css
new file mode 100644
index 0000000000..90ef117707
--- /dev/null
+++ b/apps/client/src/widgets/react/SelectableCard.css
@@ -0,0 +1,64 @@
+.selectable-card-grid {
+ display: grid;
+ grid-template-columns: repeat(var(--selectable-card-columns, 3), 1fr);
+ gap: 0.75em;
+}
+
+.selectable-card {
+ display: flex;
+ align-items: center;
+ gap: 0.75em;
+ width: 100%;
+ padding: 0.75em;
+ text-align: left;
+ border: 1px solid var(--main-border-color);
+ border-radius: 8px;
+ background: var(--accented-background-color);
+ cursor: pointer;
+}
+
+.selectable-card:hover {
+ border-color: var(--main-text-color);
+}
+
+.selectable-card.selected {
+ border-color: var(--main-text-color);
+ background: var(--active-item-background-color);
+ color: var(--active-item-text-color);
+}
+
+.selectable-card-icon {
+ flex-shrink: 0;
+ width: 1.5em;
+ height: 1.5em;
+ background-color: currentColor;
+ -webkit-mask: var(--card-mask) center / contain no-repeat;
+ mask: var(--card-mask) center / contain no-repeat;
+}
+
+/* Boxicons-based card icon (used when `icon` is supplied instead of `iconUrl`). */
+.selectable-card-bxicon {
+ flex-shrink: 0;
+ font-size: 1.5em;
+ line-height: 1;
+}
+
+.selectable-card-text {
+ display: flex;
+ flex-direction: column;
+ min-width: 0;
+}
+
+.selectable-card-name {
+ font-weight: bold;
+}
+
+.selectable-card-description {
+ color: var(--muted-text-color);
+ font-size: 0.85em;
+}
+
+.selectable-card.selected .selectable-card-description {
+ color: inherit;
+ opacity: 0.8;
+}
diff --git a/apps/client/src/widgets/react/SelectableCard.tsx b/apps/client/src/widgets/react/SelectableCard.tsx
new file mode 100644
index 0000000000..ccd82509da
--- /dev/null
+++ b/apps/client/src/widgets/react/SelectableCard.tsx
@@ -0,0 +1,43 @@
+import "./SelectableCard.css";
+
+import type { ComponentChildren } from "preact";
+
+export interface SelectableCardProps {
+ title: ComponentChildren;
+ description?: ComponentChildren;
+ /** URL of an SVG rendered monochrome via a CSS mask (e.g. an imported `*.svg?url`). Provide either this or {@link icon}. */
+ iconUrl?: string;
+ /** Boxicons class (e.g. `"bx bx-import"`) shown in place of {@link iconUrl}. */
+ icon?: string;
+ selected: boolean;
+ onSelect: () => void;
+ className?: string;
+}
+
+/**
+ * A selectable tile: an icon, a bold title and an optional description, with a highlighted "selected"
+ * state. Used by the import and export dialogs to pick a provider/format. Lay several out together with
+ * {@link SelectableCardGrid}.
+ */
+export default function SelectableCard({ title, description, iconUrl, icon, selected, onSelect, className }: SelectableCardProps) {
+ return (
+ <button type="button" className={`selectable-card ${selected ? "selected" : ""} ${className ?? ""}`} onClick={onSelect}>
+ {iconUrl
+ ? <span className="selectable-card-icon" style={{ "--card-mask": `url("${iconUrl}")` }} />
+ : icon ? <span className={`selectable-card-bxicon ${icon}`} /> : null}
+ <span className="selectable-card-text">
+ <span className="selectable-card-name">{title}</span>
+ {description && <span className="selectable-card-description">{description}</span>}
+ </span>
+ </button>
+ );
+}
+
+/** Lays out {@link SelectableCard}s in an even grid. `columns` defaults to 3. */
+export function SelectableCardGrid({ columns, className, children }: { columns?: number; className?: string; children: ComponentChildren }) {
+ return (
+ <div className={`selectable-card-grid ${className ?? ""}`} style={columns ? { "--selectable-card-columns": columns } : undefined}>
+ {children}
+ </div>
+ );
+}