Skip to content

Commit

Permalink
feat: improve $removeClassesFromNodesOfType
Browse files Browse the repository at this point in the history
  • Loading branch information
2wheeh committed Apr 20, 2024
1 parent 20d90ff commit 61c415d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
20 changes: 0 additions & 20 deletions ui/src/lib/utils/editor/$remove-class-from-nodes-of-type.ts

This file was deleted.

28 changes: 28 additions & 0 deletions ui/src/lib/utils/editor/nodes-of-type.ts
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);
});
}
4 changes: 2 additions & 2 deletions ui/src/lib/utils/editor/placeholder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import {
PARAGRAPH_PLACEHOLDER,
PLACEHOLDER_CLASS_NAME,
} from '@/lib/constants/editor';
import { $removeClassFromNodesOfType } from '@/lib/utils/editor/$remove-class-from-nodes-of-type';
import { $removeClassesFromNodesOfType } from '@/lib/utils/editor/nodes-of-type';

export function $removePlaceholderFromParagraphNodes(editor: LexicalEditor) {
return $removeClassFromNodesOfType(ParagraphNode, PLACEHOLDER_CLASS_NAME, editor);
return $removeClassesFromNodesOfType(editor, ParagraphNode, PLACEHOLDER_CLASS_NAME);
}

function $getPlaceholderText(node: LexicalNode) {
Expand Down

0 comments on commit 61c415d

Please sign in to comment.