-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Figma Embed to Playground (#2705)
- Loading branch information
1 parent
3a8a531
commit 64d7bb2
Showing
7 changed files
with
217 additions
and
2 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,130 @@ | ||
/** | ||
* 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. | ||
* | ||
*/ | ||
|
||
import type { | ||
EditorConfig, | ||
ElementFormatType, | ||
LexicalEditor, | ||
LexicalNode, | ||
NodeKey, | ||
Spread, | ||
} from 'lexical'; | ||
|
||
import {BlockWithAlignableContents} from '@lexical/react/LexicalBlockWithAlignableContents'; | ||
import { | ||
DecoratorBlockNode, | ||
SerializedDecoratorBlockNode, | ||
} from '@lexical/react/LexicalDecoratorBlockNode'; | ||
import * as React from 'react'; | ||
|
||
type FigmaComponentProps = Readonly<{ | ||
className: Readonly<{ | ||
base: string; | ||
focus: string; | ||
}>; | ||
format: ElementFormatType | null; | ||
nodeKey: NodeKey; | ||
documentID: string; | ||
}>; | ||
|
||
function FigmaComponent({ | ||
className, | ||
format, | ||
nodeKey, | ||
documentID, | ||
}: FigmaComponentProps) { | ||
return ( | ||
<BlockWithAlignableContents | ||
className={className} | ||
format={format} | ||
nodeKey={nodeKey}> | ||
<iframe | ||
width="560" | ||
height="315" | ||
src={`https://www.figma.com/embed?embed_host=lexical&url=\ | ||
https://www.figma.com/file/${documentID}`} | ||
allowFullScreen={true} | ||
/> | ||
</BlockWithAlignableContents> | ||
); | ||
} | ||
|
||
export type SerializedFigmaNode = Spread< | ||
{ | ||
documentID: string; | ||
type: 'figma'; | ||
version: 1; | ||
}, | ||
SerializedDecoratorBlockNode | ||
>; | ||
|
||
export class FigmaNode extends DecoratorBlockNode { | ||
__id: string; | ||
|
||
static getType(): string { | ||
return 'figma'; | ||
} | ||
|
||
static clone(node: FigmaNode): FigmaNode { | ||
return new FigmaNode(node.__id, node.__format, node.__key); | ||
} | ||
|
||
static importJSON(serializedNode: SerializedFigmaNode): FigmaNode { | ||
const node = $createFigmaNode(serializedNode.documentID); | ||
node.setFormat(serializedNode.format); | ||
return node; | ||
} | ||
|
||
exportJSON(): SerializedFigmaNode { | ||
return { | ||
...super.exportJSON(), | ||
documentID: this.__id, | ||
type: 'figma', | ||
version: 1, | ||
}; | ||
} | ||
|
||
constructor(id: string, format?: ElementFormatType, key?: NodeKey) { | ||
super(format, key); | ||
this.__id = id; | ||
} | ||
|
||
updateDOM(): false { | ||
return false; | ||
} | ||
|
||
decorate(_editor: LexicalEditor, config: EditorConfig): JSX.Element { | ||
const embedBlockTheme = config.theme.embedBlock || {}; | ||
const className = { | ||
base: embedBlockTheme.base || '', | ||
focus: embedBlockTheme.focus || '', | ||
}; | ||
return ( | ||
<FigmaComponent | ||
className={className} | ||
format={this.__format} | ||
nodeKey={this.getKey()} | ||
documentID={this.__id} | ||
/> | ||
); | ||
} | ||
|
||
isTopLevel(): true { | ||
return true; | ||
} | ||
} | ||
|
||
export function $createFigmaNode(documentID: string): FigmaNode { | ||
return new FigmaNode(documentID); | ||
} | ||
|
||
export function $isFigmaNode( | ||
node: FigmaNode | LexicalNode | null | undefined, | ||
): node is FigmaNode { | ||
return node instanceof FigmaNode; | ||
} |
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
38 changes: 38 additions & 0 deletions
38
packages/lexical-playground/src/plugins/FigmaPlugin/index.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* 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. | ||
* | ||
*/ | ||
|
||
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext'; | ||
import {$insertBlockNode} from '@lexical/utils'; | ||
import {COMMAND_PRIORITY_EDITOR, createCommand, LexicalCommand} from 'lexical'; | ||
import {useEffect} from 'react'; | ||
|
||
import {$createFigmaNode, FigmaNode} from '../../nodes/FigmaNode'; | ||
|
||
export const INSERT_FIGMA_COMMAND: LexicalCommand<string> = createCommand(); | ||
|
||
export default function FigmaPlugin(): JSX.Element | null { | ||
const [editor] = useLexicalComposerContext(); | ||
|
||
useEffect(() => { | ||
if (!editor.hasNodes([FigmaNode])) { | ||
throw new Error('FigmaPlugin: FigmaNode not registered on editor'); | ||
} | ||
|
||
return editor.registerCommand<string>( | ||
INSERT_FIGMA_COMMAND, | ||
(payload) => { | ||
const figmaNode = $createFigmaNode(payload); | ||
$insertBlockNode(figmaNode); | ||
return true; | ||
}, | ||
COMMAND_PRIORITY_EDITOR, | ||
); | ||
}, [editor]); | ||
|
||
return null; | ||
} |