Skip to content

Build your personal knowledge base with Trilium Notes

feat(share): limit the width of text notes, unless #fullContentWidth

Elian Doran37c9eea1ab5e
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/UI Elements/Content width.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/UI Elements/Content width.html
index f8acb62476..787b60e497 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/UI Elements/Content width.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/UI Elements/Content width.html
@@ -28,4 +28,9 @@
default, such as the&nbsp;<a class="reference-link" href="#root/_help_grjYqerjn243">Canvas</a>.
In that case the <em>Full width</em> toggle will not be displayed and the
label will have no effect.</p>
-</aside>
\ No newline at end of file
+</aside>
+<h2>Shared notes</h2>
+<p>On a <a href="#root/_help_R9pX4DGra2Vt">shared page</a>, a text note is
+ shown at a fixed reading width, whatever the width configured in Options.
+ A note with <code>fullContentWidth</code> uses the whole width of the page
+ there too.</p>
\ No newline at end of file
diff --git a/docs/User Guide/User Guide/Basic Concepts and Features/UI Elements/Content width.md b/docs/User Guide/User Guide/Basic Concepts and Features/UI Elements/Content width.md
index 795543f596..72e4fbd376 100644
--- a/docs/User Guide/User Guide/Basic Concepts and Features/UI Elements/Content width.md
+++ b/docs/User Guide/User Guide/Basic Concepts and Features/UI Elements/Content width.md
@@ -19,4 +19,8 @@ For notes with large elements such as <a class="reference-link" href="../../Not
* Or manually apply the `fullContentWidth` [label](../../Advanced%20Usage/Attributes/Labels.md) to the note.
> [!NOTE]
-> Some [note types](../../Note%20Types.md) are full width by default, such as the <a class="reference-link" href="../../Note%20Types/Canvas.md">Canvas</a>. In that case the _Full width_ toggle will not be displayed and the label will have no effect.
\ No newline at end of file
+> Some [note types](../../Note%20Types.md) are full width by default, such as the <a class="reference-link" href="../../Note%20Types/Canvas.md">Canvas</a>. In that case the _Full width_ toggle will not be displayed and the label will have no effect.
+
+## Shared notes
+
+On a [shared page](../../Advanced%20Usage/Sharing.md), a text note is shown at a fixed reading width, whatever the width configured in Options. A note with `fullContentWidth` uses the whole width of the page there too.
\ No newline at end of file
diff --git a/packages/share-theme/src/model/page.spec.ts b/packages/share-theme/src/model/page.spec.ts
index f4c6744331..8f69bfecdf 100644
--- a/packages/share-theme/src/model/page.spec.ts
+++ b/packages/share-theme/src/model/page.spec.ts
@@ -359,8 +359,8 @@ describe("getLastUpdated", () => {
});
describe("getContentClasses", () => {
- const classes = (type: string, mime: string, isEmpty = false) =>
- getContentClasses(fakeNote({ noteId: "page", type, mime }), isEmpty);
+ const classes = (type: string, mime: string, isEmpty = false, labels: Record<string, string> = {}) =>
+ getContentClasses(fakeNote({ noteId: "page", type, mime, labels }), isEmpty);
it("styles text and Markdown notes as the editor's content, and marks empty content", () => {
expect(classes("text", "text/html")).toBe("type-text ck-content");
@@ -368,6 +368,13 @@ describe("getContentClasses", () => {
expect(classes("code", "application/javascript")).toBe("type-code");
expect(classes("book", "", true)).toBe("type-book no-content");
});
+
+ it("marks content as full width with #fullContentWidth, as the app does", () => {
+ expect(classes("text", "text/html", false, { fullContentWidth: "" }))
+ .toBe("type-text ck-content full-content-width");
+ expect(classes("text", "text/html", false, { fullContentWidth: "false" }))
+ .toBe("type-text ck-content");
+ });
});
describe("getChildLinks", () => {
@@ -431,6 +438,7 @@ function fakeNote(options: FakeNoteOptions): FakeNote {
getIcon: (prefixes) => [ options.icon ?? "bx bx-note", ...prefixes ?? [] ].join(" "),
getLabelValue: (name) => labels[name] ?? null,
hasLabel: (name) => name in labels,
+ isLabelTruthy: (name) => name in labels && labels[name] !== "false",
getRelationValue: (name) => relations[name] ?? null,
hasRelation: (name) => name in relations,
getRelations: (name) => (name === "shareHtml" ? options.snippets ?? [] : [])
diff --git a/packages/share-theme/src/model/page.ts b/packages/share-theme/src/model/page.ts
index a78186f49b..3324d2905b 100644
--- a/packages/share-theme/src/model/page.ts
+++ b/packages/share-theme/src/model/page.ts
@@ -16,6 +16,8 @@ export interface ShareNote {
utcDateModified?: string;
getLabelValue(name: string): string | null | undefined;
hasLabel(name: string): boolean;
+ /** Whether the label is set to anything but `false`. */
+ isLabelTruthy(name: string): boolean;
getRelationValue(name: string): string | null | undefined;
hasRelation(name: string): boolean;
getRelations(name: string): { targetNote?: ShareNote | null }[];
@@ -328,7 +330,8 @@ export function getShareLink(note: ShareNote, sanitizeUrl: (url: string) => stri
/**
* Returns the classes of the content element: the note's type, `ck-content` for content the text
- * editor's styles apply to (text notes and Markdown code notes), and `no-content` when empty.
+ * editor's styles apply to (text notes and Markdown code notes), `full-content-width` for a note
+ * with `#fullContentWidth`, as in the app, and `no-content` when empty.
*/
export function getContentClasses(note: ShareNote, isEmpty = false) {
const isEditorContent = note.type === "text"
@@ -336,6 +339,7 @@ export function getContentClasses(note: ShareNote, isEmpty = false) {
return [
`type-${note.type}`,
isEditorContent && "ck-content",
+ note.isLabelTruthy("fullContentWidth") && "full-content-width",
isEmpty && "no-content"
].filter(Boolean).join(" ");
}
diff --git a/packages/share-theme/src/page/layout.css b/packages/share-theme/src/page/layout.css
index a182608bac..6971ca87ce 100644
--- a/packages/share-theme/src/page/layout.css
+++ b/packages/share-theme/src/page/layout.css
@@ -68,6 +68,13 @@ body {
}
}
+/* A text note reads at a limited width, as in the app, unless it has `#fullContentWidth`. */
+#content.ck-content:not(.full-content-width),
+#content.ck-content:not(.full-content-width) ~ * {
+ max-width: 48rem;
+ margin-inline: auto;
+}
+
#mobile-backdrop {
display: none;
}
diff --git a/packages/trilium-core/src/share/content_renderer.spec.ts b/packages/trilium-core/src/share/content_renderer.spec.ts
index c9cbb633a0..72b0cd2e55 100644
--- a/packages/trilium-core/src/share/content_renderer.spec.ts
+++ b/packages/trilium-core/src/share/content_renderer.spec.ts
@@ -1146,6 +1146,27 @@ describe("content_renderer", () => {
}
});
+ describe("Content width", () => {
+ it("marks a page with #fullContentWidth, inherited or its own, for the full width", () => {
+ const shareRootNote = buildShareNote({
+ id: shareRoot.SHARE_ROOT_NOTE_ID,
+ children: [
+ { id: "cappedPage", content: "<p>a</p>" },
+ { "id": "widePage", "content": "<p>b</p>", "#fullContentWidth": "" },
+ { "id": "narrowPage", "content": "<p>c</p>", "#fullContentWidth": "false" }
+ ]
+ });
+ const contentClasses = (note: SNote) => parse(String(renderNoteContent(note)))
+ .querySelector("#content")?.classList.value ?? [];
+
+ const [ capped, wide, narrow ] = shareRootNote.getChildNotes().map(contentClasses);
+ expect(capped).toContain("ck-content");
+ expect(capped).not.toContain("full-content-width");
+ expect(wide).toContain("full-content-width");
+ expect(narrow).not.toContain("full-content-width");
+ });
+ });
+
describe("Site logo", () => {
it("shows the site root's note icon, or the ~shareLogo image when there is one", () => {
const shareRootNote = buildShareNote({