Skip to content

Commit

Permalink
fix: custom override method removes support for asynchronous #672
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Jul 3, 2024
1 parent 3a6878e commit 0e705d6
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 35 deletions.
8 changes: 4 additions & 4 deletions docs/en/guide/override.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Editor from "@hufe921/canvas-editor"

const instance = new Editor(container, <IElement[]>data, options)

instance.override.overrideFunction = () => void | Promise<void> | IOverrideResult | Promise<IOverrideResult>
instance.override.overrideFunction = () => unknown | IOverrideResult
```

```typescript
Expand All @@ -23,7 +23,7 @@ Feature: Override internal paste function
Usage:

```javascript
instance.override.paste = (evt?: ClipboardEvent) => void | Promise<void> | IOverrideResult | Promise<IOverrideResult>
instance.override.paste = (evt?: ClipboardEvent) => unknown | IOverrideResult
```

## copy
Expand All @@ -33,7 +33,7 @@ Feature: Override internal copy function
Usage:

```javascript
instance.override.copy = () => void | Promise<void> | IOverrideResult | Promise<IOverrideResult>
instance.override.copy = () => unknown | IOverrideResult
```

## drop
Expand All @@ -43,5 +43,5 @@ Feature: Override internal drop function
Usage:

```javascript
instance.override.drop = (evt: DragEvent) => void | Promise<void> | IOverrideResult | Promise<IOverrideResult>
instance.override.drop = (evt: DragEvent) => unknown | IOverrideResult
```
8 changes: 4 additions & 4 deletions docs/guide/override.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Editor from "@hufe921/canvas-editor"

const instance = new Editor(container, <IElement[]>data, options)

instance.override.overrideFunction = () => void | Promise<void> | IOverrideResult | Promise<IOverrideResult>
instance.override.overrideFunction = () => unknown | IOverrideResult
```

```typescript
Expand All @@ -23,7 +23,7 @@ interface IOverrideResult {
用法:

```javascript
instance.override.paste = (evt?: ClipboardEvent) => void | Promise<void> | IOverrideResult | Promise<IOverrideResult>
instance.override.paste = (evt?: ClipboardEvent) => unknown | IOverrideResult
```

## copy
Expand All @@ -33,7 +33,7 @@ instance.override.paste = (evt?: ClipboardEvent) => void | Promise<void> | IOver
用法:

```javascript
instance.override.copy = () => void | Promise<void> | IOverrideResult | Promise<IOverrideResult>
instance.override.copy = () => unknown | IOverrideResult
```

## drop
Expand All @@ -43,5 +43,5 @@ instance.override.copy = () => void | Promise<void> | IOverrideResult | Promise<
用法:

```javascript
instance.override.drop = (evt: DragEvent) => void | Promise<void> | IOverrideResult | Promise<IOverrideResult>
instance.override.drop = (evt: DragEvent) => unknown | IOverrideResult
```
5 changes: 2 additions & 3 deletions src/editor/core/event/handlers/copy.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { ElementType } from '../../../dataset/enum/Element'
import { IElement } from '../../../interface/Element'
import { ITr } from '../../../interface/table/Tr'
import { isPromiseFunction } from '../../../utils'
import { writeElementList } from '../../../utils/clipboard'
import { zipElementList } from '../../../utils/element'
import { IOverrideResult } from '../../override/Override'
import { CanvasEvent } from '../CanvasEvent'

export async function copy(host: CanvasEvent) {
export function copy(host: CanvasEvent) {
const draw = host.getDraw()
// 自定义粘贴事件
const { copy } = draw.getOverride()
if (copy) {
const overrideResult = isPromiseFunction(copy) ? await copy() : copy()
const overrideResult = copy()
// 默认阻止默认事件
if ((<IOverrideResult>overrideResult)?.preventDefault !== false) return
}
Expand Down
5 changes: 2 additions & 3 deletions src/editor/core/event/handlers/drop.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { isPromiseFunction } from '../../../utils'
import { IOverrideResult } from '../../override/Override'
import { CanvasEvent } from '../CanvasEvent'
import { pasteImage } from './paste'

export async function drop(evt: DragEvent, host: CanvasEvent) {
export function drop(evt: DragEvent, host: CanvasEvent) {
const draw = host.getDraw()
// 自定义拖放事件
const { drop } = draw.getOverride()
if (drop) {
const overrideResult = isPromiseFunction(drop) ? await drop(evt) : drop(evt)
const overrideResult = drop(evt)
// 默认阻止默认事件
if ((<IOverrideResult>overrideResult)?.preventDefault !== false) return
}
Expand Down
9 changes: 3 additions & 6 deletions src/editor/core/event/handlers/paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
getElementListByHTML
} from '../../../utils/element'
import { CanvasEvent } from '../CanvasEvent'
import { isPromiseFunction } from '../../../utils'
import { IOverrideResult } from '../../override/Override'

export function pasteElement(host: CanvasEvent, elementList: IElement[]) {
Expand Down Expand Up @@ -98,7 +97,7 @@ export function pasteImage(host: CanvasEvent, file: File | Blob) {
}
}

export async function pasteByEvent(host: CanvasEvent, evt: ClipboardEvent) {
export function pasteByEvent(host: CanvasEvent, evt: ClipboardEvent) {
const draw = host.getDraw()
const isReadonly = draw.isReadonly()
if (isReadonly) return
Expand All @@ -107,9 +106,7 @@ export async function pasteByEvent(host: CanvasEvent, evt: ClipboardEvent) {
// 自定义粘贴事件
const { paste } = draw.getOverride()
if (paste) {
const overrideResult = isPromiseFunction(paste)
? await paste(evt)
: paste(evt)
const overrideResult = paste(evt)
// 默认阻止默认事件
if ((<IOverrideResult>overrideResult)?.preventDefault !== false) return
}
Expand Down Expand Up @@ -165,7 +162,7 @@ export async function pasteByApi(host: CanvasEvent, options?: IPasteOption) {
// 自定义粘贴事件
const { paste } = draw.getOverride()
if (paste) {
const overrideResult = isPromiseFunction(paste) ? await paste() : paste()
const overrideResult = paste()
// 默认阻止默认事件
if ((<IOverrideResult>overrideResult)?.preventDefault !== false) return
}
Expand Down
14 changes: 3 additions & 11 deletions src/editor/core/override/Override.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,8 @@ export interface IOverrideResult {

export class Override {
public paste:
| ((
evt?: ClipboardEvent
) => void | Promise<void> | IOverrideResult | Promise<IOverrideResult>)
| undefined
public copy:
| (() => void | Promise<void> | IOverrideResult | Promise<IOverrideResult>)
| undefined
public drop:
| ((
evt: DragEvent
) => void | Promise<void> | IOverrideResult | Promise<IOverrideResult>)
| ((evt?: ClipboardEvent) => unknown | IOverrideResult)
| undefined
public copy: (() => unknown | IOverrideResult) | undefined
public drop: ((evt: DragEvent) => unknown | IOverrideResult) | undefined
}
4 changes: 0 additions & 4 deletions src/editor/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,3 @@ export function isObjectEqual(obj1: unknown, obj2: unknown): boolean {
}
return !obj1Keys.some(key => obj2[key] !== obj1[key])
}

export function isPromiseFunction(fn: Function): boolean {
return fn?.constructor?.name === 'AsyncFunction'
}

0 comments on commit 0e705d6

Please sign in to comment.