62 lines
1.9 KiB
Swift
62 lines
1.9 KiB
Swift
//
|
|
// SaveClipIntent.swift
|
|
// Note
|
|
//
|
|
// Created by Kimi Code CLI on 2026/7/15.
|
|
//
|
|
|
|
import AppIntents
|
|
import UIKit
|
|
|
|
struct SaveClipIntent: AppIntent {
|
|
|
|
static var title: LocalizedStringResource = "保存剪贴板到 Note"
|
|
static var description: IntentDescription? = "将一段文本保存到 Note App 的收集箱,稍后统一处理。"
|
|
|
|
@Parameter(title: "文案", description: "要保存的文本内容", default: "")
|
|
var text: String
|
|
|
|
@Parameter(title: "来源", description: "文案来源,例如应用名称", default: "快捷指令")
|
|
var source: String?
|
|
|
|
init() {}
|
|
|
|
init(text: String, source: String? = nil) {
|
|
self.text = text
|
|
self.source = source
|
|
}
|
|
|
|
func perform() async throws -> some IntentResult & ReturnsValue<String> {
|
|
var inputText = text
|
|
if inputText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
|
|
inputText = await MainActor.run {
|
|
UIPasteboard.general.string ?? ""
|
|
}
|
|
}
|
|
|
|
let trimmed = inputText.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
guard !trimmed.isEmpty else {
|
|
return .result(value: "文案为空,未保存。")
|
|
}
|
|
|
|
guard !isSystemClipboardPlaceholder(trimmed) else {
|
|
return .result(value: "剪贴板没有有效文本,未保存。")
|
|
}
|
|
|
|
let formattedText = TextFormatter.formatForShortcut(trimmed)
|
|
try await ClipStore.shared.save(text: formattedText, source: source)
|
|
return .result(value: "已保存到收集箱。")
|
|
}
|
|
|
|
private func isSystemClipboardPlaceholder(_ text: String) -> Bool {
|
|
let lowercased = text.lowercased()
|
|
if lowercased.hasPrefix("clipboard ") && lowercased.contains(" at ") {
|
|
return true
|
|
}
|
|
if text == "获取剪贴板" {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
}
|