Skip to content

AI-native design editor. Open-source Figma alternative.

Compress OpenPencil clipboard data with deflate, put lossless format first

Danila Poyarkov9771917d99e4
diff --git a/packages/core/src/clipboard.ts b/packages/core/src/clipboard.ts
index 34a6de196..52f552274 100644
--- a/packages/core/src/clipboard.ts
+++ b/packages/core/src/clipboard.ts
@@ -354,7 +354,14 @@ export function parseOpenPencilClipboard(
if (!match) return null
try {
- const decoded = JSON.parse(new TextDecoder().decode(Uint8Array.fromBase64(match[1])))
+ const raw = Uint8Array.fromBase64(match[1])
+ let bytes: Uint8Array
+ try {
+ bytes = inflateSync(raw)
+ } catch {
+ bytes = raw
+ }
+ const decoded = JSON.parse(new TextDecoder().decode(bytes))
if (decoded.format === 'openpencil/v1' && Array.isArray(decoded.nodes)) {
restoreTextPictures(decoded.nodes)
const images = new Map<string, Uint8Array>()
@@ -417,7 +424,8 @@ export function buildOpenPencilClipboardHTML(
nodes: nodeTree,
images
}
- return `<!--(openpencil)${new TextEncoder().encode(JSON.stringify(data)).toBase64()}(/openpencil)-->`
+ const compressed = deflateSync(new TextEncoder().encode(JSON.stringify(data)))
+ return `<!--(openpencil)${compressed.toBase64()}(/openpencil)-->`
}
function collectNodeTree(
diff --git a/src/stores/editor.ts b/src/stores/editor.ts
index 468d96330..56faddeef 100644
--- a/src/stores/editor.ts
+++ b/src/stores/editor.ts
@@ -1929,7 +1929,7 @@ export function createEditorStore() {
const internalHtml = buildOpenPencilClipboardHTML(nodes, graph, textPicBuilder)
const figmaHtml = buildFigmaClipboardHTML(nodes, graph)
- const html = figmaHtml ? figmaHtml + internalHtml : internalHtml
+ const html = figmaHtml ? internalHtml + figmaHtml : internalHtml
clipboardData.setData('text/html', html)
clipboardData.setData('text/plain', names)
}