Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Lexical/Core/Editor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ public class Editor: NSObject {

// MARK: Node Transforms

/// Adds a Transform, allowing you to make changes in response to an EditorState update.
/// Registers a Transform, allowing you to make changes in response to an EditorState update.
/// - Parameters:
/// - nodeType: The node type you want to listen for changes to
/// - transform: Code to run allowing you to further modify the node
Expand All @@ -841,7 +841,7 @@ public class Editor: NSObject {
/// followed by an update. This is highly discouraged as it triggers an additional reconciliation pass. Additionally, each
/// cycle creates a brand new EditorState object which can interfere with plugins like HistoryPlugin (undo-redo)
/// if not handled correctly.
public func addNodeTransform(nodeType: NodeType, transform: @escaping NodeTransform) -> () -> Void {
public func registerNodeTransform(nodeType: NodeType, transform: @escaping NodeTransform) -> () -> Void {
// NB: In the web code, closures can be compared for identity but in Swift, closures are
// by design not Equatable. Therefore, we generate a tag for each closure passed in
// and use that for our removal/cleanup logic.
Expand All @@ -867,6 +867,11 @@ public class Editor: NSObject {
}
}
}

@available(*, deprecated, renamed: "registerNodeTransform(nodeType:transform:)")
public func addNodeTransform(nodeType: NodeType, transform: @escaping NodeTransform) -> () -> Void {
registerNodeTransform(nodeType: nodeType, transform: transform)
}

internal func parseEditorState(json: Data) throws -> EditorState {
let previousActiveEditorState = self.editorState
Expand Down
2 changes: 1 addition & 1 deletion Lexical/Documentation.docc/Editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
- ``registerTextContentListener(listener:)``
- ``registerCommand(type:listener:priority:)``
- ``registerNode(nodeType:constructor:)``
- ``addNodeTransform(nodeType:transform:)``
- ``registerNodeTransform(nodeType:transform:)``
- ``registerCustomDrawing(customAttribute:layer:granularity:handler:)``
- ``registerErrorListener(listener:)``

Expand Down
2 changes: 1 addition & 1 deletion Lexical/Documentation.docc/QuickStart.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ If you want to make programmatic changes to the content of your editor, there ar

* Trigger an update with ``Editor/update(_:)``
* Setting the editor state via ``Editor/setEditorState(_:)``
* Applying a change as part of an existing update via ``Editor/addNodeTransform(nodeType:transform:)``
* Applying a change as part of an existing update via ``Editor/registerNodeTransform(nodeType:transform:)``
* Using a command listener with ``Editor/registerCommand(type:listener:priority:)``

The most common way to update the editor is to use ``Editor/update(_:)``. Calling this function requires a closure to be passed in that will provide access to mutate the underlying editor state. When starting a fresh update, the current editor state is cloned and used as the starting point. From a technical perspective, this means that Lexical leverages a technique called double-buffering during updates. There's an editor state to represent what is current on the screen, and another work-in-progress editor state that represents future changes.
Expand Down
6 changes: 3 additions & 3 deletions LexicalTests/Tests/TransformsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class TransformTests: XCTestCase {
for (index, key) in transforms.enumerated() {
let nextKey = index < transforms.count - 1 ? transforms[index + 1] : TransformTests.lastTransformKey

let teardown = editor.addNodeTransform(nodeType: NodeType.text, transform: { [weak self] node in
let teardown = editor.registerNodeTransform(nodeType: NodeType.text, transform: { [weak self] node in
guard let strongSelf = self else {
XCTFail("strongSelf reference not found for text transform")
return
Expand All @@ -70,7 +70,7 @@ class TransformTests: XCTestCase {
teardowns.append(teardown)
}

let infiniteTransform = editor.addNodeTransform(nodeType: NodeType.text, transform: { [weak self] node in
let infiniteTransform = editor.registerNodeTransform(nodeType: NodeType.text, transform: { [weak self] node in
guard let strongSelf = self else {
XCTFail("strongSelf reference not found for text transform")
return
Expand All @@ -91,7 +91,7 @@ class TransformTests: XCTestCase {
}
})

let combinedTransform = editor.addNodeTransform(nodeType: NodeType.text, transform: { [weak self] node in
let combinedTransform = editor.registerNodeTransform(nodeType: NodeType.text, transform: { [weak self] node in
guard let strongSelf = self else {
XCTFail("strongSelf reference not found for text transform")
return
Expand Down