Skip to content

Commit

Permalink
feat: implement multiline command - #2
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Sep 13, 2022
1 parent a3cbe78 commit 1c8d48a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 25 deletions.
22 changes: 20 additions & 2 deletions PlantUML/PlantUMLEditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ struct PlantUMLEditorView: View {
GeometryReader { _ in
HStack {
EditorView()
.onReceive( customKeyboard.$itemsToAdd ) { items in
print( "\(items)")
appendBelow(values: items)
}
if !isPreviewVisible {
PlantUMLDiagramView( url: diagram.buildURL() )
}
Expand Down Expand Up @@ -176,13 +180,27 @@ extension PlantUMLEditorView {
diagram.items[ offset ].rawValue = value
}

func addBelow( theItem item: SyntaxStructure? ) {
func appendBelow( theItem item: SyntaxStructure? = nil, values: [String] ) {
let offset = (item != nil) ?
diagram.items.firstIndex { $0.id == item!.id } :
indexFromFocusedItem()

guard let offset = offset else { return }
let newItem = SyntaxStructure( rawValue: "")

values.map { SyntaxStructure( rawValue: $0) }
.enumerated()
.forEach { (index, item ) in
diagram.items.insert( item, at: offset + index + 1)
}
}

func addBelow( theItem item: SyntaxStructure? = nil, value: String = "" ) {
let offset = (item != nil) ?
diagram.items.firstIndex { $0.id == item!.id } :
indexFromFocusedItem()

guard let offset = offset else { return }
let newItem = SyntaxStructure( rawValue: value)

diagram.items.insert( newItem, at: offset + 1)
focusedItem = .row( id: newItem.id )
Expand Down
14 changes: 4 additions & 10 deletions PlantUML/PlantUMLTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct PlantUMLTextField: View {

VStack {

HStack(spacing: 15){
HStack(spacing: 15) {

TextField( "", text: $value )
.textInputAutocapitalization(.never)
Expand All @@ -54,7 +54,6 @@ struct PlantUMLTextField: View {
}

}
// .background(Color("Color")
// .animation(.easeInOut(duration: 2), value: 1.0)
.edgesIgnoringSafeArea(.all)
.onAppear {
Expand All @@ -65,11 +64,6 @@ struct PlantUMLTextField: View {
// queue: .main) { ( object ) in
// print( "UIWindowFirstResponderDidChangeNotification \(object)" )
// }
NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillChangeFrameNotification,
object: nil,
queue: .main) { (_) in
self.showKeyboard = false
}
}

}
Expand All @@ -83,9 +77,9 @@ extension PlantUMLTextField {

print( "show keyboard")

if let rootViewController = getWindows()?.first?.rootViewController {
rootViewController.view.endEditing(true)
}
// if let rootViewController = getWindows()?.first?.rootViewController {
// rootViewController.view.endEditing(true)
// }

self.showKeyboard.toggle()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import Combine
import SwiftUI
import PlantUMLFramework


public class CustomKeyboardObject : ObservableObject {

@Published public var showKeyboard = false


@Published public var itemsToAdd:[String] = []

private var controller:UIHostingController<PlantUMLKeyboardView>?

private var keyboardRect:CGRect = .zero
Expand Down Expand Up @@ -75,7 +78,7 @@ public class CustomKeyboardObject : ObservableObject {

print( "keyboardRect: \(keyboardRect)")

let controller = UIHostingController( rootView: PlantUMLKeyboardView( show: showKeyboardBinding) )
let controller = UIHostingController( rootView: PlantUMLKeyboardView( customKeyboard: self ) )
self.controller = controller
controller.view.frame = CGRect( origin: keyboardRect.origin, size: keyboardRect.size )
keyboardWindow.addSubview( controller.view )
Expand Down
43 changes: 32 additions & 11 deletions PlantUMLKeyboard/Sources/PlantUMLKeyboard/PlantUMLKeyboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ struct Symbol : Identifiable, CustomStringConvertible {

var id:String
private var _value:String?

private var _additionalValues:[String]?

var value: String {
get { _value ?? id }
}

init( _ id:String, _ value:String? = nil) {

var additionalValues: [String]? {
get { _additionalValues }
}

init( _ id:String, _ value:String? = nil, _ additionalValues: [String]? = nil) {
self.id = id
self._value = value
self._additionalValues = additionalValues
}
}

Expand Down Expand Up @@ -53,8 +59,9 @@ fileprivate var plantUMLSymbols:[[Symbol]] = [

[
Symbol("[#red]"),
Symbol("note", "note"),
Symbol("end note"),
Symbol("note left", "note left /' of participant '/", ["this note is displayed left", "end note"]),
Symbol("note right", "note right 'of participant", ["this note is displayed right", "end note"]),
Symbol("note over", "note over participant1 ', participant2", ["this note is displayed over participant1", "end note"]),
]

]
Expand All @@ -72,11 +79,12 @@ fileprivate var plantUMLImages:[[UIImage?]] = {

public struct PlantUMLKeyboardView: View {

@Binding var show : Bool
@ObservedObject var customKeyboard: CustomKeyboardObject

public init( show: Binding<Bool> ) {
self._show = show
}
// public init( show: Binding<Bool>, result:Binding<[String]> ) {
// self._show = show
// self._result = result
// }

public var body : some View{

Expand Down Expand Up @@ -114,7 +122,7 @@ public struct PlantUMLKeyboardView: View {
.cornerRadius(25)

Button(action: {
self.show.toggle()
customKeyboard.showKeyboard.toggle()
}) {
Image(systemName: "xmark").foregroundColor(.black)
}
Expand All @@ -137,6 +145,10 @@ public struct PlantUMLKeyboardView: View {
// From your question I assume that you do not want to replace a selection, only insert some text where the cursor is.
handleToYourTextView.replace(range, withText: symbol.value )
}

if let additionalValues = symbol.additionalValues {
customKeyboard.itemsToAdd = additionalValues
}

}
}
Expand Down Expand Up @@ -174,8 +186,17 @@ extension PlantUMLKeyboardView {
}

struct PlantUMLKeyboardView_Previews: PreviewProvider {

struct UserView : View {
@ObservedObject var customKeyboard = CustomKeyboardObject()

public var body : some View {
PlantUMLKeyboardView( customKeyboard: customKeyboard )
}
}

static var previews: some View {
PlantUMLKeyboardView( show: Binding.constant(true) )
UserView()
.previewInterfaceOrientation(.landscapeLeft)
}
}

0 comments on commit 1c8d48a

Please sign in to comment.