-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a75fb83
commit 285e135
Showing
6 changed files
with
354 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import * as vscode from 'vscode'; | ||
|
||
const StateKey = 'paper-documents'; | ||
|
||
interface Document { | ||
content: string; | ||
id: string; | ||
type: 'doc' | 'mindmap'; | ||
name: string; | ||
} | ||
|
||
interface State { | ||
activeDocument: null | string; | ||
documents: Document[]; | ||
} | ||
|
||
export class DocsManager { | ||
context: vscode.ExtensionContext; | ||
|
||
constructor(context: vscode.ExtensionContext) { | ||
this.context = context; | ||
} | ||
|
||
getActiveDocument(): Document { | ||
if (this.context.workspaceState.get(StateKey) === undefined) { | ||
const newDocId = new Date().valueOf().toString(); | ||
this.context.workspaceState.update(StateKey, { | ||
activeDocument: newDocId, | ||
documents: [ | ||
{ | ||
content: | ||
this.context.workspaceState.get('paper-content') || | ||
'', | ||
id: newDocId, | ||
type: 'doc', | ||
name: 'Untitled', | ||
}, | ||
], | ||
} as State); | ||
} | ||
|
||
const state: State = this.context.workspaceState.get(StateKey) as any; | ||
const activeDocument = state.documents.find( | ||
item => item.id === state.activeDocument | ||
); | ||
if (!activeDocument) { | ||
throw new Error('Active document not found.'); | ||
} | ||
return activeDocument; | ||
} | ||
|
||
setActiveDocument(id: string) { | ||
const state: State = this.context.workspaceState.get(StateKey) as any; | ||
this.context.workspaceState.update(StateKey, { | ||
...state, | ||
activeDocument: id, | ||
} as State); | ||
} | ||
|
||
updateDocument(id: string, content: string) { | ||
const state: State = this.context.workspaceState.get(StateKey) as any; | ||
const updatedDocuments = state.documents.map(item => { | ||
if (item.id === id) { | ||
return { | ||
...item, | ||
content, | ||
}; | ||
} | ||
return item; | ||
}); | ||
|
||
const nextState: State = { | ||
...state, | ||
documents: updatedDocuments, | ||
}; | ||
this.context.workspaceState.update(StateKey, nextState); | ||
} | ||
|
||
createDocument(name: string, content: string, type: 'doc' | 'mindmap') { | ||
const state: State = this.context.workspaceState.get(StateKey) as any; | ||
const nextState: State = { | ||
...state, | ||
documents: [ | ||
...state.documents, | ||
{ | ||
id: new Date().valueOf().toString(), | ||
content, | ||
type, | ||
name, | ||
}, | ||
], | ||
}; | ||
this.context.workspaceState.update(StateKey, nextState); | ||
} | ||
|
||
deleteDocument(id: string) { | ||
const state: State = this.context.workspaceState.get(StateKey) as any; | ||
const nextState: State = { | ||
...state, | ||
documents: state.documents.filter(item => item.id !== id), | ||
}; | ||
this.context.workspaceState.update(StateKey, nextState); | ||
} | ||
|
||
getDocumentsList(): Document[] { | ||
const state: State = this.context.workspaceState.get(StateKey) as any; | ||
return state.documents; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { | ||
Button, | ||
Flex, | ||
Input, | ||
Popover, | ||
PopoverArrow, | ||
PopoverBody, | ||
PopoverCloseButton, | ||
PopoverContent, | ||
PopoverFooter, | ||
PopoverHeader, | ||
PopoverTrigger, | ||
Portal, | ||
} from '@chakra-ui/react'; | ||
import React, { useEffect, useState } from 'react'; | ||
import { PlusSquare, ArrowRight } from 'react-feather'; | ||
import { ClientDoc } from './types'; | ||
|
||
const API_URL = `http://localhost:${(window as any).port || '4545'}`; | ||
|
||
interface Props { | ||
onActiveDocumentChange: () => void; | ||
} | ||
|
||
export default function CreateDoc({ onActiveDocumentChange }: Props) { | ||
const [docs, setDocs] = useState<ClientDoc[]>([]); | ||
const [docName, setDocName] = useState(''); | ||
|
||
const getDocs = async () => { | ||
const response = await fetch(`${API_URL}/documents`); | ||
const documents = await response.json(); | ||
setDocs(documents); | ||
}; | ||
|
||
const changeActiveDocument = async (id: string) => { | ||
await fetch(`${API_URL}/changeActiveDocument`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
id, | ||
}), | ||
}); | ||
|
||
onActiveDocumentChange(); | ||
}; | ||
|
||
useEffect(() => { | ||
getDocs(); | ||
}, []); | ||
|
||
const createDoc = async (name: string) => { | ||
const response = await fetch(`${API_URL}/create`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
name, | ||
type: 'doc', | ||
}), | ||
}); | ||
const documents = await response.json(); | ||
setDocs(documents); | ||
}; | ||
|
||
return ( | ||
<Popover colorScheme="blackAlpha"> | ||
<PopoverTrigger> | ||
<Button | ||
size="sm" | ||
backgroundColor="#2F2E31" | ||
color="#f1f0ee" | ||
_hover={{ backgroundColor: '#090909' }} | ||
onClick={() => {}} | ||
> | ||
<PlusSquare size="18px" strokeWidth="2px" /> | ||
</Button> | ||
</PopoverTrigger> | ||
<Portal> | ||
<PopoverContent | ||
zIndex={9999999999} | ||
backgroundColor="#2F2E31" | ||
colorScheme="blackAlpha" | ||
> | ||
<PopoverArrow backgroundColor="#2F2E31" /> | ||
<PopoverHeader>Docs</PopoverHeader> | ||
<PopoverCloseButton /> | ||
<PopoverBody> | ||
{docs.map(item => ( | ||
<Flex | ||
_hover={{ | ||
backgroundColor: '#090909', | ||
}} | ||
cursor="pointer" | ||
padding="3px" | ||
borderRadius="2px" | ||
onClick={() => { | ||
changeActiveDocument(item.id); | ||
}} | ||
> | ||
{item.name || item.id} | ||
</Flex> | ||
))} | ||
</PopoverBody> | ||
<PopoverFooter> | ||
<Flex> | ||
<Input | ||
size="medium" | ||
borderRadius="3px" | ||
padding="2px" | ||
variant="outline" | ||
placeholder="Name your doc" | ||
value={docName} | ||
onChange={e => setDocName(e.target.value)} | ||
/> | ||
<Button | ||
size="sm" | ||
backgroundColor="#2F2E31" | ||
color="#f1f0ee" | ||
_hover={{ backgroundColor: '#090909' }} | ||
onClick={() => { | ||
createDoc(docName); | ||
}} | ||
isDisabled={docName.trim() === ''} | ||
> | ||
<ArrowRight size="18px" strokeWidth="2px" /> | ||
</Button> | ||
</Flex> | ||
</PopoverFooter> | ||
</PopoverContent> | ||
</Portal> | ||
</Popover> | ||
); | ||
} |
Oops, something went wrong.