From de21dcc519b8ec791805b62bb1e7d9797bd7a7ea Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Mon, 28 Aug 2023 18:14:28 +0200 Subject: [PATCH] feat: add gutter toggle support --- PlantUML/PlantUMLContentView.swift | 10 ++-- .../Sources/CodeViewer/CodeViewer.swift | 46 ++++++++++--------- .../Sources/CodeViewer/CodeWebView.swift | 10 ++++ 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/PlantUML/PlantUMLContentView.swift b/PlantUML/PlantUMLContentView.swift index 0c532f6..e7696e8 100644 --- a/PlantUML/PlantUMLContentView.swift +++ b/PlantUML/PlantUMLContentView.swift @@ -22,7 +22,7 @@ import AppSecureStorage struct PlantUMLContentView: View { - typealias PlantUMLLineEditorView = CodeViewer + typealias PlantUMLEditorView = CodeViewer @Environment(\.scenePhase) var scene @Environment(\.interfaceOrientation) var interfaceOrientation: InterfaceOrientationHolder @@ -44,7 +44,7 @@ struct PlantUMLContentView: View { @State var keyboardTab: String = "general" @State private var isScaleToFit = true - @State private var showLine:Bool = false + @State private var showLine:Bool = true @State private var saving = false @State private var diagramImage:UIImage? @@ -148,12 +148,12 @@ extension PlantUMLContentView { var EditorView_Fragment: some View { - PlantUMLLineEditorView( content: $document.text, - mode: .plantuml, + PlantUMLEditorView( content: $document.text, darkTheme: CodeWebView.Theme(rawValue: darkTheme)!, lightTheme: CodeWebView.Theme(rawValue: lightTheme)!, isReadOnly: false, - fontSize: CGFloat(fontSize) + fontSize: CGFloat(fontSize), + showGutter: showLine ) // PlantUMLLineEditorView( text: $document.text, diff --git a/PlantUMLEditor/Sources/CodeViewer/CodeViewer.swift b/PlantUMLEditor/Sources/CodeViewer/CodeViewer.swift index 7a29f77..ed90ba5 100644 --- a/PlantUMLEditor/Sources/CodeViewer/CodeViewer.swift +++ b/PlantUMLEditor/Sources/CodeViewer/CodeViewer.swift @@ -35,69 +35,76 @@ public struct CodeViewer: ViewRepresentable { @Environment(\.colorScheme) var colorScheme var textDidChanged: ((String) -> Void)? - private let mode: CodeWebView.Mode private let darkTheme: CodeWebView.Theme private let lightTheme: CodeWebView.Theme private let isReadOnly: Bool private let fontSize: CGFloat + private let showGutter: Bool public init( content: Binding, - mode: CodeWebView.Mode = .plain_text, darkTheme: CodeWebView.Theme = .solarized_dark, lightTheme: CodeWebView.Theme = .solarized_light, isReadOnly: Bool = false, fontSize: CGFloat = 12, + showGutter: Bool = true, textDidChanged: ((String) -> Void)? = nil ) { self._content = content - self.mode = mode self.darkTheme = darkTheme self.lightTheme = lightTheme self.isReadOnly = isReadOnly self.fontSize = fontSize self.textDidChanged = textDidChanged + self.showGutter = showGutter } public func makeCoordinator() -> Coordinator { - Coordinator(content: $content, colorScheme: colorScheme, fontSize: fontSize ) + Coordinator(content: $content, + colorScheme: colorScheme, + fontSize: fontSize, + showGutter: showGutter ) } - private func getWebView(context: Context) -> CodeWebView { + private func makeWebView(context: Context) -> CodeWebView { let codeView = CodeWebView() + codeView.setMode(.plantuml) codeView.setReadOnly(isReadOnly) - codeView.setMode(mode) codeView.setFontSize(fontSize) codeView.setContent(content) codeView.clearSelection() + codeView.setShowGutter(showGutter) codeView.textDidChanged = { text in context.coordinator.set(content: text) self.textDidChanged?(text) } codeView.setTheme( colorScheme == .dark ? darkTheme : lightTheme ) -// codeView.setFocus() - + return codeView } private func updateView(_ webview: CodeWebView, context: Context) { + if context.coordinator.colorScheme != colorScheme { colorScheme == .dark ? webview.setTheme(darkTheme) : webview.setTheme(lightTheme) - context.coordinator.set(colorScheme: colorScheme) + context.coordinator.colorScheme = colorScheme } - print( "fontSize: \(fontSize)" ) - if context.coordinator.fontSize != fontSize { context.coordinator.fontSize = fontSize webview.setFontSize(fontSize) } + + if context.coordinator.showGutter != showGutter { + context.coordinator.showGutter = showGutter + webview.setShowGutter(showGutter) + } } // MARK: macOS public func makeNSView(context: Context) -> CodeWebView { - getWebView(context: context) + makeWebView(context: context) } public func updateNSView(_ webview: CodeWebView, context: Context) { @@ -106,7 +113,7 @@ public struct CodeViewer: ViewRepresentable { // MARK: iOS public func makeUIView(context: Context) -> CodeWebView { - getWebView(context: context) + makeWebView(context: context) } public func updateUIView(_ webview: CodeWebView, context: Context) { @@ -118,14 +125,15 @@ public extension CodeViewer { class Coordinator: NSObject { @Binding private(set) var content: String - - private(set) var colorScheme: ColorScheme + var colorScheme: ColorScheme var fontSize: CGFloat + var showGutter: Bool - init(content: Binding, colorScheme: ColorScheme, fontSize: CGFloat ) { + init(content: Binding, colorScheme: ColorScheme, fontSize: CGFloat, showGutter: Bool ) { _content = content self.colorScheme = colorScheme self.fontSize = fontSize + self.showGutter = showGutter } func set(content: String) { @@ -134,12 +142,6 @@ public extension CodeViewer { } } - func set(colorScheme: ColorScheme) { - if self.colorScheme != colorScheme { - self.colorScheme = colorScheme - } - } - } } diff --git a/PlantUMLEditor/Sources/CodeViewer/CodeWebView.swift b/PlantUMLEditor/Sources/CodeViewer/CodeWebView.swift index a7f870f..4772f2b 100644 --- a/PlantUMLEditor/Sources/CodeViewer/CodeWebView.swift +++ b/PlantUMLEditor/Sources/CodeViewer/CodeWebView.swift @@ -130,6 +130,15 @@ public class CodeWebView: CustomView { let script = "document.getElementById('editor').style.fontSize='\(fontSize)px';" callJavascript(javascriptString: script) } + + func setShowGutter(_ show: Bool) { + callJavascript(javascriptString: "editor.renderer.setShowGutter(\(show));") + } + + func setShowLineNumbers(_ show: Bool) { + let jscode = "editor.setOptions({ showLineNumbers: \(show) })"; + callJavascript(javascriptString: jscode ) + } func clearSelection() { let script = "editor.clearSelection();" @@ -142,6 +151,7 @@ public class CodeWebView: CustomView { callback(result) } } + } extension CodeWebView {