Compare commits

...

56 Commits

Author SHA1 Message Date
44e64df922 Update README.md: Remove toggle instruction from Markdown preview mode description 2026-02-01 04:27:22 +01:00
7e2964e9f9 Remove unused import in src/App.tsx 2026-02-01 04:25:16 +01:00
2f84b8bce1 Remove unused tab keymap functionality in App.tsx 2026-02-01 04:24:10 +01:00
485e8b4ed8 Reorder imports in App.tsx for consistency 2026-02-01 04:19:24 +01:00
eecba280a3 Remove unused dependency in package.json 2026-02-01 04:19:24 +01:00
4cc25c1f87 Remove unused CSS import in App.tsx 2026-02-01 04:16:03 +01:00
f4baeeca3d Update keymap configuration in App.tsx 2026-02-01 04:12:15 +01:00
2265ae70b5 Add editorReady state and update dependencies 2026-02-01 04:10:26 +01:00
18945e578e Change font family to inherit in index.css 2026-02-01 04:10:26 +01:00
27ca21094d Prevent default behavior for save shortcut in editor 2026-02-01 04:10:26 +01:00
9671f63828 Refactor editor setup and add tab keymap functionality 2026-02-01 04:09:24 +01:00
2f459a0df4 Hide line numbers and measure in print mode 2026-02-01 04:04:27 +01:00
fd734a7c64 Import CodeMirror styles in App.tsx 2026-02-01 04:03:20 +01:00
7518f032a2 Remove unused CSS for line numbers 2026-02-01 04:02:04 +01:00
3456700ccd Add effect to request editor measure on state changes 2026-02-01 04:01:56 +01:00
f0b7a49611 Change font size in index.css to use CSS variable 2026-02-01 04:01:56 +01:00
9f962afcb5 Update package.json with new dependencies 2026-02-01 04:01:56 +01:00
0413d87a55 Remove src/lib/tln.ts file 2026-02-01 04:01:04 +01:00
aeafaf1d96 Update editor styles for CodeMirror integration 2026-02-01 04:00:57 +01:00
7921384ddf Replace textarea with div for CodeMirror in App.tsx 2026-02-01 04:00:33 +01:00
b843b72670 Update App.tsx to use editorView and compartments for line numbers and editable state 2026-02-01 04:00:21 +01:00
14ed5afe7b Add useEffect to initialize CodeMirror editor in App.tsx 2026-02-01 03:59:56 +01:00
34d8434fb3 Update App.tsx with new refs for editor components 2026-02-01 03:59:56 +01:00
9ecbe6ec65 Update App.tsx with CodeMirror imports and configurations 2026-02-01 03:59:44 +01:00
3ccb7bbe68 Set height to 100% for folder-item in index.css 2026-02-01 03:53:02 +01:00
28809839f2 Use markdownHtml for dangerouslySetInnerHTML in App.tsx 2026-02-01 03:53:02 +01:00
97a3871a8d Add markdownHtml memoization in App.tsx 2026-02-01 03:53:02 +01:00
8c1cf0e6a0 Add useEffect to refresh line numbers in App.tsx 2026-02-01 03:51:30 +01:00
6c528d6fb0 Import refreshLineNumbers in App.tsx 2026-02-01 03:51:30 +01:00
81a7a4fac8 Add refreshLineNumbers function to tln.ts 2026-02-01 03:51:30 +01:00
5b5371d3a8 Add CSS for hidden textarea and line numbers 2026-02-01 03:51:03 +01:00
a1f129a9cb No actual change detected in this commit 2026-02-01 03:51:03 +01:00
44b00e4a13 Import and use appendLineNumbers and removeLineNumbers functions 2026-02-01 03:51:03 +01:00
9dbce4ebee Add line number functionality to text area 2026-02-01 03:51:03 +01:00
18ae1c3d3b Remove onScroll event listener from textarea 2026-02-01 03:51:03 +01:00
15a1b22c91 Remove unused handleTextareaScroll function 2026-02-01 03:51:03 +01:00
afe8a39085 Simplify and optimize App.tsx by removing line numbers and improving markdown preview handling 2026-02-01 03:49:47 +01:00
ddba57d28b Refactor line number handling in App.tsx 2026-02-01 03:49:27 +01:00
10c6043e4e Simplify handleTextareaScroll and remove unused memoized functions in App.tsx 2026-02-01 03:49:09 +01:00
574e7899a5 Remove unused state and ref variables in App.tsx 2026-02-01 03:49:09 +01:00
2d256af21f Remove unused CSS classes in src/index.css 2026-02-01 03:32:32 +01:00
3ed9de1c54 Remove line number measure element from App.tsx 2026-02-01 03:32:27 +01:00
dd288313b9 Simplify line measurement logic in App.tsx 2026-02-01 03:32:27 +01:00
699b55b8fd Remove unused measureRef from App.tsx 2026-02-01 03:32:27 +01:00
997c8767e6 Optimize line metrics computation in App.tsx 2026-02-01 03:32:11 +01:00
b5a6b556c9 Update visible range calculation in App.tsx 2026-02-01 03:32:11 +01:00
51e8fea77b Refactor line calculation logic in App.tsx 2026-02-01 03:31:21 +01:00
7e170dfe85 Update refs and remove unused variables in App.tsx 2026-02-01 03:31:21 +01:00
78758331bb Remove unused FenwickTree class from src/App.tsx 2026-02-01 03:30:53 +01:00
2ea212fd53 Add relative positioning and adjust line numbers in index.css 2026-02-01 03:29:02 +01:00
b907855513 Refactor line number rendering in App.tsx 2026-02-01 03:28:53 +01:00
0024acdf40 Update line height calculation and management in App.tsx 2026-02-01 03:28:40 +01:00
9936fd2c0f Optimize scroll handling in App.tsx 2026-02-01 03:28:09 +01:00
1c9af949b4 Implement line number calculation and visibility tracking in App.tsx 2026-02-01 03:28:05 +01:00
3a2569daad Add state variables for line height and visible range in App.tsx 2026-02-01 03:28:05 +01:00
28edef621c Add FenwickTree class to src/App.tsx 2026-02-01 03:27:23 +01:00
4 changed files with 143 additions and 169 deletions

View File

@@ -6,7 +6,7 @@ TextDB is a **non-destructive**, completely offline, local-first text editor wit
- Non-destructive editing: immutable manual saves + safe autosaved drafts.
- Version history panel with quick restore.
- Markdown preview mode (toggle with Tab).
- Markdown preview mode.
- Sort your texts in nested folders with expandable tree; move items around via right-click.
- Open/import `.txt` and `.md` files; export to `.txt` or print / `.pdf`.
- Settings for theme, text size, and optional display of line numbers in the textarea.

View File

@@ -10,9 +10,12 @@
"tauri": "tauri"
},
"dependencies": {
"@codemirror/commands": "^6.10.1",
"@codemirror/state": "^6.5.4",
"@codemirror/view": "^6.39.12",
"@tauri-apps/api": "^2.0.0",
"@tauri-apps/plugin-dialog": "^2.0.0",
"@tauri-apps/plugin-clipboard-manager": "^2.0.0",
"@tauri-apps/plugin-dialog": "^2.0.0",
"@tauri-apps/plugin-fs": "^2.0.0",
"@tauri-apps/plugin-shell": "^2.0.0",
"@tauri-apps/plugin-sql": "^2.0.0",

View File

@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { open, save } from "@tauri-apps/plugin-dialog";
import { open as openExternal } from "@tauri-apps/plugin-shell";
import { writeText as writeClipboardText } from "@tauri-apps/plugin-clipboard-manager";
@@ -8,6 +8,14 @@ import { getCurrentWindow } from "@tauri-apps/api/window";
import { appDataDir } from "@tauri-apps/api/path";
import { listen } from "@tauri-apps/api/event";
import { invoke } from "@tauri-apps/api/core";
import { Compartment, EditorState, Transaction } from "@codemirror/state";
import {
EditorView,
keymap,
lineNumbers,
highlightActiveLineGutter
} from "@codemirror/view";
import { defaultKeymap, history, historyKeymap } from "@codemirror/commands";
import folderIcon from "../src-tauri/icons/folder.png";
import folderIconBright from "../src-tauri/icons/folder_b.png";
import historyIcon from "../src-tauri/icons/history.png";
@@ -142,25 +150,23 @@ export default function App() {
const [showLineNumbers, setShowLineNumbers] = useState(() => {
return localStorage.getItem("textdb.lineNumbers") === "true";
});
const [lineHeights, setLineHeights] = useState<number[]>([]);
const [measureTick, setMeasureTick] = useState(0);
const [sidebarCollapsed, setSidebarCollapsed] = useState(() => {
return localStorage.getItem("textdb.sidebarCollapsed") === "true";
});
const [editorReady, setEditorReady] = useState(false);
const bodyRef = useRef(body);
const textareaRef = useRef<HTMLTextAreaElement | null>(null);
const lineNumbersRef = useRef<HTMLDivElement | null>(null);
const measureRef = useRef<HTMLDivElement | null>(null);
const editorViewRef = useRef<EditorView | null>(null);
const editorValueRef = useRef("");
const lineNumbersCompartmentRef = useRef(new Compartment());
const editableCompartmentRef = useRef(new Compartment());
const historySnapshotRef = useRef<HistorySnapshot | null>(null);
const recentOpenRef = useRef(new Map<string, number>());
const ignoreTextBlurRef = useRef(false);
const ignoreFolderBlurRef = useRef(false);
useEffect(() => {
bodyRef.current = body;
}, [body]);
bodyRef.current = body;
useEffect(() => {
@@ -194,11 +200,50 @@ export default function App() {
}
}, [selectedTextId]);
const editorHostRef = useCallback((node: HTMLDivElement | null) => {
if (!node) {
if (editorViewRef.current) {
editorViewRef.current.destroy();
editorViewRef.current = null;
setEditorReady(false);
}
return;
}
if (editorViewRef.current) return;
const state = EditorState.create({
doc: bodyRef.current,
extensions: [
EditorView.lineWrapping,
history(),
keymap.of([...defaultKeymap, ...historyKeymap]),
lineNumbersCompartmentRef.current.of([]),
editableCompartmentRef.current.of(EditorView.editable.of(true)),
EditorView.updateListener.of((update) => {
if (!update.docChanged) return;
const value = update.state.doc.toString();
editorValueRef.current = value;
setBody(value);
})
]
});
const view = new EditorView({
state,
parent: node
});
editorViewRef.current = view;
editorValueRef.current = bodyRef.current;
setEditorReady(true);
}, []);
const isViewingHistory = viewingVersion !== null;
const isDirty = !isViewingHistory && body !== lastPersistedBody;
const hasText = body.trim().length > 0;
const showLineNumbersActive = showLineNumbers && !markdownPreview;
const hasSearch = search.trim().length > 0;
const markdownHtml = useMemo(
() => (markdownPreview ? markdownToHTML(body) : ""),
[body, markdownPreview]
);
const folderById = useMemo(() => {
const map = new Map<string, Folder>();
@@ -323,63 +368,45 @@ export default function App() {
const sidebarExpandIconSrc =
theme === "light" ? sidebarExpandIconBright : sidebarExpandIcon;
const lines = useMemo(() => body.split("\n"), [body]);
const lineNumbers = useMemo(() => lines.map((_, index) => index + 1), [lines]);
const handleTextareaScroll = useCallback((event: React.UIEvent<HTMLTextAreaElement>) => {
if (!showLineNumbersActive) return;
if (lineNumbersRef.current) {
lineNumbersRef.current.scrollTop = event.currentTarget.scrollTop;
}
}, [showLineNumbersActive]);
useEffect(() => {
if (!showLineNumbersActive) return;
const textarea = textareaRef.current;
if (!textarea || typeof ResizeObserver === "undefined") return;
const observer = new ResizeObserver(() => {
setMeasureTick((tick) => tick + 1);
const view = editorViewRef.current;
if (!view) return;
const extensions = showLineNumbersActive
? [lineNumbers(), highlightActiveLineGutter()]
: [];
view.dispatch({
effects: lineNumbersCompartmentRef.current.reconfigure(extensions)
});
observer.observe(textarea);
return () => observer.disconnect();
}, [showLineNumbersActive]);
}, [editorReady, showLineNumbersActive]);
useEffect(() => {
if (!showLineNumbersActive) return;
let raf = 0;
const handleResize = () => {
if (raf) cancelAnimationFrame(raf);
raf = requestAnimationFrame(() => {
setMeasureTick((tick) => tick + 1);
});
};
window.addEventListener("resize", handleResize);
return () => {
window.removeEventListener("resize", handleResize);
if (raf) cancelAnimationFrame(raf);
};
}, [showLineNumbersActive]);
const view = editorViewRef.current;
if (!view) return;
view.dispatch({
effects: editableCompartmentRef.current.reconfigure(
EditorView.editable.of(!isViewingHistory && !markdownPreview)
)
});
}, [editorReady, isViewingHistory, markdownPreview]);
useLayoutEffect(() => {
if (!showLineNumbersActive) return;
const textarea = textareaRef.current;
const measure = measureRef.current;
if (!textarea || !measure) return;
measure.style.width = `${textarea.clientWidth}px`;
const heights = Array.from(measure.children).map((child) =>
Math.ceil((child as HTMLElement).getBoundingClientRect().height)
);
setLineHeights(heights);
if (lineNumbersRef.current) {
lineNumbersRef.current.scrollTop = textarea.scrollTop;
}
}, [lines, showLineNumbersActive, textSize, measureTick, sidebarCollapsed, historyOpen]);
useEffect(() => {
if (showLineNumbersActive && textareaRef.current && lineNumbersRef.current) {
lineNumbersRef.current.scrollTop = textareaRef.current.scrollTop;
}
}, [showLineNumbersActive, body]);
const view = editorViewRef.current;
if (!view) return;
if (body === editorValueRef.current) return;
view.dispatch({
changes: { from: 0, to: view.state.doc.length, insert: body },
annotations: Transaction.addToHistory.of(false)
});
editorValueRef.current = body;
}, [body]);
useEffect(() => {
const view = editorViewRef.current;
if (!view) return;
view.requestMeasure();
}, [markdownPreview, textSize, sidebarCollapsed, historyOpen]);
const refreshTexts = useCallback(async () => {
@@ -1180,6 +1207,7 @@ export default function App() {
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (event.defaultPrevented) return;
const isSave =
(event.metaKey || event.ctrlKey) && event.key.toLowerCase() === "s";
if (isSave) {
@@ -1190,20 +1218,7 @@ export default function App() {
return;
}
if (
event.key === "Tab" &&
!event.metaKey &&
!event.ctrlKey &&
!event.altKey &&
selectedTextId &&
!settingsOpen &&
!confirmState &&
!editingFolderId &&
!editingTextId
) {
event.preventDefault();
setMarkdownPreview((value) => !value);
}
if (event.key === "Tab") return;
};
window.addEventListener("keydown", handleKeyDown);
@@ -1520,45 +1535,17 @@ export default function App() {
markdownPreview ? " editor__textarea-wrap--preview" : ""
}`}
>
{showLineNumbersActive ? (
<div className="line-measure" ref={measureRef} aria-hidden="true">
{lines.map((line, index) => (
<div key={index} className="line-measure__line">
{line.length > 0 ? line : " "}
</div>
))}
</div>
) : null}
{showLineNumbersActive ? (
<div className="line-numbers" ref={lineNumbersRef}>
{lineNumbers.map((line, index) => (
<div
key={line}
className="line-numbers__line"
style={{ height: lineHeights[index] ? `${lineHeights[index]}px` : undefined }}
>
{line}
</div>
))}
</div>
) : null}
{markdownPreview ? (
<div
className="markdown-preview md-root"
dangerouslySetInnerHTML={{ __html: markdownToHTML(body) }}
onClick={handleMarkdownPreviewClick}
/>
) : (
<textarea
ref={textareaRef}
className="editor__textarea"
value={body}
onChange={(event) => setBody(event.target.value)}
onScroll={handleTextareaScroll}
placeholder="Write your text here…"
readOnly={isViewingHistory}
/>
)}
<div
ref={editorHostRef}
className={`editor__codemirror${
markdownPreview ? " editor__codemirror--hidden" : ""
}`}
/>
<div
className={`markdown-preview md-root${markdownPreview ? "" : " markdown-preview--hidden"}`}
dangerouslySetInnerHTML={{ __html: markdownHtml }}
onClick={handleMarkdownPreviewClick}
/>
</div>
<div className="editor__footer">

View File

@@ -595,63 +595,45 @@ body:not([data-theme="light"]) .folder-item {
border-color: rgba(255, 255, 255, 0.18);
}
.line-numbers {
padding: 14px 10px;
min-width: 3ch;
text-align: right;
font-family: "SF Mono", "Menlo", "Consolas", monospace;
font-size: 0.95rem;
line-height: 1.5;
color: var(--muted);
background: var(--bg-contrast);
border-right: 1px solid var(--border);
user-select: none;
overflow: hidden;
}
.line-numbers__line {
height: 1.5em;
display: flex;
align-items: flex-start;
}
.line-measure {
position: absolute;
top: 0;
left: 0;
visibility: hidden;
pointer-events: none;
white-space: pre-wrap;
word-break: break-word;
overflow-wrap: anywhere;
font-family: "SF Mono", "Menlo", "Consolas", monospace;
font-size: 0.95rem;
line-height: 1.5;
padding: 14px;
}
.line-measure__line {
white-space: pre-wrap;
}
.editor__textarea {
.editor__codemirror {
flex: 1;
width: 100%;
resize: none;
border: none;
padding: 14px;
font-family: "SF Mono", "Menlo", "Consolas", monospace;
font-size: 0.95rem;
line-height: 1.5;
white-space: pre-wrap;
word-break: break-word;
overflow-wrap: anywhere;
background: transparent;
color: var(--ink);
min-height: 0;
}
.editor__textarea:focus {
outline: none;
.editor__codemirror--hidden {
display: none;
}
.editor__codemirror .cm-editor {
height: 100%;
background: transparent;
color: var(--ink);
font-family: inherit;
font-size: var(--base-font-size);
line-height: 1.5;
}
.editor__codemirror .cm-scroller {
overflow: auto;
}
.editor__codemirror .cm-content {
padding: 14px;
}
.editor__codemirror .cm-gutters {
background: var(--bg-contrast);
border-right: 1px solid var(--border);
color: var(--muted);
}
.editor__codemirror .cm-gutterElement {
padding: 0 10px 0 0;
}
.editor__codemirror .cm-activeLineGutter {
background: transparent;
}
.markdown-preview {
@@ -662,6 +644,10 @@ body:not([data-theme="light"]) .folder-item {
color: var(--ink);
}
.markdown-preview--hidden {
display: none;
}
.editor__footer {
display: flex;
flex-wrap: wrap;
@@ -1035,9 +1021,7 @@ body[data-theme="light"] .editor__textarea-wrap:focus-within {
body.print-markdown .sidebar,
body.print-markdown .history,
body.print-markdown .editor__header,
body.print-markdown .editor__footer,
body.print-markdown .line-numbers,
body.print-markdown .line-measure {
body.print-markdown .editor__footer {
display: none !important;
}