Skip to content

Commit

Permalink
feat: add gutter toggle support
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Aug 28, 2023
1 parent 2d80c5a commit de21dcc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
10 changes: 5 additions & 5 deletions PlantUML/PlantUMLContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import AppSecureStorage


struct PlantUMLContentView: View {
typealias PlantUMLLineEditorView = CodeViewer
typealias PlantUMLEditorView = CodeViewer

@Environment(\.scenePhase) var scene
@Environment(\.interfaceOrientation) var interfaceOrientation: InterfaceOrientationHolder
Expand All @@ -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?

Expand Down Expand Up @@ -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,
Expand Down
46 changes: 24 additions & 22 deletions PlantUMLEditor/Sources/CodeViewer/CodeViewer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
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) {
Expand All @@ -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) {
Expand All @@ -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<String>, colorScheme: ColorScheme, fontSize: CGFloat ) {
init(content: Binding<String>, colorScheme: ColorScheme, fontSize: CGFloat, showGutter: Bool ) {
_content = content
self.colorScheme = colorScheme
self.fontSize = fontSize
self.showGutter = showGutter
}

func set(content: String) {
Expand All @@ -134,12 +142,6 @@ public extension CodeViewer {
}
}

func set(colorScheme: ColorScheme) {
if self.colorScheme != colorScheme {
self.colorScheme = colorScheme
}
}

}
}

Expand Down
10 changes: 10 additions & 0 deletions PlantUMLEditor/Sources/CodeViewer/CodeWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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();"
Expand All @@ -142,6 +151,7 @@ public class CodeWebView: CustomView {
callback(result)
}
}

}

extension CodeWebView {
Expand Down

0 comments on commit de21dcc

Please sign in to comment.