Refactor editor setup and add tab keymap functionality
This commit is contained in:
53
src/App.tsx
53
src/App.tsx
@@ -157,20 +157,18 @@ export default function App() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const bodyRef = useRef(body);
|
const bodyRef = useRef(body);
|
||||||
const editorHostRef = useRef<HTMLDivElement | null>(null);
|
|
||||||
const editorViewRef = useRef<EditorView | null>(null);
|
const editorViewRef = useRef<EditorView | null>(null);
|
||||||
const editorValueRef = useRef("");
|
const editorValueRef = useRef("");
|
||||||
const lineNumbersCompartmentRef = useRef(new Compartment());
|
const lineNumbersCompartmentRef = useRef(new Compartment());
|
||||||
const editableCompartmentRef = useRef(new Compartment());
|
const editableCompartmentRef = useRef(new Compartment());
|
||||||
|
const tabKeymapCompartmentRef = useRef(new Compartment());
|
||||||
const historySnapshotRef = useRef<HistorySnapshot | null>(null);
|
const historySnapshotRef = useRef<HistorySnapshot | null>(null);
|
||||||
const recentOpenRef = useRef(new Map<string, number>());
|
const recentOpenRef = useRef(new Map<string, number>());
|
||||||
const ignoreTextBlurRef = useRef(false);
|
const ignoreTextBlurRef = useRef(false);
|
||||||
const ignoreFolderBlurRef = useRef(false);
|
const ignoreFolderBlurRef = useRef(false);
|
||||||
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
bodyRef.current = body;
|
bodyRef.current = body;
|
||||||
}, [body]);
|
|
||||||
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -204,17 +202,24 @@ export default function App() {
|
|||||||
}
|
}
|
||||||
}, [selectedTextId]);
|
}, [selectedTextId]);
|
||||||
|
|
||||||
useEffect(() => {
|
const editorHostRef = useCallback((node: HTMLDivElement | null) => {
|
||||||
const host = editorHostRef.current;
|
if (!node) {
|
||||||
if (!host || editorViewRef.current) return;
|
if (editorViewRef.current) {
|
||||||
|
editorViewRef.current.destroy();
|
||||||
|
editorViewRef.current = null;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (editorViewRef.current) return;
|
||||||
const state = EditorState.create({
|
const state = EditorState.create({
|
||||||
doc: body,
|
doc: bodyRef.current,
|
||||||
extensions: [
|
extensions: [
|
||||||
EditorView.lineWrapping,
|
EditorView.lineWrapping,
|
||||||
history(),
|
history(),
|
||||||
keymap.of([...defaultKeymap, ...historyKeymap]),
|
keymap.of([...defaultKeymap, ...historyKeymap]),
|
||||||
lineNumbersCompartmentRef.current.of([]),
|
lineNumbersCompartmentRef.current.of([]),
|
||||||
editableCompartmentRef.current.of(EditorView.editable.of(true)),
|
editableCompartmentRef.current.of(EditorView.editable.of(true)),
|
||||||
|
tabKeymapCompartmentRef.current.of([]),
|
||||||
EditorView.updateListener.of((update) => {
|
EditorView.updateListener.of((update) => {
|
||||||
if (!update.docChanged) return;
|
if (!update.docChanged) return;
|
||||||
const value = update.state.doc.toString();
|
const value = update.state.doc.toString();
|
||||||
@@ -225,14 +230,10 @@ export default function App() {
|
|||||||
});
|
});
|
||||||
const view = new EditorView({
|
const view = new EditorView({
|
||||||
state,
|
state,
|
||||||
parent: host
|
parent: node
|
||||||
});
|
});
|
||||||
editorViewRef.current = view;
|
editorViewRef.current = view;
|
||||||
editorValueRef.current = body;
|
editorValueRef.current = bodyRef.current;
|
||||||
return () => {
|
|
||||||
view.destroy();
|
|
||||||
editorViewRef.current = null;
|
|
||||||
};
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const isViewingHistory = viewingVersion !== null;
|
const isViewingHistory = viewingVersion !== null;
|
||||||
@@ -390,6 +391,32 @@ export default function App() {
|
|||||||
});
|
});
|
||||||
}, [isViewingHistory, markdownPreview]);
|
}, [isViewingHistory, markdownPreview]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const view = editorViewRef.current;
|
||||||
|
if (!view) return;
|
||||||
|
const keys = keymap.of([
|
||||||
|
{
|
||||||
|
key: "Tab",
|
||||||
|
run: () => {
|
||||||
|
if (
|
||||||
|
!selectedTextId ||
|
||||||
|
settingsOpen ||
|
||||||
|
confirmState ||
|
||||||
|
editingFolderId ||
|
||||||
|
editingTextId
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
setMarkdownPreview((value) => !value);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
view.dispatch({
|
||||||
|
effects: tabKeymapCompartmentRef.current.reconfigure(keys)
|
||||||
|
});
|
||||||
|
}, [confirmState, editingFolderId, editingTextId, selectedTextId, settingsOpen]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const view = editorViewRef.current;
|
const view = editorViewRef.current;
|
||||||
if (!view) return;
|
if (!view) return;
|
||||||
|
|||||||
Reference in New Issue
Block a user