@@ -13,6 +13,10 @@ struct ContentView: View {
13
13
14
14
@ObservedObject var state : RichEditorState
15
15
@State private var isInspectorPresented = false
16
+ @State private var fileName : String = " "
17
+ @State private var exportFormat : RichTextDataFormat ? = nil
18
+ @State private var otherExportFormat : RichTextExportOption ? = nil
19
+ @State private var exportService : StandardRichTextExportService = . init( )
16
20
17
21
init ( state: RichEditorState ? = nil ) {
18
22
if let state {
@@ -61,22 +65,109 @@ struct ContentView: View {
61
65
}
62
66
. padding ( 10 )
63
67
. toolbar {
64
- ToolbarItem ( placement: . automatic) {
65
- Button (
66
- action: {
67
- print ( " Exported JSON == \( state. output ( ) ) " )
68
- } ,
69
- label: {
70
- Image ( systemName: " printer.inverse " )
71
- . padding ( )
72
- } )
68
+ ToolbarItemGroup ( placement: . automatic) {
69
+ toolBarGroup
73
70
}
74
71
}
75
72
. background ( colorScheme == . dark ? . black : . gray. opacity ( 0.07 ) )
76
73
. navigationTitle ( " Rich Editor " )
77
- #if os(iOS)
78
- . navigationBarTitleDisplayMode( . inline)
74
+ . alert ( " Enter file name " , isPresented: getBindingAlert ( ) ) {
75
+ TextField ( " Enter file name " , text: $fileName)
76
+ Button ( " OK " , action: submit)
77
+ } message: {
78
+ Text ( " Please enter file name " )
79
+ }
80
+ . focusedValue ( \. richEditorState, state)
81
+ . toolbarRole ( . automatic)
82
+ . richTextFormatSheetConfig ( . init( colorPickers: colorPickers) )
83
+ . richTextFormatSidebarConfig (
84
+ . init(
85
+ colorPickers: colorPickers,
86
+ fontPicker: isMac
87
+ )
88
+ )
89
+ . richTextFormatToolbarConfig ( . init( colorPickers: [ ] ) )
90
+ }
91
+ }
92
+
93
+ var toolBarGroup : some View {
94
+ return Group {
95
+ RichTextExportMenu . init (
96
+ formatAction: { format in
97
+ exportFormat = format
98
+ } ,
99
+ otherOptionAction: { format in
100
+ otherExportFormat = format
101
+ }
102
+ )
103
+ #if !os(macOS)
104
+ . frame( width: 25 , alignment: . center)
79
105
#endif
106
+ Button (
107
+ action: {
108
+ print ( " Exported JSON == \( state. outputAsString ( ) ) " )
109
+ } ,
110
+ label: {
111
+ Image ( systemName: " printer.inverse " )
112
+ }
113
+ )
114
+ #if !os(macOS)
115
+ . frame( width: 25 , alignment: . center)
116
+ #endif
117
+ Toggle ( isOn: $isInspectorPresented) {
118
+ Image . richTextFormatBrush
119
+ . resizable ( )
120
+ . aspectRatio ( 1 , contentMode: . fit)
121
+ }
122
+ #if !os(macOS)
123
+ . frame( width: 25 , alignment: . center)
124
+ #endif
125
+ }
126
+ }
127
+
128
+ func getBindingAlert( ) -> Binding < Bool > {
129
+ . init( get: { exportFormat != nil || otherExportFormat != nil } , set: { newValue in
130
+ exportFormat = nil
131
+ otherExportFormat = nil
132
+ } )
133
+ }
134
+
135
+ func submit( ) {
136
+ guard !fileName. isEmpty else { return }
137
+ var path : URL ?
138
+
139
+ if let exportFormat {
140
+ path = try ? exportService. generateExportFile ( withName: fileName, content: state. attributedString, format: exportFormat)
141
+ }
142
+ if let otherExportFormat {
143
+ switch otherExportFormat {
144
+ case . pdf:
145
+ path = try ? exportService. generatePdfExportFile ( withName: fileName, content: state. attributedString)
146
+ case . json:
147
+ path = try ? exportService. generateJsonExportFile ( withName: fileName, content: state. richText)
148
+ }
80
149
}
150
+ if let path {
151
+ print ( " Exported at path == \( path) " )
152
+ }
153
+ }
154
+ }
155
+
156
+ private extension ContentView {
157
+
158
+ var isMac : Bool {
159
+ #if os(macOS)
160
+ true
161
+ #else
162
+ false
163
+ #endif
164
+ }
165
+
166
+ var colorPickers : [ RichTextColor ] {
167
+ [ . foreground, . background]
168
+ }
169
+
170
+ var formatToolbarEdge : VerticalEdge {
171
+ isMac ? . top : . bottom
81
172
}
82
173
}
0 commit comments