From e454a3d7fa7022d33dfea713fb60583721afa616 Mon Sep 17 00:00:00 2001 From: Amy Worrall Date: Sun, 26 Nov 2023 19:28:21 +0000 Subject: [PATCH] Renamed transform API to match Lexical JS --- Lexical/Core/Editor.swift | 9 +++++++-- Lexical/Documentation.docc/Editor.md | 2 +- Lexical/Documentation.docc/QuickStart.md | 2 +- LexicalTests/Tests/TransformsTests.swift | 6 +++--- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Lexical/Core/Editor.swift b/Lexical/Core/Editor.swift index f9cef6a9..424c4ba0 100644 --- a/Lexical/Core/Editor.swift +++ b/Lexical/Core/Editor.swift @@ -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 @@ -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. @@ -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 diff --git a/Lexical/Documentation.docc/Editor.md b/Lexical/Documentation.docc/Editor.md index 96fd05e4..d79418f8 100644 --- a/Lexical/Documentation.docc/Editor.md +++ b/Lexical/Documentation.docc/Editor.md @@ -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:)`` diff --git a/Lexical/Documentation.docc/QuickStart.md b/Lexical/Documentation.docc/QuickStart.md index 0dc729b6..eba01f54 100644 --- a/Lexical/Documentation.docc/QuickStart.md +++ b/Lexical/Documentation.docc/QuickStart.md @@ -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. diff --git a/LexicalTests/Tests/TransformsTests.swift b/LexicalTests/Tests/TransformsTests.swift index 6e3ab411..300ab60f 100644 --- a/LexicalTests/Tests/TransformsTests.swift +++ b/LexicalTests/Tests/TransformsTests.swift @@ -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 @@ -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 @@ -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