Skip to content

Commit e942e17

Browse files
authored
Merge pull request #30 from cs-open/feat/ts
chore(Text): fix Text typescript
2 parents c4ec8ec + a982bc5 commit e942e17

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

examples/astro/src/components/Canvas.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
useReactFabric,
88
ReactFabricProvider,
99
Control,
10+
Text,
1011
// PluginGridLine,
1112
} from '@cs-open/react-fabric'
1213
import './Canvas.css'
@@ -46,6 +47,7 @@ export default function Counter() {
4647
/>
4748
</Control>
4849
))}
50+
<Text text="red" />
4951
{/* <PluginGridLine></PluginGridLine> */}
5052
</ReactFabric>
5153
</ReactFabricProvider>
@@ -57,7 +59,7 @@ const Toolbar = () => {
5759
const { resetViewport, zoomIn, zoomOut, manualZoom, canvas } = useReactFabric()
5860
return (
5961
<div>
60-
<button onClick={resetViewport}>重置</button>
62+
<button onClick={() => resetViewport()}>重置</button>
6163
<button onClick={zoomIn}>放大</button>
6264
<span> {Math.round(manualZoom * 100)}%</span>
6365
<button onClick={zoomOut}>缩小</button>

packages/react/src/components/BackgroundImage/index.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import { useDidUpdate } from '../../hooks/useDidUpdate'
1212
import { useStore, useStoreApi } from '../../hooks/useStore'
1313
import type { ReactFabricState } from '../../types/store'
1414

15-
export type Handle = {}
15+
export type Handle = {
16+
instance: FabricImage | null
17+
}
1618

1719
type ScaleMode = {
1820
scaleToFit?: boolean
@@ -22,6 +24,7 @@ export type BackgroundImageProps = Partial<ImageProps> & {
2224
src: string
2325
onLoad?: (imageSource: FabricImage<Partial<ImageProps>, SerializedImageProps, ObjectEvents>) => void
2426
onScaling?: (scale: BasicTransformEvent<TPointerEvent>) => void
27+
onError?: (e: any) => void
2528

2629
/** 自动缩放至容器宽高 */
2730
} & ScaleMode
@@ -32,7 +35,7 @@ const selector = (s: ReactFabricState) => ({
3235
})
3336

3437
const BackgroundImage = forwardRef<Handle, BackgroundImageProps>(
35-
({ src, onLoad, onScaling, scaleToFit, scaleToCover, ...options }, ref) => {
38+
({ src, onLoad, onScaling, onError, scaleToFit, scaleToCover, ...options }, ref) => {
3639
const backgroundImageRef = useRef<FabricImage | null>(null)
3740

3841
const store = useStoreApi()
@@ -131,6 +134,7 @@ const BackgroundImage = forwardRef<Handle, BackgroundImageProps>(
131134
setLoading(true)
132135
FabricImage.fromURL(src, { crossOrigin: 'anonymous' })
133136
.then(imageSource => {
137+
onLoad?.(imageSource)
134138
const currentSrc = imageSource.getSrc()
135139
const latestSrc = backgroundImageRef.current?.getSrc()
136140

@@ -165,12 +169,14 @@ const BackgroundImage = forwardRef<Handle, BackgroundImageProps>(
165169
}
166170

167171
// imageSource.angle = options.angle || 0
168-
onLoad?.(imageSource)
169172
} else {
170173
console.warn('ReactFabric:BackgroundImage: !canvas', canvas)
171174
}
172175
})
173-
.catch(console.error)
176+
.catch(e => {
177+
onError?.(e)
178+
console.error('[ReactFabric] BackgroundImage: fromURL error', e)
179+
})
174180
.finally(() => {
175181
if (domNode) domNode.dataset.src = src
176182
setLoading(false)

packages/react/src/components/Objects/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Line from '../Line'
66
import IText from '../IText'
77

88
export type ObjectsProps = {
9-
objects: { type: string;[index: string]: any }[]
9+
objects: { type: string; [index: string]: any }[]
1010
}
1111

1212
const Objects = ({ objects }: ObjectsProps) => {
@@ -27,7 +27,7 @@ const Objects = ({ objects }: ObjectsProps) => {
2727
if (!Component) {
2828
return null
2929
}
30-
return <Component {...options} />
30+
return <Component {...(options as any)} />
3131
})}
3232
</>
3333
)

packages/react/src/components/Text/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { useInstancePosition } from '../../hooks/useInstancePosition'
1010

1111
export type Handle = FabricText | undefined
1212

13-
export type TextProps<T = unknown> = Partial<ConstructorParameters<typeof FabricText>[1] & AllObjectEvents> & {
13+
export type TextProps<T = unknown> = Partial<FabricText & AllObjectEvents> & {
1414
group?: BaseGroup
1515
text: string
1616
children?: ReactNode
@@ -65,4 +65,5 @@ const Text = forwardRef<Handle, TextProps>(({ group, text, children, ...props },
6565
return useInstancePosition(instance, children)
6666
})
6767

68+
Text.displayName = 'Text'
6869
export default memo(Text)

0 commit comments

Comments
 (0)