Add toolbar for JSON editor
This commit is contained in:
@@ -4,6 +4,36 @@ import CodeEditSourceEditor
|
||||
import CodeEditTextView
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
public final class CodeEditEditorController: ObservableObject {
|
||||
weak var controller: TextViewController?
|
||||
|
||||
@Published public var canUndo = false
|
||||
@Published public var canRedo = false
|
||||
|
||||
public init() {}
|
||||
|
||||
public func undo() {
|
||||
controller?.textView.undoManager?.undo()
|
||||
updateUndoState()
|
||||
}
|
||||
|
||||
public func redo() {
|
||||
controller?.textView.undoManager?.redo()
|
||||
updateUndoState()
|
||||
}
|
||||
|
||||
public func insertSymbol(_ symbol: String) {
|
||||
guard let textView = controller?.textView else { return }
|
||||
textView.insertText(symbol, replacementRange: textView.selectedRange())
|
||||
}
|
||||
|
||||
func updateUndoState() {
|
||||
canUndo = controller?.textView.undoManager?.canUndo ?? false
|
||||
canRedo = controller?.textView.undoManager?.canRedo ?? false
|
||||
}
|
||||
}
|
||||
|
||||
private extension NSColor {
|
||||
var forEditor: NSColor {
|
||||
usingColorSpace(.sRGB) ?? self
|
||||
@@ -53,6 +83,13 @@ private func makeConfiguration(isEditable: Bool) -> SourceEditorConfiguration {
|
||||
struct CodeEditTextView: NSViewRepresentable {
|
||||
@Binding var text: String
|
||||
let isEditable: Bool
|
||||
let editorController: CodeEditEditorController?
|
||||
|
||||
init(text: Binding<String>, isEditable: Bool, editorController: CodeEditEditorController? = nil) {
|
||||
_text = text
|
||||
self.isEditable = isEditable
|
||||
self.editorController = editorController
|
||||
}
|
||||
|
||||
func makeNSView(context: Context) -> NSView {
|
||||
let controller = TextViewController(
|
||||
@@ -79,6 +116,11 @@ struct CodeEditTextView: NSViewRepresentable {
|
||||
|
||||
context.coordinator.controller = controller
|
||||
context.coordinator.setupObservation()
|
||||
editorController?.controller = controller
|
||||
Task { @MainActor in
|
||||
editorController?.updateUndoState()
|
||||
}
|
||||
|
||||
return containerView
|
||||
}
|
||||
|
||||
@@ -90,19 +132,22 @@ struct CodeEditTextView: NSViewRepresentable {
|
||||
if controller.configuration.behavior.isEditable != isEditable {
|
||||
controller.configuration = makeConfiguration(isEditable: isEditable)
|
||||
}
|
||||
editorController?.controller = controller
|
||||
}
|
||||
|
||||
func makeCoordinator() -> Coordinator {
|
||||
Coordinator(text: $text)
|
||||
Coordinator(text: $text, editorController: editorController)
|
||||
}
|
||||
|
||||
class Coordinator: NSObject {
|
||||
var controller: TextViewController?
|
||||
@Binding var text: String
|
||||
private var observation: NSObjectProtocol?
|
||||
private weak var editorController: CodeEditEditorController?
|
||||
|
||||
init(text: Binding<String>) {
|
||||
init(text: Binding<String>, editorController: CodeEditEditorController?) {
|
||||
_text = text
|
||||
self.editorController = editorController
|
||||
super.init()
|
||||
}
|
||||
|
||||
@@ -115,6 +160,9 @@ struct CodeEditTextView: NSViewRepresentable {
|
||||
) { [weak self] _ in
|
||||
guard let self, let controller = self.controller else { return }
|
||||
self.text = controller.text
|
||||
Task { @MainActor in
|
||||
self.editorController?.updateUndoState()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ public struct MacApplication: Scene {
|
||||
@StateObject private var environments = ExtensionEnvironments()
|
||||
|
||||
private let profileEditor: (Binding<String>, Bool) -> AnyView = { text, isEditable in
|
||||
AnyView(CodeEditTextView(text: text, isEditable: isEditable))
|
||||
AnyView(ProfileEditorWrapperView(text: text, isEditable: isEditable))
|
||||
}
|
||||
|
||||
public init() {}
|
||||
|
||||
@@ -13,7 +13,7 @@ public struct MainView: View {
|
||||
@State private var pendingSettingsPage: SettingsPage?
|
||||
|
||||
private let profileEditor: (Binding<String>, Bool) -> AnyView = { text, isEditable in
|
||||
AnyView(CodeEditTextView(text: text, isEditable: isEditable))
|
||||
AnyView(ProfileEditorWrapperView(text: text, isEditable: isEditable))
|
||||
}
|
||||
|
||||
public init() {}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
import ApplicationLibrary
|
||||
import Libbox
|
||||
import SwiftUI
|
||||
|
||||
struct ProfileEditorWrapperView: View {
|
||||
@Binding var text: String
|
||||
let isEditable: Bool
|
||||
|
||||
@StateObject private var controller = CodeEditEditorController()
|
||||
@State private var configurationError: String?
|
||||
@State private var validationTask: Task<Void, Never>?
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 0) {
|
||||
CodeEditTextView(text: $text, isEditable: isEditable, editorController: controller)
|
||||
|
||||
if isEditable {
|
||||
EditorToolbarView(
|
||||
canUndo: controller.canUndo,
|
||||
canRedo: controller.canRedo,
|
||||
onUndo: { controller.undo() },
|
||||
onRedo: { controller.redo() },
|
||||
onFormat: { formatConfiguration() },
|
||||
onInsertSymbol: { controller.insertSymbol($0) },
|
||||
configurationError: configurationError,
|
||||
onDismissError: { configurationError = nil }
|
||||
)
|
||||
}
|
||||
}
|
||||
.onChangeCompat(of: text) {
|
||||
if isEditable {
|
||||
scheduleValidation()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func scheduleValidation() {
|
||||
configurationError = nil
|
||||
validationTask?.cancel()
|
||||
validationTask = Task {
|
||||
try? await Task.sleep(nanoseconds: 2 * NSEC_PER_SEC)
|
||||
guard !Task.isCancelled else { return }
|
||||
await checkConfiguration()
|
||||
}
|
||||
}
|
||||
|
||||
private func checkConfiguration() async {
|
||||
let content = text
|
||||
if content.isEmpty { return }
|
||||
var error: NSError?
|
||||
LibboxCheckConfig(content, &error)
|
||||
if let error {
|
||||
configurationError = error.localizedDescription
|
||||
} else {
|
||||
configurationError = nil
|
||||
}
|
||||
}
|
||||
|
||||
private func formatConfiguration() {
|
||||
let content = text
|
||||
if content.isEmpty { return }
|
||||
var error: NSError?
|
||||
let result = LibboxFormatConfig(content, &error)
|
||||
if let error {
|
||||
configurationError = error.localizedDescription
|
||||
return
|
||||
}
|
||||
if let formatted = result?.value, formatted != content {
|
||||
text = formatted
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user