From c9293feb66ed3959c12adccdefdce87bff59396a Mon Sep 17 00:00:00 2001 From: Thomas Gauvin <35609369+thomasgauvin@users.noreply.github.com> Date: Sat, 9 Mar 2024 15:52:36 -0500 Subject: [PATCH] backup --- ... a new note.md => This is a new note 2.md} | 0 .../Maybe I need to debounce more.md | 0 .../Untitled Folder (1)/This is a test.md | 18 --- .../Untitled Folder/Happy thanksgiving!.md | 1 - .../Untitled Folder/Untitled Folder/Tes.md | 0 .../Untitled Folder/Untitled Folder/Test.md | 1 - .../We'll see you at Christmas!.md | 1 - .../Untitled this is a test.md | 0 .../folder/subfolder/test55.md | 3 - ...est TESt \342\234\205 \360\237\247\220.md" | 3 - .../Interesting1234568915.md} | 0 .../wait renaming works cool.md | 0 .../test11/new-folder10/new file.md | 0 .../test11/test this is a test.md | 1 - .../\342\234\205 Todo list.md" | 31 ++--- .../{Folder.tsx => FileSystemItem.tsx} | 25 +++- .../FileSystem/LocalFileSystem.tsx | 45 ++++--- .../FileSystem/RightClickMenu.tsx | 40 +++++- .../FileSystem/WorkspaceSelector.tsx | 118 +++++++++++++----- src/components/MarkdownEditor/FileEditor.tsx | 14 ++- src/components/MarkdownEditor/OnClickLink.tsx | 37 ++++++ src/components/MarkdownEditor/OnFocus.tsx | 21 ++++ .../MarkdownEditor/RemirrorComponent.tsx | 8 +- .../MarkdownEditor/RemirrorMarkdownEditor.tsx | 45 ++----- src/models/DirectoryNode.ts | 43 +++++++ src/pages/Editor.tsx | 10 +- 26 files changed, 323 insertions(+), 142 deletions(-) rename sample-markdown-folder/{This is a new note.md => This is a new note 2.md} (100%) delete mode 100644 sample-markdown-folder/folder/Untitled Folder (1)/Maybe I need to debounce more.md delete mode 100644 sample-markdown-folder/folder/Untitled Folder (1)/This is a test.md delete mode 100644 sample-markdown-folder/folder/Untitled Folder (1)/Untitled Folder/Happy thanksgiving!.md delete mode 100644 sample-markdown-folder/folder/Untitled Folder (1)/Untitled Folder/Untitled Folder/Tes.md delete mode 100644 sample-markdown-folder/folder/Untitled Folder (1)/Untitled Folder/Untitled Folder/Test.md delete mode 100644 sample-markdown-folder/folder/Untitled Folder (1)/Untitled Folder/Untitled Folder/We'll see you at Christmas!.md delete mode 100644 sample-markdown-folder/folder/Untitled Folder (1)/Untitled Folder/Untitled this is a test.md delete mode 100644 sample-markdown-folder/folder/subfolder/test55.md delete mode 100644 "sample-markdown-folder/folder/subfolder/thi si a test TESt \342\234\205 \360\237\247\220.md" rename sample-markdown-folder/{folder/Interesting.md => sample folder for notes 2/Interesting1234568915.md} (100%) rename sample-markdown-folder/{folder => sample folder for notes 2}/wait renaming works cool.md (100%) delete mode 100644 sample-markdown-folder/test11/new-folder10/new file.md delete mode 100644 sample-markdown-folder/test11/test this is a test.md rename src/components/FileSystemAdapters/FileSystem/{Folder.tsx => FileSystemItem.tsx} (86%) create mode 100644 src/components/MarkdownEditor/OnClickLink.tsx create mode 100644 src/components/MarkdownEditor/OnFocus.tsx diff --git a/sample-markdown-folder/This is a new note.md b/sample-markdown-folder/This is a new note 2.md similarity index 100% rename from sample-markdown-folder/This is a new note.md rename to sample-markdown-folder/This is a new note 2.md diff --git a/sample-markdown-folder/folder/Untitled Folder (1)/Maybe I need to debounce more.md b/sample-markdown-folder/folder/Untitled Folder (1)/Maybe I need to debounce more.md deleted file mode 100644 index e69de29..0000000 diff --git a/sample-markdown-folder/folder/Untitled Folder (1)/This is a test.md b/sample-markdown-folder/folder/Untitled Folder (1)/This is a test.md deleted file mode 100644 index c8cd475..0000000 --- a/sample-markdown-folder/folder/Untitled Folder (1)/This is a test.md +++ /dev/null @@ -1,18 +0,0 @@ -Interesting, it's not that bad - -## He's a prick - -### How long since you spoke - -**Ok pretty cool** - -* Where **~are~** my - - -1. Where are my numbers - -2. Test - - -Tes -test \ No newline at end of file diff --git a/sample-markdown-folder/folder/Untitled Folder (1)/Untitled Folder/Happy thanksgiving!.md b/sample-markdown-folder/folder/Untitled Folder (1)/Untitled Folder/Happy thanksgiving!.md deleted file mode 100644 index 3596890..0000000 --- a/sample-markdown-folder/folder/Untitled Folder (1)/Untitled Folder/Happy thanksgiving!.md +++ /dev/null @@ -1 +0,0 @@ -**Hey** _there_! Happy thanksgiving! :D \ No newline at end of file diff --git a/sample-markdown-folder/folder/Untitled Folder (1)/Untitled Folder/Untitled Folder/Tes.md b/sample-markdown-folder/folder/Untitled Folder (1)/Untitled Folder/Untitled Folder/Tes.md deleted file mode 100644 index e69de29..0000000 diff --git a/sample-markdown-folder/folder/Untitled Folder (1)/Untitled Folder/Untitled Folder/Test.md b/sample-markdown-folder/folder/Untitled Folder (1)/Untitled Folder/Untitled Folder/Test.md deleted file mode 100644 index 4b7f922..0000000 --- a/sample-markdown-folder/folder/Untitled Folder (1)/Untitled Folder/Untitled Folder/Test.md +++ /dev/null @@ -1 +0,0 @@ -This is a test, who can guess how it works \ No newline at end of file diff --git a/sample-markdown-folder/folder/Untitled Folder (1)/Untitled Folder/Untitled Folder/We'll see you at Christmas!.md b/sample-markdown-folder/folder/Untitled Folder (1)/Untitled Folder/Untitled Folder/We'll see you at Christmas!.md deleted file mode 100644 index 4cbaf6f..0000000 --- a/sample-markdown-folder/folder/Untitled Folder (1)/Untitled Folder/Untitled Folder/We'll see you at Christmas!.md +++ /dev/null @@ -1 +0,0 @@ -❤️ diff --git a/sample-markdown-folder/folder/Untitled Folder (1)/Untitled Folder/Untitled this is a test.md b/sample-markdown-folder/folder/Untitled Folder (1)/Untitled Folder/Untitled this is a test.md deleted file mode 100644 index e69de29..0000000 diff --git a/sample-markdown-folder/folder/subfolder/test55.md b/sample-markdown-folder/folder/subfolder/test55.md deleted file mode 100644 index 9489fdd..0000000 --- a/sample-markdown-folder/folder/subfolder/test55.md +++ /dev/null @@ -1,3 +0,0 @@ -hello this is a test - -# test 4 \ No newline at end of file diff --git "a/sample-markdown-folder/folder/subfolder/thi si a test TESt \342\234\205 \360\237\247\220.md" "b/sample-markdown-folder/folder/subfolder/thi si a test TESt \342\234\205 \360\237\247\220.md" deleted file mode 100644 index 721d7ab..0000000 --- "a/sample-markdown-folder/folder/subfolder/thi si a test TESt \342\234\205 \360\237\247\220.md" +++ /dev/null @@ -1,3 +0,0 @@ -this is a link - -[test55.md](./test55.md) \ No newline at end of file diff --git a/sample-markdown-folder/folder/Interesting.md b/sample-markdown-folder/sample folder for notes 2/Interesting1234568915.md similarity index 100% rename from sample-markdown-folder/folder/Interesting.md rename to sample-markdown-folder/sample folder for notes 2/Interesting1234568915.md diff --git a/sample-markdown-folder/folder/wait renaming works cool.md b/sample-markdown-folder/sample folder for notes 2/wait renaming works cool.md similarity index 100% rename from sample-markdown-folder/folder/wait renaming works cool.md rename to sample-markdown-folder/sample folder for notes 2/wait renaming works cool.md diff --git a/sample-markdown-folder/test11/new-folder10/new file.md b/sample-markdown-folder/test11/new-folder10/new file.md deleted file mode 100644 index e69de29..0000000 diff --git a/sample-markdown-folder/test11/test this is a test.md b/sample-markdown-folder/test11/test this is a test.md deleted file mode 100644 index 9944a9f..0000000 --- a/sample-markdown-folder/test11/test this is a test.md +++ /dev/null @@ -1 +0,0 @@ -This is a test file \ No newline at end of file diff --git "a/sample-markdown-folder/\342\234\205 Todo list.md" "b/sample-markdown-folder/\342\234\205 Todo list.md" index c4ce226..b4c047d 100644 --- "a/sample-markdown-folder/\342\234\205 Todo list.md" +++ "b/sample-markdown-folder/\342\234\205 Todo list.md" @@ -66,23 +66,27 @@ * Rename files issue causing duplicate selected files ✅ -* Renaming a folder does not update the folder directory when in a note within the folder - * Folder view should not have a Editor view, or the Editor view should be functional to rename a folder ✅ -* Easier way to go from title to text. Maybe enter should go to text, or tab. +* Easier way to go from title to text. Maybe enter should go to text, or tab. ✅ + +* Deleting files should automatically update the folder view ✅ + +* Renaming files in tree view should update the folder view ✅ -* Deleting files should automatically update the folder view +* Renaming a folder does not update the folder directory when in a note within the folder ✅ -* Renaming files should not be supported in the file tree viewer, or should but currently isn't working +* Deleting an empty top level folder caused an error ✅ -* **Add new folder/new file to tree viewer** +* Renaming files should not be supported in the file tree viewer, or should but currently isn't working ✅ + +* Add new folder/new file to tree viewer ✅ * Make it so that you can start editing and save later (maybe make it a zip on unsupported devices) * Make it mobile UI responsive -* Make the UI have resizable side bar +* Have the UI have top level folders as sections ### P1 @@ -93,20 +97,17 @@ * Autocomplete with Bring your own OpenAI key (or Ollama integration) +* Export to website (static site generation) + * add support to add links within the project ⏸️ * hide folder create/delete until hover of item ⏸️ * add lots of CSS transitions ⏸️ +* Make the UI have resizable side bar + ### P2 -* Add electron app for desktop, no notifications, and support for browsers that don't work - -* ❌ styling for mobile - - * local file system doesn't even work on mobile... - - -### P3+ \ No newline at end of file +* Add electron app for desktop, no notifications, and support for browsers that don't work \ No newline at end of file diff --git a/src/components/FileSystemAdapters/FileSystem/Folder.tsx b/src/components/FileSystemAdapters/FileSystem/FileSystemItem.tsx similarity index 86% rename from src/components/FileSystemAdapters/FileSystem/Folder.tsx rename to src/components/FileSystemAdapters/FileSystem/FileSystemItem.tsx index cfe193c..0c62811 100644 --- a/src/components/FileSystemAdapters/FileSystem/Folder.tsx +++ b/src/components/FileSystemAdapters/FileSystem/FileSystemItem.tsx @@ -4,7 +4,7 @@ import { ChevronDown, ChevronRight, FilePlus, FolderPlus } from "lucide-react"; import RightClickMenu from "./RightClickMenu"; import * as ContextMenu from "@radix-ui/react-context-menu"; -export function Folder({ +export function FileSystemItem({ node, depth, handleFileSelect, @@ -12,7 +12,8 @@ export function Folder({ handleDeleteFile, handleCreateFile, handleCreateFolder, - handleRenameFolder + handleRenameFolder, + handleRenameFile, }: { node: DirectoryNode; depth: number; @@ -22,6 +23,7 @@ export function Folder({ handleCreateFile: (node: DirectoryNode) => void; handleCreateFolder: (node: DirectoryNode) => void; handleRenameFolder: (node: DirectoryNode) => void; + handleRenameFile: (node: DirectoryNode) => void; }) { const [expanded, setExpanded] = useState(depth === 0); @@ -106,7 +108,7 @@ export function Folder({ )} ) : ( - // Recursively render directory )} handleCreateFile(child) + : undefined + } + onCreateFolder={ + child.isDirectory() + ? () => handleCreateFolder(child) + : undefined + } onDelete={() => handleDeleteFile(child)} - onRename={() => handleRenameFolder(child)} + onRename={ + child.isDirectory() ? + () => handleRenameFolder(child) : + () => handleRenameFile(child) + } /> diff --git a/src/components/FileSystemAdapters/FileSystem/LocalFileSystem.tsx b/src/components/FileSystemAdapters/FileSystem/LocalFileSystem.tsx index 57625b8..925f1c0 100644 --- a/src/components/FileSystemAdapters/FileSystem/LocalFileSystem.tsx +++ b/src/components/FileSystemAdapters/FileSystem/LocalFileSystem.tsx @@ -6,12 +6,12 @@ import { useEffect } from "react"; import DirectoryNode, { createDirectoryNode, } from "../../../models/DirectoryNode"; -import { Folder } from "./Folder"; +import { FileSystemItem } from "./FileSystemItem"; import { FilePlus, FolderPlus } from "lucide-react"; //sample react function export const LocalFileSystem = ({ - selectedDirectory, + selectedDirectory, //rootDirectory setSelectedDirectory, selectedFile, setSelectedFile, @@ -37,17 +37,6 @@ export const LocalFileSystem = ({ fetchEntries(); }, [selectedDirectory]); - // const handleDirectorySelect = async () => { - // try { - // const directoryHandle = await (window as any).showDirectoryPicker(); - // setSelectedDirectory( - // await createDirectoryNode(directoryHandle, undefined) - // ); - // } catch (error) { - // console.error("Error selecting directory:", error); - // } - // }; - const getDirectoryRecursive = async ( directoryHandle: FileSystemDirectoryHandle ) => { @@ -65,8 +54,21 @@ export const LocalFileSystem = ({ const handleDeleteFile = async (node: DirectoryNode) => { console.log("deleting file"); - const parent = await node.delete(); - setSelectedFile(getClosestParentsFirstFile(parent)); // TODO: set to root directory + await node.delete(); + + //determine if the selected file still exists, if it does, set the selected file to it (leave as is), otherwise set it to null + if(selectedFile){ + const selectedFileAsFoundInCurrentDirectory = + selectedDirectory?.findChildByPath(selectedFile?.getFullPath()); + if(selectedFileAsFoundInCurrentDirectory){ + setSelectedFile(selectedFileAsFoundInCurrentDirectory.getCopy() || null); + } else { + setSelectedFile(null); + } + } + else{ + setSelectedFile(null); + } }; const createFileHandlingDuplicates = async (parent: DirectoryNode) => { @@ -134,7 +136,15 @@ export const LocalFileSystem = ({ const newName = prompt("Enter new name for folder", node.name); if (newName) { await node.renameFolder(newName); - setSelectedFile(getClosestParentsFirstFile(node)); + setSelectedFile(selectedFile?.getCopy() || null); + } + } + + const handleRenameFile = async (node: DirectoryNode) => { + const newName = prompt("Enter new name for file", node.getName() || node.name); + if (newName) { + await node.renameFile(newName); + setSelectedFile(selectedFile?.getCopy() || null); } } @@ -193,7 +203,7 @@ export const LocalFileSystem = ({
-
diff --git a/src/components/FileSystemAdapters/FileSystem/RightClickMenu.tsx b/src/components/FileSystemAdapters/FileSystem/RightClickMenu.tsx index 6f20b4a..d6d0cb9 100644 --- a/src/components/FileSystemAdapters/FileSystem/RightClickMenu.tsx +++ b/src/components/FileSystemAdapters/FileSystem/RightClickMenu.tsx @@ -1,8 +1,10 @@ -import { FolderPen, Trash } from "lucide-react"; +import { FilePlus, FolderPen, FolderPlus, Trash } from "lucide-react"; import * as ContextMenu from "@radix-ui/react-context-menu"; -const RightClickMenu = ({ onDelete, onRename }: +const RightClickMenu = ({ onCreateFile, onCreateFolder, onDelete, onRename }: { + onCreateFile: (() => void) | undefined, + onCreateFolder: (() => void) | undefined, onDelete: () => void, onRename: () => void }) => { @@ -11,7 +13,39 @@ const RightClickMenu = ({ onDelete, onRename }: + > + { + onCreateFile && ( + { + onCreateFile(); + }} + > + +
Create note
+
+ ) + } + { + onCreateFolder && ( + { + onCreateFolder(); + }} + > + +
Create folder
+
+ ) + } { + const emptyBrowserDirectory = new DirectoryNode( + "Notes", + undefined, + undefined, + undefined, + undefined, + undefined, + undefined, + undefined, + undefined + ); + + setSelectedDirectory(emptyBrowserDirectory); + } + useEffect(() => { setIsSupported('showDirectoryPicker' in window && typeof window.showDirectoryPicker === 'function'); }, []); return (
-
-
-
-

- Noted -

-

- A local-first open-source note taking app. -

-
-
- -
+
+
+
+
+

+ Noted +

+

+ A local-first open-source note taking app. +

+
+
+ +
-
-

- Select a folder -

-

- Select a new empty folder or select an existing folder that will - contain your notes. -

+
+

+ Save location +

+

+ Select a location to save your notes +

+
-
-
- {isSupported ? ( - - ) : ( + className="rounded w-full hover:border-zinc-200 bg-zinc-100 hover:bg-zinc-200" + > + Local folder + +
+ ) : (

- This browser does not support file access at this time. - Please use a compatible browser (Google Chrome, Microsoft Edge, or Opera). + This browser does not support file access at this time. Please + use a compatible browser (Google Chrome, Microsoft Edge, or + Opera) for direct file saving.

- )} + )} + {/*
+ +
+
+ +
*/} +
+
+ +
+
+ +
diff --git a/src/components/MarkdownEditor/FileEditor.tsx b/src/components/MarkdownEditor/FileEditor.tsx index c1efbb9..889a09a 100644 --- a/src/components/MarkdownEditor/FileEditor.tsx +++ b/src/components/MarkdownEditor/FileEditor.tsx @@ -3,6 +3,7 @@ import DirectoryNode from "../../models/DirectoryNode"; import { RemirrorComponent } from "./RemirrorComponent"; import { useDebouncedCallback } from "use-debounce"; import TextareaAutosize from 'react-textarea-autosize'; +import { useCommands } from "@remirror/react"; export const FileEditor = ({ selectedFile, @@ -13,12 +14,13 @@ export const FileEditor = ({ }) => { const [title, setTitle] = useState(selectedFile?.getName() || "Untitled"); + const [shouldFocus, setShouldFocus] = useState(false); const debouncedChangeFileName = useDebouncedCallback( async ({ value }) => { console.log('debounced callback') await selectedFile?.renameFile(value); - setSelectedFile(selectedFile?.getCopy()); + setSelectedFile(selectedFile?.getCopy()); //force a rerender of the file system }, 700 ); @@ -41,6 +43,13 @@ export const FileEditor = ({ } }; + const handleOnKeyDown = (event : React.KeyboardEvent) => { + if (event.key === 'Enter' || event.key === 'Tab') { + event.preventDefault(); // Prevents the default behavior of the Enter key in the textarea + setShouldFocus(true); + } + } + if (!selectedFile) return <>; return ( @@ -49,11 +58,14 @@ export const FileEditor = ({ placeholder="Untitled" value={title} onChange={handleOnChangeFileName} + onKeyDown={handleOnKeyDown} className="text-5xl font-bold w-full bg-transparent focus:outline-none pb-4 resize-none" /> ); diff --git a/src/components/MarkdownEditor/OnClickLink.tsx b/src/components/MarkdownEditor/OnClickLink.tsx new file mode 100644 index 0000000..6c5a604 --- /dev/null +++ b/src/components/MarkdownEditor/OnClickLink.tsx @@ -0,0 +1,37 @@ +import { FC, PropsWithChildren, useCallback } from "react"; + +import { useExtensionEvent } from "@remirror/react"; +import DirectoryNode from "../../models/DirectoryNode"; +import { LinkExtension } from "remirror/extensions"; + +export const OnClickLink = ({ + selectedFile, + setSelectedFile, +}: { + selectedFile: DirectoryNode | undefined; + setSelectedFile: (file: DirectoryNode) => void | undefined; +}) => { + useExtensionEvent( + LinkExtension, + "onClick", + useCallback((_, data) => { + if (!selectedFile) return false; + + const urlWithoutProtocol = data.href.replace(/(^\w+:|^)\/\//, ""); //remove protocol, even when remirror adds just // + const urlWithHttps = "https://" + urlWithoutProtocol; + const url = new URL(urlWithHttps); + if (!url.host.startsWith(".")) { + //open in new tab + window.open(urlWithHttps, "_blank"); // Open the link in a new tab or window + return true; + } + const filePath = decodeURI(urlWithoutProtocol); + setSelectedFile( + selectedFile.findNodeByRelativePath(filePath)?.getCopy() ?? selectedFile + ); + return true; + }, []) + ); + + return <>; +}; diff --git a/src/components/MarkdownEditor/OnFocus.tsx b/src/components/MarkdownEditor/OnFocus.tsx new file mode 100644 index 0000000..f2ffc68 --- /dev/null +++ b/src/components/MarkdownEditor/OnFocus.tsx @@ -0,0 +1,21 @@ + +import { useCommands } from "@remirror/react"; + +export const OnFocus = ({ + shouldFocus, + setShouldFocus +}: { + shouldFocus: boolean + setShouldFocus: (shouldFocus: boolean) => void +}) => { + const { focus } = useCommands(); + + if(shouldFocus){ + console.log('focus') + console.log(shouldFocus) + focus(); + setShouldFocus(false); + } + + return <>; +}; diff --git a/src/components/MarkdownEditor/RemirrorComponent.tsx b/src/components/MarkdownEditor/RemirrorComponent.tsx index fdf19bf..fcfe286 100644 --- a/src/components/MarkdownEditor/RemirrorComponent.tsx +++ b/src/components/MarkdownEditor/RemirrorComponent.tsx @@ -40,10 +40,14 @@ interface SetProgress { export const RemirrorComponent = ({ selectedFile, - setSelectedFile, + setSelectedFile, + shouldFocus, + setShouldFocus }: { selectedFile: DirectoryNode; setSelectedFile: (file: DirectoryNode) => void; + shouldFocus: boolean; + setShouldFocus: (shouldFocus: boolean) => void; }) => { const debouncedPersistMarkdown = useDebouncedCallback( // function @@ -140,6 +144,8 @@ export const RemirrorComponent = ({ customUploadHandler={customUploadHandler} selectedFile={selectedFile} setSelectedFile={setSelectedFile} + shouldFocus={shouldFocus} + setShouldFocus={setShouldFocus} >
diff --git a/src/components/MarkdownEditor/RemirrorMarkdownEditor.tsx b/src/components/MarkdownEditor/RemirrorMarkdownEditor.tsx index e6d63c5..b7e89b4 100644 --- a/src/components/MarkdownEditor/RemirrorMarkdownEditor.tsx +++ b/src/components/MarkdownEditor/RemirrorMarkdownEditor.tsx @@ -35,13 +35,14 @@ import { } from "@remirror/react"; import type { CreateEditorStateProps, Extension } from "remirror"; import type { RemirrorProps, UseThemeProps } from "@remirror/react"; -import { useExtensionEvent } from "@remirror/react"; import { OnChangeMarkdown } from "./OnChangeMarkdown"; import { DelayedImage, FileWithProgress } from "./RemirrorComponent"; import { RemirrorCustomToolbar } from "./RemirrorCustomToolbar/RemirrorCustomToolbar"; +import { OnClickLink } from "./OnClickLink"; import "./index.css"; import DirectoryNode from "../../models/DirectoryNode"; +import { OnFocus } from "./OnFocus"; interface ReactEditorProps extends Pick, @@ -52,43 +53,13 @@ interface ReactEditorProps customUploadHandler?: (files: FileWithProgress[]) => DelayedImage[]; setSelectedFile: (file: DirectoryNode) => void; selectedFile: DirectoryNode; + shouldFocus?: boolean; + setShouldFocus?: (shouldFocus: boolean) => void; } export interface MarkdownEditorProps extends Partial> {} -const OnClickLink = ({ - selectedFile, - setSelectedFile, -}: { - selectedFile: DirectoryNode | undefined; - setSelectedFile: (file: DirectoryNode) => void | undefined; -}) => { - useExtensionEvent( - LinkExtension, - "onClick", - useCallback((_, data) => { - if (!selectedFile) return false; - - const urlWithoutProtocol = data.href.replace(/(^\w+:|^)\/\//, ""); //remove protocol, even when remirror adds just // - const urlWithHttps = "https://" + urlWithoutProtocol; - const url = new URL(urlWithHttps); - if (!url.host.startsWith(".")) { - //open in new tab - window.open(urlWithHttps, "_blank"); // Open the link in a new tab or window - return true; - } - const filePath = decodeURI(urlWithoutProtocol); - setSelectedFile( - selectedFile.findNodeByRelativePath(filePath)?.getCopy() ?? selectedFile - ); - return true; - }, []) - ); - - return <>; -}; - /** * The editor which is used to create the annotation. Supports formatting. */ @@ -102,6 +73,8 @@ export const RemirrorMarkdownEditor: FC< customUploadHandler, selectedFile, setSelectedFile, + shouldFocus, + setShouldFocus, ...rest }) => { const extensions: () => Extension[] = useCallback( @@ -150,7 +123,7 @@ export const RemirrorMarkdownEditor: FC< classNames={["overflow-hidden", "outline-none", "prose", "prose-zinc"]} > {/* */} - + { @@ -161,6 +134,10 @@ export const RemirrorMarkdownEditor: FC< selectedFile={selectedFile} setSelectedFile={setSelectedFile || (() => {})} /> + {})} + /> {children} ); diff --git a/src/models/DirectoryNode.ts b/src/models/DirectoryNode.ts index b62969e..16e6ccb 100644 --- a/src/models/DirectoryNode.ts +++ b/src/models/DirectoryNode.ts @@ -48,6 +48,7 @@ class DirectoryNode { return ( removeMdExtension(this.fileHandle?.name) || removeMdExtension(this.directoryHandle?.name) || + this.name || null ); } @@ -419,6 +420,48 @@ class DirectoryNode { return currentNode; }; + findChildByPath = (path: string): DirectoryNode | undefined => { + console.log("relative path:", path); + // Split the relative path into individual segments + const pathSegments = path.split("/"); + + // Start from the current node + let currentNode: DirectoryNode | undefined = this; + + for (let segment of pathSegments) { + // Handle parent directory indicator ".." + if (segment === "." || segment === this.name) { + continue; + } + if (segment === "..") { + if (!currentNode?.parent) { + // If the current node does not have a parent, return null, we weren't able to find the node + return undefined; + } + currentNode = currentNode?.parent; + } else { + // Find the child node with the matching name + const matchingChild : DirectoryNode | undefined = currentNode?.children.find( + (child) => child.name === segment + ); + + if (!matchingChild) { + // If the child is not found or is not a directory, return undefined + return undefined; + } + + if (matchingChild?.isDirectory()) { + currentNode = matchingChild; + continue; + } + + currentNode = matchingChild; + } + } + + return currentNode; + } + async delete(skipConfirmation?: boolean): Promise { //return parent if it exists if (!skipConfirmation) { diff --git a/src/pages/Editor.tsx b/src/pages/Editor.tsx index 64b02ce..4ac089a 100644 --- a/src/pages/Editor.tsx +++ b/src/pages/Editor.tsx @@ -6,16 +6,14 @@ import { PanelLeftOpen, PanelLeftClose } from "lucide-react"; import { WorkspaceSelector } from "../components/FileSystemAdapters/FileSystem/WorkspaceSelector"; export const EditorPage: React.FC = () => { - const [selectedFile, setSelectedFile] = useState( - undefined - ); // [selectedFile, setSelectedFile + const [selectedFile, setSelectedFile] = useState(null); // [selectedFile, setSelectedFile const [selectedDirectory, setSelectedDirectory] = useState(null); const [panelIsOpen, setPanelIsOpen] = useState(true); - const handleSetSelectedFile = async (node: DirectoryNode | undefined) => { - if (!node) return; + const handleSetSelectedFile = async (node: DirectoryNode | null) => { + if (!node) return setSelectedFile(null); await node.loadFileContent(); setSelectedFile(node); }; @@ -33,7 +31,7 @@ export const EditorPage: React.FC = () => {