improves plak
This commit is contained in:
@@ -7,7 +7,8 @@ struct MarginDate: Equatable {
|
||||
var rect: NSRect
|
||||
}
|
||||
|
||||
/// The one editable text view of the page, on TextKit 1: the reading column, the margin dates, paste and drop.
|
||||
/// The one editable text view of the page, on TextKit 1: the reading column, the margin dates, paste and drop,
|
||||
/// and the editors of code blocks and tables.
|
||||
final class PageTextView: NSTextView {
|
||||
let pageText: PageText
|
||||
/// Images pasted or dropped, and the range they replace.
|
||||
@@ -17,6 +18,14 @@ final class PageTextView: NSTextView {
|
||||
var pasteboard: NSPasteboard = .general
|
||||
/// Esc, which hides Plak.
|
||||
var onHide: (() -> Void)?
|
||||
/// The editor open over a code block or table, the object it edits, and where that object was last seen.
|
||||
var editor: ObjectEditor?
|
||||
var editedAttachment: NSTextAttachment?
|
||||
var editorLocation = 0
|
||||
/// The content of the open editor changed.
|
||||
var onEditorChange: (() -> Void)?
|
||||
/// True while a closing editor puts its object back; the typing scroll leaves the page alone then.
|
||||
var isReplacingObject = false
|
||||
private let ownedStorage: NSTextStorage
|
||||
private(set) var sideSpace: CGFloat = 0
|
||||
private(set) var topSpace: CGFloat = 0
|
||||
@@ -89,11 +98,13 @@ final class PageTextView: NSTextView {
|
||||
textStorage.beginEditing()
|
||||
textStorage.addAttributes(pageText.attributes, range: all)
|
||||
textStorage.enumerateAttribute(.attachment, in: all) { value, range, _ in
|
||||
guard let image = value as? ImageAttachment else { return }
|
||||
guard let attachment = value as? NSTextAttachment else { return }
|
||||
textStorage.removeAttribute(.baselineOffset, range: range)
|
||||
(image.attachmentCell as? MissingImageCell)?.setFont(pageText.font)
|
||||
(attachment.attachmentCell as? MissingImageCell)?.setFont(pageText.font)
|
||||
(attachment.attachmentCell as? ObjectCell)?.setFont(pageText.codeFont)
|
||||
}
|
||||
textStorage.endEditing()
|
||||
editor?.setFont(pageText.codeFont)
|
||||
}
|
||||
|
||||
// MARK: Column and spaces
|
||||
@@ -168,8 +179,16 @@ final class PageTextView: NSTextView {
|
||||
|
||||
// MARK: Clicks
|
||||
|
||||
/// A plain click on a code block or table opens its editor; any click closes an open one first.
|
||||
override func mouseDown(with event: NSEvent) {
|
||||
if convert(event.locationInWindow, from: nil).y > textBottom {
|
||||
if editor != nil { endEditing(nil) }
|
||||
// Measured after the edit is put back: a removed object moves the text under the pointer.
|
||||
let point = convert(event.locationInWindow, from: nil)
|
||||
if event.modifierFlags.intersection([.shift, .command, .option, .control]).isEmpty, let location = object(at: point) {
|
||||
openEditor(at: location, point: point)
|
||||
return
|
||||
}
|
||||
if point.y > textBottom {
|
||||
window?.makeFirstResponder(self)
|
||||
setSelectedRange(NSRange(location: length, length: 0))
|
||||
return
|
||||
@@ -182,7 +201,9 @@ final class PageTextView: NSTextView {
|
||||
/// During an input-method composition Esc cancels the composition, not the page.
|
||||
/// NSTextView has no cancelOperation of its own, so super would crash.
|
||||
override func cancelOperation(_ sender: Any?) {
|
||||
if hasMarkedText() {
|
||||
if editor != nil {
|
||||
endEditing(.after)
|
||||
} else if hasMarkedText() {
|
||||
inputContext?.discardMarkedText()
|
||||
unmarkText()
|
||||
} else if let onHide {
|
||||
@@ -237,38 +258,55 @@ final class PageTextView: NSTextView {
|
||||
}
|
||||
|
||||
override func pasteAsPlainText(_ sender: Any?) {
|
||||
paste(from: pasteboard)
|
||||
paste(from: pasteboard, detectsObjects: false)
|
||||
}
|
||||
|
||||
override func pasteAsRichText(_ sender: Any?) {
|
||||
paste(from: pasteboard)
|
||||
}
|
||||
|
||||
func paste(from pasteboard: NSPasteboard) {
|
||||
/// Pasted PHP, JSON, XML or CSV becomes a code block or table, unless the text already holds an object line.
|
||||
/// Paste as Plain Text keeps fences and image lines as text.
|
||||
func paste(from pasteboard: NSPasteboard, detectsObjects: Bool = true) {
|
||||
let images = PasteboardImages.read(pasteboard)
|
||||
if !images.isEmpty {
|
||||
onImages?(images, selectedRange())
|
||||
} else if let text = pasteboard.string(forType: .string), !text.isEmpty {
|
||||
insert(pageText.attributedString(plainText: text), replacing: selectedRange())
|
||||
guard detectsObjects else { return insert(pageText.plainString(text), replacing: selectedRange()) }
|
||||
let plain = pageText.attributedString(plainText: text)
|
||||
if !plain.containsAttachments, case let .object(kind, body) = PasteKind(text) {
|
||||
insertObjects([pageText.objectString(kind, body)], replacing: selectedRange())
|
||||
} else {
|
||||
insert(plain, replacing: selectedRange())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Text dropped from a drag, or brought by a service.
|
||||
/// Text dropped from a drag, or brought by a service; only ⌘V turns text into an object.
|
||||
override func readSelection(from pboard: NSPasteboard, type: NSPasteboard.PasteboardType) -> Bool {
|
||||
guard canPaste(from: pboard) else { return false }
|
||||
paste(from: pboard)
|
||||
let images = PasteboardImages.read(pboard)
|
||||
if !images.isEmpty {
|
||||
onImages?(images, selectedRange())
|
||||
} else if let text = pboard.string(forType: .string), !text.isEmpty {
|
||||
insert(pageText.attributedString(plainText: text), replacing: selectedRange())
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/// Puts the images in place of `range`, each on a line of its own.
|
||||
func insertImages(_ paths: [String], replacing range: NSRange) {
|
||||
guard !paths.isEmpty else { return }
|
||||
insertObjects(paths.map(pageText.imageString), replacing: range)
|
||||
}
|
||||
|
||||
/// Puts the objects in place of `range`, each on a line of its own.
|
||||
func insertObjects(_ objects: [NSAttributedString], replacing range: NSRange) {
|
||||
guard !objects.isEmpty else { return }
|
||||
let string = self.string as NSString
|
||||
let text = NSMutableAttributedString()
|
||||
if range.location > 0, string.character(at: range.location - 1) != 0x0A { text.append(pageText.newline()) }
|
||||
for (index, path) in paths.enumerated() {
|
||||
for (index, object) in objects.enumerated() {
|
||||
if index > 0 { text.append(pageText.newline()) }
|
||||
text.append(pageText.imageString(path))
|
||||
text.append(object)
|
||||
}
|
||||
// On an empty line the line moves below the images, so a blank line under it stays blank.
|
||||
let end = NSMaxRange(range)
|
||||
@@ -352,8 +390,10 @@ final class PageTextView: NSTextView {
|
||||
showDropCaret(nil)
|
||||
}
|
||||
|
||||
/// An open editor closes first: a drop would move the object under it.
|
||||
override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
|
||||
PasteboardImages.contains(sender.draggingPasteboard) ? imageDragUpdated(sender) : super.draggingEntered(sender)
|
||||
if editor != nil { endEditing(nil) }
|
||||
return PasteboardImages.contains(sender.draggingPasteboard) ? imageDragUpdated(sender) : super.draggingEntered(sender)
|
||||
}
|
||||
|
||||
override func draggingUpdated(_ sender: NSDraggingInfo) -> NSDragOperation {
|
||||
|
||||
Reference in New Issue
Block a user