Make sure file drop insertion only for dropped URLs

This commit is contained in:
1024jp 2018-02-01 19:43:41 +09:00
parent 77414e5fe4
commit d03a835953
2 changed files with 42 additions and 30 deletions

View File

@ -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.

View File

@ -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) {