Skip to content

cf240 [Grida Canvas] Implement cross-window clipboard paste #372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

softmarshmallow
Copy link
Member

@softmarshmallow softmarshmallow commented May 30, 2025

Implemented IO Clipboard, for cross document, cross editor, cross window copy-paste of grida documents

day-240-grida-canvas-io-cross-window-clipboard.mp4

use https://madebyevan.com/clipboard-test/ for testing payload

Limitations
This does not handle copy / paste of assets.

Summary

  • sync clipboard data to system clipboard
  • parse text/html clipboard payload on paste events
  • insert copied nodes using new IDs when pasting across windows
  • centralize clipboard encode/decode logic with custom data attribute
  • add unique payload_id to clipboard state

Testing

  • pnpm turbo test

Summary by CodeRabbit

  • New Features
    • Added support for copying and pasting rich clipboard data with unique identifiers for reliable content transfer.
    • Clipboard data is now encoded and decoded as HTML, enhancing compatibility and robustness across browsers.
  • Bug Fixes
    • Improved handling of pasted HTML content, ensuring accurate extraction and insertion of clipboard data even when wrapped by browser-added tags.
  • Tests
    • Introduced comprehensive tests verifying clipboard encoding and decoding, including edge cases and error handling.
  • Chores
    • Updated dependencies and package scripts to support new clipboard functionality and testing.

Copy link

codesandbox bot commented May 30, 2025

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

Copy link

coderabbitai bot commented May 30, 2025

Walkthrough

The changes implement robust HTML clipboard support for Grida's canvas editor. Clipboard payloads are now encoded as base64 JSON within custom HTML attributes and decoded on paste, allowing for accurate transfer of scene data. This includes updates to the clipboard state structure, reducer logic, new encoding/decoding utilities, tests, and dependency updates.

Changes

File(s) Change Summary
editor/grida-canvas-react/provider.tsx Enhanced paste/copy logic to handle HTML clipboard data with new encode/decode utilities and payload ID checks.
editor/grida-canvas/index.ts Added payload_id property to user_clipboard in IEditorUserClipboardState.
editor/grida-canvas/reducers/document.reducer.ts Added payload_id (UUID) to clipboard state on copy/cut actions.
packages/grida-canvas-io/index.ts Introduced io.clipboard namespace with ClipboardPayload interface and encode/decode functions.
packages/grida-canvas-io/tests/clipboard.test.ts Added tests for clipboard HTML encoding/decoding, including edge and error cases.
editor/package.json, packages/grida-canvas-io/package.json Updated fast-xml-parser dependency; added test script and Jest config for @grida/io.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant EditorUI
    participant Clipboard
    participant IOClipboard

    User->>EditorUI: Copy/Cut
    EditorUI->>IOClipboard: encodeClipboardHtml(payload)
    IOClipboard-->>EditorUI: HTML string with base64 data
    EditorUI->>Clipboard: Write HTML string

    User->>EditorUI: Paste
    EditorUI->>Clipboard: Read clipboard
    Clipboard-->>EditorUI: HTML string
    EditorUI->>IOClipboard: decodeClipboardHtml(html)
    IOClipboard-->>EditorUI: ClipboardPayload or null
    EditorUI->>EditorUI: Insert scene(s) or fallback to text
Loading

Poem

🐇
A hop, a skip, a clipboard leap,
Now Grida’s scenes are safe to keep!
With base64 and HTML,
Our copy-paste works oh-so-well.
Tests abound and bugs retreat—
This update makes our tool complete!
🌱✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fcebde1 and c728d6d.

📒 Files selected for processing (2)
  • editor/grida-canvas/reducers/document.reducer.ts (2 hunks)
  • packages/grida-canvas-io/__tests__/clipboard.test.ts (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • editor/grida-canvas/reducers/document.reducer.ts
  • packages/grida-canvas-io/tests/clipboard.test.ts
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@vercel vercel bot temporarily deployed to Preview – docs May 30, 2025 14:14 Inactive
Copy link

vercel bot commented May 30, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
grida ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 30, 2025 5:21pm
6 Skipped Deployments
Name Status Preview Comments Updated (UTC)
code ⬜️ Ignored (Inspect) May 30, 2025 5:21pm
legacy ⬜️ Ignored (Inspect) May 30, 2025 5:21pm
backgrounds ⬜️ Skipped (Inspect) May 30, 2025 5:21pm
blog ⬜️ Skipped (Inspect) May 30, 2025 5:21pm
docs ⬜️ Skipped (Inspect) May 30, 2025 5:21pm
viewer ⬜️ Skipped (Inspect) May 30, 2025 5:21pm

@vercel vercel bot temporarily deployed to Preview – backgrounds May 30, 2025 14:14 Inactive
@vercel vercel bot temporarily deployed to Preview – blog May 30, 2025 14:14 Inactive
@vercel vercel bot temporarily deployed to Preview – viewer May 30, 2025 14:14 Inactive
…able-cross-window-copy-paste-functionality
@vercel vercel bot temporarily deployed to Preview – blog May 30, 2025 14:35 Inactive
@vercel vercel bot temporarily deployed to Preview – docs May 30, 2025 14:35 Inactive
@vercel vercel bot temporarily deployed to Preview – backgrounds May 30, 2025 14:35 Inactive
@vercel vercel bot temporarily deployed to Preview – viewer May 30, 2025 14:35 Inactive
@softmarshmallow softmarshmallow changed the title Implement cross-window clipboard paste [Grida Canvas] Implement cross-window clipboard paste May 30, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9a7dd02 and fcebde1.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (7)
  • editor/grida-canvas-react/provider.tsx (6 hunks)
  • editor/grida-canvas/index.ts (1 hunks)
  • editor/grida-canvas/reducers/document.reducer.ts (1 hunks)
  • editor/package.json (1 hunks)
  • packages/grida-canvas-io/__tests__/clipboard.test.ts (1 hunks)
  • packages/grida-canvas-io/index.ts (1 hunks)
  • packages/grida-canvas-io/package.json (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
packages/grida-canvas-io/__tests__/clipboard.test.ts (1)
packages/grida-canvas-io/index.ts (1)
  • ClipboardPayload (12-16)
editor/grida-canvas-react/provider.tsx (2)
editor/grida-canvas-react/use-editor.tsx (1)
  • useEditorState (35-47)
editor/grida-canvas/reducers/tools/id.ts (1)
  • nid (10-15)
🔇 Additional comments (8)
editor/package.json (1)

146-146:

❓ Verification inconclusive

Verify major version update compatibility and security.

The fast-xml-parser dependency is being updated from ^4.4.0 to ^5.2.3, which is a major version bump that may introduce breaking changes. Ensure this update is compatible with existing code and check for any security advisories.


🏁 Script executed:

#!/bin/bash
# Description: Check for security advisories and compatibility issues with fast-xml-parser v5.2.3

# Check if version exists and get release notes
curl -s https://api.github.com/repos/NaturalIntelligence/fast-xml-parser/releases | jq '.[] | select(.tag_name | contains("v5.2.3") or contains("5.2.3")) | {tag_name, published_at, body}'

# Check for security advisories
gh api graphql -f query='
{
  securityVulnerabilities(first: 10, ecosystem: NPM, package: "fast-xml-parser") {
    nodes {
      advisory {
        summary
        severity
        publishedAt
      }
      vulnerableVersionRange
      firstPatchedVersion {
        identifier
      }
    }
  }
}'

# Check changelog for breaking changes between v4 and v5
curl -s https://api.github.com/repos/NaturalIntelligence/fast-xml-parser/compare/v4.4.0...v5.2.3 | jq '.status'

Length of output: 1617


Review [email protected] bump for compatibility and security.

  • Location: editor/package.json (line 146, bumped from ^4.4.0 to ^5.2.3)
  • Release status: 5.2.3 is a published npm version and is “ahead” of 4.4.0.
  • Security: No known advisories affect versions ≥ 4.4.1; all vulnerabilities were patched before v5.
  • Action required: v5 contains breaking changes—please review the migration guide (https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/MIGRATION.md) and rerun your XML/HTML clipboard parsing tests to confirm existing code still works as expected.
packages/grida-canvas-io/package.json (1)

6-21: LGTM! Clean package.json updates for testing infrastructure.

The changes properly add:

  • Test script for Jest execution
  • fast-xml-parser dependency consistent with editor package
  • Minimal Jest configuration with ts-jest preset for TypeScript support

The formatting and structure are correct.

editor/grida-canvas/index.ts (1)

326-327: LGTM! Well-documented interface extension for clipboard identification.

The addition of payload_id as a required string property is well-documented and serves a clear purpose for distinguishing clipboard contents across windows. This enables robust clipboard state management.

packages/grida-canvas-io/__tests__/clipboard.test.ts (1)

3-75: Well-structured test suite with comprehensive coverage.

The test suite effectively covers:

  • Basic encode/decode functionality
  • Browser HTML manipulation scenarios
  • Error handling for invalid inputs

Good job on the test implementation!

packages/grida-canvas-io/index.ts (2)

18-23: Solid implementation of clipboard HTML encoding.

The encoding correctly handles UTF-8 conversion and base64 encoding. The use of a custom data attribute with a unique prefix prevents conflicts.


51-73: Excellent implementation with comprehensive documentation and error handling.

The decode function:

  • Handles browser HTML manipulation gracefully
  • Provides clear JSDoc with examples
  • Returns null safely on errors
  • Efficiently decodes base64 data

Great work on the implementation!

editor/grida-canvas-react/provider.tsx (2)

1219-1243: Well-implemented clipboard paste handling with proper fallbacks.

The implementation correctly:

  • Decodes clipboard HTML data
  • Checks payload_id to distinguish between internal and external pastes
  • Creates new IDs for inserted nodes using the nid utility
  • Falls back to text insertion if decoding fails

Excellent integration!


1312-1325: Clean implementation of clipboard synchronization.

The updated implementation properly uses the new encoding function and handles potential errors gracefully.

@@ -94,6 +94,7 @@ export default function documentReducer<S extends editor.state.IEditorState>(
return produce(state, (draft) => {
// [copy]
draft.user_clipboard = {
payload_id: crypto.randomUUID(),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Verify browser compatibility for crypto.randomUUID().

The implementation correctly adds unique payload IDs to clipboard data. However, crypto.randomUUID() requires browser support and may not be available in all environments (e.g., older browsers, some Node.js contexts).

Consider checking browser compatibility or providing a fallback:

-          payload_id: crypto.randomUUID(),
+          payload_id: crypto.randomUUID?.() || `${Date.now()}-${Math.random()}`,

Let's verify the current browser support:


🏁 Script executed:

#!/bin/bash
# Description: Check crypto.randomUUID() usage in the codebase and verify it's consistently used

# Search for other uses of crypto.randomUUID in the codebase
rg -A 2 -B 2 "crypto\.randomUUID"

# Search for alternative UUID generation methods
rg -A 2 -B 2 "randomUUID|uuid|UUID"

Length of output: 68119


Add a fallback for crypto.randomUUID() using the existing uuid library

To avoid runtime errors in environments where crypto.randomUUID is unavailable (e.g. older Safari or non-Web Crypto contexts), fall back to the project’s uuid package:

• File: editor/grida-canvas/reducers/document.reducer.ts
Line: 97

// At top of file, alongside other imports
+ import { v4 as uuidv4 } from 'uuid';-          payload_id: crypto.randomUUID(),
+          payload_id: crypto.randomUUID?.() ?? uuidv4(),

This ensures a stable UUID in all supported runtimes.

🤖 Prompt for AI Agents
In editor/grida-canvas/reducers/document.reducer.ts at line 97, the use of
crypto.randomUUID() may cause runtime errors in environments lacking support.
Modify the code to check if crypto.randomUUID is available; if not, use the
existing uuid library's UUID generation function as a fallback. This ensures
consistent UUID generation across all supported environments.

const parser = new XMLParser({
ignoreAttributes: (key) => !key.startsWith(__data_grida_io_prefix),
attributeNamePrefix: "@",
unpairedTags: ["meta"],
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider adding more unpaired tags to XMLParser configuration.

Browsers might add other self-closing tags when manipulating clipboard content. Consider expanding the unpaired tags list to prevent parsing errors.

-          unpairedTags: ["meta"],
+          unpairedTags: ["meta", "link", "br", "hr", "img", "input", "area", "base", "col", "embed", "source", "track", "wbr"],
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
unpairedTags: ["meta"],
unpairedTags: [
"meta",
"link",
"br",
"hr",
"img",
"input",
"area",
"base",
"col",
"embed",
"source",
"track",
"wbr"
],
🤖 Prompt for AI Agents
In packages/grida-canvas-io/index.ts at line 56, the XMLParser configuration
currently includes only "meta" as an unpaired tag. To prevent parsing errors
caused by browsers adding other self-closing tags in clipboard content, expand
the unpairedTags array to include additional common self-closing tags such as
"img", "br", "hr", "input", and "link". Update the configuration to cover these
tags to improve robustness.

@vercel vercel bot temporarily deployed to Preview – backgrounds May 30, 2025 17:17 Inactive
@vercel vercel bot temporarily deployed to Preview – viewer May 30, 2025 17:17 Inactive
@vercel vercel bot temporarily deployed to Preview – docs May 30, 2025 17:17 Inactive
@vercel vercel bot temporarily deployed to Preview – blog May 30, 2025 17:17 Inactive
@softmarshmallow softmarshmallow merged commit f6b297c into main May 30, 2025
10 checks passed
@softmarshmallow softmarshmallow changed the title [Grida Canvas] Implement cross-window clipboard paste cf240 [Grida Canvas] Implement cross-window clipboard paste May 30, 2025
@coderabbitai coderabbitai bot mentioned this pull request May 31, 2025
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant