feat: Allow exporting multiple Super notes and select what format to export them to (#2191)

This commit is contained in:
Aman Harwara
2023-02-01 00:47:28 +05:30
committed by GitHub
parent 5c17ae3c4e
commit 506a1e83f1
12 changed files with 185 additions and 9 deletions

View File

@@ -41,6 +41,7 @@ import ReadonlyPlugin from './Plugins/ReadonlyPlugin/ReadonlyPlugin'
import { SuperSearchContextProvider } from './Plugins/SearchPlugin/Context'
import { SearchPlugin } from './Plugins/SearchPlugin/SearchPlugin'
import ModalOverlay from '@/Components/Modal/ModalOverlay'
import { SuperEditorNodes } from './SuperEditorNodes'
const NotePreviewCharLimit = 160
@@ -170,7 +171,7 @@ export const SuperEditor: FunctionComponent<Props> = ({
<BlocksEditorComposer
readonly={note.current.locked}
initialValue={note.current.text}
nodes={[FileNode, BubbleNode]}
nodes={SuperEditorNodes}
>
<BlocksEditor
onChange={handleChange}

View File

@@ -0,0 +1,4 @@
import { FileNode } from './Plugins/EncryptedFilePlugin/Nodes/FileNode'
import { BubbleNode } from './Plugins/ItemBubblePlugin/Nodes/BubbleNode'
export const SuperEditorNodes = [FileNode, BubbleNode]

View File

@@ -0,0 +1,46 @@
import { createHeadlessEditor } from '@lexical/headless'
import { $convertToMarkdownString } from '@lexical/markdown'
import { MarkdownTransformers } from '@standardnotes/blocks-editor'
import { $generateHtmlFromNodes } from '@lexical/html'
import { BlockEditorNodes } from '@standardnotes/blocks-editor/src/Lexical/Nodes/AllNodes'
import BlocksEditorTheme from '@standardnotes/blocks-editor/src/Lexical/Theme/Theme'
import { SNNote } from '@standardnotes/models'
import { SuperEditorNodes } from './SuperEditorNodes'
export const exportSuperNote = (note: SNNote, format: 'txt' | 'md' | 'html' | 'json') => {
const headlessEditor = createHeadlessEditor({
namespace: 'BlocksEditor',
theme: BlocksEditorTheme,
editable: false,
onError: (error: Error) => console.error(error),
nodes: [...SuperEditorNodes, ...BlockEditorNodes],
})
headlessEditor.setEditorState(headlessEditor.parseEditorState(note.text))
let content: string | undefined
headlessEditor.update(() => {
switch (format) {
case 'md':
content = $convertToMarkdownString(MarkdownTransformers)
break
case 'html':
content = $generateHtmlFromNodes(headlessEditor)
break
case 'json':
content = JSON.stringify(headlessEditor.toJSON())
break
case 'txt':
default:
content = note.text
break
}
})
if (!content) {
throw new Error('Could not export note')
}
return content
}