187 lines
9.2 KiB
Swift
187 lines
9.2 KiB
Swift
import AppKit
|
|
|
|
/// Opens an editor over a code block or table, keeps it in place, and puts the edit back as one undoable change.
|
|
extension PageTextView {
|
|
/// The location of the code block or table under a point; nil when the point is not on one.
|
|
func object(at point: NSPoint) -> Int? {
|
|
guard let layoutManager, let textContainer, let textStorage, textStorage.length > 0 else { return nil }
|
|
let inContainer = NSPoint(x: point.x - textContainerOrigin.x, y: point.y - textContainerOrigin.y)
|
|
let location = layoutManager.characterIndexForGlyph(at: layoutManager.glyphIndex(for: inContainer, in: textContainer))
|
|
guard location < textStorage.length, isObject(at: location), objectRect(at: location).contains(point) else { return nil }
|
|
return location
|
|
}
|
|
|
|
func isObject(at location: Int) -> Bool {
|
|
let attachment = textStorage?.attribute(.attachment, at: location, effectiveRange: nil)
|
|
return attachment is CodeAttachment || attachment is TableAttachment
|
|
}
|
|
|
|
/// Where the attachment at `location` is drawn, in this view's coordinates.
|
|
func objectRect(at location: Int) -> NSRect {
|
|
guard let layoutManager else { return .zero }
|
|
layoutManager.ensureLayout(forCharacterRange: NSRange(location: location, length: 1))
|
|
let glyph = layoutManager.glyphIndexForCharacter(at: location)
|
|
let fragment = layoutManager.lineFragmentRect(forGlyphAt: glyph, effectiveRange: nil)
|
|
let position = layoutManager.location(forGlyphAt: glyph)
|
|
let size = layoutManager.attachmentSize(forGlyphAt: glyph)
|
|
return NSRect(x: fragment.minX + position.x, y: fragment.minY + position.y - size.height, width: size.width, height: size.height)
|
|
.offsetBy(dx: textContainerOrigin.x, dy: textContainerOrigin.y)
|
|
}
|
|
|
|
/// Opens the editor of the object at `location`; a click point puts the caret or picks the cell there.
|
|
func openEditor(at location: Int, point: NSPoint? = nil) {
|
|
if editor != nil { endEditing(nil) }
|
|
guard let textStorage, location < textStorage.length,
|
|
let attachment = textStorage.attribute(.attachment, at: location, effectiveRange: nil) as? NSTextAttachment else { return }
|
|
let rect = objectRect(at: location)
|
|
let opened: ObjectEditor
|
|
switch attachment {
|
|
case let code as CodeAttachment:
|
|
opened = CodeEditorView(kind: code.kind, body: code.body, font: pageText.codeFont)
|
|
case let table as TableAttachment:
|
|
let layout = TableLayout(table.table, font: pageText.codeFont)
|
|
let cell = point.flatMap { layout.position(at: NSPoint(x: $0.x - rect.minX, y: $0.y - rect.minY)) }
|
|
opened = TableGridView(table: table.table, font: pageText.codeFont, selected: cell ?? TablePosition(row: 0, column: 0))
|
|
default:
|
|
return
|
|
}
|
|
opened.onEnd = { [weak self] exit in self?.endEditing(exit) }
|
|
opened.onResize = { [weak self] in self?.positionEditor() }
|
|
opened.onChange = { [weak self] in self?.onEditorChange?() }
|
|
editor = opened
|
|
editedAttachment = attachment
|
|
editorLocation = location
|
|
addSubview(opened.view)
|
|
positionEditor()
|
|
window?.makeFirstResponder(opened.focusView)
|
|
if let code = opened as? CodeEditorView {
|
|
let caret = point.map { code.characterIndexForInsertion(at: code.convert($0, from: self)) } ?? (code.string as NSString).length
|
|
code.setSelectedRange(NSRange(location: caret, length: 0))
|
|
}
|
|
}
|
|
|
|
/// The location of the object the open editor edits, found again when the text moved it; nil when it is gone.
|
|
func editedLocation() -> Int? {
|
|
guard let editedAttachment, let textStorage else { return nil }
|
|
if editorLocation < textStorage.length,
|
|
textStorage.attribute(.attachment, at: editorLocation, effectiveRange: nil) as AnyObject? === editedAttachment {
|
|
return editorLocation
|
|
}
|
|
var found: Int?
|
|
textStorage.enumerateAttribute(.attachment, in: NSRange(location: 0, length: textStorage.length)) { value, range, stop in
|
|
guard value as AnyObject? === editedAttachment else { return }
|
|
found = range.location
|
|
stop.pointee = true
|
|
}
|
|
if let found { editorLocation = found }
|
|
return found
|
|
}
|
|
|
|
/// Puts the editor over its object; the object takes the height of the editor, so the text below makes room.
|
|
func positionEditor() {
|
|
guard let editor, let textContainer, let layoutManager, let location = editedLocation(),
|
|
let cell = objectCell(at: location) else { return }
|
|
var rect = objectRect(at: location)
|
|
let size = editor.size(available: textContainer.size.width + textContainerOrigin.x - rect.minX)
|
|
if cell.editingHeight != size.height {
|
|
cell.editingHeight = size.height
|
|
let range = NSRange(location: location, length: 1)
|
|
layoutManager.invalidateLayout(forCharacterRange: range, actualCharacterRange: nil)
|
|
layoutManager.invalidateDisplay(forCharacterRange: range)
|
|
sizeToFit()
|
|
rect = objectRect(at: location)
|
|
}
|
|
editor.view.frame = NSRect(origin: rect.origin, size: size)
|
|
}
|
|
|
|
/// The edit of the open editor and the place of its object, for a save while the editor stays open.
|
|
func editorDraft() -> (location: Int, edit: ObjectEdit)? {
|
|
guard let editor, let location = editedLocation() else { return nil }
|
|
return (location, editor.edit())
|
|
}
|
|
|
|
/// Closes the editor and replaces its object with the edit; `exit` places the caret, nil leaves it.
|
|
func endEditing(_ exit: EditorExit?) {
|
|
guard let closing = editor, let textStorage else { return }
|
|
let found = editedLocation()
|
|
let edit = closing.edit()
|
|
editor = nil
|
|
editedAttachment = nil
|
|
if let responder = window?.firstResponder as? NSView, responder.isDescendant(of: closing.view) {
|
|
window?.makeFirstResponder(self)
|
|
}
|
|
closing.view.removeFromSuperview()
|
|
guard let location = found else { return }
|
|
if let cell = objectCell(at: location) {
|
|
cell.editingHeight = nil
|
|
layoutManager?.invalidateLayout(forCharacterRange: NSRange(location: location, length: 1), actualCharacterRange: nil)
|
|
layoutManager?.invalidateDisplay(forCharacterRange: NSRange(location: location, length: 1))
|
|
}
|
|
var removedAt: Int?
|
|
if let (range, text) = replacement(edit, at: location) {
|
|
change(range, to: text)
|
|
if text.length == 0 { removedAt = range.location }
|
|
}
|
|
var newlineAdded = false
|
|
if removedAt == nil, exit == .after || exit == .below, location + 1 == textStorage.length {
|
|
change(NSRange(location: textStorage.length, length: 0), to: pageText.newline())
|
|
newlineAdded = true
|
|
}
|
|
sizeToFit()
|
|
guard let exit else { return }
|
|
let caret: Int
|
|
if let removedAt {
|
|
caret = removedAt
|
|
} else if newlineAdded {
|
|
caret = location + 2
|
|
} else {
|
|
switch exit {
|
|
case .after: caret = location + 1
|
|
case .above: caret = max(0, location - 1)
|
|
case .below: caret = location + 2
|
|
}
|
|
}
|
|
setSelectedRange(NSRange(location: min(caret, length), length: 0))
|
|
scrollRangeToVisible(selectedRange())
|
|
}
|
|
|
|
/// One undoable change of the page, without the typing scroll.
|
|
private func change(_ range: NSRange, to text: NSAttributedString) {
|
|
guard let textStorage else { return }
|
|
isReplacingObject = true
|
|
breakUndoCoalescing()
|
|
if shouldChangeText(in: range, replacementString: text.string) {
|
|
textStorage.replaceCharacters(in: range, with: text)
|
|
didChangeText()
|
|
}
|
|
breakUndoCoalescing()
|
|
isReplacingObject = false
|
|
}
|
|
|
|
private func objectCell(at location: Int) -> ObjectCell? {
|
|
(textStorage?.attribute(.attachment, at: location, effectiveRange: nil) as? NSTextAttachment)?.attachmentCell as? ObjectCell
|
|
}
|
|
|
|
/// The range an edit replaces and its new text; a removed object takes its line with it.
|
|
private func replacement(_ edit: ObjectEdit, at location: Int) -> (NSRange, NSAttributedString)? {
|
|
guard let textStorage, location < textStorage.length else { return nil }
|
|
switch edit {
|
|
case .unchanged:
|
|
return nil
|
|
case let .replaced(kind, body):
|
|
var attributes = textStorage.attributes(at: location, effectiveRange: nil)
|
|
attributes[.attachment] = pageText.object(kind, body)
|
|
return (NSRange(location: location, length: 1), NSAttributedString(string: "\u{FFFC}", attributes: attributes))
|
|
case .removed:
|
|
let string = textStorage.string as NSString
|
|
var range = NSRange(location: location, length: 1)
|
|
if location + 1 < string.length, string.character(at: location + 1) == 0x0A {
|
|
range.length = 2
|
|
} else if location > 0, string.character(at: location - 1) == 0x0A {
|
|
range = NSRange(location: location - 1, length: 2)
|
|
}
|
|
return (range, NSAttributedString())
|
|
}
|
|
}
|
|
}
|