updates plug with all sorts of awesome improvements
This commit is contained in:
@@ -22,8 +22,10 @@ public final class PageViewController: NSViewController, NSTextViewDelegate, NST
|
||||
let scrollView = NSScrollView()
|
||||
let textView: PageTextView
|
||||
let dragStrip = DragStripView()
|
||||
let bookmarkIndex = BookmarkIndexView()
|
||||
private let banner = NSTextField(labelWithString: "")
|
||||
private var textSize = TextSize()
|
||||
private var columnWidth = ColumnWidth()
|
||||
private var isLoading = false
|
||||
/// The characters edited since the last stamp of created dates.
|
||||
private var unstamped: NSRange?
|
||||
@@ -69,10 +71,10 @@ public final class PageViewController: NSViewController, NSTextViewDelegate, NST
|
||||
textView.onEditorChange = { [weak self] in self?.store.changed() }
|
||||
textView.textStorage?.delegate = self
|
||||
|
||||
banner.font = .systemFont(ofSize: 12)
|
||||
banner.font = PlakFonts.body(12)
|
||||
banner.textColor = Theme.ink
|
||||
banner.drawsBackground = true
|
||||
banner.backgroundColor = Theme.selection
|
||||
banner.backgroundColor = Theme.panel
|
||||
banner.alignment = .center
|
||||
banner.isHidden = true
|
||||
banner.translatesAutoresizingMaskIntoConstraints = false
|
||||
@@ -83,7 +85,12 @@ public final class PageViewController: NSViewController, NSTextViewDelegate, NST
|
||||
dragStrip.setDropTarget(textView)
|
||||
textView.dragStrip = dragStrip
|
||||
|
||||
bookmarkIndex.isHidden = true
|
||||
bookmarkIndex.onSelect = { [weak self] index in self?.showBookmark(entry: index) }
|
||||
textView.bookmarkIndex = bookmarkIndex
|
||||
|
||||
root.addSubview(scrollView)
|
||||
root.addSubview(bookmarkIndex)
|
||||
root.addSubview(dragStrip)
|
||||
root.addSubview(banner)
|
||||
NSLayoutConstraint.activate([
|
||||
@@ -102,6 +109,7 @@ public final class PageViewController: NSViewController, NSTextViewDelegate, NST
|
||||
textView.textStorage?.setAttributedString(textView.pageText.attributedString(file))
|
||||
isLoading = false
|
||||
textView.undoManager?.removeAllActions()
|
||||
textView.reloadBookmarks()
|
||||
}
|
||||
|
||||
/// The page file text of the page as it is now, with the edit of an open editor in it.
|
||||
@@ -126,6 +134,13 @@ public final class PageViewController: NSViewController, NSTextViewDelegate, NST
|
||||
scrollEndToMiddle()
|
||||
}
|
||||
|
||||
/// Pastes at the end of the page as a new block, then shows the end in the middle.
|
||||
public func append(from pasteboard: NSPasteboard) {
|
||||
closeEditor()
|
||||
textView.append(from: pasteboard)
|
||||
showEnd()
|
||||
}
|
||||
|
||||
// MARK: Images
|
||||
|
||||
/// Saves each image in the image folder and puts it in the text; a failed image is skipped.
|
||||
@@ -173,6 +188,23 @@ public final class PageViewController: NSViewController, NSTextViewDelegate, NST
|
||||
textView.scrollRangeToVisible(textView.selectedRange())
|
||||
}
|
||||
|
||||
// MARK: Column width
|
||||
|
||||
@objc public func makeColumnWider(_ sender: Any?) {
|
||||
columnWidth.wider()
|
||||
applyColumnWidth()
|
||||
}
|
||||
|
||||
@objc public func makeColumnNarrower(_ sender: Any?) {
|
||||
columnWidth.narrower()
|
||||
applyColumnWidth()
|
||||
}
|
||||
|
||||
private func applyColumnWidth() {
|
||||
layoutPage()
|
||||
textView.scrollRangeToVisible(textView.selectedRange())
|
||||
}
|
||||
|
||||
// MARK: Text changes
|
||||
|
||||
public func textStorage(_ textStorage: NSTextStorage, willProcessEditing editedMask: NSTextStorageEditActions,
|
||||
@@ -184,8 +216,12 @@ public final class PageViewController: NSViewController, NSTextViewDelegate, NST
|
||||
public func textDidChange(_ notification: Notification) {
|
||||
stampBlocks()
|
||||
store.changed()
|
||||
textView.invalidateMargin()
|
||||
if textView.topSpace > PageMetrics.topInset || textView.frame.height <= clip.bounds.height * 2 { layoutPage() }
|
||||
textView.reloadBookmarks()
|
||||
if textView.topSpace > PageMetrics.topInset || textView.frame.height <= clip.bounds.height * 2 {
|
||||
layoutPage()
|
||||
} else {
|
||||
layoutBookmarkIndex()
|
||||
}
|
||||
if !textView.isReplacingObject { keepCaretAboveFloor() }
|
||||
}
|
||||
|
||||
@@ -213,7 +249,7 @@ public final class PageViewController: NSViewController, NSTextViewDelegate, NST
|
||||
private func layoutPage() {
|
||||
let visible = clip.bounds.size
|
||||
guard visible.width > 0, let container = textView.textContainer, let layoutManager = textView.layoutManager else { return }
|
||||
let column = max(1, min(PageMetrics.columnWidth(bodySize: textSize.body), visible.width - PageMetrics.sideMargin * 2))
|
||||
let column = max(1, min(PageMetrics.columnWidth(bodySize: textSize.body, characters: columnWidth.characters), visible.width - PageMetrics.sideMargin * 2))
|
||||
if container.size.width != column {
|
||||
container.size = NSSize(width: column, height: CGFloat.greatestFiniteMagnitude)
|
||||
}
|
||||
@@ -227,6 +263,40 @@ public final class PageViewController: NSViewController, NSTextViewDelegate, NST
|
||||
if textView.frame.width != visible.width { textView.setFrameSize(NSSize(width: visible.width, height: textView.frame.height)) }
|
||||
textView.sizeToFit()
|
||||
textView.positionEditor()
|
||||
layoutBookmarkIndex()
|
||||
}
|
||||
|
||||
/// Shows the bookmark index at the top left when bookmarks are wide, as wide as the side space.
|
||||
private func layoutBookmarkIndex() {
|
||||
let labels = textView.bookmarksAreWide ? textView.bookmarks.suffix(PageMetrics.bookmarkIndexCount).map(\.label) : []
|
||||
let wasHidden = bookmarkIndex.isHidden
|
||||
bookmarkIndex.isHidden = labels.isEmpty
|
||||
if !labels.isEmpty {
|
||||
let inset = PageMetrics.bookmarkIndexInset
|
||||
let width = max(0, textView.sideSpace - inset - PageMetrics.bookmarkGap * 2)
|
||||
bookmarkIndex.show(labels: labels, font: textView.bookmarkFont, width: width)
|
||||
let top = view.bounds.height - PageMetrics.topInset
|
||||
let height = max(0, min(bookmarkIndex.contentHeight, top - inset))
|
||||
let frame = NSRect(x: inset, y: top - height, width: width, height: height)
|
||||
if bookmarkIndex.frame != frame { bookmarkIndex.frame = frame }
|
||||
// When the entries do not fit, the lowest bookmarks stay in sight.
|
||||
bookmarkIndex.contentView.scroll(to: NSPoint(x: 0, y: max(0, bookmarkIndex.contentHeight - height)))
|
||||
bookmarkIndex.reflectScrolledClipView(bookmarkIndex.contentView)
|
||||
}
|
||||
if wasHidden != bookmarkIndex.isHidden || !labels.isEmpty { view.window?.invalidateCursorRects(for: textView) }
|
||||
}
|
||||
|
||||
/// Puts the caret at the end of the bookmark line and scrolls that line to the vertical middle.
|
||||
/// `entry` counts from the first entry of the index, which holds only the last bookmarks.
|
||||
func showBookmark(entry: Int) {
|
||||
let shown = textView.bookmarks.suffix(PageMetrics.bookmarkIndexCount)
|
||||
let index = shown.startIndex + entry
|
||||
closeEditor()
|
||||
guard textView.bookmarks.indices.contains(index) else { return }
|
||||
let end = NSMaxRange(textView.bookmarks[index].range)
|
||||
view.window?.makeFirstResponder(textView)
|
||||
textView.setSelectedRange(NSRange(location: end, length: 0))
|
||||
scroll(toY: textView.lineRect(at: end).midY - clip.bounds.height / 2)
|
||||
}
|
||||
|
||||
@objc private func clipFrameChanged(_ notification: Notification) {
|
||||
|
||||
Reference in New Issue
Block a user