78 lines
3.3 KiB
Swift
78 lines
3.3 KiB
Swift
//
|
|
// NSAttributedString+Diff.swift
|
|
// Sdifft
|
|
//
|
|
// Created by WzxJiang on 18/5/29.
|
|
// Copyright © 2018年 WzxJiang. All rights reserved.
|
|
//
|
|
// https://github.com/Wzxhaha/Sdifft
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
|
|
import Foundation
|
|
|
|
public struct DiffAttributes {
|
|
public let add, delete, same: [NSAttributedString.Key: Any]
|
|
// swiftlint:disable line_length
|
|
public init(add: [NSAttributedString.Key: Any], delete: [NSAttributedString.Key: Any], same: [NSAttributedString.Key: Any]) {
|
|
self.add = add
|
|
self.delete = delete
|
|
self.same = same
|
|
}
|
|
}
|
|
|
|
extension NSAttributedString {
|
|
/// Get attributedString with `Diff` and attributes
|
|
///
|
|
/// - Parameters:
|
|
/// - diff: Diff
|
|
/// - attributes: DiffAttributes
|
|
/// - Returns: NSAttributedString
|
|
public static func attributedString(with diff: Diff<String>, attributes: DiffAttributes) -> NSAttributedString {
|
|
let attributedString = NSMutableAttributedString()
|
|
diff.modifications.forEach {
|
|
if let add = $0.add {
|
|
attributedString.append(NSAttributedString(string: add, attributes: attributes.add))
|
|
}
|
|
if let delete = $0.delete {
|
|
attributedString.append(NSAttributedString(string: delete, attributes: attributes.delete))
|
|
}
|
|
if let same = $0.same {
|
|
attributedString.append(NSAttributedString(string: same, attributes: attributes.same))
|
|
}
|
|
}
|
|
return attributedString
|
|
}
|
|
public static func attributedString(with diff: Diff<[String]>, attributes: DiffAttributes) -> NSAttributedString {
|
|
let attributedString = NSMutableAttributedString()
|
|
diff.modifications.forEach {
|
|
if let add = $0.add {
|
|
attributedString.append(NSAttributedString(string: add.joined(), attributes: attributes.add))
|
|
}
|
|
if let delete = $0.delete {
|
|
attributedString.append(NSAttributedString(string: delete.joined(), attributes: attributes.delete))
|
|
}
|
|
if let same = $0.same {
|
|
attributedString.append(NSAttributedString(string: same.joined(), attributes: attributes.same))
|
|
}
|
|
}
|
|
return attributedString
|
|
}
|
|
}
|