-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: prevent other platform import pencilkit
- Loading branch information
Showing
10 changed files
with
157 additions
and
121 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 was deleted.
Oops, something went wrong.
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,81 @@ | ||
import { type ForwardedRef, forwardRef, useImperativeHandle, useRef } from 'react'; | ||
import { processColor, Text } from 'react-native'; | ||
import { type PencilKitProps, type PencilKitRef, PencilKitUtil } from 'react-native-pencil-kit'; | ||
|
||
import NativePencilKitView, { Commands } from '../spec/RNPencilKitNativeComponent'; | ||
|
||
function PencilKitComponent( | ||
{ | ||
alwaysBounceHorizontal = true, | ||
alwaysBounceVertical = true, | ||
isRulerActive = false, | ||
drawingPolicy = 'default', | ||
backgroundColor, | ||
isOpaque = true, | ||
onToolPickerFramesObscuredDidChange, | ||
onToolPickerIsRulerActiveDidChange, | ||
onToolPickerSelectedToolDidChange, | ||
onToolPickerVisibilityDidChange, | ||
onCanvasViewDidBeginUsingTool, | ||
onCanvasViewDidEndUsingTool, | ||
onCanvasViewDidFinishRendering, | ||
onCanvasViewDrawingDidChange, | ||
...rest | ||
}: PencilKitProps, | ||
ref: ForwardedRef<PencilKitRef>, | ||
) { | ||
const nativeRef = useRef(null); | ||
useImperativeHandle( | ||
ref, | ||
() => ({ | ||
clear: () => Commands.clear(nativeRef.current!), | ||
showToolPicker: () => Commands.showToolPicker(nativeRef.current!), | ||
hideToolPicker: () => Commands.hideToolPicker(nativeRef.current!), | ||
redo: () => Commands.redo(nativeRef.current!), | ||
undo: () => Commands.undo(nativeRef.current!), | ||
saveDrawing: (path) => Commands.saveDrawing(nativeRef.current!, path), | ||
loadDrawing: (path) => Commands.loadDrawing(nativeRef.current!, path), | ||
getBase64Data: () => Commands.getBase64Data(nativeRef.current!), | ||
loadBase64Data: (base64) => Commands.loadBase64Data(nativeRef.current!, base64), | ||
setTool: ({ color, toolType, width }) => | ||
Commands.setTool( | ||
nativeRef.current!, | ||
toolType, | ||
width ?? 0, | ||
color ? (processColor(color) as number) : 0, | ||
), | ||
}), | ||
[], | ||
); | ||
|
||
if (!PencilKitUtil.isPencilKitAvailable()) { | ||
return ( | ||
<Text>{"This iOS version doesn't support pencilkit. The minimum requirement is 14.0"}</Text> | ||
); | ||
} | ||
|
||
return ( | ||
<NativePencilKitView | ||
ref={nativeRef} | ||
{...{ | ||
alwaysBounceHorizontal, | ||
alwaysBounceVertical, | ||
isRulerActive, | ||
drawingPolicy, | ||
backgroundColor: processColor(backgroundColor) as number, | ||
isOpaque, | ||
onToolPickerFramesObscuredDidChange, | ||
onToolPickerIsRulerActiveDidChange, | ||
onToolPickerSelectedToolDidChange, | ||
onToolPickerVisibilityDidChange, | ||
onCanvasViewDidBeginUsingTool, | ||
onCanvasViewDidEndUsingTool, | ||
onCanvasViewDidFinishRendering, | ||
onCanvasViewDrawingDidChange, | ||
}} | ||
{...rest} | ||
/> | ||
); | ||
} | ||
|
||
export const PencilKit = forwardRef(PencilKitComponent); |
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,48 @@ | ||
import { type ForwardedRef, forwardRef } from 'react'; | ||
import { type ColorValue, Text, type ViewProps } from 'react-native'; | ||
import type { DirectEventHandler, WithDefault } from 'react-native/Libraries/Types/CodegenTypes'; | ||
|
||
export type PencilKitProps = { | ||
alwaysBounceVertical?: boolean; | ||
alwaysBounceHorizontal?: boolean; | ||
isRulerActive?: boolean; | ||
backgroundColor?: ColorValue; | ||
drawingPolicy?: WithDefault<'default' | 'anyinput' | 'pencilonly', 'default'>; | ||
isOpaque?: boolean; | ||
|
||
onToolPickerVisibilityDidChange?: DirectEventHandler<{}>; | ||
onToolPickerIsRulerActiveDidChange?: DirectEventHandler<{}>; | ||
onToolPickerFramesObscuredDidChange?: DirectEventHandler<{}>; | ||
onToolPickerSelectedToolDidChange?: DirectEventHandler<{}>; | ||
onCanvasViewDidBeginUsingTool?: DirectEventHandler<{}>; | ||
onCanvasViewDidEndUsingTool?: DirectEventHandler<{}>; | ||
onCanvasViewDrawingDidChange?: DirectEventHandler<{}>; | ||
onCanvasViewDidFinishRendering?: DirectEventHandler<{}>; | ||
} & ViewProps; | ||
export type PencilKitTool = | ||
| 'pen' | ||
| 'pencil' | ||
| 'marker' | ||
| 'monoline' | ||
| 'fountainPen' | ||
| 'watercolor' | ||
| 'crayon' | ||
| 'eraserVector' | ||
| 'eraserBitmap' | ||
| 'eraserFixedWidthBitmap'; | ||
export type PencilKitRef = { | ||
clear: () => void; | ||
showToolPicker: () => void; | ||
hideToolPicker: () => void; | ||
redo: () => void; | ||
undo: () => void; | ||
saveDrawing: (path: string) => void; | ||
loadDrawing: (path: string) => string; | ||
getBase64Data: () => void; | ||
loadBase64Data: (base64: string) => void; | ||
setTool: (params: { toolType: PencilKitTool; width?: number; color?: ColorValue }) => void; | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
export const PencilKit = forwardRef((props: PencilKitProps, ref: ForwardedRef<PencilKitRef>) => { | ||
return <Text>{"This platform doesn't support pencilkit"}</Text>; | ||
}); |
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,117 +1,15 @@ | ||
import { type ForwardedRef, forwardRef, useImperativeHandle, useRef } from 'react'; | ||
import { type ColorValue, processColor, type ViewProps } from 'react-native'; | ||
import type { DirectEventHandler, WithDefault } from 'react-native/Libraries/Types/CodegenTypes'; | ||
import { type ForwardedRef, forwardRef } from 'react'; | ||
|
||
import NativePencilKitView, { Commands } from './spec/RNPencilKitNativeComponent'; | ||
import type { PencilKitProps, PencilKitRef, PencilKitTool } from './component/PencilKit'; | ||
import { PencilKit } from './component/PencilKit'; | ||
import { PencilKitUtil } from './util/PencilKitUtil'; | ||
|
||
export type PencilKitProps = { | ||
alwaysBounceVertical?: boolean; | ||
alwaysBounceHorizontal?: boolean; | ||
isRulerActive?: boolean; | ||
backgroundColor?: ColorValue; | ||
drawingPolicy?: WithDefault<'default' | 'anyinput' | 'pencilonly', 'default'>; | ||
isOpaque?: boolean; | ||
|
||
onToolPickerVisibilityDidChange?: DirectEventHandler<{}>; | ||
onToolPickerIsRulerActiveDidChange?: DirectEventHandler<{}>; | ||
onToolPickerFramesObscuredDidChange?: DirectEventHandler<{}>; | ||
onToolPickerSelectedToolDidChange?: DirectEventHandler<{}>; | ||
onCanvasViewDidBeginUsingTool?: DirectEventHandler<{}>; | ||
onCanvasViewDidEndUsingTool?: DirectEventHandler<{}>; | ||
onCanvasViewDrawingDidChange?: DirectEventHandler<{}>; | ||
onCanvasViewDidFinishRendering?: DirectEventHandler<{}>; | ||
} & ViewProps; | ||
export type PencilKitTool = | ||
| 'pen' | ||
| 'pencil' | ||
| 'marker' | ||
| 'monoline' | ||
| 'fountainPen' | ||
| 'watercolor' | ||
| 'crayon' | ||
| 'eraserVector' | ||
| 'eraserBitmap' | ||
| 'eraserFixedWidthBitmap'; | ||
export type PencilKitRef = { | ||
clear: () => void; | ||
showToolPicker: () => void; | ||
hideToolPicker: () => void; | ||
redo: () => void; | ||
undo: () => void; | ||
saveDrawing: (path: string) => void; | ||
loadDrawing: (path: string) => string; | ||
getBase64Data: () => void; | ||
loadBase64Data: (base64: string) => void; | ||
setTool: (params: { toolType: PencilKitTool; width?: number; color?: ColorValue }) => void; | ||
}; | ||
function PencilKit( | ||
{ | ||
alwaysBounceHorizontal = true, | ||
alwaysBounceVertical = true, | ||
isRulerActive = false, | ||
drawingPolicy = 'default', | ||
backgroundColor, | ||
isOpaque = true, | ||
onToolPickerFramesObscuredDidChange, | ||
onToolPickerIsRulerActiveDidChange, | ||
onToolPickerSelectedToolDidChange, | ||
onToolPickerVisibilityDidChange, | ||
onCanvasViewDidBeginUsingTool, | ||
onCanvasViewDidEndUsingTool, | ||
onCanvasViewDidFinishRendering, | ||
onCanvasViewDrawingDidChange, | ||
...rest | ||
}: PencilKitProps, | ||
ref: ForwardedRef<PencilKitRef>, | ||
) { | ||
const nativeRef = useRef(null); | ||
useImperativeHandle( | ||
ref, | ||
() => ({ | ||
clear: () => Commands.clear(nativeRef.current!), | ||
showToolPicker: () => Commands.showToolPicker(nativeRef.current!), | ||
hideToolPicker: () => Commands.hideToolPicker(nativeRef.current!), | ||
redo: () => Commands.redo(nativeRef.current!), | ||
undo: () => Commands.undo(nativeRef.current!), | ||
saveDrawing: (path) => Commands.saveDrawing(nativeRef.current!, path), | ||
loadDrawing: (path) => Commands.loadDrawing(nativeRef.current!, path), | ||
getBase64Data: () => Commands.getBase64Data(nativeRef.current!), | ||
loadBase64Data: (base64) => Commands.loadBase64Data(nativeRef.current!, base64), | ||
setTool: ({ color, toolType, width }) => | ||
Commands.setTool( | ||
nativeRef.current!, | ||
toolType, | ||
width ?? 0, | ||
color ? (processColor(color) as number) : 0, | ||
), | ||
}), | ||
[], | ||
); | ||
|
||
return ( | ||
<NativePencilKitView | ||
ref={nativeRef} | ||
{...{ | ||
alwaysBounceHorizontal, | ||
alwaysBounceVertical, | ||
isRulerActive, | ||
drawingPolicy, | ||
backgroundColor: processColor(backgroundColor) as number, | ||
isOpaque, | ||
onToolPickerFramesObscuredDidChange, | ||
onToolPickerIsRulerActiveDidChange, | ||
onToolPickerSelectedToolDidChange, | ||
onToolPickerVisibilityDidChange, | ||
onCanvasViewDidBeginUsingTool, | ||
onCanvasViewDidEndUsingTool, | ||
onCanvasViewDidFinishRendering, | ||
onCanvasViewDrawingDidChange, | ||
}} | ||
{...rest} | ||
/> | ||
); | ||
} | ||
|
||
const PencilKitView = forwardRef(PencilKit); | ||
export const PencilKitView = forwardRef( | ||
(props: PencilKitProps, ref: ForwardedRef<PencilKitRef>) => { | ||
return <PencilKit {...props} ref={ref} />; | ||
}, | ||
); | ||
export default PencilKitView; | ||
export { PencilKitView as PencilKit }; | ||
|
||
export type { PencilKitProps, PencilKitRef, PencilKitTool }; | ||
export { PencilKitUtil }; |
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,6 @@ | ||
import NativeRNPencilKitUtil from '../spec/NativeRNPencilKitUtil'; | ||
|
||
export const PencilKitUtil = { | ||
isPencilKitAvailable: NativeRNPencilKitUtil.isPencilKitAvailable, | ||
getAvailableTools: NativeRNPencilKitUtil.getAvailableTools, | ||
}; |
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,4 @@ | ||
export const PencilKitUtil = { | ||
isPencilKitAvailable: (): boolean => false, | ||
getAvailableTools: (): string[] => [], | ||
}; |
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