Skip to content

Commit ce4eb10

Browse files
authored
feat: make clipboard operations async for consistency #1276
Convert all clipboard-related operations to async/await pattern to ensure proper handling of asynchronous clipboard API calls. This includes copy, cut, and write operations across the codebase.
1 parent 1537cb6 commit ce4eb10

File tree

6 files changed

+19
-19
lines changed

6 files changed

+19
-19
lines changed

src/editor/core/command/CommandAdapt.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,14 @@ export class CommandAdapt {
162162
this.draw.setMode(payload)
163163
}
164164

165-
public cut() {
165+
public async cut() {
166166
const isDisabled = this.draw.isReadonly() || this.draw.isDisabled()
167167
if (isDisabled) return
168-
this.canvasEvent.cut()
168+
await this.canvasEvent.cut()
169169
}
170170

171-
public copy(payload?: ICopyOption) {
172-
this.canvasEvent.copy(payload)
171+
public async copy(payload?: ICopyOption) {
172+
await this.canvasEvent.copy(payload)
173173
}
174174

175175
public paste(payload?: IPasteOption) {

src/editor/core/event/CanvasEvent.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,12 @@ export class CanvasEvent {
176176
input(data, this)
177177
}
178178

179-
public cut() {
180-
cut(this)
179+
public async cut() {
180+
await cut(this)
181181
}
182182

183-
public copy(options?: ICopyOption) {
184-
copy(this, options)
183+
public async copy(options?: ICopyOption) {
184+
await copy(this, options)
185185
}
186186

187187
public compositionstart() {

src/editor/core/event/handlers/copy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { getTextFromElementList, zipElementList } from '../../../utils/element'
77
import { IOverrideResult } from '../../override/Override'
88
import { CanvasEvent } from '../CanvasEvent'
99

10-
export function copy(host: CanvasEvent, options?: ICopyOption) {
10+
export async function copy(host: CanvasEvent, options?: ICopyOption) {
1111
const draw = host.getDraw()
1212
// 自定义粘贴事件
1313
const { copy } = draw.getOverride()
@@ -68,5 +68,5 @@ export function copy(host: CanvasEvent, options?: ICopyOption) {
6868
]
6969
}
7070
if (!copyElementList?.length) return
71-
writeElementList(copyElementList, draw.getOptions())
71+
await writeElementList(copyElementList, draw.getOptions())
7272
}

src/editor/core/event/handlers/cut.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { writeElementList } from '../../../utils/clipboard'
22
import { CanvasEvent } from '../CanvasEvent'
33

4-
export function cut(host: CanvasEvent) {
4+
export async function cut(host: CanvasEvent) {
55
const draw = host.getDraw()
66
const rangeManager = draw.getRange()
77
const { startIndex, endIndex } = rangeManager.getRange()
@@ -32,7 +32,7 @@ export function cut(host: CanvasEvent) {
3232
}
3333
const options = draw.getOptions()
3434
// 写入粘贴板
35-
writeElementList(elementList.slice(start + 1, end + 1), options)
35+
await writeElementList(elementList.slice(start + 1, end + 1), options)
3636
const control = draw.getControl()
3737
let curIndex: number
3838
if (control.getActiveControl() && control.getIsRangeWithinControl()) {

src/editor/utils/clipboard.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function removeClipboardData() {
2828
localStorage.removeItem(EDITOR_CLIPBOARD)
2929
}
3030

31-
export function writeClipboardItem(
31+
export async function writeClipboardItem(
3232
text: string,
3333
html: string,
3434
elementList: IElement[]
@@ -42,7 +42,7 @@ export function writeClipboardItem(
4242
[plainText.type]: plainText,
4343
[htmlText.type]: htmlText
4444
})
45-
window.navigator.clipboard.write([item])
45+
await window.navigator.clipboard.write([item])
4646
} else {
4747
const fakeElement = document.createElement('div')
4848
fakeElement.setAttribute('contenteditable', 'true')
@@ -66,7 +66,7 @@ export function writeClipboardItem(
6666
setClipboardData({ text, elementList })
6767
}
6868

69-
export function writeElementList(
69+
export async function writeElementList(
7070
elementList: IElement[],
7171
options: DeepRequired<IEditorOption>
7272
) {
@@ -78,7 +78,7 @@ export function writeElementList(
7878
clipboardDom.remove()
7979
const html = clipboardDom.innerHTML
8080
if (!text && !html && !elementList.length) return
81-
writeClipboardItem(text, html, zipElementList(elementList))
81+
await writeClipboardItem(text, html, zipElementList(elementList))
8282
}
8383

8484
export function getIsClipboardContainFile(clipboardData: DataTransfer) {

src/plugins/copy/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function copyWithCopyrightPlugin(
1111
) {
1212
const copy = editor.command.executeCopy
1313

14-
editor.command.executeCopy = () => {
14+
editor.command.executeCopy = async () => {
1515
const { copyrightText } = options || {}
1616
if (copyrightText) {
1717
const rangeText = editor.command.getRangeText()
@@ -22,9 +22,9 @@ export function copyWithCopyrightPlugin(
2222
const item = new ClipboardItem({
2323
[plainText.type]: plainText
2424
})
25-
window.navigator.clipboard.write([item])
25+
await window.navigator.clipboard.write([item])
2626
} else {
27-
copy()
27+
await copy()
2828
}
2929
}
3030
}

0 commit comments

Comments
 (0)