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
1 change: 1 addition & 0 deletions Lexical/Core/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public enum DirtyType {
case fullReconcile
}

@available(*, deprecated, message: "Use new styles system")
@objc public enum TextFormatType: Int {
case bold
case italic
Expand Down
6 changes: 5 additions & 1 deletion Lexical/Core/Editor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ public class Editor: NSObject {

internal var nodeTransforms: [NodeType: [(Int, NodeTransform)]] = [:]

// Styles. For all methods to manipulate these, see Styles.swift
internal var registeredStyles: StylesRegistrationDict = [:]

// Used to help co-ordinate selection and events
internal var compositionKey: NodeKey?
public var dirtyType: DirtyType = .noDirtyNodes // TODO: I made this public to work around an issue in playground. @amyworrall
Expand Down Expand Up @@ -161,6 +164,7 @@ public class Editor: NSObject {
public static func createHeadless(editorConfig: EditorConfig) -> Editor {
let editor = Editor(editorConfig: editorConfig)
editor.headless = true
registerRichText(editor: editor)
return editor
}

Expand Down Expand Up @@ -398,7 +402,7 @@ public class Editor: NSObject {
if selection != nil {
try paragraph.select(anchorOffset: nil, focusOffset: nil)
if let selection = selection as? RangeSelection {
selection.clearFormat()
selection.clearStoredStyles()
}
}
}
Expand Down
20 changes: 9 additions & 11 deletions Lexical/Core/EditorState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,17 @@ public class EditorState: NSObject {
The JSON string is designed to be interoperable with Lexical JavaScript (subject to the individual node classes using matching keys).
*/
public func toJSON() throws -> String {
let string: String? = try read {
guard let rootNode = getRootNode() else {
throw LexicalError.invariantViolation("Could not get RootNode")
}
let persistedEditorState = SerializedEditorState(rootNode: rootNode)
let encodedData = try JSONEncoder().encode(persistedEditorState)
guard let jsonString = String(data: encodedData, encoding: .utf8) else { return "" }
return jsonString
guard getActiveEditor() != nil else {
throw LexicalError.invariantViolation("Requires editor to export JSON")
}
if let string {
return string
guard let rootNode = getRootNode() else {
throw LexicalError.invariantViolation("Could not get RootNode")
}
throw LexicalError.invariantViolation("Expected string")
let persistedEditorState = SerializedEditorState(rootNode: rootNode)
let encodedData = try JSONEncoder().encode(persistedEditorState)
guard let jsonString = String(data: encodedData, encoding: .utf8) else { throw LexicalError.invariantViolation("Expected string") }

return jsonString
}

/**
Expand Down
1 change: 1 addition & 0 deletions Lexical/Core/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public enum LexicalError: Error {
case sanityCheck(errorMessage: String, textViewText: String, fullReconcileText: String)
case reconciler(String)
case rangeCacheSearch(String)
case styleValidation(String)
}
14 changes: 5 additions & 9 deletions Lexical/Core/Events.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,8 @@ internal func onSelectionChange(editor: Editor) {
}

try lexicalSelection.applyNativeSelection(nativeSelection)
lexicalSelection.styles = (try? lexicalSelection.anchor.getNode().getStyles()) ?? [:]

switch lexicalSelection.anchor.type {
case .text:
guard let anchorNode = try lexicalSelection.anchor.getNode() as? TextNode else { break }
lexicalSelection.format = anchorNode.getFormat()
case .element:
lexicalSelection.format = TextFormat()
default:
break
}
editor.dispatchCommand(type: .selectionChange, payload: nil)
}
} catch {
Expand Down Expand Up @@ -217,6 +209,10 @@ internal func handleIndentAndOutdent(insertTab: (Node) -> Void, indentOrOutdent:

public func registerRichText(editor: Editor) {

// Style defaults and commands are handled in Styles.swift
registerDefaultStyles(editor: editor)
registerStyleCommands(editor: editor)

_ = editor.registerCommand(type: .insertLineBreak, listener: { [weak editor] payload in
guard let editor else { return false }
do {
Expand Down
16 changes: 13 additions & 3 deletions Lexical/Core/Nodes/CodeHighlightNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,31 @@ public class CodeHighlightNode: TextNode {

override public init() {
super.init()
self.type = NodeType.codeHighlight
}

required init(text: String, highlightType: String?, key: NodeKey? = nil) {
super.init(text: text, key: key)
self.highlightType = highlightType
self.type = NodeType.codeHighlight
}

public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
try super.init(from: decoder)

self.highlightType = try container.decode(String.self, forKey: .highlightType)
self.type = NodeType.codeHighlight
}

public required convenience init(text: String, key: NodeKey?) {
self.init(text: text, highlightType: nil, key: key)
}

public required init(styles: StylesDict, key: NodeKey?) {
fatalError("init(styles:key:) has not been implemented")
}

public override class func getType() -> NodeType {
return .codeHighlight
}

override public func encode(to encoder: Encoder) throws {
try super.encode(to: encoder)
Expand All @@ -48,7 +53,12 @@ public class CodeHighlightNode: TextNode {
}

// Prevent formatting (bold, underline, etc)
@available(*, deprecated)
override public func setFormat(format: TextFormat) throws -> CodeHighlightNode {
return try self.getWritable()
}

override public func setStyles(_ stylesDict: StylesDict) throws {}

override public func setStyle<T>(_ style: T.Type, _ value: T.StyleValueType?) throws where T : Style {}
}
11 changes: 8 additions & 3 deletions Lexical/Core/Nodes/CodeNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,26 @@ public class CodeNode: ElementNode {

override public init() {
super.init()
self.type = NodeType.code
}

required init(language: String, key: NodeKey? = nil) {
super.init(key)
self.language = language
self.type = NodeType.code
}

public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
try super.init(from: decoder)

self.language = try container.decode(String.self, forKey: .language)
self.type = NodeType.code
}

public required init(styles: StylesDict, key: NodeKey?) {
super.init(styles: styles, key: key)
}

public override class func getType() -> NodeType {
return .code
}

override public func encode(to encoder: Encoder) throws {
Expand Down
14 changes: 9 additions & 5 deletions Lexical/Core/Nodes/DecoratorNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,22 @@ import UIKit

*/
open class DecoratorNode: Node {
override public init() {
super.init()
public init() {
super.init(styles: [:], key: nil)
}

override public required init(_ key: NodeKey?) {
super.init(key)
public required init(_ key: NodeKey?) {
super.init(styles: [:], key: key)
}

public required init(from decoder: Decoder) throws {
try super.init(from: decoder)
}


public required init(styles: StylesDict, key: NodeKey?) {
super.init(styles: styles, key: key)
}

override open func encode(to encoder: Encoder) throws {
try super.encode(to: encoder)
}
Expand Down
15 changes: 9 additions & 6 deletions Lexical/Core/Nodes/ElementNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ open class ElementNode: Node {
return direction
}

override public init() {
super.init()
public init() {
super.init(styles: [:], key: nil)
}

override public init(_ key: NodeKey?) {
super.init(key)
public init(_ key: NodeKey?) {
super.init(styles: [:], key: key)
}

public required init(from decoder: Decoder) throws {
Expand Down Expand Up @@ -73,14 +73,17 @@ open class ElementNode: Node {
node.parent = self.key
}
}


public required init(styles: StylesDict, key: NodeKey?) {
super.init(styles: styles, key: key)
}

override open func encode(to encoder: Encoder) throws {
try super.encode(to: encoder)
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.getChildren(), forKey: .children)
try container.encode(self.direction, forKey: .direction)
try container.encode(self.indent, forKey: .indent)
try container.encode("", forKey: .format)
}

@discardableResult
Expand Down
15 changes: 10 additions & 5 deletions Lexical/Core/Nodes/HeadingNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,28 @@ public class HeadingNode: ElementNode {
self.tag = tag

super.init()
self.type = NodeType.heading
}

public required init(_ key: NodeKey?, tag: HeadingTagType) {
self.tag = tag
super.init(key)
self.type = NodeType.heading
}

override class public func getType() -> NodeType {
return .heading
}

public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.tag = try container.decode(HeadingTagType.self, forKey: .tag)
try super.init(from: decoder)

self.type = NodeType.heading
}


public required init(styles: StylesDict, key: NodeKey?) {
tag = .h1
super.init(styles: styles, key: key)
}

override public func encode(to encoder: Encoder) throws {
try super.encode(to: encoder)
var container = encoder.container(keyedBy: CodingKeys.self)
Expand Down
19 changes: 10 additions & 9 deletions Lexical/Core/Nodes/LineBreakNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,35 @@
*/

public class LineBreakNode: Node {
override public init() {
super.init()
self.type = NodeType.linebreak
public init() {
super.init(styles: [:], key: nil)
}

override required init(_ key: NodeKey?) {
super.init(key)
self.type = NodeType.linebreak
required public init(styles: StylesDict, key: NodeKey?) {
super.init(styles: styles, key: key)
}

public required init(from decoder: Decoder) throws {
try super.init(from: decoder)
self.type = NodeType.linebreak
}

public override class func getType() -> NodeType {
.linebreak
}

override public func encode(to encoder: Encoder) throws {
try super.encode(to: encoder)
}

override public func clone() -> Self {
Self(key)
Self(styles: styles, key: key)
}

override public func getPostamble() -> String {
return "\n"
}

public func createLineBreakNode() -> LineBreakNode {
return LineBreakNode()
return LineBreakNode(styles: [:], key: nil)
}
}
Loading