Compare commits
56 Commits
e1e223ed55
...
44e64df922
| Author | SHA1 | Date | |
|---|---|---|---|
| 44e64df922 | |||
| 7e2964e9f9 | |||
| 2f84b8bce1 | |||
| 485e8b4ed8 | |||
| eecba280a3 | |||
| 4cc25c1f87 | |||
| f4baeeca3d | |||
| 2265ae70b5 | |||
| 18945e578e | |||
| 27ca21094d | |||
| 9671f63828 | |||
| 2f459a0df4 | |||
| fd734a7c64 | |||
| 7518f032a2 | |||
| 3456700ccd | |||
| f0b7a49611 | |||
| 9f962afcb5 | |||
| 0413d87a55 | |||
| aeafaf1d96 | |||
| 7921384ddf | |||
| b843b72670 | |||
| 14ed5afe7b | |||
| 34d8434fb3 | |||
| 9ecbe6ec65 | |||
| 3ccb7bbe68 | |||
| 28809839f2 | |||
| 97a3871a8d | |||
| 8c1cf0e6a0 | |||
| 6c528d6fb0 | |||
| 81a7a4fac8 | |||
| 5b5371d3a8 | |||
| a1f129a9cb | |||
| 44b00e4a13 | |||
| 9dbce4ebee | |||
| 18ae1c3d3b | |||
| 15a1b22c91 | |||
| afe8a39085 | |||
| ddba57d28b | |||
| 10c6043e4e | |||
| 574e7899a5 | |||
| 2d256af21f | |||
| 3ed9de1c54 | |||
| dd288313b9 | |||
| 699b55b8fd | |||
| 997c8767e6 | |||
| b5a6b556c9 | |||
| 51e8fea77b | |||
| 7e170dfe85 | |||
| 78758331bb | |||
| 2ea212fd53 | |||
| b907855513 | |||
| 0024acdf40 | |||
| 9936fd2c0f | |||
| 1c9af949b4 | |||
| 3a2569daad | |||
| 28edef621c |
@@ -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.
|
||||
|
||||
@@ -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",
|
||||
|
||||
209
src/App.tsx
209
src/App.tsx
@@ -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">
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user