Repository navigation
Expand file tree
/
Copy pathNodeSharing.swift
More file actions
76 lines (68 loc) · 2.1 KB
/
Copy pathNodeSharing.swift
File metadata and controls
76 lines (68 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import Foundation
import LogseqModel
enum NodeSharePolicy {
static func text(pageTitle: String, blocks: [LogseqBlock]) -> String {
let lines = blocks
.map { $0.title.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
.map { "- \($0)" }
return ([pageTitle] + lines).joined(separator: "\n")
}
static func localAssetPaths(blocks: [LogseqBlock]) -> [String] {
var paths: [String] = []
var seen = Set<String>()
for block in blocks where block.isAsset {
guard let path = block.localPath, !path.isEmpty, !seen.contains(path) else {
continue
}
seen.insert(path)
paths.append(path)
}
return paths
}
}
#if os(iOS)
import UIKit
struct NodeSharePayload: Identifiable {
let id = UUID()
let items: [Any]
init(pageTitle: String, blocks: [LogseqBlock]) {
var items: [Any] = [NodeSharePolicy.text(pageTitle: pageTitle, blocks: blocks)]
var sharedURLs = Set<URL>()
for block in blocks where block.isAsset {
guard
let url = LocalAssetPath.resolve(
block.localPath,
title: block.title,
assetType: block.assetType
),
!sharedURLs.contains(url)
else {
continue
}
sharedURLs.insert(url)
items.append(url)
}
self.items = items
}
init(text: String, localAssetPaths: [String]) {
var items: [Any] = [text]
var sharedURLs = Set<URL>()
for path in localAssetPaths {
let title = URL(fileURLWithPath: path).lastPathComponent
guard
let url = LocalAssetPath.resolve(path, title: title, assetType: nil),
!sharedURLs.contains(url)
else {
continue
}
sharedURLs.insert(url)
items.append(url)
}
self.items = items
}
init(fileURL: URL) {
items = [fileURL]
}
}
#endif