Compare commits
18 Commits
5af8038ec0
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 1a525c763e | |||
| a0dffdc715 | |||
| 4765d48021 | |||
| f888e28d15 | |||
| c934dbad84 | |||
| 4088db8ec0 | |||
| 255f171aa8 | |||
| 70870e1ec2 | |||
| f1a380d105 | |||
| 01ad237af3 | |||
| 7d49f7d5a4 | |||
| 87eb958869 | |||
| 4340fd3635 | |||
| ac2eac5fcd | |||
| a386802a55 | |||
| 1f94b9884c | |||
| 58a8d27315 | |||
| 7f4fc34dbf |
@@ -184,6 +184,7 @@
|
|||||||
knownRegions = (
|
knownRegions = (
|
||||||
en,
|
en,
|
||||||
Base,
|
Base,
|
||||||
|
"zh-Hans",
|
||||||
);
|
);
|
||||||
mainGroup = CFD40B073003C283009C9F2A;
|
mainGroup = CFD40B073003C283009C9F2A;
|
||||||
minimizedProjectReferenceProxies = 1;
|
minimizedProjectReferenceProxies = 1;
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ import UIKit
|
|||||||
class ClipEditViewController: UIViewController {
|
class ClipEditViewController: UIViewController {
|
||||||
|
|
||||||
private var textView: UITextView!
|
private var textView: UITextView!
|
||||||
|
private var floatingDoneButton: UIButton!
|
||||||
|
private var floatingDoneButtonBottomConstraint: NSLayoutConstraint!
|
||||||
private var clip: Clip
|
private var clip: Clip
|
||||||
|
|
||||||
init(clip: Clip) {
|
init(clip: Clip) {
|
||||||
@@ -29,6 +31,45 @@ class ClipEditViewController: UIViewController {
|
|||||||
|
|
||||||
setupNavigationBar()
|
setupNavigationBar()
|
||||||
setupTextView()
|
setupTextView()
|
||||||
|
setupKeyboardObservers()
|
||||||
|
}
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
NotificationCenter.default.removeObserver(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func setupKeyboardObservers() {
|
||||||
|
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
|
||||||
|
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc private func keyboardWillShow(_ notification: Notification) {
|
||||||
|
guard let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return }
|
||||||
|
let keyboardFrameInView = view.convert(keyboardFrame, from: nil)
|
||||||
|
let intersection = keyboardFrameInView.intersection(view.bounds)
|
||||||
|
let keyboardHeight = intersection.height
|
||||||
|
|
||||||
|
let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval ?? 0.25
|
||||||
|
|
||||||
|
UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState) {
|
||||||
|
self.textView.contentInset.bottom = keyboardHeight
|
||||||
|
self.textView.verticalScrollIndicatorInsets.bottom = keyboardHeight
|
||||||
|
self.floatingDoneButtonBottomConstraint.constant = -(keyboardHeight + 8)
|
||||||
|
self.floatingDoneButton.alpha = 1
|
||||||
|
self.view.layoutIfNeeded()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc private func keyboardWillHide(_ notification: Notification) {
|
||||||
|
let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval ?? 0.25
|
||||||
|
|
||||||
|
UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState) {
|
||||||
|
self.textView.contentInset.bottom = 0
|
||||||
|
self.textView.verticalScrollIndicatorInsets.bottom = 0
|
||||||
|
self.floatingDoneButtonBottomConstraint.constant = -16
|
||||||
|
self.floatingDoneButton.alpha = 0
|
||||||
|
self.view.layoutIfNeeded()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override func viewDidAppear(_ animated: Bool) {
|
override func viewDidAppear(_ animated: Bool) {
|
||||||
@@ -39,7 +80,7 @@ class ClipEditViewController: UIViewController {
|
|||||||
private func setupNavigationBar() {
|
private func setupNavigationBar() {
|
||||||
let saveButton = UIBarButtonItem(
|
let saveButton = UIBarButtonItem(
|
||||||
title: "保存",
|
title: "保存",
|
||||||
style: .done,
|
style: .prominent,
|
||||||
target: self,
|
target: self,
|
||||||
action: #selector(didTapSaveButton)
|
action: #selector(didTapSaveButton)
|
||||||
)
|
)
|
||||||
@@ -61,15 +102,40 @@ class ClipEditViewController: UIViewController {
|
|||||||
textView.text = clip.text
|
textView.text = clip.text
|
||||||
view.addSubview(textView)
|
view.addSubview(textView)
|
||||||
|
|
||||||
|
var doneConfig = UIButton.Configuration.glass()
|
||||||
|
doneConfig.title = "完成"
|
||||||
|
doneConfig.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
|
||||||
|
var outgoing = incoming
|
||||||
|
outgoing.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
|
||||||
|
return outgoing
|
||||||
|
}
|
||||||
|
doneConfig.baseForegroundColor = .label
|
||||||
|
doneConfig.cornerStyle = .capsule
|
||||||
|
doneConfig.contentInsets = NSDirectionalEdgeInsets(top: 15, leading: 22, bottom: 15, trailing: 22)
|
||||||
|
floatingDoneButton = UIButton(configuration: doneConfig)
|
||||||
|
floatingDoneButton.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
floatingDoneButton.alpha = 0
|
||||||
|
floatingDoneButton.addTarget(self, action: #selector(didTapFloatingDoneButton), for: .touchUpInside)
|
||||||
|
view.addSubview(floatingDoneButton)
|
||||||
|
|
||||||
let safeArea = view.safeAreaLayoutGuide
|
let safeArea = view.safeAreaLayoutGuide
|
||||||
|
floatingDoneButtonBottomConstraint = floatingDoneButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -16)
|
||||||
|
|
||||||
NSLayoutConstraint.activate([
|
NSLayoutConstraint.activate([
|
||||||
textView.topAnchor.constraint(equalTo: safeArea.topAnchor, constant: 16),
|
textView.topAnchor.constraint(equalTo: safeArea.topAnchor, constant: 16),
|
||||||
textView.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor, constant: 16),
|
textView.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor, constant: 16),
|
||||||
textView.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor, constant: -16),
|
textView.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor, constant: -16),
|
||||||
textView.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor, constant: -16)
|
textView.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor, constant: -16),
|
||||||
|
|
||||||
|
floatingDoneButton.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor, constant: -16),
|
||||||
|
floatingDoneButtonBottomConstraint
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@objc private func didTapFloatingDoneButton() {
|
||||||
|
textView.resignFirstResponder()
|
||||||
|
}
|
||||||
|
|
||||||
@objc private func didTapSaveButton() {
|
@objc private func didTapSaveButton() {
|
||||||
let updatedText = textView.text ?? ""
|
let updatedText = textView.text ?? ""
|
||||||
guard !updatedText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
|
guard !updatedText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
|
||||||
@@ -85,8 +151,9 @@ class ClipEditViewController: UIViewController {
|
|||||||
Task {
|
Task {
|
||||||
do {
|
do {
|
||||||
try await ClipStore.shared.update(updatedClip)
|
try await ClipStore.shared.update(updatedClip)
|
||||||
await MainActor.run {
|
_ = await MainActor.run {
|
||||||
self.navigationController?.popViewController(animated: true)
|
self.navigationController?.popViewController(animated: true)
|
||||||
|
return ()
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
await MainActor.run {
|
await MainActor.run {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ class ClipInboxViewController: UIViewController {
|
|||||||
|
|
||||||
private var tableView: UITableView!
|
private var tableView: UITableView!
|
||||||
private var emptyLabel: UILabel!
|
private var emptyLabel: UILabel!
|
||||||
|
private var archiveButton: UIButton!
|
||||||
private var clips: [Clip] = []
|
private var clips: [Clip] = []
|
||||||
|
|
||||||
override func viewDidLoad() {
|
override func viewDidLoad() {
|
||||||
@@ -20,8 +21,10 @@ class ClipInboxViewController: UIViewController {
|
|||||||
view.backgroundColor = .systemGroupedBackground
|
view.backgroundColor = .systemGroupedBackground
|
||||||
|
|
||||||
setupNavigationBar()
|
setupNavigationBar()
|
||||||
|
setupArchiveButton()
|
||||||
setupTableView()
|
setupTableView()
|
||||||
setupEmptyLabel()
|
setupEmptyLabel()
|
||||||
|
setupAppStateObserver()
|
||||||
}
|
}
|
||||||
|
|
||||||
override func viewWillAppear(_ animated: Bool) {
|
override func viewWillAppear(_ animated: Bool) {
|
||||||
@@ -29,6 +32,23 @@ class ClipInboxViewController: UIViewController {
|
|||||||
loadClips()
|
loadClips()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
NotificationCenter.default.removeObserver(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func setupAppStateObserver() {
|
||||||
|
NotificationCenter.default.addObserver(
|
||||||
|
self,
|
||||||
|
selector: #selector(appDidBecomeActive),
|
||||||
|
name: UIApplication.didBecomeActiveNotification,
|
||||||
|
object: nil
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc private func appDidBecomeActive() {
|
||||||
|
loadClips()
|
||||||
|
}
|
||||||
|
|
||||||
private func setupNavigationBar() {
|
private func setupNavigationBar() {
|
||||||
let clearButton = UIBarButtonItem(
|
let clearButton = UIBarButtonItem(
|
||||||
title: "清空",
|
title: "清空",
|
||||||
@@ -53,10 +73,58 @@ class ClipInboxViewController: UIViewController {
|
|||||||
tableView.topAnchor.constraint(equalTo: view.topAnchor),
|
tableView.topAnchor.constraint(equalTo: view.topAnchor),
|
||||||
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
||||||
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
||||||
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
|
tableView.bottomAnchor.constraint(equalTo: archiveButton.topAnchor, constant: -16)
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func setupArchiveButton() {
|
||||||
|
var config = UIButton.Configuration.filled()
|
||||||
|
config.title = "归档"
|
||||||
|
config.baseBackgroundColor = .systemBlue
|
||||||
|
config.baseForegroundColor = .white
|
||||||
|
config.cornerStyle = .medium
|
||||||
|
config.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 16, bottom: 0, trailing: 16)
|
||||||
|
|
||||||
|
archiveButton = UIButton(configuration: config)
|
||||||
|
archiveButton.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
archiveButton.addTarget(self, action: #selector(didTapArchiveButton), for: .touchUpInside)
|
||||||
|
view.addSubview(archiveButton)
|
||||||
|
|
||||||
|
NSLayoutConstraint.activate([
|
||||||
|
archiveButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 16),
|
||||||
|
archiveButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -16),
|
||||||
|
archiveButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -16),
|
||||||
|
archiveButton.heightAnchor.constraint(equalToConstant: 52)
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc private func didTapArchiveButton() {
|
||||||
|
guard clips.count >= 2 else {
|
||||||
|
let alert = UIAlertController(title: "提示", message: "至少需要两条文案才能归档。", preferredStyle: .alert)
|
||||||
|
alert.addAction(UIAlertAction(title: "确定", style: .default))
|
||||||
|
present(alert, animated: true)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let title = self.archiveTitle()
|
||||||
|
let itemsText = clips.map { "\($0.text)\n\n---" }.joined(separator: "\n\n")
|
||||||
|
let archivedText = "\(title)\n\n\(itemsText)"
|
||||||
|
let viewController = FormatNoteViewController()
|
||||||
|
viewController.initialText = archivedText
|
||||||
|
navigationController?.pushViewController(viewController, animated: true)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func archiveTitle() -> String {
|
||||||
|
let dateFormatter = DateFormatter()
|
||||||
|
dateFormatter.locale = Locale(identifier: "zh_CN")
|
||||||
|
dateFormatter.dateFormat = "yyyy年MM月dd日"
|
||||||
|
let dateString = dateFormatter.string(from: Date())
|
||||||
|
let weekdays = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
|
||||||
|
let weekdayIndex = Calendar.current.component(.weekday, from: Date()) - 1
|
||||||
|
let weekday = (weekdayIndex >= 0 && weekdayIndex < weekdays.count) ? weekdays[weekdayIndex] : ""
|
||||||
|
return "【读书摘抄】\(dateString) \(weekday)"
|
||||||
|
}
|
||||||
|
|
||||||
private func setupEmptyLabel() {
|
private func setupEmptyLabel() {
|
||||||
emptyLabel = UILabel()
|
emptyLabel = UILabel()
|
||||||
emptyLabel.translatesAutoresizingMaskIntoConstraints = false
|
emptyLabel.translatesAutoresizingMaskIntoConstraints = false
|
||||||
@@ -96,6 +164,7 @@ class ClipInboxViewController: UIViewController {
|
|||||||
let isEmpty = clips.isEmpty
|
let isEmpty = clips.isEmpty
|
||||||
emptyLabel.isHidden = !isEmpty
|
emptyLabel.isHidden = !isEmpty
|
||||||
tableView.isHidden = isEmpty
|
tableView.isHidden = isEmpty
|
||||||
|
archiveButton.isHidden = isEmpty
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc private func didTapClearButton() {
|
@objc private func didTapClearButton() {
|
||||||
@@ -177,8 +246,10 @@ extension ClipInboxViewController: UITableViewDataSource {
|
|||||||
|
|
||||||
var config = cell.defaultContentConfiguration()
|
var config = cell.defaultContentConfiguration()
|
||||||
config.text = previewText(for: clip.text)
|
config.text = previewText(for: clip.text)
|
||||||
|
config.textProperties.numberOfLines = 0
|
||||||
config.secondaryText = formattedDate(clip.createdAt)
|
config.secondaryText = formattedDate(clip.createdAt)
|
||||||
config.secondaryTextProperties.color = .secondaryLabel
|
config.secondaryTextProperties.color = .secondaryLabel
|
||||||
|
config.textToSecondaryTextVerticalPadding = 12
|
||||||
cell.contentConfiguration = config
|
cell.contentConfiguration = config
|
||||||
cell.accessoryType = .disclosureIndicator
|
cell.accessoryType = .disclosureIndicator
|
||||||
|
|
||||||
@@ -186,9 +257,7 @@ extension ClipInboxViewController: UITableViewDataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func previewText(for text: String) -> String {
|
private func previewText(for text: String) -> String {
|
||||||
let lines = text.components(separatedBy: .newlines)
|
let preview = text.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
let firstTwoLines = Array(lines.prefix(2))
|
|
||||||
let preview = firstTwoLines.joined(separator: " ").trimmingCharacters(in: .whitespaces)
|
|
||||||
if preview.isEmpty {
|
if preview.isEmpty {
|
||||||
return "(无内容)"
|
return "(无内容)"
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-5
@@ -21,6 +21,17 @@ struct Clip: Codable, Identifiable, Equatable, Sendable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum ClipStoreError: Error, LocalizedError {
|
||||||
|
case appGroupUnavailable
|
||||||
|
|
||||||
|
var errorDescription: String? {
|
||||||
|
switch self {
|
||||||
|
case .appGroupUnavailable:
|
||||||
|
return "无法访问 App Group 容器,请检查签名和 entitlements 配置。"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
actor ClipStore {
|
actor ClipStore {
|
||||||
|
|
||||||
static let shared = ClipStore()
|
static let shared = ClipStore()
|
||||||
@@ -31,13 +42,13 @@ actor ClipStore {
|
|||||||
|
|
||||||
private var cachedClips: [Clip]?
|
private var cachedClips: [Clip]?
|
||||||
|
|
||||||
private var fileURL: URL {
|
private func fileURL() throws -> URL {
|
||||||
let fileManager = FileManager.default
|
let fileManager = FileManager.default
|
||||||
guard let containerURL = fileManager.containerURL(forSecurityApplicationGroupIdentifier: appGroupIdentifier) else {
|
guard let containerURL = fileManager.containerURL(forSecurityApplicationGroupIdentifier: appGroupIdentifier) else {
|
||||||
fatalError("无法访问 App Group 容器: \(appGroupIdentifier)")
|
throw ClipStoreError.appGroupUnavailable
|
||||||
}
|
}
|
||||||
let directoryURL = containerURL.appendingPathComponent(subdirectory, isDirectory: true)
|
let directoryURL = containerURL.appendingPathComponent(subdirectory, isDirectory: true)
|
||||||
try? fileManager.createDirectory(at: directoryURL, withIntermediateDirectories: true)
|
try fileManager.createDirectory(at: directoryURL, withIntermediateDirectories: true)
|
||||||
return directoryURL.appendingPathComponent(fileName)
|
return directoryURL.appendingPathComponent(fileName)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,7 +66,7 @@ actor ClipStore {
|
|||||||
return cached
|
return cached
|
||||||
}
|
}
|
||||||
|
|
||||||
let url = fileURL
|
let url = try fileURL()
|
||||||
let fileManager = FileManager.default
|
let fileManager = FileManager.default
|
||||||
guard fileManager.fileExists(atPath: url.path) else {
|
guard fileManager.fileExists(atPath: url.path) else {
|
||||||
cachedClips = []
|
cachedClips = []
|
||||||
@@ -93,7 +104,7 @@ actor ClipStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func persist(_ clips: [Clip]) async throws {
|
private func persist(_ clips: [Clip]) async throws {
|
||||||
let url = fileURL
|
let url = try fileURL()
|
||||||
let data = try JSONEncoder().encode(clips)
|
let data = try JSONEncoder().encode(clips)
|
||||||
try data.write(to: url, options: .atomic)
|
try data.write(to: url, options: .atomic)
|
||||||
cachedClips = clips
|
cachedClips = clips
|
||||||
|
|||||||
@@ -35,10 +35,6 @@ class FormatNoteViewController: UIViewController {
|
|||||||
setupUI()
|
setupUI()
|
||||||
if let initialText = initialText {
|
if let initialText = initialText {
|
||||||
textView.text = initialText
|
textView.text = initialText
|
||||||
isNoteFormatted = true
|
|
||||||
noteButton.setTitle("复制并打开备忘录", for: .normal)
|
|
||||||
rightBarButtonMode = .reset
|
|
||||||
updateRightBarButtonItem()
|
|
||||||
}
|
}
|
||||||
setupKeyboardObservers()
|
setupKeyboardObservers()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
//
|
||||||
|
// NoteAppShortcuts.swift
|
||||||
|
// Note
|
||||||
|
//
|
||||||
|
// Created by Kimi Code CLI on 2026/7/15.
|
||||||
|
//
|
||||||
|
|
||||||
|
import AppIntents
|
||||||
|
|
||||||
|
struct NoteAppShortcuts: AppShortcutsProvider {
|
||||||
|
|
||||||
|
static var appShortcuts: [AppShortcut] {
|
||||||
|
AppShortcut(
|
||||||
|
intent: SaveClipIntent(),
|
||||||
|
phrases: ["保存剪贴板到 ${applicationName}"],
|
||||||
|
shortTitle: "保存剪贴板",
|
||||||
|
systemImageName: "doc.text"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,13 +6,14 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import AppIntents
|
import AppIntents
|
||||||
|
import UIKit
|
||||||
|
|
||||||
struct SaveClipIntent: AppIntent {
|
struct SaveClipIntent: AppIntent {
|
||||||
|
|
||||||
static var title: LocalizedStringResource = "保存剪贴板到 Note"
|
static var title: LocalizedStringResource = "保存剪贴板到 Note"
|
||||||
static var description: IntentDescription? = "将一段文本保存到 Note App 的收集箱,稍后统一处理。"
|
static var description: IntentDescription? = "将一段文本保存到 Note App 的收集箱,稍后统一处理。"
|
||||||
|
|
||||||
@Parameter(title: "文案", description: "要保存的文本内容", requestValueDialog: "请提供要保存的文案")
|
@Parameter(title: "文案", description: "要保存的文本内容", default: "")
|
||||||
var text: String
|
var text: String
|
||||||
|
|
||||||
@Parameter(title: "来源", description: "文案来源,例如应用名称", default: "快捷指令")
|
@Parameter(title: "来源", description: "文案来源,例如应用名称", default: "快捷指令")
|
||||||
@@ -26,7 +27,14 @@ struct SaveClipIntent: AppIntent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func perform() async throws -> some IntentResult & ReturnsValue<String> {
|
func perform() async throws -> some IntentResult & ReturnsValue<String> {
|
||||||
let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)
|
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 {
|
guard !trimmed.isEmpty else {
|
||||||
return .result(value: "文案为空,未保存。")
|
return .result(value: "文案为空,未保存。")
|
||||||
}
|
}
|
||||||
@@ -35,7 +43,8 @@ struct SaveClipIntent: AppIntent {
|
|||||||
return .result(value: "剪贴板没有有效文本,未保存。")
|
return .result(value: "剪贴板没有有效文本,未保存。")
|
||||||
}
|
}
|
||||||
|
|
||||||
try await ClipStore.shared.save(text: trimmed, source: source)
|
let formattedText = TextFormatter.formatForShortcut(trimmed)
|
||||||
|
try await ClipStore.shared.save(text: formattedText, source: source)
|
||||||
return .result(value: "已保存到收集箱。")
|
return .result(value: "已保存到收集箱。")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import Foundation
|
|||||||
|
|
||||||
enum TextFormatter {
|
enum TextFormatter {
|
||||||
|
|
||||||
static func formatLog(_ text: String) -> String {
|
static nonisolated func formatLog(_ text: String) -> String {
|
||||||
var content = text
|
var content = text
|
||||||
|
|
||||||
content = content.replacingOccurrences(of: "# ", with: "")
|
content = content.replacingOccurrences(of: "# ", with: "")
|
||||||
@@ -31,7 +31,14 @@ enum TextFormatter {
|
|||||||
return content
|
return content
|
||||||
}
|
}
|
||||||
|
|
||||||
static func formatForCloud(_ text: String) -> String {
|
static nonisolated func formatForCloud(_ text: String) -> String {
|
||||||
|
var content = formatForShortcut(text)
|
||||||
|
content = "# " + content
|
||||||
|
content = insertEmptyLineAfterFirstLine(content)
|
||||||
|
return content
|
||||||
|
}
|
||||||
|
|
||||||
|
static nonisolated func formatForShortcut(_ text: String) -> String {
|
||||||
var content = text
|
var content = text
|
||||||
content = content.replacingOccurrences(of: " ", with: "")
|
content = content.replacingOccurrences(of: " ", with: "")
|
||||||
content = content.replacingOccurrences(of: " ", with: "")
|
content = content.replacingOccurrences(of: " ", with: "")
|
||||||
@@ -71,12 +78,10 @@ enum TextFormatter {
|
|||||||
content = content.replacingOccurrences(of: " ", with: "")
|
content = content.replacingOccurrences(of: " ", with: "")
|
||||||
content = content.replacingOccurrences(of: "---", with: "---\n")
|
content = content.replacingOccurrences(of: "---", with: "---\n")
|
||||||
content = content.replacingOccurrences(of: "日星期", with: "日 星期")
|
content = content.replacingOccurrences(of: "日星期", with: "日 星期")
|
||||||
content = "# " + content
|
|
||||||
content = insertEmptyLineAfterFirstLine(content)
|
|
||||||
return content
|
return content
|
||||||
}
|
}
|
||||||
|
|
||||||
static func formatForNote(_ text: String) -> String {
|
static nonisolated func formatForNote(_ text: String) -> String {
|
||||||
var content = text
|
var content = text
|
||||||
content = content.replacingOccurrences(of: "# ", with: "")
|
content = content.replacingOccurrences(of: "# ", with: "")
|
||||||
content = content.replacingOccurrences(of: "#", with: "")
|
content = content.replacingOccurrences(of: "#", with: "")
|
||||||
@@ -111,13 +116,13 @@ enum TextFormatter {
|
|||||||
return result.trimmingCharacters(in: .whitespacesAndNewlines)
|
return result.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
}
|
}
|
||||||
|
|
||||||
static func removeEmptyLines(_ text: String) -> String {
|
static nonisolated func removeEmptyLines(_ text: String) -> String {
|
||||||
let lines = text.components(separatedBy: .newlines)
|
let lines = text.components(separatedBy: .newlines)
|
||||||
let nonEmptyLines = lines.filter { !$0.trimmingCharacters(in: .whitespaces).isEmpty }
|
let nonEmptyLines = lines.filter { !$0.trimmingCharacters(in: .whitespaces).isEmpty }
|
||||||
return nonEmptyLines.joined(separator: "\n")
|
return nonEmptyLines.joined(separator: "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
static func insertEmptyLineAfterFirstLine(_ text: String) -> String {
|
static nonisolated func insertEmptyLineAfterFirstLine(_ text: String) -> String {
|
||||||
var lines = text.components(separatedBy: .newlines)
|
var lines = text.components(separatedBy: .newlines)
|
||||||
if !lines.isEmpty {
|
if !lines.isEmpty {
|
||||||
lines.insert("", at: 1)
|
lines.insert("", at: 1)
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
/* Simplified Chinese localization */
|
||||||
Reference in New Issue
Block a user