-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #341 from MovieReviewComment/feature/issue-302/pla…
…ceholder [#302] Add Placeholder Plugin
- Loading branch information
Showing
8 changed files
with
160 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'; | ||
import { HeadingNode } from '@lexical/rich-text'; | ||
import { mergeRegister } from '@lexical/utils'; | ||
import { useEffect } from 'react'; | ||
|
||
import '@/editor/plugins/placeholder/styles.css'; | ||
|
||
import { | ||
$removePlaceholderFromParagraphNodes, | ||
$setPlaceholderOnCreate, | ||
$setPlaceholderOnSelectedParagraphNode, | ||
} from '@/lib/utils/editor/placeholder'; | ||
|
||
export function PlaceholderPlugin() { | ||
const [editor] = useLexicalComposerContext(); | ||
|
||
useEffect(() => { | ||
return mergeRegister( | ||
editor.registerMutationListener(HeadingNode, (nodes) => { | ||
// Set placeholder to heading nodes on create | ||
$setPlaceholderOnCreate(nodes, editor); | ||
}), | ||
editor.registerUpdateListener(({ editorState }) => { | ||
editorState.read(() => { | ||
// Remove placeholder from all paragraph nodes | ||
$removePlaceholderFromParagraphNodes(editor); | ||
// Set placeholder for the currently selected paragraph node | ||
$setPlaceholderOnSelectedParagraphNode(editor); | ||
}); | ||
}) | ||
); | ||
}, [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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.node-placeholder:has(br):not(:has(span))::before { | ||
position: absolute; | ||
content: attr(data-placeholder); | ||
opacity: 0.5; | ||
cursor: text; | ||
} |
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,6 @@ | ||
export const DATA_PLACEHOLDER = 'data-placeholder'; | ||
export const PLACEHOLDER_CLASS_NAME = 'node-placeholder'; | ||
|
||
export const PARAGRAPH_PLACEHOLDER = 'Type something...'; | ||
export const HEADING_PLACEHOLDER = 'Heading'; | ||
export const DEFAULT_PLACEHOLDER = 'Begin writing your review...'; |
File renamed without changes.
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,28 @@ | ||
import type { EditorState, Klass, LexicalEditor, LexicalNode } from 'lexical'; | ||
|
||
// based on the implementation of $nodesOfType from '@lexical/utils' | ||
function $forEachNodesOfType<T extends LexicalNode>( | ||
editorState: EditorState, | ||
klass: Klass<T>, | ||
callback: (node: T) => void | ||
) { | ||
const readOnly = editorState._readOnly; | ||
const klassType = klass.getType(); | ||
const nodes = editorState._nodeMap; | ||
for (const node of nodes.values()) { | ||
if (node instanceof klass && node.__type === klassType && (readOnly || node.isAttached())) { | ||
callback(node as T); | ||
} | ||
} | ||
} | ||
|
||
export function $removeClassesFromNodesOfType<T extends LexicalNode>( | ||
editor: LexicalEditor, | ||
klass: Klass<T>, | ||
...classNames: string[] | ||
) { | ||
$forEachNodesOfType(editor.getEditorState(), klass, (node) => { | ||
const element = editor.getElementByKey(node.getKey()); | ||
element?.classList.remove(...classNames); | ||
}); | ||
} |
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,78 @@ | ||
import { | ||
$getRoot, | ||
$getSelection, | ||
$isParagraphNode, | ||
$isRangeSelection, | ||
ParagraphNode, | ||
} from 'lexical'; | ||
import type { LexicalEditor, LexicalNode, NodeKey, NodeMutation } from 'lexical'; | ||
|
||
import { | ||
DATA_PLACEHOLDER, | ||
HEADING_PLACEHOLDER, | ||
PARAGRAPH_PLACEHOLDER, | ||
PLACEHOLDER_CLASS_NAME, | ||
} from '@/lib/constants/editor'; | ||
import { $removeClassesFromNodesOfType } from '@/lib/utils/editor/nodes-of-type'; | ||
|
||
export function $removePlaceholderFromParagraphNodes(editor: LexicalEditor) { | ||
return $removeClassesFromNodesOfType(editor, ParagraphNode, PLACEHOLDER_CLASS_NAME); | ||
} | ||
|
||
function $getPlaceholderText(element: HTMLElement) { | ||
const tag = element.tagName; | ||
|
||
if (tag.startsWith('H')) { | ||
const level = tag.charAt(tag.length - 1); | ||
return `${HEADING_PLACEHOLDER}${level}`; | ||
} | ||
|
||
if (tag === 'P') { | ||
return PARAGRAPH_PLACEHOLDER; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
function $setPlaceholder(nodeKey: string, editor: LexicalEditor) { | ||
const element = editor.getElementByKey(nodeKey); | ||
|
||
if (element === null) { | ||
return; | ||
} | ||
|
||
const placeholder = $getPlaceholderText(element); | ||
|
||
if (placeholder === null) { | ||
return; | ||
} | ||
|
||
element.classList.add(PLACEHOLDER_CLASS_NAME); | ||
element.setAttribute(DATA_PLACEHOLDER, placeholder); | ||
} | ||
|
||
export function $setPlaceholderOnCreate(nodes: Map<NodeKey, NodeMutation>, editor: LexicalEditor) { | ||
for (const [key, mutation] of nodes) { | ||
if (mutation === 'created') { | ||
$setPlaceholder(key, editor); | ||
} | ||
} | ||
} | ||
|
||
// Set placeholder when the selected node is paragraph node and not the first child of the root node, | ||
// Otherwise we use the default placeholder from the <RichTextPlugin /> | ||
function $shouldSetPlaceholder(node: LexicalNode) { | ||
return $isParagraphNode(node) && node !== $getRoot().getFirstChild(); | ||
} | ||
|
||
export function $setPlaceholderOnSelectedParagraphNode(editor: LexicalEditor) { | ||
const selection = $getSelection(); | ||
if (!$isRangeSelection(selection)) { | ||
return; | ||
} | ||
|
||
const targetNode = selection.anchor.getNode(); | ||
if ($shouldSetPlaceholder(targetNode)) { | ||
$setPlaceholder(targetNode.getKey(), editor); | ||
} | ||
} |