Skip to content

Commit

Permalink
feat: add plain text copy option
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Nov 24, 2024
1 parent 5e15ffe commit 34c4401
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/en/guide/command-execute.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Feature: Copy
Usage:

```javascript
instance.command.executeCopy()
instance.command.executeCopy(payload?: ICopyOption)
```

## executePaste
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/command-execute.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ instance.command.executeCut()
用法:

```javascript
instance.command.executeCopy()
instance.command.executeCopy(payload?: ICopyOption)
```

## executePaste
Expand Down
10 changes: 7 additions & 3 deletions src/editor/core/command/CommandAdapt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ import {
IGetElementByIdOption,
IUpdateElementByIdOption
} from '../../interface/Element'
import { IPasteOption, IPositionContextByEvent } from '../../interface/Event'
import {
ICopyOption,
IPasteOption,
IPositionContextByEvent
} from '../../interface/Event'
import { IMargin } from '../../interface/Margin'
import { ILocationPosition } from '../../interface/Position'
import { IRange, RangeContext, RangeRect } from '../../interface/Range'
Expand Down Expand Up @@ -153,8 +157,8 @@ export class CommandAdapt {
this.canvasEvent.cut()
}

public copy() {
this.canvasEvent.copy()
public copy(payload?: ICopyOption) {
this.canvasEvent.copy(payload)
}

public paste(payload?: IPasteOption) {
Expand Down
5 changes: 3 additions & 2 deletions src/editor/core/event/CanvasEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import click from './handlers/click'
import composition from './handlers/composition'
import drag from './handlers/drag'
import { isIOS } from '../../utils/ua'
import { ICopyOption } from '../../interface/Event'

export interface ICompositionInfo {
elementList: IElement[]
Expand Down Expand Up @@ -178,8 +179,8 @@ export class CanvasEvent {
cut(this)
}

public copy() {
copy(this)
public copy(options?: ICopyOption) {
copy(this, options)
}

public compositionstart() {
Expand Down
12 changes: 10 additions & 2 deletions src/editor/core/event/handlers/copy.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ElementType } from '../../../dataset/enum/Element'
import { IElement } from '../../../interface/Element'
import { ICopyOption } from '../../../interface/Event'
import { ITr } from '../../../interface/table/Tr'
import { writeElementList } from '../../../utils/clipboard'
import { zipElementList } from '../../../utils/element'
import { getTextFromElementList, zipElementList } from '../../../utils/element'
import { IOverrideResult } from '../../override/Override'
import { CanvasEvent } from '../CanvasEvent'

export function copy(host: CanvasEvent) {
export function copy(host: CanvasEvent, options?: ICopyOption) {
const draw = host.getDraw()
// 自定义粘贴事件
const { copy } = draw.getOverride()
Expand Down Expand Up @@ -59,6 +60,13 @@ export function copy(host: CanvasEvent) {
? rangeManager.getRangeRowElementList()
: rangeManager.getSelectionElementList()
}
if (options?.isPlainText && copyElementList?.length) {
copyElementList = [
{
value: getTextFromElementList(copyElementList)
}
]
}
if (!copyElementList?.length) return
writeElementList(copyElementList, draw.getOptions())
}
4 changes: 4 additions & 0 deletions src/editor/interface/Event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ export interface IPositionContextByEvent {
element: IElement | null
rangeRect: RangeRect | null
}

export interface ICopyOption {
isPlainText: boolean
}

0 comments on commit 34c4401

Please sign in to comment.