@blocksuite/block-std
Advanced tools
Comparing version 0.0.0-20231018100009-361737d3-nightly to 0.0.0-20231018165053-6a3da92f-nightly
@@ -1,2 +0,2 @@ | ||
import type { BaseAdapter, JobMiddleware, Page, Slice } from '@blocksuite/store'; | ||
import type { BaseAdapter, BlockSnapshot, JobMiddleware, Page, Slice } from '@blocksuite/store'; | ||
export declare class Clipboard { | ||
@@ -16,3 +16,6 @@ std: BlockSuite.Std; | ||
paste: (event: ClipboardEvent, page: Page, parent?: string, index?: number) => Promise<Slice | null | undefined>; | ||
writeToClipboard(updateItems: (items: Record<string, unknown>) => Promise<Record<string, unknown>>): Promise<void>; | ||
readFromClipboard(clipboardData: DataTransfer): any; | ||
pasteBlockSnapshot: (snapshot: BlockSnapshot, page: Page, parent?: string, index?: number) => Promise<import("@blocksuite/store").BaseBlockModel<object>>; | ||
} | ||
//# sourceMappingURL=index.d.ts.map |
@@ -43,53 +43,20 @@ import { assertExists } from '@blocksuite/global/utils'; | ||
} | ||
const items = { | ||
'text/plain': '', | ||
'text/html': '', | ||
'image/png': '', | ||
}; | ||
await Promise.all(Array.from(this._adapterMap.keys()).map(async (type) => { | ||
const item = await this._getClipboardItem(slice, type); | ||
if (typeof item === 'string') { | ||
items[type] = item; | ||
} | ||
})); | ||
const text = items['text/plain']; | ||
const innerHTML = items['text/html']; | ||
const png = items['image/png']; | ||
delete items['text/plain']; | ||
delete items['text/html']; | ||
delete items['image/png']; | ||
const snapshot = lz.compressToEncodedURIComponent(JSON.stringify(items)); | ||
const html = `<div data-blocksuite-snapshot=${snapshot}>${innerHTML}</div>`; | ||
const htmlBlob = new Blob([html], { | ||
type: 'text/html', | ||
const adapterKeys = Array.from(this._adapterMap.keys()); | ||
await this.writeToClipboard(async (_items) => { | ||
const items = { ..._items }; | ||
await Promise.all(adapterKeys.map(async (type) => { | ||
const item = await this._getClipboardItem(slice, type); | ||
if (typeof item === 'string') { | ||
items[type] = item; | ||
} | ||
})); | ||
return items; | ||
}); | ||
const clipboardItems = { | ||
'text/html': htmlBlob, | ||
}; | ||
if (text.length > 0) { | ||
const textBlob = new Blob([text], { | ||
type: 'text/plain', | ||
}); | ||
clipboardItems['text/plain'] = textBlob; | ||
} | ||
if (png.length > 0) { | ||
const pngBlob = new Blob([png], { | ||
type: 'image/png', | ||
}); | ||
clipboardItems['image/png'] = pngBlob; | ||
} | ||
await navigator.clipboard.write([new ClipboardItem(clipboardItems)]); | ||
}; | ||
this.paste = async (event, page, parent, index) => { | ||
const data = event.clipboardData; | ||
if (!data) { | ||
if (!data) | ||
return; | ||
} | ||
const items = data.getData('text/html'); | ||
try { | ||
const domParser = new DOMParser(); | ||
const doc = domParser.parseFromString(items, 'text/html'); | ||
const dom = doc.querySelector('[data-blocksuite-snapshot]'); | ||
assertExists(dom); | ||
const json = JSON.parse(lz.decompressFromEncodedURIComponent(dom.dataset.blocksuiteSnapshot)); | ||
const json = this.readFromClipboard(data); | ||
const slice = await this._getSnapshotByPriority(type => json[type], page, parent, index); | ||
@@ -104,2 +71,6 @@ assertExists(slice); | ||
}; | ||
this.pasteBlockSnapshot = async (snapshot, page, parent, index) => { | ||
const job = this._getJob(); | ||
return job.snapshotToBlock(snapshot, page, parent, index); | ||
}; | ||
} | ||
@@ -120,3 +91,47 @@ _getJob() { | ||
} | ||
async writeToClipboard(updateItems) { | ||
const _items = { | ||
'text/plain': '', | ||
'text/html': '', | ||
'image/png': '', | ||
}; | ||
const items = await updateItems(_items); | ||
const text = items['text/plain']; | ||
const innerHTML = items['text/html']; | ||
const png = items['image/png']; | ||
delete items['text/plain']; | ||
delete items['text/html']; | ||
delete items['image/png']; | ||
const snapshot = lz.compressToEncodedURIComponent(JSON.stringify(items)); | ||
const html = `<div data-blocksuite-snapshot=${snapshot}>${innerHTML}</div>`; | ||
const htmlBlob = new Blob([html], { | ||
type: 'text/html', | ||
}); | ||
const clipboardItems = { | ||
'text/html': htmlBlob, | ||
}; | ||
if (text.length > 0) { | ||
const textBlob = new Blob([text], { | ||
type: 'text/plain', | ||
}); | ||
clipboardItems['text/plain'] = textBlob; | ||
} | ||
if (!(png instanceof Blob) && png.length > 0) { | ||
const pngBlob = new Blob([png], { | ||
type: 'image/png', | ||
}); | ||
clipboardItems['image/png'] = pngBlob; | ||
} | ||
await navigator.clipboard.write([new ClipboardItem(clipboardItems)]); | ||
} | ||
readFromClipboard(clipboardData) { | ||
const items = clipboardData.getData('text/html'); | ||
const domParser = new DOMParser(); | ||
const doc = domParser.parseFromString(items, 'text/html'); | ||
const dom = doc.querySelector('[data-blocksuite-snapshot]'); | ||
assertExists(dom); | ||
const json = JSON.parse(lz.decompressFromEncodedURIComponent(dom.dataset.blocksuiteSnapshot)); | ||
return json; | ||
} | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@blocksuite/block-std", | ||
"version": "0.0.0-20231018100009-361737d3-nightly", | ||
"version": "0.0.0-20231018165053-6a3da92f-nightly", | ||
"description": "Std for blocksuite blocks", | ||
@@ -11,3 +11,3 @@ "type": "module", | ||
"peerDependencies": { | ||
"@blocksuite/store": "0.0.0-20231018100009-361737d3-nightly" | ||
"@blocksuite/store": "0.0.0-20231018165053-6a3da92f-nightly" | ||
}, | ||
@@ -18,6 +18,6 @@ "dependencies": { | ||
"zod": "^3.22.2", | ||
"@blocksuite/global": "0.0.0-20231018100009-361737d3-nightly" | ||
"@blocksuite/global": "0.0.0-20231018165053-6a3da92f-nightly" | ||
}, | ||
"devDependencies": { | ||
"@blocksuite/store": "0.0.0-20231018100009-361737d3-nightly" | ||
"@blocksuite/store": "0.0.0-20231018165053-6a3da92f-nightly" | ||
}, | ||
@@ -24,0 +24,0 @@ "exports": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
176289
2217
+ Added@blocksuite/global@0.0.0-20231018165053-6a3da92f-nightly(transitive)
+ Added@blocksuite/store@0.0.0-20231018165053-6a3da92f-nightly(transitive)
+ Added@blocksuite/virgo@0.0.0-20231018165053-6a3da92f-nightly(transitive)
- Removed@blocksuite/global@0.0.0-20231018100009-361737d3-nightly(transitive)
- Removed@blocksuite/store@0.0.0-20231018100009-361737d3-nightly(transitive)
- Removed@blocksuite/virgo@0.0.0-20231018100009-361737d3-nightly(transitive)
Updated@blocksuite/global@0.0.0-20231018165053-6a3da92f-nightly