Skip to content

Commit

Permalink
feat: integrate new line_editor version
Browse files Browse the repository at this point in the history
integrate line_editor with text as main input/output argument + refine state management across Editor and OpenAI
  • Loading branch information
bsorrentino committed Apr 2, 2023
1 parent 296050f commit 3667ef6
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 83 deletions.
1 change: 1 addition & 0 deletions PROMPT.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# OpenAI Prompt

* `make a new sequence that represent an OAuth2 classic flow`
* `make a new sequence that represent an OAuth2 classic flow with refresh token`

14 changes: 7 additions & 7 deletions PlantUML/PlantUML+OpenAI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ extension OpenAIView {
var Input_Fragment: some View {

ZStack(alignment: .bottomTrailing ) {

TextEditor(text: $instruction)
.font(.title3.monospaced() )
.lineSpacing(15)
Expand All @@ -189,12 +190,12 @@ extension OpenAIView {
HStack(spacing: 10) {
Spacer()

Button( action: onUndo,
label: {
Label( "Undo", systemImage: "arrow.uturn.backward")
.labelStyle(.titleAndIcon)
})
.disabled( isEditing || service.clipboard.isEmpty )
// Button( action: onUndo,
// label: {
// Label( "Undo", systemImage: "arrow.uturn.backward")
// .labelStyle(.titleAndIcon)
// })
// .disabled( isEditing || service.clipboard.isEmpty )

Button( action: {

Expand All @@ -203,7 +204,6 @@ extension OpenAIView {
service.status = .Ready
service.clipboard.push( result.isEmpty ? input : result )
service.prompt.push( instruction )

result = res
}
}
Expand Down
60 changes: 34 additions & 26 deletions PlantUML/PlantUMLContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import LineEditor


struct PlantUMLContentView: View {
typealias PlantUMLLineEditorView = StandardLineEditorView<SyntaxStructure,Symbol>
typealias PlantUMLLineEditorView = StandardLineEditorView<Symbol>

class ViewState : ObservableObject, Identifiable {

Expand Down Expand Up @@ -50,28 +50,8 @@ struct PlantUMLContentView: View {
@State private var fontSize = CGFloat(12)
@State private var showLine:Bool = false
@State private var saving = false
@State private var openAIResult:String = ""
@State private var diagramImage:UIImage?

var OpenAIView_Fragment: some View {

OpenAIView( service: openAIService, result: $openAIResult, input: document.text,
onUndo: {
if let text = openAIService.clipboard.pop() {
document.setText( text )
saving = true
document.updateRequest.send()
}
}
)
.onChange(of: openAIResult ) { result in
document.setText( result )
saving = true
document.updateRequest.send()
}

}

var body: some View {

VStack {
Expand All @@ -91,6 +71,16 @@ struct PlantUMLContentView: View {
}
}
.id( viewState.id )
.onChange(of: document.openAIResult ) { result in
document.setText( result )
document.editorText = PlantUMLDocumentProxy.buildDocumentText(from: result)
// saving = true
// document.updateRequest.send()
}
.onChange(of: document.editorText ) { _ in
saving = true
document.updateRequest.send()
}
.onReceive(document.updateRequest.publisher) { _ in
withAnimation(.easeInOut(duration: 1.0)) {
document.save()
Expand Down Expand Up @@ -142,24 +132,42 @@ struct PlantUMLContentView: View {
}
}

//
// MARK: - OpenAI extension -
//
extension PlantUMLContentView {

var OpenAIView_Fragment: some View {

OpenAIView( service: openAIService, result: $document.openAIResult, input: document.text,
onUndo: {
if let text = openAIService.clipboard.pop() {
document.setText( text )
saving = true
document.updateRequest.send()
}
}
)

}


}

//
// MARK: - Editor extension -
//
extension PlantUMLContentView {

var EditorView_Fragment: some View {

PlantUMLLineEditorView( items: $document.items,
PlantUMLLineEditorView( text: $document.editorText,
fontSize: $fontSize,
showLine: $showLine) { onHide, onPressSymbol in
PlantUMLKeyboardView( selectedTab: $keyboardTab,
onHide: onHide,
onPressSymbol: onPressSymbol)
}
.onChange(of: document.items ) { _ in
saving = true
document.updateRequest.send()
}
}


Expand Down
30 changes: 23 additions & 7 deletions PlantUML/PlantUMLDocumentProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,41 @@ class DebounceRequest {
class PlantUMLDocumentProxy : ObservableObject, CustomStringConvertible {

@Binding var object: PlantUMLDocument
@Published var items:Array<SyntaxStructure>
// private var items:Array<SyntaxStructure>
@Published var editorText: String
@Published var openAIResult:String = ""

private(set) var text:String

let updateRequest = DebounceRequest( debounceInSeconds: 0.5)

var description: String {
self.items.map { $0.rawValue }.joined( separator: "\n" )
self.editorText
// self.items.map { $0.rawValue }.joined( separator: "\n" )
}

let presenter = PlantUMLBrowserPresenter( format: .imagePng )

private var textCancellable:AnyCancellable?

init( document: Binding<PlantUMLDocument> ) {
let contents = "@startuml\n\(document.wrappedValue.text)\n@enduml"
self._object = document
self.text = contents
self.items = Self.buildSyntaxStructureItems( from: contents)
self.text = "@startuml\n\(document.wrappedValue.text)\n@enduml"
self.editorText = document.wrappedValue.text
// self.items = Self.buildSyntaxStructureItems( from: contents)


}

func setText( _ text: String ) {

self.text = text
self.items = Self.buildSyntaxStructureItems( from: text )

// self.items = Self.buildSyntaxStructureItems( from: text )
}

func buildURL() -> URL {
let script = PlantUMLScript( items: items )
let script = PlantUMLScript( items: Self.buildSyntaxStructureItems( from: self.text ) )

return presenter.url( of: script )
}
Expand All @@ -80,6 +87,15 @@ class PlantUMLDocumentProxy : ObservableObject, CustomStringConvertible {

extension PlantUMLDocumentProxy {

public static func buildDocumentText( from text: String ) -> String {
return text
.split(whereSeparator: \.isNewline)
.filter { line in
line != "@startuml" && line != "@enduml"
}
.joined(separator: "\n")
}

private static func buildSyntaxStructureItems( from text: String ) -> Array<SyntaxStructure> {
return text
.split(whereSeparator: \.isNewline)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
}
},
{
"identity" : "plantuml4ipad",
"identity" : "swiftui-lineeditor",
"kind" : "remoteSourceControl",
"location" : "https://github.com/bsorrentino/PlantUML4iPad.git",
"location" : "https://github.com/bsorrentino/SwiftUI-LineEditor.git",
"state" : {
"branch" : "line_editor",
"revision" : "c32ca4eebd381f6030d1b0d3f09bfc7b08bdf0b0"
"branch" : "feature/issue1",
"revision" : "266e3603c2da9adac5f9e0ceaf3caef8b0a7580b"
}
}
],
Expand Down
37 changes: 0 additions & 37 deletions PlantUMLApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@
A0D3C65E28984A10000838D7 /* PlantUMLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0D3C65D28984A10000838D7 /* PlantUMLTests.swift */; };
A0D3C66828984A11000838D7 /* PlantUMLUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0D3C66728984A11000838D7 /* PlantUMLUITests.swift */; };
A0D3C66A28984A11000838D7 /* PlantUMLUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0D3C66928984A11000838D7 /* PlantUMLUITestsLaunchTests.swift */; };
A0DC176429D345BB00CB70E6 /* OpenAIKit in Frameworks */ = {isa = PBXBuildFile; productRef = A0DC176329D345BB00CB70E6 /* OpenAIKit */; };
A0DC176629D345D600CB70E6 /* PlantUMLKeyboard in Frameworks */ = {isa = PBXBuildFile; productRef = A0DC176529D345D600CB70E6 /* PlantUMLKeyboard */; };
A0DC176829D345DB00CB70E6 /* LineEditor in Frameworks */ = {isa = PBXBuildFile; productRef = A0DC176729D345DB00CB70E6 /* LineEditor */; };
A0F2B14229353C2D00A44481 /* SwiftUI+Rotate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0F2B14129353C2D00A44481 /* SwiftUI+Rotate.swift */; };
A0F2B14329353C2D00A44481 /* SwiftUI+Rotate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0F2B14129353C2D00A44481 /* SwiftUI+Rotate.swift */; };
A0FB8F9C295616BF00910896 /* View+UIImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = A08AA78329561170004DE329 /* View+UIImage.swift */; };
Expand Down Expand Up @@ -96,9 +94,7 @@
buildActionMask = 2147483647;
files = (
A01A6FBC29D3464500C3A1F6 /* PlantUMLFramework in Frameworks */,
A0DC176829D345DB00CB70E6 /* LineEditor in Frameworks */,
A0DC176629D345D600CB70E6 /* PlantUMLKeyboard in Frameworks */,
A0DC176429D345BB00CB70E6 /* OpenAIKit in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -220,9 +216,7 @@
);
name = PlantUMLApp;
packageProductDependencies = (
A0DC176329D345BB00CB70E6 /* OpenAIKit */,
A0DC176529D345D600CB70E6 /* PlantUMLKeyboard */,
A0DC176729D345DB00CB70E6 /* LineEditor */,
A01A6FBB29D3464500C3A1F6 /* PlantUMLFramework */,
);
productName = PlantUML;
Expand Down Expand Up @@ -298,8 +292,6 @@
);
mainGroup = A0D3C63D28984A0E000838D7;
packageReferences = (
A0F3291E2902AC5E00E0C47C /* XCRemoteSwiftPackageReference "PlantUML4iPad" */,
A0DC176229D345BB00CB70E6 /* XCRemoteSwiftPackageReference "OpenAIKit" */,
);
productRefGroup = A0D3C64728984A0E000838D7 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -713,44 +705,15 @@
};
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
A0DC176229D345BB00CB70E6 /* XCRemoteSwiftPackageReference "OpenAIKit" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/marcodotio/OpenAIKit.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 1.2.0;
};
};
A0F3291E2902AC5E00E0C47C /* XCRemoteSwiftPackageReference "PlantUML4iPad" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/bsorrentino/PlantUML4iPad.git";
requirement = {
branch = line_editor;
kind = branch;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
A01A6FBB29D3464500C3A1F6 /* PlantUMLFramework */ = {
isa = XCSwiftPackageProductDependency;
productName = PlantUMLFramework;
};
A0DC176329D345BB00CB70E6 /* OpenAIKit */ = {
isa = XCSwiftPackageProductDependency;
package = A0DC176229D345BB00CB70E6 /* XCRemoteSwiftPackageReference "OpenAIKit" */;
productName = OpenAIKit;
};
A0DC176529D345D600CB70E6 /* PlantUMLKeyboard */ = {
isa = XCSwiftPackageProductDependency;
productName = PlantUMLKeyboard;
};
A0DC176729D345DB00CB70E6 /* LineEditor */ = {
isa = XCSwiftPackageProductDependency;
package = A0F3291E2902AC5E00E0C47C /* XCRemoteSwiftPackageReference "PlantUML4iPad" */;
productName = LineEditor;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = A0D3C63E28984A0E000838D7 /* Project object */;
Expand Down
4 changes: 2 additions & 2 deletions PlantUMLKeyboard/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ let package = Package(
// .package(url: "https://github.com/siteline/SwiftUI-Introspect.git", branch: "master" ),
.package(path: "../PlantUMLFramework" ),
.package(url: "https://github.com/marcodotio/OpenAIKit.git", from: "1.2.0"),
.package(url: "https://github.com/bsorrentino/PlantUML4iPad.git", branch: "line_editor"),
.package(url: "https://github.com/bsorrentino/SwiftUI-LineEditor.git", branch: "feature/issue1"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "PlantUMLKeyboard",
dependencies: [
.product(name: "LineEditor", package: "PlantUML4iPad"),
.product(name: "LineEditor", package: "SwiftUI-LineEditor"),
.product(name: "OpenAIKit", package: "OpenAIKit"),
"PlantUMLFramework",
],
Expand Down

0 comments on commit 3667ef6

Please sign in to comment.