Skip to content

Build your personal knowledge base with Trilium Notes

fix(text): restore RTL direction for read-only notes

Elian Doran477e2fbe8e67
diff --git a/apps/client/src/widgets/react/RawHtml.spec.tsx b/apps/client/src/widgets/react/RawHtml.spec.tsx
new file mode 100644
index 0000000000..9ad3b4d145
--- /dev/null
+++ b/apps/client/src/widgets/react/RawHtml.spec.tsx
@@ -0,0 +1,49 @@
+import { render } from "preact";
+import { afterEach, beforeEach, describe, expect, it } from "vitest";
+
+import RawHtml, { RawHtmlBlock } from "./RawHtml";
+
+let container: HTMLDivElement;
+
+beforeEach(() => {
+ container = document.createElement("div");
+ document.body.appendChild(container);
+});
+
+afterEach(() => {
+ render(null, container);
+ container.remove();
+});
+
+describe("RawHtml", () => {
+ it("renders the HTML and every declared attribute onto the element", () => {
+ render(
+ <RawHtmlBlock
+ className="ck-content"
+ html="<p>שלום</p>"
+ dir="rtl"
+ tabindex={100}
+ style={{ color: "red" }}
+ />,
+ container
+ );
+
+ const el = container.querySelector("div");
+ expect(el?.innerHTML).toBe("<p>שלום</p>");
+ expect(el?.className).toBe("ck-content");
+ // `dir` and `tabindex` are declared on RawHtmlProps but were once dropped by the
+ // props destructure, which silently disabled RTL layout for read-only text notes.
+ expect(el?.getAttribute("dir")).toBe("rtl");
+ expect(el?.getAttribute("tabindex")).toBe("100");
+ expect(el?.style.color).toBe("red");
+ });
+
+ it("omits the optional attributes when they are not given", () => {
+ render(<RawHtml html="<b>hi</b>" />, container);
+
+ const el = container.querySelector("span");
+ expect(el?.innerHTML).toBe("<b>hi</b>");
+ expect(el?.hasAttribute("dir")).toBe(false);
+ expect(el?.hasAttribute("tabindex")).toBe(false);
+ });
+});
diff --git a/apps/client/src/widgets/react/RawHtml.tsx b/apps/client/src/widgets/react/RawHtml.tsx
index 502fc56f5d..a6e251425b 100644
--- a/apps/client/src/widgets/react/RawHtml.tsx
+++ b/apps/client/src/widgets/react/RawHtml.tsx
@@ -18,12 +18,14 @@ export function RawHtmlBlock({containerRef, ...props}: RawHtmlProps & { containe
return <div ref={containerRef} {...getProps(props)} />;
}
-function getProps({ className, html, style, onClick }: RawHtmlProps) {
+function getProps({ className, html, style, onClick, dir, tabindex }: RawHtmlProps) {
return {
className,
dangerouslySetInnerHTML: getHtml(html ?? ""),
style,
- onClick
+ onClick,
+ dir,
+ tabindex
};
}
diff --git a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx
index e4638867d6..5ed15d1f5e 100644
--- a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx
@@ -36,8 +36,13 @@ vi.mock("@triliumnext/ckeditor5", () => ({}));
vi.stubGlobal("logError", vi.fn());
vi.stubGlobal("logInfo", vi.fn());
-function setupHarness({ isVisible: initialIsVisible }: { isVisible: boolean }) {
- const note = buildNote({ title: "Big note", type: "text", content: "<p>hello</p>" });
+function setupHarness({ isVisible: initialIsVisible, language }: { isVisible: boolean; language?: string }) {
+ const note = buildNote({
+ title: "Big note",
+ type: "text",
+ content: "<p>hello</p>",
+ ...(language ? { "#language": language } : {})
+ });
// Count blob fetches without changing what they resolve to.
const originalGetBlob = note.getBlob.bind(note);
@@ -171,3 +176,38 @@ describe("ReadOnlyText reacting to content changes (#10575)", () => {
expect(harness.getBlobSpy).toHaveBeenCalledTimes(2);
});
});
+
+/**
+ * The direction is resolved from the note's `#language` label and handed to the content
+ * element as `dir`, which is what the RTL rules in the content stylesheets key off (they
+ * match on the `.ck-content` ancestor carrying it). The editor sets it via CKEditor's own
+ * `language.content` config, so a read-only note that drops it flips back to LTR the moment
+ * editing is disabled.
+ */
+describe("ReadOnlyText text direction", () => {
+ let cleanupContainer: HTMLElement | undefined;
+
+ afterEach(() => {
+ if (cleanupContainer) {
+ render(null, cleanupContainer);
+ cleanupContainer.remove();
+ cleanupContainer = undefined;
+ }
+ });
+
+ async function mountWithLanguage(language?: string) {
+ const harness = setupHarness({ isVisible: true, language });
+ cleanupContainer = harness.container;
+ await harness.mount();
+ return harness.container.querySelector(".note-detail-readonly-text-content");
+ }
+
+ it("marks the content right-to-left for a note in an RTL language", async () => {
+ expect((await mountWithLanguage("he"))?.getAttribute("dir")).toBe("rtl");
+ });
+
+ it("marks the content left-to-right otherwise", async () => {
+ expect((await mountWithLanguage("en"))?.getAttribute("dir")).toBe("ltr");
+ expect((await mountWithLanguage(undefined))?.getAttribute("dir")).toBe("ltr");
+ });
+});