-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
7 changed files
with
132 additions
and
20 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,26 @@ | ||
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext'; | ||
import { | ||
COMMAND_PRIORITY_LOW, | ||
createCommand, | ||
} from 'lexical'; | ||
import {useEffect} from 'react'; | ||
|
||
export const SAVE_COMMAND = createCommand('SAVE_COMMAND'); | ||
|
||
export function SavePlugin({ onSave = () => {} }) { | ||
const [editor] = useLexicalComposerContext(); | ||
|
||
useEffect(() => { | ||
editor.registerCommand( | ||
SAVE_COMMAND, | ||
() => { | ||
console.log('save command fired'); | ||
onSave(editor.getEditorState()); | ||
}, | ||
COMMAND_PRIORITY_LOW, | ||
); | ||
|
||
}, [editor]); | ||
|
||
return null; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React, { useEffect } from "react"; | ||
|
||
import { | ||
$getSelection, | ||
$getRoot, | ||
$createParagraphNode, | ||
$createTextNode, | ||
$setSelection} from "lexical"; | ||
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext"; | ||
|
||
import { OnChangePlugin } from "@lexical/react/LexicalOnChangePlugin"; | ||
|
||
export const ControlledValuePlugin = ({ value, onChange, isRichtext, format }) => { | ||
useAdoptPlaintextValue(value, isRichtext); | ||
|
||
const handleChange = (editorState) => { | ||
editorState.read(() => { | ||
onChange(editorState); | ||
}); | ||
}; | ||
|
||
return <OnChangePlugin onChange={handleChange} />; | ||
}; | ||
|
||
export const useAdoptPlaintextValue = (value, isRichtext) => { | ||
const [editor] = useLexicalComposerContext(); | ||
|
||
useEffect(() => { | ||
editor.update(() => { | ||
console.log(`Value updated %o`, value); | ||
if (isRichtext) { | ||
const paragraphNode = $createParagraphNode(); | ||
const textNode = $createTextNode(value); | ||
paragraphNode.append(textNode); | ||
$getRoot().clear(); | ||
$getRoot().append(paragraphNode); | ||
} else { | ||
const root = $getRoot(); | ||
root.append(paragraphNode); | ||
const initialSelection = $getSelection()?.clone() ?? null; | ||
$getRoot().clear(); | ||
$getRoot().select(); // for some reason this is not even necessary | ||
$getSelection()?.insertText(value); | ||
$setSelection(initialSelection); | ||
} | ||
}); | ||
}, [value, editor]); | ||
}; | ||
|
||
// export const useAdoptPlaintextValue = (value: string) => { | ||
// const [editor] = useLexicalComposerContext(); | ||
|
||
// useEffect(() => { | ||
// editor.update(() => { | ||
// const paragraph = $createParagraphNode(); | ||
// const text = $createTextNode(value); | ||
// paragraph.append(text); | ||
// $getRoot().clear(); | ||
// $getRoot().append(paragraph); | ||
// }); | ||
// }, [value, 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