-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from 10play/amir/rnr-39-dark-mode-configurable…
…-styles Amir/rnr 39 dark mode configurable styles
- Loading branch information
Showing
31 changed files
with
887 additions
and
305 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
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 EDITOR_BACKGROUND_COLOR = '#1C1C1E'; | ||
const DEFAULT_TEXT_COLOR = 'white'; | ||
const darkEditorCss = ` | ||
* { | ||
background-color: ${EDITOR_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: EDITOR_BACKGROUND_COLOR, | ||
}} | ||
ref={rootRef} | ||
> | ||
<View | ||
style={{ | ||
...exampleStyles.fullScreen, | ||
paddingHorizontal: 12, | ||
backgroundColor: EDITOR_BACKGROUND_COLOR, | ||
}} | ||
> | ||
<RichText editor={editor} /> | ||
</View> | ||
<KeyboardAvoidingView | ||
behavior={Platform.OS === 'ios' ? 'padding' : 'height'} | ||
style={exampleStyles.keyboardAvoidingView} | ||
> | ||
<ToolbarWithColor | ||
editor={editor} | ||
activeKeyboard={activeKeyboard} | ||
setActiveKeyboard={setActiveKeyboard} | ||
/> | ||
<CustomKeyboard | ||
editor={editor} | ||
rootRef={rootRef} | ||
keyboards={[ColorKeyboard]} | ||
activeKeyboardID={activeKeyboard} | ||
setActiveKeyboardID={setActiveKeyboard} | ||
/> | ||
</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>darl</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,50 @@ | ||
import { useState } from 'react'; | ||
import type { EditorBridge } from '../types'; | ||
import type { Subscription } from '../types/Subscription'; | ||
|
||
class _EditorHelper { | ||
editorLastInstance: EditorBridge | undefined; | ||
cbs: ((editor: EditorBridge | undefined) => void)[] = []; | ||
|
||
constructor() { | ||
this.editorLastInstance = undefined; | ||
} | ||
|
||
setEditorLastInstance(editorLastInstance: EditorBridge) { | ||
this.editorLastInstance = editorLastInstance; | ||
this.cbs.forEach((cb) => { | ||
cb(editorLastInstance); | ||
}); | ||
} | ||
|
||
subscribe: Subscription<EditorBridge | undefined> = (cb) => { | ||
this.cbs.push(cb); | ||
return () => { | ||
this.cbs = this.cbs.filter((sub) => sub !== cb); | ||
}; | ||
}; | ||
} | ||
|
||
export const EditorHelper = new _EditorHelper(); | ||
|
||
export const useRemoteEditorBridge = () => { | ||
const [editor, _setEditor] = useState<EditorBridge | undefined>( | ||
EditorHelper.editorLastInstance | ||
); | ||
|
||
// TODO - | ||
// There is currently a bug on ios where the keyboard isn't unmounted RCTRootView isn't unmounted | ||
// When removed from subview, because of this we can't rely on it to unsubscribe. Once this is fixed we can | ||
// add this again make it be reactive | ||
// useEffect(() => { | ||
// const unsubscribe = EditorHelper.subscribe((editor) => { | ||
// setEditor(editor); | ||
// }); | ||
|
||
// return () => { | ||
// unsubscribe(); | ||
// }; | ||
// }, []); | ||
|
||
return editor; | ||
}; |
Oops, something went wrong.