-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
187 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import type { NativeStackScreenProps } from '@react-navigation/native-stack'; | ||
import React, { useRef } from 'react'; | ||
import { | ||
SafeAreaView, | ||
View, | ||
KeyboardAvoidingView, | ||
Platform, | ||
StyleSheet, | ||
} from 'react-native'; | ||
import { | ||
RichText, | ||
Toolbar, | ||
useEditorBridge, | ||
ColorKeyboard, | ||
CustomKeyboard, | ||
DEFAULT_TOOLBAR_ITEMS, | ||
useKeyboard, | ||
type EditorBridge, | ||
useBridgeState, | ||
TenTapStartKit, | ||
CoreBridge, | ||
darkEditorTheme, | ||
} from '@10play/tentap-editor'; | ||
import { Images } from '../../../src/assets'; | ||
|
||
const BACKGROUND_COLOR = '#1C1C1E'; | ||
const DEFAULT_TEXT_COLOR = 'white'; | ||
const darkEditorCss = ` | ||
* { | ||
background-color: ${BACKGROUND_COLOR}; | ||
color: ${DEFAULT_TEXT_COLOR}; | ||
} | ||
`; | ||
|
||
export const DarkEditor = ({}: NativeStackScreenProps<any, any, any>) => { | ||
const editor = useEditorBridge({ | ||
autofocus: true, | ||
avoidIosKeyboard: true, | ||
initialContent, | ||
bridgeExtensions: [ | ||
...TenTapStartKit, | ||
CoreBridge.configureCSS(darkEditorCss), | ||
], | ||
theme: darkEditorTheme, | ||
}); | ||
|
||
const rootRef = useRef(null); | ||
const [activeKeyboard, setActiveKeyboard] = React.useState<string>(); | ||
|
||
return ( | ||
<SafeAreaView | ||
style={{ ...exampleStyles.fullScreen, backgroundColor: BACKGROUND_COLOR }} | ||
ref={rootRef} | ||
> | ||
<View | ||
style={{ | ||
...exampleStyles.fullScreen, | ||
paddingHorizontal: 12, | ||
backgroundColor: BACKGROUND_COLOR, | ||
}} | ||
> | ||
<RichText | ||
editor={editor} | ||
style={{ backgroundColor: BACKGROUND_COLOR }} | ||
/> | ||
</View> | ||
<KeyboardAvoidingView | ||
behavior={Platform.OS === 'ios' ? 'padding' : 'height'} | ||
style={exampleStyles.keyboardAvoidingView} | ||
> | ||
<ToolbarWithColor | ||
editor={editor} | ||
activeKeyboard={activeKeyboard} | ||
setActiveKeyboard={setActiveKeyboard} | ||
/> | ||
<CustomKeyboard | ||
rootRef={rootRef} | ||
activeKeyboardID={activeKeyboard} | ||
setActiveKeyboardID={setActiveKeyboard} | ||
keyboards={[ColorKeyboard]} | ||
editor={editor} | ||
/> | ||
</KeyboardAvoidingView> | ||
</SafeAreaView> | ||
); | ||
}; | ||
|
||
interface ToolbarWithColorProps { | ||
editor: EditorBridge; | ||
activeKeyboard: string | undefined; | ||
setActiveKeyboard: (id: string | undefined) => void; | ||
} | ||
const ToolbarWithColor = ({ | ||
editor, | ||
activeKeyboard, | ||
setActiveKeyboard, | ||
}: ToolbarWithColorProps) => { | ||
// Get updates of editor state | ||
const editorState = useBridgeState(editor); | ||
|
||
const { isKeyboardUp: isNativeKeyboardUp } = useKeyboard(); | ||
const customKeyboardOpen = activeKeyboard !== undefined; | ||
const isKeyboardUp = isNativeKeyboardUp || customKeyboardOpen; | ||
|
||
// Here we make sure not to hide the keyboard if our custom keyboard is visible | ||
const hideToolbar = | ||
!isKeyboardUp || (!editorState.isFocused && !customKeyboardOpen); | ||
|
||
return ( | ||
<Toolbar | ||
editor={editor} | ||
hidden={hideToolbar} | ||
items={[ | ||
{ | ||
onPress: () => () => { | ||
const isActive = activeKeyboard === ColorKeyboard.id; | ||
if (isActive) editor.focus(); | ||
setActiveKeyboard(isActive ? undefined : ColorKeyboard.id); | ||
}, | ||
active: () => activeKeyboard === ColorKeyboard.id, | ||
disabled: () => false, | ||
image: () => Images.palette, | ||
}, | ||
...DEFAULT_TOOLBAR_ITEMS, | ||
]} | ||
/> | ||
); | ||
}; | ||
|
||
const exampleStyles = StyleSheet.create({ | ||
fullScreen: { | ||
flex: 1, | ||
}, | ||
keyboardAvoidingView: { | ||
position: 'absolute', | ||
width: '100%', | ||
bottom: 0, | ||
}, | ||
}); | ||
|
||
const initialContent = `<p>DARK EDITOR</p>`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters