Skip to content

Build your personal knowledge base with Trilium Notes

fix(options): code preview doesn't reflect tab width

Elian Doran249b08f1ce04
diff --git a/apps/client/src/widgets/type_widgets/options/code_notes.tsx b/apps/client/src/widgets/type_widgets/options/code_notes.tsx
index 4b8192be96..511c40b38f 100644
--- a/apps/client/src/widgets/type_widgets/options/code_notes.tsx
+++ b/apps/client/src/widgets/type_widgets/options/code_notes.tsx
@@ -139,7 +139,10 @@ function CodeNotePreview({ themeName, wordWrapping, indentSize }: { themeName: s
}, [ wordWrapping ]);
useEffect(() => {
- editorRef.current?.setIndentSize(indentSize);
+ const editor = editorRef.current;
+ if (!editor) return;
+ editor.setIndentSize(indentSize);
+ editor.setText(reindentSample(codeNoteSample, indentSize));
}, [ indentSize ]);
useEffect(() => {
@@ -160,6 +163,15 @@ function CodeNotePreview({ themeName, wordWrapping, indentSize }: { themeName: s
);
}
+const SAMPLE_BASE_INDENT = 4;
+
+function reindentSample(sample: string, indentSize: number): string {
+ return sample.replace(/^( +)/gm, (match) => {
+ const level = match.length / SAMPLE_BASE_INDENT;
+ return " ".repeat(Math.round(level) * indentSize);
+ });
+}
+
function CodeMimeTypes() {
return (
<OptionsSection title={t("code_mime_types.title")}>
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 b766dad698..7a6319562e 100644
--- a/apps/client/src/widgets/type_widgets/options/text_notes.tsx
+++ b/apps/client/src/widgets/type_widgets/options/text_notes.tsx
@@ -313,9 +313,9 @@ greet(n); // Print "Hello World" for n times
* @param {number} times The number of times to print the \`Hello World!\` message.
*/
function greet(times) {
- for (let i = 0; i++; i < times) {
- console.log("Hello World!");
- }
+\tfor (let i = 0; i++; i < times) {
+\t\tconsole.log("Hello World!");
+\t}
}
`;