mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
Clean up SwiftLint warnings and add pseudo-placeholder text to editor
This commit is contained in:
parent
ba64716840
commit
c655e628e1
@ -1,14 +1,12 @@
|
||||
/**
|
||||
* Based on:
|
||||
*
|
||||
* MacEditorTextView
|
||||
* Copyright (c) Thiago Holanda 2020
|
||||
* https://twitter.com/tholanda
|
||||
*
|
||||
* MIT license
|
||||
*
|
||||
* See: https://gist.github.com/unnamedd/6e8c3fbc806b8deb60fa65d6b9affab0
|
||||
*/
|
||||
// Based on:
|
||||
//
|
||||
// MacEditorTextView
|
||||
// Copyright (c) Thiago Holanda 2020
|
||||
// https://twitter.com/tholanda
|
||||
//
|
||||
// MIT license
|
||||
//
|
||||
// See: https://gist.github.com/unnamedd/6e8c3fbc806b8deb60fa65d6b9affab0
|
||||
|
||||
import Combine
|
||||
import SwiftUI
|
||||
@ -18,15 +16,15 @@ struct MacEditorTextView: NSViewRepresentable {
|
||||
var isFirstResponder: Bool = false
|
||||
var isEditable: Bool = true
|
||||
var font: NSFont? = .systemFont(ofSize: 14, weight: .regular)
|
||||
|
||||
var onEditingChanged: () -> Void = {}
|
||||
var onCommit : () -> Void = {}
|
||||
var onTextChange : (String) -> Void = { _ in }
|
||||
|
||||
|
||||
var onEditingChanged: () -> Void = {}
|
||||
var onCommit: () -> Void = {}
|
||||
var onTextChange: (String) -> Void = { _ in }
|
||||
|
||||
func makeCoordinator() -> Coordinator {
|
||||
Coordinator(self)
|
||||
}
|
||||
|
||||
|
||||
func makeNSView(context: Context) -> CustomTextView {
|
||||
let textView = CustomTextView(
|
||||
text: text,
|
||||
@ -35,10 +33,10 @@ struct MacEditorTextView: NSViewRepresentable {
|
||||
font: font
|
||||
)
|
||||
textView.delegate = context.coordinator
|
||||
|
||||
|
||||
return textView
|
||||
}
|
||||
|
||||
|
||||
func updateNSView(_ view: CustomTextView, context: Context) {
|
||||
view.text = text
|
||||
view.selectedRanges = context.coordinator.selectedRanges
|
||||
@ -48,40 +46,40 @@ struct MacEditorTextView: NSViewRepresentable {
|
||||
// MARK: - Coordinator
|
||||
|
||||
extension MacEditorTextView {
|
||||
|
||||
|
||||
class Coordinator: NSObject, NSTextViewDelegate {
|
||||
var parent: MacEditorTextView
|
||||
var selectedRanges: [NSValue] = []
|
||||
var didBecomeFirstResponder: Bool = false
|
||||
|
||||
|
||||
init(_ parent: MacEditorTextView) {
|
||||
self.parent = parent
|
||||
}
|
||||
|
||||
|
||||
func textDidBeginEditing(_ notification: Notification) {
|
||||
guard let textView = notification.object as? NSTextView else {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
self.parent.text = textView.string
|
||||
self.parent.onEditingChanged()
|
||||
}
|
||||
|
||||
|
||||
func textDidChange(_ notification: Notification) {
|
||||
guard let textView = notification.object as? NSTextView else {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
self.parent.text = textView.string
|
||||
self.selectedRanges = textView.selectedRanges
|
||||
self.parent.onTextChange(textView.string)
|
||||
}
|
||||
|
||||
|
||||
func textDidEndEditing(_ notification: Notification) {
|
||||
guard let textView = notification.object as? NSTextView else {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
self.parent.text = textView.string
|
||||
self.parent.onCommit()
|
||||
}
|
||||
@ -94,104 +92,103 @@ final class CustomTextView: NSView {
|
||||
private var isFirstResponder: Bool
|
||||
private var isEditable: Bool
|
||||
private var font: NSFont?
|
||||
|
||||
|
||||
weak var delegate: NSTextViewDelegate?
|
||||
|
||||
|
||||
var text: String {
|
||||
didSet {
|
||||
textView.string = text
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var selectedRanges: [NSValue] = [] {
|
||||
didSet {
|
||||
guard selectedRanges.count > 0 else {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
textView.selectedRanges = selectedRanges
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private lazy var scrollView: NSScrollView = {
|
||||
let scrollView = NSScrollView()
|
||||
scrollView.drawsBackground = true
|
||||
scrollView.drawsBackground = false
|
||||
scrollView.borderType = .noBorder
|
||||
scrollView.hasVerticalScroller = true
|
||||
scrollView.hasHorizontalRuler = false
|
||||
scrollView.autoresizingMask = [.width, .height]
|
||||
scrollView.translatesAutoresizingMaskIntoConstraints = false
|
||||
|
||||
|
||||
return scrollView
|
||||
}()
|
||||
|
||||
|
||||
private lazy var textView: NSTextView = {
|
||||
let contentSize = scrollView.contentSize
|
||||
let textStorage = NSTextStorage()
|
||||
|
||||
|
||||
|
||||
let layoutManager = NSLayoutManager()
|
||||
textStorage.addLayoutManager(layoutManager)
|
||||
|
||||
|
||||
|
||||
let textContainer = NSTextContainer(containerSize: scrollView.frame.size)
|
||||
textContainer.widthTracksTextView = true
|
||||
textContainer.containerSize = NSSize(
|
||||
width: contentSize.width,
|
||||
height: CGFloat.greatestFiniteMagnitude
|
||||
)
|
||||
|
||||
|
||||
layoutManager.addTextContainer(textContainer)
|
||||
|
||||
|
||||
let textView = NSTextView(frame: .zero, textContainer: textContainer)
|
||||
textView.autoresizingMask = .width
|
||||
textView.backgroundColor = NSColor.textBackgroundColor
|
||||
textView.delegate = self.delegate
|
||||
textView.drawsBackground = true
|
||||
textView.font = self.font
|
||||
textView.isEditable = self.isEditable
|
||||
|
||||
let textView = NSTextView(frame: .zero, textContainer: textContainer)
|
||||
textView.autoresizingMask = .width
|
||||
textView.delegate = self.delegate
|
||||
textView.drawsBackground = false
|
||||
textView.font = self.font
|
||||
textView.isEditable = self.isEditable
|
||||
textView.isHorizontallyResizable = false
|
||||
textView.isVerticallyResizable = true
|
||||
textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
|
||||
textView.minSize = NSSize(width: 0, height: contentSize.height)
|
||||
textView.textColor = NSColor.labelColor
|
||||
|
||||
textView.isVerticallyResizable = true
|
||||
textView.maxSize = NSSize(
|
||||
width: CGFloat.greatestFiniteMagnitude,
|
||||
height: CGFloat.greatestFiniteMagnitude
|
||||
)
|
||||
textView.minSize = NSSize(width: 0, height: contentSize.height)
|
||||
textView.textColor = NSColor.labelColor
|
||||
|
||||
return textView
|
||||
}()
|
||||
|
||||
|
||||
// MARK: - Init
|
||||
init(text: String, isEditable: Bool, isFirstResponder: Bool, font: NSFont?) {
|
||||
self.font = font
|
||||
self.isFirstResponder = isFirstResponder
|
||||
self.isEditable = isEditable
|
||||
self.text = text
|
||||
|
||||
self.font = font
|
||||
self.isFirstResponder = isFirstResponder
|
||||
self.isEditable = isEditable
|
||||
self.text = text
|
||||
|
||||
super.init(frame: .zero)
|
||||
}
|
||||
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Life cycle
|
||||
|
||||
|
||||
override func viewWillDraw() {
|
||||
super.viewWillDraw()
|
||||
|
||||
|
||||
setupScrollViewConstraints()
|
||||
setupTextView()
|
||||
|
||||
|
||||
if isFirstResponder {
|
||||
self.window?.makeFirstResponder(self.textView)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func setupScrollViewConstraints() {
|
||||
scrollView.translatesAutoresizingMaskIntoConstraints = false
|
||||
|
||||
|
||||
addSubview(scrollView)
|
||||
|
||||
|
||||
NSLayoutConstraint.activate([
|
||||
scrollView.topAnchor.constraint(equalTo: topAnchor),
|
||||
scrollView.trailingAnchor.constraint(equalTo: trailingAnchor),
|
||||
@ -199,7 +196,7 @@ final class CustomTextView: NSView {
|
||||
scrollView.leadingAnchor.constraint(equalTo: leadingAnchor)
|
||||
])
|
||||
}
|
||||
|
||||
|
||||
func setupTextView() {
|
||||
scrollView.documentView = textView
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ struct PostEditorView: View {
|
||||
updatingBodyFromServer: $updatingBodyFromServer
|
||||
)
|
||||
.padding()
|
||||
.background(Color(NSColor.controlBackgroundColor))
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .status) {
|
||||
PostEditorStatusToolbarView(post: post)
|
||||
|
@ -51,15 +51,24 @@ struct PostTextEditingView: View {
|
||||
// .padding(4)
|
||||
// .background(Color(NSColor.controlBackgroundColor))
|
||||
// }
|
||||
MacEditorTextView(
|
||||
text: $combinedText,
|
||||
isFirstResponder: post.status == PostStatus.local.rawValue,
|
||||
isEditable: true,
|
||||
font: NSFont(name: appearance.rawValue, size: 17),
|
||||
onEditingChanged: onEditingChanged,
|
||||
onCommit: onCommit,
|
||||
onTextChange: onTextChange
|
||||
)
|
||||
ZStack(alignment: .topLeading) {
|
||||
if combinedText.count == 0 {
|
||||
Text("Write…")
|
||||
.foregroundColor(Color(NSColor.placeholderTextColor))
|
||||
.padding(.horizontal, 5)
|
||||
.font(.custom(appearance.rawValue, size: 17, relativeTo: .body))
|
||||
}
|
||||
MacEditorTextView(
|
||||
text: $combinedText,
|
||||
isFirstResponder: post.status == PostStatus.local.rawValue,
|
||||
isEditable: true,
|
||||
font: NSFont(name: appearance.rawValue, size: 17),
|
||||
onEditingChanged: onEditingChanged,
|
||||
onCommit: onCommit,
|
||||
onTextChange: onTextChange
|
||||
)
|
||||
}
|
||||
.background(Color(NSColor.controlBackgroundColor))
|
||||
.onAppear(perform: {
|
||||
switch post.appearance {
|
||||
case "sans":
|
||||
@ -69,6 +78,7 @@ struct PostTextEditingView: View {
|
||||
default:
|
||||
self.appearance = .serif
|
||||
}
|
||||
print("Font: \(appearance.rawValue)")
|
||||
|
||||
if post.title.isEmpty {
|
||||
self.combinedText = post.body
|
||||
|
Loading…
Reference in New Issue
Block a user