@@ -9,183 +9,183 @@ import RichEditorSwiftUI
9
9
import SwiftUI
10
10
11
11
struct ContentView : View {
12
- @Environment ( \. colorScheme) var colorScheme
13
-
14
- @ObservedObject var state : RichEditorState
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( )
20
-
21
- init ( state: RichEditorState ? = nil ) {
22
- if let state {
23
- self . state = state
24
- } else {
25
- if let richText = readJSONFromFile (
26
- fileName: " Sample_json " ,
27
- type: RichText . self)
28
- {
29
- self . state = . init( richText: richText)
30
- } else {
31
- self . state = . init( input: " Hello World! " )
32
- }
33
- }
12
+ @Environment ( \. colorScheme) var colorScheme
13
+
14
+ @ObservedObject var state : RichEditorState
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( )
20
+
21
+ init ( state: RichEditorState ? = nil ) {
22
+ if let state {
23
+ self . state = state
24
+ } else {
25
+ if let richText = readJSONFromFile (
26
+ fileName: " Sample_json " ,
27
+ type: RichText . self)
28
+ {
29
+ self . state = . init( richText: richText)
30
+ } else {
31
+ self . state = . init( input: " Hello World! " )
32
+ }
34
33
}
34
+ }
35
35
36
- var body : some View {
37
- NavigationStack {
38
- VStack {
39
- #if os(macOS)
40
- RichTextFormat . Toolbar ( context: state)
41
- #endif
42
-
43
- RichTextEditor (
44
- context: _state,
45
- viewConfiguration: { _ in
46
-
47
- }
48
- )
49
- . background (
50
- colorScheme == . dark ? . gray. opacity ( 0.3 ) : Color . white
51
- )
52
- . cornerRadius ( 10 )
53
-
54
- #if os(iOS)
55
- RichTextKeyboardToolbar (
56
- context: state,
57
- leadingButtons: { $0 } ,
58
- trailingButtons: { $0 } ,
59
- formatSheet: { $0 }
60
- )
61
- #endif
62
- }
63
- #if os(iOS) || os(macOS)
64
- . inspector( isPresented: $isInspectorPresented) {
65
- RichTextFormat . Sidebar ( context: state)
66
- #if os(macOS)
67
- . inspectorColumnWidth(
68
- min: 200 , ideal: 200 , max: 315 )
69
- #endif
70
- }
71
- #endif
72
- . padding( 10 )
73
- #if os(iOS) || os(macOS)
74
- . toolbar {
75
- ToolbarItemGroup ( placement: . automatic) {
76
- toolBarGroup
77
- }
78
- }
79
- #endif
80
- . background( colorScheme == . dark ? . black : . gray. opacity ( 0.07 ) )
81
- . navigationTitle ( " Rich Editor " )
82
- . alert ( " Enter file name " , isPresented: getBindingAlert ( ) ) {
83
- TextField ( " Enter file name " , text: $fileName)
84
- Button ( " OK " , action: submit)
85
- } message: {
86
- Text ( " Please enter file name " )
87
- }
88
- . focusedValue ( \. richEditorState, state)
89
- . toolbarRole ( . automatic)
90
- #if os(iOS) || os(macOS) || os(visionOS)
91
- . richTextFormatSheetConfig( . init( colorPickers: colorPickers) )
92
- . richTextFormatSidebarConfig (
93
- . init(
94
- colorPickers: colorPickers,
95
- fontPicker: isMac
96
- )
97
- )
98
- . richTextFormatToolbarConfig ( . init( colorPickers: [ ] ) )
36
+ var body : some View {
37
+ NavigationStack {
38
+ VStack {
39
+ #if macOS
40
+ RichTextFormat . Toolbar ( context: state)
41
+ #endif
42
+
43
+ RichTextEditor (
44
+ context: _state,
45
+ viewConfiguration: { _ in
46
+
47
+ }
48
+ )
49
+ . background (
50
+ colorScheme == . dark ? . gray. opacity ( 0.3 ) : Color . white
51
+ )
52
+ . cornerRadius ( 10 )
53
+
54
+ #if os(iOS)
55
+ RichTextKeyboardToolbar (
56
+ context: state,
57
+ leadingButtons: { $0 } ,
58
+ trailingButtons: { $0 } ,
59
+ formatSheet: { $0 }
60
+ )
61
+ #endif
62
+ }
63
+ #if os(iOS) || macOS
64
+ . inspector( isPresented: $isInspectorPresented) {
65
+ RichTextFormat . Sidebar ( context: state)
66
+ #if macOS
67
+ . inspectorColumnWidth(
68
+ min: 200 , ideal: 200 , max: 315 )
99
69
#endif
100
70
}
101
- }
102
-
103
- #if os(iOS) || os(macOS)
104
- var toolBarGroup : some View {
105
- return Group {
106
- RichTextExportMenu . init (
107
- formatAction: { format in
108
- exportFormat = format
109
- } ,
110
- otherOptionAction: { format in
111
- otherExportFormat = format
112
- }
113
- )
114
- #if !os(macOS)
115
- . frame( width: 25 , alignment: . center)
116
- #endif
117
- Button (
118
- action: {
119
- print ( " Exported JSON == \( state. outputAsString ( ) ) " )
120
- } ,
121
- label: {
122
- Image ( systemName: " printer.inverse " )
123
- }
124
- )
125
- #if !os(macOS)
126
- . frame( width: 25 , alignment: . center)
127
- #endif
128
- Toggle ( isOn: $isInspectorPresented) {
129
- Image . richTextFormatBrush
130
- . resizable ( )
131
- . aspectRatio ( 1 , contentMode: . fit)
132
- }
133
- #if !os(macOS)
134
- . frame( width: 25 , alignment: . center)
135
- #endif
136
- }
71
+ #endif
72
+ . padding( 10 )
73
+ #if os(iOS) || macOS
74
+ . toolbar {
75
+ ToolbarItemGroup ( placement: . automatic) {
76
+ toolBarGroup
77
+ }
137
78
}
138
- #endif
139
-
140
- func getBindingAlert( ) -> Binding < Bool > {
141
- . init(
142
- get: { exportFormat != nil || otherExportFormat != nil } ,
143
- set: { newValue in
144
- exportFormat = nil
145
- otherExportFormat = nil
146
- } )
79
+ #endif
80
+ . background( colorScheme == . dark ? . black : . gray. opacity ( 0.07 ) )
81
+ . navigationTitle ( " Rich Editor " )
82
+ . alert ( " Enter file name " , isPresented: getBindingAlert ( ) ) {
83
+ TextField ( " Enter file name " , text: $fileName)
84
+ Button ( " OK " , action: submit)
85
+ } message: {
86
+ Text ( " Please enter file name " )
87
+ }
88
+ . focusedValue ( \. richEditorState, state)
89
+ . toolbarRole ( . automatic)
90
+ #if os(iOS) || macOS || os(visionOS)
91
+ . richTextFormatSheetConfig( . init( colorPickers: colorPickers) )
92
+ . richTextFormatSidebarConfig (
93
+ . init(
94
+ colorPickers: colorPickers,
95
+ fontPicker: isMac
96
+ )
97
+ )
98
+ . richTextFormatToolbarConfig ( . init( colorPickers: [ ] ) )
99
+ #endif
147
100
}
148
-
149
- func submit( ) {
150
- guard !fileName. isEmpty else { return }
151
- var path : URL ?
152
-
153
- if let exportFormat {
154
- path = try ? exportService. generateExportFile (
155
- withName: fileName, content: state. attributedString,
156
- format: exportFormat)
157
- }
158
- if let otherExportFormat {
159
- switch otherExportFormat {
160
- case . pdf:
161
- path = try ? exportService. generatePdfExportFile (
162
- withName: fileName, content: state. attributedString)
163
- case . json:
164
- path = try ? exportService. generateJsonExportFile (
165
- withName: fileName, content: state. richText)
166
- }
167
- }
168
- if let path {
169
- print ( " Exported at path == \( path) " )
101
+ }
102
+
103
+ #if os(iOS) || macOS
104
+ var toolBarGroup : some View {
105
+ return Group {
106
+ RichTextExportMenu . init (
107
+ formatAction: { format in
108
+ exportFormat = format
109
+ } ,
110
+ otherOptionAction: { format in
111
+ otherExportFormat = format
112
+ }
113
+ )
114
+ #if !macOS
115
+ . frame( width: 25 , alignment: . center)
116
+ #endif
117
+ Button (
118
+ action: {
119
+ print ( " Exported JSON == \( state. outputAsString ( ) ) " )
120
+ } ,
121
+ label: {
122
+ Image ( systemName: " printer.inverse " )
123
+ }
124
+ )
125
+ #if !macOS
126
+ . frame( width: 25 , alignment: . center)
127
+ #endif
128
+ Toggle ( isOn: $isInspectorPresented) {
129
+ Image . richTextFormatBrush
130
+ . resizable ( )
131
+ . aspectRatio ( 1 , contentMode: . fit)
170
132
}
133
+ #if !macOS
134
+ . frame( width: 25 , alignment: . center)
135
+ #endif
136
+ }
137
+ }
138
+ #endif
139
+
140
+ func getBindingAlert( ) -> Binding < Bool > {
141
+ . init(
142
+ get: { exportFormat != nil || otherExportFormat != nil } ,
143
+ set: { newValue in
144
+ exportFormat = nil
145
+ otherExportFormat = nil
146
+ } )
147
+ }
148
+
149
+ func submit( ) {
150
+ guard !fileName. isEmpty else { return }
151
+ var path : URL ?
152
+
153
+ if let exportFormat {
154
+ path = try ? exportService. generateExportFile (
155
+ withName: fileName, content: state. attributedString,
156
+ format: exportFormat)
157
+ }
158
+ if let otherExportFormat {
159
+ switch otherExportFormat {
160
+ case . pdf:
161
+ path = try ? exportService. generatePdfExportFile (
162
+ withName: fileName, content: state. attributedString)
163
+ case . json:
164
+ path = try ? exportService. generateJsonExportFile (
165
+ withName: fileName, content: state. richText)
166
+ }
171
167
}
168
+ if let path {
169
+ print ( " Exported at path == \( path) " )
170
+ }
171
+ }
172
172
}
173
173
174
174
extension ContentView {
175
175
176
- var isMac : Bool {
177
- #if os( macOS)
178
- true
179
- #else
180
- false
181
- #endif
182
- }
176
+ var isMac : Bool {
177
+ #if macOS
178
+ true
179
+ #else
180
+ false
181
+ #endif
182
+ }
183
183
184
- var colorPickers : [ RichTextColor ] {
185
- [ . foreground, . background]
186
- }
184
+ var colorPickers : [ RichTextColor ] {
185
+ [ . foreground, . background]
186
+ }
187
187
188
- var formatToolbarEdge : VerticalEdge {
189
- isMac ? . top : . bottom
190
- }
188
+ var formatToolbarEdge : VerticalEdge {
189
+ isMac ? . top : . bottom
190
+ }
191
191
}
0 commit comments