-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from vimdotmd/refactor-2
Refactor: Extract components
- Loading branch information
Showing
52 changed files
with
176 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { FileState } from "~/src/components/file/state"; | ||
import { useEffect } from "react"; | ||
import { Editor } from "~src/components/editor/state/state"; | ||
|
||
interface Params { | ||
setFileDirty: FileState["setDirty"]; | ||
editor: Editor | null; | ||
} | ||
|
||
export const useFileDirty = (params: Params): void => { | ||
const { setFileDirty, editor } = params; | ||
useEffect(() => { | ||
if (editor === null) return; | ||
const dirty = editor.onDidChangeModelContent(() => { | ||
setFileDirty(true); | ||
}); | ||
return () => void dirty.dispose(); | ||
}, [editor]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { FileHandle, FileState } from "~/src/components/file/state"; | ||
import { fileSystem } from "~/src/components/file/system"; | ||
import * as monaco from "monaco-editor"; | ||
import { useEffect } from "react"; | ||
import { Editor, EditorModel } from "~src/components/editor/state/state"; | ||
|
||
interface Params { | ||
editor: Editor | null; | ||
fileHandle: FileState["handle"]; | ||
} | ||
|
||
const loadFile = (handle: FileHandle, editor: Editor): (() => void) => { | ||
let model: null | EditorModel = null; | ||
fileSystem.read(handle).then((text) => { | ||
model = monaco.editor.createModel(text, "markdown"); | ||
editor.setModel(model); | ||
}); | ||
return () => void model?.dispose(); | ||
}; | ||
|
||
export const useFileLoad = (params: Params): void => { | ||
const { fileHandle, editor } = params; | ||
|
||
useEffect(() => { | ||
if (fileHandle === null) return; | ||
if (editor === null) throw Error("Editor is not created"); | ||
const dispose = loadFile(fileHandle, editor); | ||
return () => void dispose(); | ||
}, [fileHandle, editor]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useEffect, useState } from "react"; | ||
import { SetState } from "~/src/utils/state"; | ||
import { set } from "idb-keyval"; | ||
|
||
export type FileHandle = FileSystemFileHandle; | ||
|
||
export interface FileState { | ||
handle: FileHandle | null; | ||
setHandle: SetState<FileHandle | null>; | ||
dirty: boolean; | ||
setDirty: SetState<boolean>; | ||
} | ||
|
||
const useSaveHandle = (handle: FileHandle | null): void => { | ||
useEffect(() => { | ||
if (handle !== null) set("handle", handle); | ||
}, [handle]); | ||
}; | ||
|
||
export const useFile = (): FileState => { | ||
const [handle, setHandle] = useState<FileHandle | null>(null); | ||
const [dirty, setDirty] = useState(false); | ||
|
||
useSaveHandle(handle); | ||
|
||
return { handle, setHandle, dirty, setDirty }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 12 additions & 8 deletions
20
src/app/toolbar/open.tsx → src/components/toolbar/open.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.