-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/open section in writer (#95)
* Very basic prototype working to open sections in writer * Open/close sections and update content in project json, save section history when switching between sections * Adding to test project, add animation to folder collapse * Prevent renaming to an existing section id, reset sectionHistory and activeSectionId upon rename * Add Section title to main pane * Updating test project
- Loading branch information
1 parent
06fb1f6
commit c039e67
Showing
15 changed files
with
458 additions
and
55 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,113 @@ | ||
import { useEffect, useState, CSSProperties } from 'react'; | ||
import { | ||
Plate, | ||
PlateProvider, | ||
usePlateEditorRef, | ||
createPlugins, | ||
} from '@udecode/plate'; | ||
import { ReactEditor } from 'slate-react'; | ||
import ScrollContainer from './ScrollContainer'; | ||
import useStore from 'renderer/store/useStore'; | ||
import { | ||
deserializePlainText, | ||
serializePlainText, | ||
createDeserializePlainTextPlugin, | ||
} from '../writer/serialize'; | ||
import { findItemDeep } from './TreeView/utilities'; | ||
|
||
const blankEditorValue = [ | ||
{ | ||
type: 'p', | ||
children: [ | ||
{ | ||
text: '', | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
const BasicWriterComp = () => { | ||
const editableProps = { | ||
style: { | ||
width: '100%', | ||
minHeight: '100%', | ||
boxSizing: 'border-box', | ||
paddingBottom: '10vh', | ||
display: 'flex', | ||
flexDirection: 'column', | ||
gap: '1em', | ||
} as CSSProperties, | ||
spellCheck: false, | ||
autoFocus: true, | ||
}; | ||
const activeSectionId = useStore((state) => state.activeSectionId); | ||
const [initialValue, setInitialValue] = useState(blankEditorValue); | ||
const [editorId, setEditorId] = useState(''); | ||
const editor = usePlateEditorRef(); | ||
|
||
const plugins = createPlugins([createDeserializePlainTextPlugin()]); | ||
|
||
useEffect(() => { | ||
if (editor) { | ||
const { sectionHistory } = useStore.getState(); | ||
const history = sectionHistory?.get(activeSectionId); | ||
if (history) { | ||
editor.history = JSON.parse(JSON.stringify(history)); | ||
} | ||
ReactEditor.focus(editor); | ||
} | ||
}); | ||
|
||
useEffect(() => { | ||
if (activeSectionId != '') { | ||
const { content } = useStore.getState(); | ||
const sectionContent = findItemDeep(content, activeSectionId)?.content; | ||
if (sectionContent) { | ||
const nodes = deserializePlainText(sectionContent); | ||
if (nodes.length) { | ||
setInitialValue(nodes); | ||
} | ||
} else { | ||
setInitialValue(blankEditorValue); | ||
} | ||
setEditorId(activeSectionId); | ||
} | ||
}, [activeSectionId]); | ||
|
||
const handleChange = () => { | ||
if (activeSectionId != '') { | ||
const { setSectionHistory } = useStore.getState(); | ||
const { updateSectionContent } = useStore.getState(); | ||
setSectionHistory(activeSectionId, editor.history); | ||
updateSectionContent( | ||
activeSectionId, | ||
serializePlainText(editor.children) | ||
); | ||
} | ||
}; | ||
|
||
return ( | ||
<ScrollContainer> | ||
<Plate | ||
key={editorId} | ||
id={editorId} | ||
plugins={plugins} | ||
editableProps={editableProps} | ||
onChange={handleChange} | ||
initialValue={initialValue} | ||
/> | ||
</ScrollContainer> | ||
); | ||
}; | ||
|
||
const BasicWriter = () => { | ||
const activeSectionId = useStore((state) => state.activeSectionId); | ||
|
||
return ( | ||
<PlateProvider id={activeSectionId}> | ||
<BasicWriterComp /> | ||
</PlateProvider> | ||
); | ||
}; | ||
|
||
export default BasicWriter; |
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
16 changes: 9 additions & 7 deletions
16
app/renderer/components/TreeView/components/TreeItem/SortableTreeItem.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.