-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(editor): add native clipboard extension
- Loading branch information
Showing
9 changed files
with
117 additions
and
36 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
23 changes: 23 additions & 0 deletions
23
blocksuite/affine/shared/src/services/native-clipboard-service.ts
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,23 @@ | ||
import type { ExtensionType } from '@blocksuite/block-std'; | ||
import { createIdentifier } from '@blocksuite/global/di'; | ||
|
||
/** | ||
* Copies the image as PNG in Electron. | ||
*/ | ||
export interface NativeClipboardService { | ||
copyAsPNG(arrayBuffer: ArrayBuffer): Promise<boolean>; | ||
} | ||
|
||
export const NativeClipboardProvider = createIdentifier<NativeClipboardService>( | ||
'NativeClipboardService' | ||
); | ||
|
||
export function NativeClipboardExtension( | ||
nativeClipboardProvider: NativeClipboardService | ||
): ExtensionType { | ||
return { | ||
setup: di => { | ||
di.addImpl(NativeClipboardProvider, nativeClipboardProvider); | ||
}, | ||
}; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import type { IpcMainInvokeEvent } from 'electron'; | ||
import { clipboard, nativeImage } from 'electron'; | ||
|
||
import type { NamespaceHandlers } from '../type'; | ||
|
||
export const clipboardHandlers = { | ||
copyAsImageFromString: async (_: IpcMainInvokeEvent, dataURL: string) => { | ||
clipboard.writeImage(nativeImage.createFromDataURL(dataURL)); | ||
copyAsPNG: async (_, arrayBuffer: ArrayBuffer) => { | ||
const image = nativeImage.createFromBuffer(Buffer.from(arrayBuffer)); | ||
if (image.isEmpty()) return false; | ||
clipboard.writeImage(image); | ||
return true; | ||
}, | ||
} satisfies NamespaceHandlers; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { test } from '@affine-test/kit/electron'; | ||
import { importImage } from '@affine-test/kit/utils/image'; | ||
import { pasteByKeyboard } from '@affine-test/kit/utils/keyboard'; | ||
import { | ||
clickNewPageButton, | ||
getBlockSuiteEditorTitle, | ||
} from '@affine-test/kit/utils/page-logic'; | ||
import { expect } from '@playwright/test'; | ||
|
||
test('should be able to insert SVG images', async ({ page }) => { | ||
await page.waitForTimeout(500); | ||
await clickNewPageButton(page); | ||
const title = getBlockSuiteEditorTitle(page); | ||
await title.focus(); | ||
await page.keyboard.press('Enter'); | ||
|
||
await importImage(page, 'affine.svg'); | ||
|
||
const svg = page.locator('affine-image').first(); | ||
await expect(svg).toBeVisible(); | ||
}); | ||
|
||
test('should paste it as PNG after copying SVG', async ({ page }) => { | ||
await page.waitForTimeout(500); | ||
await clickNewPageButton(page); | ||
const title = getBlockSuiteEditorTitle(page); | ||
await title.focus(); | ||
await page.keyboard.press('Enter'); | ||
|
||
await importImage(page, 'affine.svg'); | ||
|
||
const svg = page.locator('affine-image').first(); | ||
await expect(svg).toBeVisible(); | ||
|
||
await svg.hover(); | ||
|
||
await page.waitForTimeout(500); | ||
const imageToolbar = page.locator('affine-image-toolbar'); | ||
await expect(imageToolbar).toBeVisible(); | ||
await imageToolbar.getByRole('button', { name: 'More' }).click(); | ||
|
||
const moveMenu = page.locator('.image-more-popup-menu'); | ||
await moveMenu.getByRole('button', { name: /^Copy$/ }).click(); | ||
|
||
await svg.click(); | ||
|
||
await page.keyboard.press('Enter'); | ||
await pasteByKeyboard(page); | ||
|
||
const png = page.locator('affine-image').nth(1); | ||
await expect(png).toBeVisible(); | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.