Skip to content
This repository was archived by the owner on Sep 12, 2024. It is now read-only.

Commit a4ff926

Browse files
committed
Open and share code as carbon code screenshot
1 parent 6434c2c commit a4ff926

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

Snip/Backend/Endpoint.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ public enum Endpoint {
1919

2020
public func path() -> String {
2121
switch self {
22-
case .token:
23-
return "https://snip.picta-hub.io/github-token"
24-
case .user:
25-
return "https://api.github.com/user"
26-
case .gists:
27-
return "https://api.github.com/gists"
28-
case .getGist(let id), .updateGist(let id):
29-
return "https://api.github.com/gists/\(id)"
22+
case .token:
23+
return "https://snip.picta-hub.io/github-token"
24+
case .user:
25+
return "https://api.github.com/user"
26+
case .gists:
27+
return "https://api.github.com/gists"
28+
case .getGist(let id), .updateGist(let id):
29+
return "https://api.github.com/gists/\(id)"
3030
}
3131
}
3232
}

Snip/Components/CodeViewer/CodeActionsTopBar.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ struct CodeActionsTopBar: View {
8888
ToolbarItem(placement: .status) {
8989
if viewModel.remoteURL != nil {
9090
Button(action: viewModel.openRemoteURL) {
91+
Image(systemName: "curlybraces.square")
92+
}
93+
.help(NSLocalizedString("Open_Post", comment: ""))
94+
.onHover { inside in
95+
if inside {
96+
NSCursor.pointingHand.push()
97+
} else {
98+
NSCursor.pop()
99+
}
100+
}
101+
}
102+
}
103+
104+
ToolbarItem(placement: .status) {
105+
if viewModel.snipCode.count > 0 {
106+
Button(action: viewModel.shareCode) {
91107
Image(systemName: "square.and.arrow.up")
92108
}
93109
.help(NSLocalizedString("Open_Post", comment: ""))
@@ -202,6 +218,8 @@ final class CodeActionsViewModel: ObservableObject {
202218
@Published var remoteURL: String?
203219
@Published var isPreviewAvailable: Bool = false
204220

221+
var snipMode: String = "text"
222+
205223
var onRename: (String) -> Void
206224
var onToggleFavorite: () -> Void
207225
var onDelete: () -> Void
@@ -235,6 +253,7 @@ final class CodeActionsViewModel: ObservableObject {
235253
this.snipLastUpdate = snipItem.lastUpdateDate
236254
this.syncState = snipItem.syncState ?? .local
237255
this.remoteURL = snipItem.remoteURL
256+
this.snipMode = snipItem.mode.name
238257
this.isPreviewAvailable = snipItem.mode == CodeMode.html.mode() || snipItem.mode == CodeMode.markdown.mode()
239258
}
240259
}
@@ -249,6 +268,16 @@ final class CodeActionsViewModel: ObservableObject {
249268
NSWorkspace.shared.open(url)
250269
}
251270

271+
func shareCode() {
272+
let baseURL = "https://carbon.now.sh/?bg=rgba%28171%2C+184%2C+195%2C+1%29&t=blackboard&wt=none&l=\(snipMode)&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=56px&ph=56px&ln=false&fl=1&fm=Hack&fs=14px&lh=133%25&si=false&es=2x&wm=false&code="
273+
274+
if let escapedCode = snipCode.addingPercentEncoding(withAllowedCharacters:NSCharacterSet.urlQueryAllowed),
275+
let doubleEscapedCode = escapedCode.addingPercentEncoding(withAllowedCharacters:NSCharacterSet.urlQueryAllowed),
276+
let carbonURL = URL(string: "\(baseURL)\(doubleEscapedCode)") {
277+
NSWorkspace.shared.open(carbonURL)
278+
}
279+
}
280+
252281
func toggleSidebar() {
253282
NSApp.keyWindow?.firstResponder?.tryToPerform(#selector(NSSplitViewController.toggleSidebar(_:)), with: nil)
254283
}

0 commit comments

Comments
 (0)