Skip to content

Commit 9e94f49

Browse files
authored
Switch to SVGView (#205)
1 parent b2a3304 commit 9e94f49

File tree

6 files changed

+24
-104
lines changed

6 files changed

+24
-104
lines changed

Package.resolved

Lines changed: 4 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ let package = Package(
1111
],
1212
dependencies: [
1313
.package(url: "https://github.com/scinfu/SwiftSoup.git", from: "2.7.5"),
14-
.package(url: "https://github.com/SVGKit/SVGKit", from: "3.0.0"),
14+
.package(url: "https://github.com/exyte/SVGView.git", from: "1.0.6"),
1515
],
1616
targets: [
1717
.target(
1818
name: "GutenbergKit",
19-
dependencies: ["SwiftSoup", "SVGKit"],
19+
dependencies: ["SwiftSoup", "SVGView"],
2020
path: "ios/Sources/GutenbergKit",
2121
exclude: [],
2222
resources: [.copy("Gutenberg")]

ios/Demo-iOS/Gutenberg.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 4 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ios/Sources/GutenbergKit/Sources/Helpers/BlockIconCache.swift

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import Foundation
2-
import SVGKit
2+
import SVGView
33

44
@MainActor
55
final class BlockIconCache: ObservableObject {
6-
var icons: [String: Result<SVGKImage, Error>] = [:]
6+
var icons: [String: Result<SVGNode, Error>] = [:]
77

8-
func getIcon(for block: BlockType) -> SVGKImage? {
8+
func getIcon(for block: BlockType) -> SVGNode? {
99
if let result = icons[block.id] {
1010
return try? result.get()
1111
}
@@ -14,19 +14,15 @@ final class BlockIconCache: ObservableObject {
1414
return try? result.get()
1515
}
1616

17-
private func _getIcon(for block: BlockType) throws -> SVGKImage {
18-
guard let svg = block.icon,
19-
!svg.isEmpty,
20-
let source = SVGKSourceString.source(fromContentsOf: svg),
21-
let image = SVGKImage(source: source) else {
17+
private func _getIcon(for block: BlockType) throws -> SVGNode {
18+
guard let svg = block.icon, !svg.isEmpty else {
2219
throw BlockIconCacheError.unknown
2320
}
24-
if let result = image.parseErrorsAndWarnings,
25-
let error = result.errorsFatal.firstObject {
21+
guard let image = SVGParser.parse(string: svg) else {
2622
#if DEBUG
27-
debugPrint("failed to parse SVG for block: \(block.name) with errors: \(String(describing: result.errorsFatal))\n\n\(svg)")
23+
debugPrint("failed to parse SVG for block: \(block.name), svg: \(svg)")
2824
#endif
29-
throw (error as? Error) ?? BlockIconCacheError.unknown
25+
throw BlockIconCacheError.unknown
3026
}
3127
return image
3228
}
Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import SwiftUI
2-
import SVGKit
2+
import SVGView
33

44
struct BlockIconView: View {
55
let block: BlockType
@@ -14,10 +14,11 @@ struct BlockIconView: View {
1414
.frame(width: size, height: size)
1515
.shadow(color: Color.black.opacity(0.1), radius: 2, x: 0, y: 1)
1616

17-
if let image = cache.getIcon(for: block),
18-
let view = SVGKFastImageView(svgkImage: image) {
19-
SVGIconView(view: view)
20-
.frame(width: size * 0.5, height: size * 0.5)
17+
if let image = cache.getIcon(for: block) {
18+
Color(.label).mask {
19+
SVGView(svg: image)
20+
}
21+
.frame(width: size * 0.5, height: size * 0.5)
2122
} else {
2223
Image(systemName: "square")
2324
.font(.system(size: size * 0.5))
@@ -27,43 +28,3 @@ struct BlockIconView: View {
2728
}
2829
}
2930
}
30-
31-
private struct SVGIconView: UIViewRepresentable {
32-
let view: SVGKFastImageView
33-
34-
@Environment(\.colorScheme) private var colorScheme
35-
36-
func makeUIView(context: Context) -> SVGKFastImageView {
37-
view.contentMode = .scaleAspectFit
38-
return view
39-
}
40-
41-
func updateUIView(_ uiView: SVGKFastImageView, context: Context) {
42-
view.image?.fillColor(color: UIColor.label)
43-
view.setNeedsDisplay()
44-
}
45-
}
46-
47-
private extension SVGKImage {
48-
/// SVGKit maintains two parallel representations of every SVG file: a
49-
/// DOMTree following W3C SVG specifications and a CALayerTree for native
50-
/// iOS rendering. The easiest and fastest way to change the colors of the
51-
/// shapes it creates is by recursively traversing the layers.
52-
private func fillColorForSubLayer(layer: CALayer, color: UIColor, opacity: Float) {
53-
if let shapeLayer = layer as? CAShapeLayer {
54-
shapeLayer.fillColor = color.cgColor
55-
shapeLayer.opacity = opacity
56-
}
57-
if let sublayers = layer.sublayers {
58-
for subLayer in sublayers {
59-
fillColorForSubLayer(layer: subLayer, color: color, opacity: opacity)
60-
}
61-
}
62-
}
63-
64-
func fillColor(color: UIColor, opacity: Float = 1.0) {
65-
if let layer = caLayerTree {
66-
fillColorForSubLayer(layer: layer, color: color, opacity: opacity)
67-
}
68-
}
69-
}

ios/Sources/GutenbergKit/Sources/Views/BlockInserter/BlockInserterBlockView.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import SwiftUI
2-
import SVGKit
32

43
struct BlockInserterBlockView: View {
54
let block: BlockType

0 commit comments

Comments
 (0)