-
Notifications
You must be signed in to change notification settings - Fork 15
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
11 changed files
with
195 additions
and
12 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
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
97 changes: 97 additions & 0 deletions
97
packages/picasso/src/LexicalEditor/plugins/CustomOnChangePlugin.ts
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,97 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
'use strict' | ||
|
||
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext' | ||
import type { EditorState, LexicalEditor } from 'lexical' | ||
|
||
const react = require('react') | ||
|
||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
const CAN_USE_DOM = | ||
typeof window !== 'undefined' && | ||
typeof window.document !== 'undefined' && | ||
typeof window.document.createElement !== 'undefined' | ||
|
||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
const useLayoutEffectImpl = CAN_USE_DOM | ||
? react.useLayoutEffect | ||
: react.useEffect | ||
const useLayoutEffect = useLayoutEffectImpl | ||
|
||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
const CustomOnChangePlugin = ({ | ||
ignoreHistoryMergeTagChange = true, | ||
ignoreSelectionChange = false, | ||
triggerOnChangeOnStart = false, | ||
onChange, | ||
}: { | ||
ignoreHistoryMergeTagChange?: boolean | ||
ignoreSelectionChange?: boolean | ||
triggerOnChangeOnStart?: boolean | ||
onChange: ( | ||
editorState: EditorState, | ||
editor: LexicalEditor, | ||
tags: Set<string> | ||
) => void | ||
}) => { | ||
const [editor] = useLexicalComposerContext() | ||
|
||
useLayoutEffect(() => { | ||
if (onChange) { | ||
return editor.registerUpdateListener( | ||
({ | ||
editorState, | ||
dirtyElements, | ||
dirtyLeaves, | ||
prevEditorState, | ||
tags, | ||
}) => { | ||
if (triggerOnChangeOnStart && prevEditorState.isEmpty()) { | ||
onChange(editorState, editor, tags) | ||
|
||
return | ||
} | ||
|
||
if ( | ||
(ignoreSelectionChange && | ||
dirtyElements.size === 0 && | ||
dirtyLeaves.size === 0) || | ||
(ignoreHistoryMergeTagChange && tags.has('history-merge')) || | ||
prevEditorState.isEmpty() | ||
) { | ||
return | ||
} | ||
|
||
onChange(editorState, editor, tags) | ||
} | ||
) | ||
} | ||
}, [editor, ignoreHistoryMergeTagChange, ignoreSelectionChange, onChange]) | ||
|
||
return null | ||
} | ||
|
||
export default CustomOnChangePlugin |
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 @@ | ||
export { default as CustomOnChangePlugin } from './CustomOnChangePlugin' |
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,10 @@ | ||
import type { HastNode } from 'hast-util-to-dom/lib' | ||
import toHtml from 'hast-util-to-html' | ||
|
||
import type { ASTType } from '../../RichText' | ||
|
||
export const getDomValue = (value: ASTType) => { | ||
const parser = new DOMParser() | ||
|
||
return parser.parseFromString(toHtml(value as HastNode), 'text/html') | ||
} |
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
41 changes: 41 additions & 0 deletions
41
packages/picasso/src/LexicalEditor/utils/setEditorValue.ts
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,41 @@ | ||
import type { LexicalEditor as LexicalEditorType } from 'lexical' | ||
import { | ||
$createParagraphNode, | ||
$isLineBreakNode, | ||
$getRoot, | ||
$isDecoratorNode, | ||
$isElementNode, | ||
} from 'lexical' | ||
import { $generateNodesFromDOM } from '@lexical/html' | ||
|
||
import type { ASTType } from '../../RichText' | ||
import { getDomValue } from './getDomValue' | ||
|
||
export const setEditorValue = (editor: LexicalEditorType, value: ASTType) => { | ||
const domValue = getDomValue(value) | ||
|
||
editor.update( | ||
() => { | ||
const root = $getRoot() | ||
const lexicalValueNodes = $generateNodesFromDOM(editor, domValue) | ||
|
||
lexicalValueNodes.forEach(node => { | ||
if ($isElementNode(node) || $isDecoratorNode(node)) { | ||
root.append(node) | ||
} else if ($isLineBreakNode(node)) { | ||
const paragraphNode = $createParagraphNode() | ||
|
||
root.append(paragraphNode) | ||
} else { | ||
const paragraphNode = $createParagraphNode() | ||
|
||
paragraphNode.append(node) | ||
root.append(paragraphNode) | ||
} | ||
}) | ||
}, | ||
{ | ||
discrete: true, | ||
} | ||
) | ||
} |
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