-
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.
feat: improve $removeClassesFromNodesOfType
- Loading branch information
Showing
3 changed files
with
30 additions
and
22 deletions.
There are no files selected for viewing
20 changes: 0 additions & 20 deletions
20
ui/src/lib/utils/editor/$remove-class-from-nodes-of-type.ts
This file was deleted.
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
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