Skip to content

Commit

Permalink
Improve CreateNewDocumentIntent to support filename (#833)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanzhong authored Jan 24, 2025
1 parent 37f0c0f commit 9b5659e
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 14 deletions.
29 changes: 22 additions & 7 deletions MarkEditMac/Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -459,18 +459,18 @@
}
}
},
"Create a new document, with an optional parameter to specify the initial content." : {
"Create a new document, with optional parameters to set the file name and the initial content." : {
"localizations" : {
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "新建一个文档,可通过一个可选参数来指定初始内容。"
"value" : "新建一个文档,可通过可选参数来设置文件名和初始内容。"
}
},
"zh-Hant" : {
"stringUnit" : {
"state" : "translated",
"value" : "新建一個文件,可透過一個可選變數來指定初始內容。"
"value" : "新建一個文件,可透過可選變數來設定檔案名和初始內容。"
}
}
}
Expand Down Expand Up @@ -829,6 +829,22 @@
}
}
},
"File Name" : {
"localizations" : {
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "文件名"
}
},
"zh-Hant" : {
"stringUnit" : {
"state" : "translated",
"value" : "檔案名"
}
}
}
},
"File Size" : {
"comment" : "Statistics label: count file size",
"localizations" : {
Expand Down Expand Up @@ -1772,19 +1788,18 @@
}
}
},
"New Document with ${initialContent}" : {
"extractionState" : "manual",
"New Document named ${fileName} with ${initialContent}" : {
"localizations" : {
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "使用 ${initialContent} 新建文档"
"value" : " ${initialContent} 新建名为 ${fileName} 的文档"
}
},
"zh-Hant" : {
"stringUnit" : {
"state" : "translated",
"value" : "使用 ${initialContent} 新增文件"
"value" : " ${initialContent} 新增名為 ${fileName} 的文件"
}
}
}
Expand Down
16 changes: 14 additions & 2 deletions MarkEditMac/Sources/Editor/Models/EditorDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,19 @@ final class EditorDocument: NSDocument {

private var textBundle: TextBundleWrapper?
private var revertedDate: Date = .distantPast
private var suggestedFilename: String?
private var suggestedTextEncoding: EditorTextEncoding?
private weak var hostViewController: EditorViewController?

/**
File name from the table of contents.
*/
private var suggestedFilename: String?

/**
File name from external apps, such as Shortcuts or URL schemes.
*/
private var externalFilename: String?

override func makeWindowControllers() {
let storyboard = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: nil)
let sceneIdentifier = NSStoryboard.SceneIdentifier("EditorWindowController")
Expand All @@ -81,6 +90,9 @@ final class EditorDocument: NSDocument {
hostViewController = contentVC
hostViewController?.representedObject = self

externalFilename = AppDocumentController.suggestedFilename
AppDocumentController.suggestedFilename = nil

NSApplication.shared.closeOpenPanels()
addWindowController(windowController)
}
Expand Down Expand Up @@ -116,7 +128,7 @@ extension EditorDocument {

override var displayName: String? {
get {
suggestedFilename ?? super.displayName
suggestedFilename ?? externalFilename ?? super.displayName
}
set {
super.displayName = newValue
Expand Down
1 change: 1 addition & 0 deletions MarkEditMac/Sources/Main/AppDocumentController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import MarkEditKit
*/
final class AppDocumentController: NSDocumentController {
static var suggestedTextEncoding: EditorTextEncoding?
static var suggestedFilename: String?

override func beginOpenPanel(_ openPanel: NSOpenPanel, forTypes inTypes: [String]?) async -> Int {
if let defaultDirectory = AppRuntimeConfig.defaultOpenDirectory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ extension AppDelegate {
return menu
}

func createUntitledFile(initialContent: String?, isIntent: Bool = false) {
func createNewFile(initialContent: String?, fileName: String? = nil, isIntent: Bool = false) {
// In EditorDocument, this is used as an external filename
AppDocumentController.suggestedFilename = fileName

// Activating the app also creates a new file if new window behavior is `newDocument`,
// prevent duplicate creation from Shortcuts like `CreateNewDocumentIntent`.
if !isIntent || (Date.timeIntervalSinceReferenceDate - States.untitledFileOpenedDate > 0.2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private extension AppDelegate {
}

@IBAction func newFileFromClipboard(_ sender: Any?) {
createUntitledFile(initialContent: NSPasteboard.general.string)
createNewFile(initialContent: NSPasteboard.general.string)
}

@IBAction func openDevelopmentGuide(_ sender: Any?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@ import AppIntents

struct CreateNewDocumentIntent: AppIntent {
static let title: LocalizedStringResource = "Create New Document"
static let description = IntentDescription("Create a new document, with an optional parameter to specify the initial content.")
static let description = IntentDescription("Create a new document, with optional parameters to set the file name and the initial content.")
static let openAppWhenRun = true
static var parameterSummary: some ParameterSummary {
Summary("New Document with \(\.$initialContent)")
Summary("New Document named \(\.$fileName) with \(\.$initialContent)")
}

@Parameter(title: "File Name")
var fileName: String?

@Parameter(title: "Initial Content")
var initialContent: String?

@MainActor
func perform() async throws -> some IntentResult {
NSApp.appDelegate?.createUntitledFile(initialContent: initialContent, isIntent: true)
NSApp.appDelegate?.createNewFile(initialContent: initialContent, fileName: fileName, isIntent: true)
return .result()
}
}

0 comments on commit 9b5659e

Please sign in to comment.