Skip to content

Build your personal knowledge base with Trilium Notes

feat(text): adjustable tab width (closes #5701)

Elian Doran22eb2697d5c4
diff --git a/apps/client/src/stylesheets/theme-next/notes/text.css b/apps/client/src/stylesheets/theme-next/notes/text.css
index 252a3da6b2..8e0f1d195e 100644
--- a/apps/client/src/stylesheets/theme-next/notes/text.css
+++ b/apps/client/src/stylesheets/theme-next/notes/text.css
@@ -667,6 +667,10 @@ html .note-detail-editable-text :not(figure, .include-note, hr):first-child {
white-space: pre;
}
+.ck-content pre code {
+ tab-size: var(--code-block-tab-width, 4);
+}
+
.code-sample-wrapper .hljs {
transition: background-color linear 100ms;
}
diff --git a/apps/client/src/widgets/type_widgets/options/text_notes.tsx b/apps/client/src/widgets/type_widgets/options/text_notes.tsx
index 00c66d92b5..f6eed42348 100644
--- a/apps/client/src/widgets/type_widgets/options/text_notes.tsx
+++ b/apps/client/src/widgets/type_widgets/options/text_notes.tsx
@@ -295,7 +295,7 @@ function CodeBlockStyle() {
/>
</OptionsRow>
- <CodeBlockPreview theme={codeBlockTheme} wordWrap={codeBlockWordWrap} />
+ <CodeBlockPreview theme={codeBlockTheme} wordWrap={codeBlockWordWrap} tabWidth={codeBlockTabWidth} />
</OptionsSection>
);
}
@@ -317,7 +317,7 @@ function greet(times) {
}
`;
-function CodeBlockPreview({ theme, wordWrap }: { theme: string, wordWrap: boolean }) {
+function CodeBlockPreview({ theme, wordWrap, tabWidth }: { theme: string, wordWrap: boolean, tabWidth: string }) {
const [ code, setCode ] = useState<string>(SAMPLE_CODE);
useEffect(() => {
@@ -337,12 +337,11 @@ function CodeBlockPreview({ theme, wordWrap }: { theme: string, wordWrap: boolea
}, [theme]);
const codeStyle = useMemo<CSSProperties>(() => {
- if (wordWrap) {
- return { whiteSpace: "pre-wrap" };
- }
- return { whiteSpace: "pre"};
-
- }, [ wordWrap ]);
+ return {
+ whiteSpace: wordWrap ? "pre-wrap" : "pre",
+ tabSize: tabWidth ?? "4"
+ };
+ }, [ wordWrap, tabWidth ]);
return (
<div className="note-detail-readonly-text-content ck-content code-sample-wrapper">
diff --git a/apps/client/src/widgets/type_widgets/text/CKEditorWithWatchdog.tsx b/apps/client/src/widgets/type_widgets/text/CKEditorWithWatchdog.tsx
index dd4d9d02c9..40fd8f55f6 100644
--- a/apps/client/src/widgets/type_widgets/text/CKEditorWithWatchdog.tsx
+++ b/apps/client/src/widgets/type_widgets/text/CKEditorWithWatchdog.tsx
@@ -20,7 +20,7 @@ export interface CKEditorApi {
addImage(noteId: string): Promise<void>;
}
-interface CKEditorWithWatchdogProps extends Pick<HTMLProps<HTMLDivElement>, "className" | "tabIndex"> {
+interface CKEditorWithWatchdogProps extends Pick<HTMLProps<HTMLDivElement>, "className" | "tabIndex" | "style"> {
contentLanguage: string | null | undefined;
isClassicEditor?: boolean;
watchdogRef: RefObject<EditorWatchdog>;
@@ -35,7 +35,7 @@ interface CKEditorWithWatchdogProps extends Pick<HTMLProps<HTMLDivElement>, "cla
containerRef?: RefObject<HTMLDivElement>;
}
-export default function CKEditorWithWatchdog({ containerRef: externalContainerRef, contentLanguage, className, tabIndex, isClassicEditor, watchdogRef: externalWatchdogRef, watchdogConfig, onNotificationWarning, onWatchdogStateChange, onChange, onEditorInitialized, editorApi, templates }: CKEditorWithWatchdogProps) {
+export default function CKEditorWithWatchdog({ containerRef: externalContainerRef, contentLanguage, className, style, tabIndex, isClassicEditor, watchdogRef: externalWatchdogRef, watchdogConfig, onNotificationWarning, onWatchdogStateChange, onChange, onEditorInitialized, editorApi, templates }: CKEditorWithWatchdogProps) {
const containerRef = useSyncedRef<HTMLDivElement>(externalContainerRef, null);
const watchdogRef = useRef<EditorWatchdog>(null);
const [ uiLanguage ] = useTriliumOption("locale");
@@ -232,7 +232,7 @@ export default function CKEditorWithWatchdog({ containerRef: externalContainerRe
}, [ editor, onChange ]);
return (
- <div ref={containerRef} className={className} tabIndex={tabIndex} />
+ <div ref={containerRef} className={className} style={style} tabIndex={tabIndex} />
);
}
diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.tsx b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
index fba7d6966e..e7cb326553 100644
--- a/apps/client/src/widgets/type_widgets/text/EditableText.tsx
+++ b/apps/client/src/widgets/type_widgets/text/EditableText.tsx
@@ -36,6 +36,7 @@ export default function EditableText({ note, parentComponent, ntxId, noteContext
const [ language ] = useNoteLabel(note, "language");
const [ textNoteEditorType ] = useTriliumOption("textNoteEditorType");
const [ codeBlockWordWrap ] = useTriliumOptionBool("codeBlockWordWrap");
+ const [ codeBlockTabWidth ] = useTriliumOption("codeBlockTabWidth");
const isClassicEditor = isMobile() || textNoteEditorType === "ckeditor-classic";
const initialized = useRef(deferred<void>());
const spacedUpdate = useEditorSpacedUpdate({
@@ -224,6 +225,7 @@ export default function EditableText({ note, parentComponent, ntxId, noteContext
{note && !!templates && <CKEditorWithWatchdog
containerRef={containerRef}
className={`note-detail-editable-text-editor use-tn-links ${codeBlockWordWrap ? "word-wrap" : ""}`}
+ style={{ "--code-block-tab-width": codeBlockTabWidth ?? "4" }}
tabIndex={300}
contentLanguage={language}
isClassicEditor={isClassicEditor}
diff --git a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx
index 3352f621a0..77cb5334e3 100644
--- a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx
+++ b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx
@@ -13,7 +13,7 @@ import { applyInlineMermaid, rewriteMermaidDiagramsInContainer } from "../../../
import { getLocaleById } from "../../../services/i18n";
import { renderMathInElement } from "../../../services/math";
import { formatCodeBlocks } from "../../../services/syntax_highlight";
-import { useNoteBlob, useNoteLabel, useTriliumEvent, useTriliumOptionBool } from "../../react/hooks";
+import { useNoteBlob, useNoteLabel, useTriliumEvent, useTriliumOption, useTriliumOptionBool } from "../../react/hooks";
import { RawHtmlBlock } from "../../react/RawHtml";
import TouchBar, { TouchBarButton, TouchBarSpacer } from "../../react/TouchBar";
import { TypeWidgetProps } from "../type_widget";
@@ -24,6 +24,7 @@ export default function ReadOnlyText({ note, noteContext, ntxId }: TypeWidgetPro
const blob = useNoteBlob(note);
const contentRef = useRef<HTMLDivElement>(null);
const [ codeBlockWordWrap ] = useTriliumOptionBool("codeBlockWordWrap");
+ const [ codeBlockTabWidth ] = useTriliumOption("codeBlockTabWidth");
const { isRtl } = useNoteLanguage(note);
// Apply necessary transforms.
@@ -59,6 +60,7 @@ export default function ReadOnlyText({ note, noteContext, ntxId }: TypeWidgetPro
<RawHtmlBlock
containerRef={contentRef}
className={clsx("note-detail-readonly-text-content ck-content use-tn-links selectable-text", codeBlockWordWrap && "word-wrap")}
+ style={{ "--code-block-tab-width": codeBlockTabWidth ?? "4" }}
tabindex={100}
dir={isRtl ? "rtl" : "ltr"}
html={blob?.content}