Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] #18

Merged
merged 7 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: add delete line
  • Loading branch information
Abizrh committed Oct 5, 2024
commit f629f67710d75d6562f922178c5a84e170cf6870
71 changes: 69 additions & 2 deletions src/components/playground/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState, useRef, useEffect, useCallback } from "react";
import { Cursor, FileSystemItem, Folder, Mode, File } from "@/types";
import { Cursor, FileSystemItem, Folder, Mode, File, TypeMode } from "@/types";
import { Highlight, themes } from "prism-react-renderer";
import { getLanguage, initializeIndexedDB } from "@/lib/utils";
import FileStatusDisplay from "./status";
Expand Down Expand Up @@ -75,8 +75,8 @@ const NeovimSimulator: React.FC = () => {
const [isSpacePressed, setIsSpacePressed] = useState(true);
const [isSaving, setIsSaving] = useState(false);
const [saveMessage, setSaveMessage] = useState("");

const [currentTheme, setCurrentTheme] = useState(theme);
const [keyPressed, setKeyPressed] = useState<string[]>([]);

/**
* Refs
Expand All @@ -85,6 +85,8 @@ const NeovimSimulator: React.FC = () => {
const fileSystemRef = useRef<HTMLDivElement>(null);
const cursorRef = useRef<HTMLSpanElement>(null);
const scrollTimeoutRef = useRef<NodeJS.Timeout | null>(null);
const keyPressedRef = useRef<string[]>([]);
const timeoutRef = useRef<NodeJS.Timeout | null>(null);

/**
* Callbacks
Expand All @@ -102,6 +104,54 @@ const NeovimSimulator: React.FC = () => {
}
}, []);

const deleteLine = useCallback(() => {
// newLine a line that the character is has been deleted
const newLines = [...lines];

//lines otherwise still have the deleted line and content

// TODO: we have to store the cursor.line position
// and then we can filter out the deleted line and content and we can set the value to undoLine
newLines.splice(cursor.line, 1);
setLines(newLines);
setCursor({ line: cursor.line, ch: 0 });
}, [cursor, lines]);

const undoLine = useCallback(() => {
if (cursor.line > 0) {
const newLines = [...lines];
newLines.splice(cursor.line, 1);
newLines.splice(cursor.line, 0, lines[cursor.line]);
setLines(newLines);
setCursor({ line: cursor.line, ch: lines[cursor.line].length });
}
}, [cursor, lines]);

const handleKeyPress = useCallback(
(event: KeyboardEvent) => {
if (mode === TypeMode.NORMAL) {
keyPressedRef.current.push(event.key);

if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}

timeoutRef.current = setTimeout(() => {
if (
keyPressedRef.current.includes("d") &&
keyPressedRef.current.length === 2
) {
deleteLine();
} else if (keyPressedRef.current.includes("u")) {
undoLine();
}
keyPressedRef.current = [];
}, 200);
}
},
[mode, deleteLine, undoLine],
);

/**
* Effects
*/
Expand Down Expand Up @@ -134,6 +184,16 @@ const NeovimSimulator: React.FC = () => {
};
}, [cursor, scrollToCursor]);

useEffect(() => {
window.addEventListener("keydown", handleKeyPress);
return () => {
window.removeEventListener("keydown", handleKeyPress);
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
};
}, [handleKeyPress]);

const handleThemeChange = (newTheme: string) => {
setCurrentTheme(newTheme);
localStorage.setItem("theme", newTheme);
Expand Down Expand Up @@ -241,6 +301,13 @@ const NeovimSimulator: React.FC = () => {
const handleEditorNavigation = (e: React.KeyboardEvent<HTMLDivElement>) => {
if (mode === "normal") {
switch (e.key) {
case "d":
setKeyPressed((prevKeyPressed) => {
const newKeyPressed = [...prevKeyPressed, "d"];
return newKeyPressed;
});

break;
case "i":
setMode("insert");
break;
Expand Down
8 changes: 4 additions & 4 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ export const initializeIndexedDB = () => {
console.error("IndexedDB error:", event);
};

request.onsuccess = (event) => {
const db = (event.target as IDBOpenDBRequest).result;
console.log("IndexedDB opened successfully", db);
};
// request.onsuccess = (event) => {
// const db = (event.target as IDBOpenDBRequest).result;
// console.log("IndexedDB opened successfully", db);
// };

request.onupgradeneeded = (event) => {
const db = (event.target as IDBOpenDBRequest).result;
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ export interface ThemeItem {
value: string;
label: string;
}

export enum TypeMode {
NORMAL = "normal",
INSERT = "insert",
VISUAL = "visual",
COMMAND = "command",
}