Skip to content

Commit 97b65fb

Browse files
committed
feat: Demo editor toggles code editor mode
1 parent 10a75f9 commit 97b65fb

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

android/app/src/main/java/com/example/gutenbergkit/EditorActivity.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ fun EditorScreen(
7474
var isModalDialogOpen by remember { mutableStateOf(false) }
7575
var hasUndoState by remember { mutableStateOf(false) }
7676
var hasRedoState by remember { mutableStateOf(false) }
77+
var isCodeEditorEnabled by remember { mutableStateOf(false) }
7778
var gutenbergViewRef by remember { mutableStateOf<GutenbergView?>(null) }
7879

7980
BackHandler(enabled = isModalDialogOpen) {
@@ -145,9 +146,12 @@ fun EditorScreen(
145146
enabled = false
146147
)
147148
DropdownMenuItem(
148-
text = { Text("Code editor") },
149-
onClick = { },
150-
enabled = false
149+
text = { Text(if (isCodeEditorEnabled) "Visual editor" else "Code editor") },
150+
onClick = {
151+
isCodeEditorEnabled = !isCodeEditorEnabled
152+
gutenbergViewRef?.textEditorEnabled = isCodeEditorEnabled
153+
showMenu = false
154+
}
151155
)
152156
DropdownMenuItem(
153157
text = { Text("Post settings") },

ios/Demo-iOS/Sources/EditorView.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ struct EditorView: View {
66
@State private var isModalDialogOpen = false
77
@State private var hasUndo = false
88
@State private var hasRedo = false
9+
@State private var isCodeEditorEnabled = false
910
@State private var editorViewController: EditorViewController?
1011
@Environment(\.dismiss) private var dismiss
1112

@@ -19,6 +20,7 @@ struct EditorView: View {
1920
isModalDialogOpen: $isModalDialogOpen,
2021
hasUndo: $hasUndo,
2122
hasRedo: $hasRedo,
23+
isCodeEditorEnabled: $isCodeEditorEnabled,
2224
editorViewController: $editorViewController
2325
)
2426
.navigationBarBackButtonHidden(true)
@@ -58,9 +60,14 @@ struct EditorView: View {
5860
private var moreMenu: some View {
5961
Menu {
6062
Section {
61-
Button(action: {}, label: {
62-
Label("Code Editor", systemImage: "curlybraces")
63-
}).disabled(true)
63+
Button(action: {
64+
isCodeEditorEnabled.toggle()
65+
}, label: {
66+
Label(
67+
isCodeEditorEnabled ? "Visual Editor" : "Code Editor",
68+
systemImage: isCodeEditorEnabled ? "doc.richtext" : "curlybraces"
69+
)
70+
})
6471
Button(action: /*@START_MENU_TOKEN@*/{}/*@END_MENU_TOKEN@*/, label: {
6572
Label("Preview", systemImage: "safari")
6673
}).disabled(true)
@@ -90,19 +97,22 @@ private struct _EditorView: UIViewControllerRepresentable {
9097
@Binding var isModalDialogOpen: Bool
9198
@Binding var hasUndo: Bool
9299
@Binding var hasRedo: Bool
100+
@Binding var isCodeEditorEnabled: Bool
93101
@Binding var editorViewController: EditorViewController?
94102

95103
init(
96104
configuration: EditorConfiguration,
97105
isModalDialogOpen: Binding<Bool>,
98106
hasUndo: Binding<Bool>,
99107
hasRedo: Binding<Bool>,
108+
isCodeEditorEnabled: Binding<Bool>,
100109
editorViewController: Binding<EditorViewController?>
101110
) {
102111
self.configuration = configuration
103112
self._isModalDialogOpen = isModalDialogOpen
104113
self._hasUndo = hasUndo
105114
self._hasRedo = hasRedo
115+
self._isCodeEditorEnabled = isCodeEditorEnabled
106116
self._editorViewController = editorViewController
107117
}
108118

@@ -132,7 +142,7 @@ private struct _EditorView: UIViewControllerRepresentable {
132142
}
133143

134144
func updateUIViewController(_ uiViewController: EditorViewController, context: Context) {
135-
// Do nothing
145+
uiViewController.isCodeEditorEnabled = isCodeEditorEnabled
136146
}
137147

138148
@MainActor

0 commit comments

Comments
 (0)