mirror of
https://github.com/coteditor/CotEditor.git
synced 2024-09-11 11:25:57 +03:00
Make sure file drop insertion only for dropped URLs
This commit is contained in:
parent
77414e5fe4
commit
d03a835953
@ -17,6 +17,7 @@ develop
|
||||
- Fix an issue where invisible symbols for control characters were not drawn in input fields in find panel.
|
||||
- [beta] Fix an issue where AppleScript (and JXA) could not communicate with some APIs.
|
||||
- [beta] Fix an issue where the views containing an encoding menu could display nothing under a specific setting condition.
|
||||
- [beta] Fix an issue where pasted URLs from specific applications missed the domain part.
|
||||
- [beta] Fix an issue where the current line highlight started at a wrong place on the RTL writing mode.
|
||||
- Fix some unwanted title case in the preferences.
|
||||
|
||||
|
@ -669,36 +669,11 @@ final class EditorTextView: NSTextView, Themable {
|
||||
}
|
||||
|
||||
// on file drop
|
||||
if let urls = pboard.readObjects(forClasses: [NSURL.self]) as? [URL], !urls.isEmpty {
|
||||
let definitions = UserDefaults.standard[.fileDropArray]
|
||||
let composer = FileDropComposer(definitions: definitions)
|
||||
let documentURL = self.document?.fileURL
|
||||
let syntaxStyle: String? = {
|
||||
guard let style = self.document?.syntaxStyle, !style.isNone else { return nil }
|
||||
return style.styleName
|
||||
}()
|
||||
var replacementString = ""
|
||||
|
||||
for url in urls {
|
||||
if let dropText = composer.dropText(forFileURL: url, documentURL: documentURL, syntaxStyle: syntaxStyle) {
|
||||
replacementString += dropText
|
||||
|
||||
} else {
|
||||
// jsut insert the absolute path if no specific setting for the file type was found
|
||||
// -> This is the default behavior of NSTextView by file dropping.
|
||||
if !replacementString.isEmpty {
|
||||
replacementString += "\n"
|
||||
}
|
||||
replacementString += url.path
|
||||
}
|
||||
}
|
||||
|
||||
// insert drop text to view
|
||||
if self.shouldChangeText(in: self.rangeForUserTextChange, replacementString: replacementString) {
|
||||
self.replaceCharacters(in: self.rangeForUserTextChange, with: replacementString)
|
||||
self.didChangeText()
|
||||
return true
|
||||
}
|
||||
if pboard.name == .dragPboard,
|
||||
let urls = pboard.readObjects(forClasses: [NSURL.self]) as? [URL],
|
||||
self.insertDroppedFiles(urls)
|
||||
{
|
||||
return true
|
||||
}
|
||||
|
||||
return super.readSelection(from: pboard, type: type)
|
||||
@ -1201,6 +1176,42 @@ final class EditorTextView: NSTextView, Themable {
|
||||
}
|
||||
|
||||
|
||||
/// insert string representation of dropped files applying user setting
|
||||
private func insertDroppedFiles(_ urls: [URL]) -> Bool {
|
||||
|
||||
guard !urls.isEmpty else { return false }
|
||||
|
||||
let composer = FileDropComposer(definitions: UserDefaults.standard[.fileDropArray])
|
||||
let documentURL = self.document?.fileURL
|
||||
let syntaxStyle: String? = {
|
||||
guard let style = self.document?.syntaxStyle, !style.isNone else { return nil }
|
||||
return style.styleName
|
||||
}()
|
||||
|
||||
let replacementString = urls.reduce(into: "") { (string, url) in
|
||||
if let dropText = composer.dropText(forFileURL: url, documentURL: documentURL, syntaxStyle: syntaxStyle) {
|
||||
string += dropText
|
||||
return
|
||||
}
|
||||
|
||||
// jsut insert the absolute path if no specific setting for the file type was found
|
||||
// -> This is the default behavior of NSTextView by file dropping.
|
||||
if !string.isEmpty {
|
||||
string += "\n"
|
||||
}
|
||||
string += url.path
|
||||
}
|
||||
|
||||
// insert drop text to view
|
||||
guard self.shouldChangeText(in: self.rangeForUserTextChange, replacementString: replacementString) else { return false }
|
||||
|
||||
self.replaceCharacters(in: self.rangeForUserTextChange, with: replacementString)
|
||||
self.didChangeText()
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
/// visible rect did change
|
||||
@objc private func didChangeVisibleRect(_ notification: Notification) {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user