Donner SVG Editor & Engine
SVG-native editor and embeddable SVG2 + CSS3 engine in C++20, with GPU (WebGPU) and compact CPU renderers, built for correctness, security, and performance.
Loading...
Searching...
No Matches
ShapeClipboardCommands.h File Reference

Pure shape-clipboard command helpers, decoupled from ImGui and from the editor shell so they can be unit-tested headlessly (see donner/editor/tests/ShapeClipboard_tests.cc). More...

#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "donner/editor/ShapeClipboardPayload.h"
#include "donner/svg/SVGDocument.h"
#include "donner/svg/SVGElement.h"
Include dependency graph for ShapeClipboardCommands.h:

Classes

struct  donner::editor::PreparePasteResult
 Result of preparing a paste. More...

Namespaces

namespace  donner
 Top-level Donner namespace, which is split into different sub-namespaces such as donner::svg and donner::css.

Enumerations

enum class  donner::editor::PastePlacement : std::uint8_t {
  EndOfRootOffset ,
  InFrontNoOffset
}
 Where a paste should insert its fragment. More...

Functions

std::optional< ShapeClipboardPayloaddonner::editor::copySelectionToPayload (const svg::SVGDocument &document, const std::vector< svg::SVGElement > &selection)
 Build a clipboard payload from the given selection (in paint order).
PreparePasteResult donner::editor::preparePaste (const svg::SVGDocument &document, const ShapeClipboardPayload &payload, PastePlacement placement, const std::optional< svg::SVGElement > &selectedGroup=std::nullopt)
 Prepare a paste of payload into document.

Detailed Description

Pure shape-clipboard command helpers, decoupled from ImGui and from the editor shell so they can be unit-tested headlessly (see donner/editor/tests/ShapeClipboard_tests.cc).

These functions implement the structural core of Cut/Copy/Paste/Paste-in-Front:

  • copySelectionToPayload serializes the selected element subtrees to a ShapeClipboardPayload. Each subtree's SVG text comes from the document's own source store substring (SVGDocument::source() + the element's XML node source range), which keeps the copied bytes byte-for-byte identical to what the user authored.
  • preparePaste parses a payload, detects id collisions against the current document, deterministically renames colliding ids (_pasted, _pasted2, ...), rewrites internal href / xlink:href / url(#id) references, and produces the merged full-document SVG source plus the ids that should be selected after the paste. It fails (returning an error, leaving its inputs untouched) when a reference cannot be repaired safely.

The editor wires the merged source through EditorCommand::Kind::PasteShapes / Kind::CutShapes so the resulting reparse is a single undo step that keeps the source pane coherent.

Enumeration Type Documentation

◆ PastePlacement

enum class donner::editor::PastePlacement : std::uint8_t
strong

Where a paste should insert its fragment.

Enumerator
EndOfRootOffset 

Default paste: append inside the root <svg> (or selected group) and offset the fragment by translate(20,20) so the new shapes are visible.

InFrontNoOffset 

Paste-in-Front: no offset; insert above the source elements in paint order when they still exist, otherwise at the end of the root.

Function Documentation

◆ copySelectionToPayload()

std::optional< ShapeClipboardPayload > donner::editor::copySelectionToPayload ( const svg::SVGDocument & document,
const std::vector< svg::SVGElement > & selection )
nodiscard

Build a clipboard payload from the given selection (in paint order).

Serializes each selected element subtree from the document source store. For in-editor elements with no source mapping, the element is skipped (a future temporary-doc round-trip can fill this gap); if no element can be serialized, returns std::nullopt.

Parameters
documentSource document (provides source() and bounds).
selectionSelected elements, in the order they should be serialized.
Returns
Payload, or std::nullopt if the selection is empty / unserializable.

◆ preparePaste()

PreparePasteResult donner::editor::preparePaste ( const svg::SVGDocument & document,
const ShapeClipboardPayload & payload,
PastePlacement placement,
const std::optional< svg::SVGElement > & selectedGroup = std::nullopt )
nodiscard

Prepare a paste of payload into document.

Does not mutate document; it computes the merged source string the editor should reparse. See file comment for id/reference repair semantics.

Parameters
documentDestination document.
payloadParsed clipboard payload.
placementDefault-offset vs. in-front placement.
selectedGroupOptional single selected group/svg container to paste into instead of the root (default paste only).