Skip to content

Commit de22282

Browse files
committed
Press Enter to Activate Attachment Action
1 parent cd2960b commit de22282

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

Sources/CodeEditTextView/TextView/TextView+Insert.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ import AppKit
99

1010
extension TextView {
1111
override public func insertNewline(_ sender: Any?) {
12+
var attachments: [AnyTextAttachment] = selectionManager.textSelections.compactMap({ selection in
13+
let content = layoutManager.contentRun(at: selection.range.location)
14+
if case let .attachment(attachment) = content?.data, attachment.range == selection.range {
15+
return attachment
16+
}
17+
return nil
18+
})
19+
20+
if !attachments.isEmpty {
21+
for attachment in attachments.sorted(by: { $0.range.location > $1.range.location }) {
22+
performAttachmentAction(attachment: attachment)
23+
}
24+
return
25+
}
26+
1227
insertText(layoutManager.detectedLineEnding.rawValue)
1328
}
1429

Sources/CodeEditTextView/TextView/TextView+Mouse.swift

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,25 @@ extension TextView {
8787
case 1:
8888
selectionManager.setSelectedRange(attachment.range)
8989
case 2:
90-
let action = attachment.attachment.attachmentAction()
91-
switch action {
92-
case .none:
93-
return
94-
case .discard:
95-
layoutManager.attachments.remove(atOffset: offset)
96-
selectionManager.setSelectedRange(NSRange(location: attachment.range.location, length: 0))
97-
case let .replace(text):
98-
replaceCharacters(in: attachment.range, with: text)
99-
}
90+
performAttachmentAction(attachment: attachment)
10091
default:
10192
break
10293
}
10394
}
10495

96+
func performAttachmentAction(attachment: AnyTextAttachment) {
97+
let action = attachment.attachment.attachmentAction()
98+
switch action {
99+
case .none:
100+
return
101+
case .discard:
102+
layoutManager.attachments.remove(atOffset: attachment.range.location)
103+
selectionManager.setSelectedRange(NSRange(location: attachment.range.location, length: 0))
104+
case let .replace(text):
105+
replaceCharacters(in: attachment.range, with: text)
106+
}
107+
}
108+
105109
override public func mouseUp(with event: NSEvent) {
106110
mouseDragAnchor = nil
107111
disableMouseAutoscrollTimer()

0 commit comments

Comments
 (0)