SPB Git

Repository Public

EditorCanvasView+Mouse.swift 22.7 KB 401 lines swift

1// ORIX Docs2// File: App/Sources/Canvas/EditorCanvasView+Mouse.swift3// Description: Mouse, clipboard, drag and drop, contextual menu and accessibility for the canvas.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import AppKit8import UniformTypeIdentifiers9import DocModel10import DocContainer11import DocLayout12import DocAutomation1314extension NSPasteboard.PasteboardType {15    static let orixFragment = NSPasteboard.PasteboardType("ai.spboucher.orix.docs.fragment")16}1718extension EditorCanvasView {19    // MARK: Hit testing2021    /// Resolves a click: stays in the story being edited, enters a note with one click, a header or footer with a double click.22    func hit(at viewPoint: CGPoint, clickCount: Int) -> (hit: HitResult, page: Int)? {23        guard let pp = pagePoint(viewPoint) else { return nil }24        let current = session.selection.story25        if let any = layout.hitTest(page: pp.page, point: pp.point, restrictTo: nil), any.story != current {26            // Only when the point is really over that other story.27            let over = layout.pages[pp.page].texts.contains { $0.story == any.story && $0.frame.insetBy(dx: -4, dy: -3).contains(pp.point) }28            var allowed = false29            switch (current, any.story) {30            case (_, .note): allowed = over31            case (.note, .body): allowed = true32            case (_, .header), (_, .footer): allowed = over && clickCount >= 233            case (.header, .body), (.footer, .body): allowed = clickCount >= 234            default: allowed = false35            }36            if allowed { return (any, pp.page) }37        }38        if let h = layout.hitTest(page: pp.page, point: pp.point, restrictTo: current) { return (h, pp.page) }39        return nil40    }4142    /// An inline image, shape or equation under the point: its position.43    private func inlineObject(at viewPoint: CGPoint, near hit: HitResult, page: Int) -> Position? {44        guard let pp = pagePoint(viewPoint), let p = session.doc.blocks(in: hit.story).paragraph(at: hit.position.path) else { return nil }45        for offset in [hit.position.offset - 1, hit.position.offset] where offset >= 0 && offset < p.length {46            guard let inline = p.run(containing: offset)?.inline else { continue }47            switch inline { case .image, .shape, .equation: break; default: continue }48            let pos = Position(path: hit.position.path, offset: offset)49            guard let loc = layout.locate(story: hit.story, position: pos, preferredPage: page), let box = loc.fragment.layout.box(at: offset) else { continue }50            let line = loc.fragment.layout.lines[loc.line]51            let rect = CGRect(x: loc.fragment.origin.x + line.xPosition(for: offset), y: loc.fragment.baseline(ofLine: loc.line) - box.ascent, width: box.width, height: box.ascent + box.descent)52            if rect.contains(pp.point) { return pos }53        }54        return nil55    }5657    // MARK: Mouse5859    override func mouseDown(with event: NSEvent) {60        window?.makeFirstResponder(self)61        if marked != nil { unmarkText() }62        goalX = nil63        let p = convert(event.locationInWindow, from: nil)64        guard let pp = pagePoint(p) else { return }65        // Resize handle of the selected object.66        if let obj = selectedObject(), obj.page == pp.page, handleRect(for: obj.rect).insetBy(dx: -4, dy: -4).contains(pp.point) {67            dragState = .resizingObject(story: session.selection.story, position: obj.position, startPoint: pp.point, startSize: obj.rect.size)68            return69        }70        // Column boundary of a table.71        if session.selection.story == .body, let b = layout.columnBoundary(page: pp.page, point: pp.point), let t = session.doc.body[b.block].table {72            let scale = t.totalWidth > 0 ? b.layout.width / t.totalWidth : 173            dragState = .resizingColumn(block: b.block, column: b.column, startX: pp.point.x, startWidth: t.columnWidths[b.column], scale: scale)74            return75        }76        // Floating object.77        if let f = layout.floatHit(page: pp.page, point: pp.point), f.story == session.selection.story || f.story == .body {78            if f.story != session.selection.story { session.activePage = nil }79            session.select(Selection(story: f.story, anchor: Position(path: f.path, offset: f.box.offset), head: Position(path: f.path, offset: f.box.offset + 1)), scroll: false)80            return81        }82        guard let (h, page) = hit(at: p, clickCount: event.clickCount) else { return }83        if h.story != session.selection.story { session.activePage = h.story == .body ? nil : page }84        let story = h.story85        if let obj = inlineObject(at: p, near: h, page: page) {86            session.select(Selection(story: story, anchor: obj, head: Position(path: obj.path, offset: obj.offset + 1)), scroll: false)87            if event.clickCount == 2, case .equation? = session.editor.inline(at: obj, in: story) { session.activeSheet = .equation(existing: obj) }88            return89        }90        if event.modifierFlags.contains(.command), let para = session.doc.blocks(in: story).paragraph(at: h.position.path),91           let link = para.run(containing: h.position.offset)?.props.link ?? para.run(containing: max(0, h.position.offset - 1))?.props.link {92            session.follow(link: link)93            return94        }95        let blocks = session.doc.blocks(in: story)96        dragState = .selecting97        switch event.clickCount {98        case 1:99            if event.modifierFlags.contains(.shift), story == session.selection.story {100                session.select(Selection(story: story, anchor: session.selection.anchor, head: h.position), upstream: h.upstream, scroll: false)101            } else {102                session.select(Selection(story: story, caret: h.position), upstream: h.upstream, scroll: false)103            }104            dragState = .selecting105        case 2:106            if let para = blocks.paragraph(at: h.position.path) {107                let r = para.plainText.wordRange(at: h.position.offset)108                session.select(Selection(story: story, anchor: Position(path: h.position.path, offset: r.lowerBound), head: Position(path: h.position.path, offset: r.upperBound)), scroll: false)109            } else {110                session.select(Selection(story: story, caret: h.position), scroll: false)111            }112        default:113            if let para = blocks.paragraph(at: h.position.path) {114                session.select(Selection(story: story, anchor: Position(path: h.position.path, offset: 0), head: Position(path: h.position.path, offset: para.length)), scroll: false)115            }116        }117    }118119    override func mouseDragged(with event: NSEvent) {120        let p = convert(event.locationInWindow, from: nil)121        guard let pp = pagePoint(p) else { return }122        switch dragState {123        case .none: break124        case .selecting:125            if let h = layout.hitTest(page: pp.page, point: pp.point, restrictTo: session.selection.story), !h.onAtomicBlock || session.selection.story == .body {126                session.select(Selection(story: session.selection.story, anchor: session.selection.anchor, head: h.position), upstream: h.upstream, scroll: false)127            }128            autoscroll(with: event)129        case .resizingColumn(let block, let column, let startX, let startWidth, let scale):130            let width = startWidth + (pp.point.x - startX) / max(0.01, scale)131            session.perform(String(localized: "Resize Column"), coalesce: "resize-column") { $0.resizeColumn(block: block, column: column, width: width) }132        case .resizingObject(let story, let position, let startPoint, let startSize):133            let factor = max(0.05, (startSize.width + (pp.point.x - startPoint.x)) / max(1, startSize.width))134            session.perform(String(localized: "Resize"), coalesce: "resize-object") { e in135                e.updateInline(at: position, in: story) { inline in136                    switch inline {137                    case .image(var img):138                        let ratio = img.height / max(1, img.width)139                        img.width = max(12, (startSize.width * factor).rounded()); img.height = (img.width * ratio).rounded()140                        inline = .image(img)141                    case .shape(var s):142                        s.width = max(12, (startSize.width + (pp.point.x - startPoint.x)).rounded())143                        s.height = max(8, (startSize.height + (pp.point.y - startPoint.y)).rounded())144                        inline = .shape(s)145                    default: break146                    }147                }148            }149        }150    }151152    override func mouseUp(with event: NSEvent) { dragState = .none }153154    override func mouseMoved(with event: NSEvent) {155        let p = convert(event.locationInWindow, from: nil)156        guard let pp = pagePoint(p), pageFrames.indices.contains(pp.page), pageFrames[pp.page].contains(p) else { NSCursor.arrow.set(); return }157        if let obj = selectedObject(), obj.page == pp.page, handleRect(for: obj.rect).insetBy(dx: -4, dy: -4).contains(pp.point) { NSCursor.crosshair.set() }158        else if layout.columnBoundary(page: pp.page, point: pp.point) != nil { NSCursor.resizeLeftRight.set() }159        else if layout.floatHit(page: pp.page, point: pp.point) != nil { NSCursor.pointingHand.set() }160        else { NSCursor.iBeam.set() }161    }162163    override func cursorUpdate(with event: NSEvent) { mouseMoved(with: event) }164165    // MARK: Clipboard166167    @objc func copy(_ sender: Any?) {168        guard let fragment = session.editor.copyFragment() else { return }169        let pb = NSPasteboard.general170        pb.clearContents()171        var types: [NSPasteboard.PasteboardType] = [.orixFragment, .string]172        // Rich text for other applications, produced by the same exporters as File > Export.173        var temp = DocumentModel(styles: session.doc.styles, pageSetup: session.doc.pageSetup, body: fragment.blocks)174        temp.lists = fragment.lists175        temp.notes = fragment.notes176        temp.bibliography = session.doc.bibliography177        temp.settings.citationStyle = session.doc.settings.citationStyle178        for (id, data) in fragment.assetData { temp.assets[id] = Asset(id: id, mimeType: fragment.assetTypes[id] ?? "image/png", data: data) }179        let rtf = try? DocumentConverter.export(temp, format: .rtf).data180        let html = try? DocumentConverter.export(temp, format: .html).data181        if rtf != nil { types.append(.rtf) }182        if html != nil { types.append(.html) }183        pb.declareTypes(types, owner: nil)184        if let json = try? JSONEncoder().encode(fragment) { pb.setData(json, forType: .orixFragment) }185        pb.setString(fragment.plainText, forType: .string)186        if let rtf { pb.setData(rtf, forType: .rtf) }187        if let html { pb.setData(html, forType: .html) }188    }189190    @objc func cut(_ sender: Any?) {191        guard !session.selection.isCaret else { return }192        copy(sender)193        session.perform(String(localized: "Cut")) { $0.deleteSelection() }194    }195196    @objc func paste(_ sender: Any?) { paste(plainOnly: false) }197    @objc func pasteAsPlainText(_ sender: Any?) { paste(plainOnly: true) }198199    private func fragment(from result: ImportResult) -> DocumentFragment {200        var blocks = result.document.body201        // A trailing empty paragraph added by converters would paste as an extra line.202        if blocks.count > 1, let last = blocks.last?.paragraph, last.isEmpty { blocks.removeLast() }203        return DocumentFragment(blocks: blocks, notes: result.document.notes, lists: result.document.lists, assets: result.document.assets)204    }205206    private func paste(plainOnly: Bool) {207        let pb = NSPasteboard.general208        if !plainOnly {209            if let data = pb.data(forType: .orixFragment), let f = try? JSONDecoder().decode(DocumentFragment.self, from: data) {210                session.perform(String(localized: "Paste")) { $0.insertFragment(f) }211                return212            }213            if let urls = pb.readObjects(forClasses: [NSURL.self], options: [.urlReadingFileURLsOnly: true]) as? [URL], let url = urls.first,214               let type = UTType(filenameExtension: url.pathExtension), type.conforms(to: .image), session.insertImage(from: url) { return }215            if let data = pb.data(forType: .rtf), let r = try? DocumentConverter.importDocument(data, format: .rtf) {216                let f = fragment(from: r)217                session.perform(String(localized: "Paste")) { $0.insertFragment(f) }218                return219            }220            if let data = pb.data(forType: .html), let r = try? DocumentConverter.importDocument(data, format: .html) {221                let f = fragment(from: r)222                session.perform(String(localized: "Paste")) { $0.insertFragment(f) }223                return224            }225            if let data = pb.data(forType: .png) ?? pb.data(forType: .tiff) {226                let png = pb.data(forType: .png) != nil227                if session.insertImage(data: data, mimeType: png ? "image/png" : "image/tiff") { return }228            }229        }230        if let s = pb.string(forType: .string) { session.perform(String(localized: "Paste")) { $0.insertText(s) } }231    }232233    @objc override func selectAll(_ sender: Any?) {234        let blocks = session.blocks235        session.select(Selection(story: session.selection.story, anchor: Position(block: 0, offset: 0), head: DocumentEditor.lastPosition(inBlock: blocks.count - 1, blocks).path.cell == nil236                                 ? DocumentEditor.lastPosition(inBlock: blocks.count - 1, blocks) : Position(block: blocks.count - 1, offset: 1)), scroll: false)237    }238239    @objc func delete(_ sender: Any?) { session.perform(String(localized: "Delete")) { $0.deleteSelection() } }240241    // MARK: Drag and drop242243    override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation { .copy }244    override func draggingUpdated(_ sender: NSDraggingInfo) -> NSDragOperation {245        let p = convert(sender.draggingLocation, from: nil)246        if let (h, _) = hit(at: p, clickCount: 1), h.story == session.selection.story {247            session.select(Selection(story: h.story, caret: h.position), upstream: h.upstream, scroll: false)248        }249        return .copy250    }251252    override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {253        let pb = sender.draggingPasteboard254        window?.makeFirstResponder(self)255        if let urls = pb.readObjects(forClasses: [NSURL.self], options: [.urlReadingFileURLsOnly: true]) as? [URL], !urls.isEmpty {256            var done = false257            for url in urls {258                if let type = UTType(filenameExtension: url.pathExtension), type.conforms(to: .image) || type.conforms(to: .pdf) { done = session.insertImage(from: url) || done }259            }260            return done261        }262        if let data = pb.data(forType: .png) ?? pb.data(forType: .tiff) { return session.insertImage(data: data, mimeType: pb.data(forType: .png) != nil ? "image/png" : "image/tiff") }263        if let s = pb.string(forType: .string) { session.perform(String(localized: "Drop")) { $0.insertText(s) }; return true }264        return false265    }266267    // MARK: Contextual menu268269    override func menu(for event: NSEvent) -> NSMenu? {270        let p = convert(event.locationInWindow, from: nil)271        let menu = NSMenu()272        if let (h, _) = hit(at: p, clickCount: 1), h.story == session.selection.story {273            let sel = session.selection274            let inside = !sel.isCaret && h.position >= sel.start && h.position <= sel.end275            if !inside { session.select(Selection(story: h.story, caret: h.position), upstream: h.upstream, scroll: false) }276            // Spelling suggestions for the word under the pointer.277            if let para = session.blocks.paragraph(at: h.position.path) {278                let text = para.plainText279                if let r = spelling.misspellings(id: para.id, text: text).first(where: { $0.lowerBound <= h.position.offset && h.position.offset <= $0.upperBound }) {280                    let word = text.utf16Substring(r)281                    let guesses = NSSpellChecker.shared.guesses(forWordRange: NSRange(location: r.lowerBound, length: r.count), in: text, language: nil, inSpellDocumentWithTag: spelling.documentTag) ?? []282                    for g in guesses.prefix(6) {283                        menu.addItem(closureMenuItem(title: g) { [weak self] in284                            guard let self else { return }285                            self.session.perform(String(localized: "Correct Spelling")) { e in286                                e.replace(TextMatch(story: h.story, path: h.position.path, range: r, context: ""), with: g)287                            }288                        })289                    }290                    if guesses.isEmpty { let none = NSMenuItem(title: String(localized: "No Guesses Found"), action: nil, keyEquivalent: ""); none.isEnabled = false; menu.addItem(none) }291                    menu.addItem(.separator())292                    menu.addItem(closureMenuItem(title: String(localized: "Ignore Spelling")) { [weak self] in293                        guard let self else { return }294                        NSSpellChecker.shared.ignoreWord(word, inSpellDocumentWithTag: self.spelling.documentTag); self.spelling.invalidate(); self.needsDisplay = true295                    })296                    menu.addItem(closureMenuItem(title: String(localized: "Learn Spelling")) { [weak self] in297                        NSSpellChecker.shared.learnWord(word); self?.spelling.invalidate(); self?.needsDisplay = true298                    })299                    menu.addItem(.separator())300                }301            }302        }303        menu.addItem(withTitle: String(localized: "Cut"), action: #selector(cut(_:)), keyEquivalent: "")304        menu.addItem(withTitle: String(localized: "Copy"), action: #selector(copy(_:)), keyEquivalent: "")305        menu.addItem(withTitle: String(localized: "Paste"), action: #selector(paste(_:)), keyEquivalent: "")306        menu.addItem(.separator())307        for command in session.contextCommands() {308            if let command { menu.addItem(commandMenuItem(command, session: session)) } else { menu.addItem(.separator()) }309        }310        return menu311    }312313    // MARK: Accessibility (the story being edited is exposed as one text area, paragraphs separated by newlines)314315    private func flatText() -> (text: String, starts: [(path: ParaPath, start: Int, length: Int)]) {316        var text = ""317        var starts: [(ParaPath, Int, Int)] = []318        var pos = 0319        DocumentModel.forEachParagraph(in: session.blocks) { p, path in320            let t = p.plainText.replacingOccurrences(of: objectReplacementCharacter, with: " ")321            starts.append((path, pos, p.length))322            text += t + "\n"323            pos += p.length + 1324        }325        return (text, starts)326    }327328    private func flatOffset(_ p: Position, _ starts: [(path: ParaPath, start: Int, length: Int)]) -> Int {329        starts.first { $0.path == p.path }.map { $0.start + min(p.offset, $0.length) } ?? 0330    }331332    private func position(flat: Int, _ starts: [(path: ParaPath, start: Int, length: Int)]) -> Position? {333        starts.last { $0.start <= flat }.map { Position(path: $0.path, offset: min($0.length, flat - $0.start)) }334    }335336    override func isAccessibilityElement() -> Bool { true }337    override func accessibilityRole() -> NSAccessibility.Role? { .textArea }338    override func accessibilityRoleDescription() -> String? { String(localized: "document text") }339    override func accessibilityLabel() -> String? { String(localized: "Document") }340    override func accessibilityValue() -> Any? { flatText().text }341    override func accessibilityNumberOfCharacters() -> Int { (flatText().text as NSString).length }342    override func accessibilitySelectedText() -> String? { session.editor.selectedText }343    override func accessibilitySelectedTextRange() -> NSRange {344        let f = flatText()345        let a = flatOffset(session.selection.start, f.starts), b = flatOffset(session.selection.end, f.starts)346        return NSRange(location: a, length: max(0, b - a))347    }348    override func setAccessibilitySelectedTextRange(_ range: NSRange) {349        let f = flatText()350        guard let a = position(flat: range.location, f.starts), let b = position(flat: range.location + range.length, f.starts) else { return }351        session.select(Selection(story: session.selection.story, anchor: a, head: b))352    }353    override func accessibilityInsertionPointLineNumber() -> Int {354        let f = flatText()355        return f.starts.firstIndex { $0.path == session.selection.head.path } ?? 0356    }357    override func accessibilityString(for range: NSRange) -> String? {358        let ns = flatText().text as NSString359        let lo = max(0, min(ns.length, range.location)), hi = max(lo, min(ns.length, range.location + range.length))360        return ns.substring(with: NSRange(location: lo, length: hi - lo))361    }362    override func accessibilityRange(forLine line: Int) -> NSRange {363        let f = flatText()364        guard f.starts.indices.contains(line) else { return NSRange(location: 0, length: 0) }365        return NSRange(location: f.starts[line].start, length: f.starts[line].length)366    }367    override func accessibilityLine(for index: Int) -> Int {368        let f = flatText()369        return f.starts.lastIndex { $0.start <= index } ?? 0370    }371    override func accessibilityFrame(for range: NSRange) -> NSRect {372        let f = flatText()373        guard let pos = position(flat: range.location, f.starts),374              let c = layout.caret(story: session.selection.story, position: pos, blocks: session.blocks, preferredPage: session.activePage), let window else { return .zero }375        var r = viewRect(page: c.page, rect: c.rect)376        if range.length > 0, let end = position(flat: range.location + range.length, f.starts),377           let c2 = layout.caret(story: session.selection.story, position: end, blocks: session.blocks, preferredPage: session.activePage), c2.page == c.page {378            r = r.union(viewRect(page: c2.page, rect: c2.rect))379        }380        return window.convertToScreen(convert(r, to: nil))381    }382    override func accessibilityVisibleCharacterRange() -> NSRange { NSRange(location: 0, length: (flatText().text as NSString).length) }383    override func accessibilityHelp() -> String? { String(localized: "Page \(session.currentPage + 1) of \(layout.pageCount)") }384}385386/// Target object that runs a closure; kept alive by the menu item it serves.387final class MenuAction: NSObject {388    private let handler: () -> Void389    init(_ handler: @escaping () -> Void) { self.handler = handler }390    @objc func run() { handler() }391}392393/// A menu item that runs a closure.394func closureMenuItem(title: String, handler: @escaping () -> Void) -> NSMenuItem {395    let action = MenuAction(handler)396    let item = NSMenuItem(title: title, action: #selector(MenuAction.run), keyEquivalent: "")397    item.target = action398    item.representedObject = action399    return item400}401
Files main
Menu

Theme