Using Document Sandbox Constants
Document Sandbox constants provide type-safe ways to interact with the Document APIs when creating and manipulating content in Adobe Express documents. This guide covers the most common patterns for document-side development.
Why Use Document Constants?
Document constants ensure consistency when working with document elements, styling, and content creation. They provide type safety, IDE autocomplete, and prevent errors from typos in string values.
For complete technical specifications of all document constants, see the Document APIs Constants Reference.
Quick Start
Document constants are available through the constants export in the document sandbox environment:
Copied to your clipboardimport { constants } from "express-document-sdk";// Access constants through the constants objectconst fillType = constants.FillType.color;const blendMode = constants.BlendMode.normal;const textAlignment = constants.TextAlignment.center;
Important: Document constants are only available in the document sandbox environment (code.js
). They cannot be accessed from the iframe UI environment.
Most Common Use Cases
Fill and Stroke Properties
Copied to your clipboardimport { editor, constants } from "express-document-sdk";// Creating solid color fillsconst rectangle = editor.createRectangle();rectangle.fill = {type: constants.FillType.color,color: { red: 1, green: 0, blue: 0, alpha: 1 }};// Adding strokesrectangle.stroke = {type: constants.StrokeType.color,color: { red: 0, green: 0, blue: 1, alpha: 1 },width: 2,position: constants.StrokePosition.inside};
Available Fill Types:
FillType.color
- Solid color fills (only available type)
Available Stroke Types:
StrokeType.color
- Solid color stroke (only available type)
Available Stroke Positions:
StrokePosition.center
- Stroke centered on edgeStrokePosition.inside
- Stroke inside the shapeStrokePosition.outside
- Stroke outside the shape
Text Alignment and Styling
Copied to your clipboardimport { editor, constants } from "express-document-sdk";// Creating and styling textconst textNode = editor.createText();textNode.text = "Hello World";// Set text alignmenttextNode.textAlignment = constants.TextAlignment.center;// Set text script styletextNode.textScriptStyle = constants.TextScriptStyle.none;
Available Text Alignments:
TextAlignment.left
- Left-aligned textTextAlignment.center
- Center-aligned textTextAlignment.right
- Right-aligned textTextAlignment.justifyLeft
- Left-justified text
Available Text Script Styles:
TextScriptStyle.none
- Normal text (standard baseline)TextScriptStyle.superscript
- Superscript textTextScriptStyle.subscript
- Subscript text
Blend Modes
Copied to your clipboardimport { editor, constants } from "express-document-sdk";// Apply blend modes to visual elementsconst shape = editor.createEllipse();shape.blendMode = constants.BlendMode.multiply;
Common Blend Modes:
BlendMode.normal
- Normal blendingBlendMode.multiply
- Multiply blendingBlendMode.screen
- Screen blendingBlendMode.overlay
- Overlay blendingBlendMode.softLight
- Soft light blendingBlendMode.hardLight
- Hard light blending
Scene Node Types
Copied to your clipboardimport { editor, constants } from "express-document-sdk";// Check node types when traversing the documenteditor.context.selection.forEach(node => {switch (node.type) {case constants.SceneNodeType.rectangle:console.log("Selected rectangle");break;case constants.SceneNodeType.ellipse:console.log("Selected ellipse");break;case constants.SceneNodeType.imageRectangle:console.log("Selected image");break;case constants.SceneNodeType.unknownMediaRectangle:console.log("Selected media");break;}});
Available Scene Node Types:
SceneNodeType.rectangle
- Rectangle shapesSceneNodeType.ellipse
- Ellipse/circle shapesSceneNodeType.text
- Text elements (not available in current types)SceneNodeType.imageRectangle
- Image elementsSceneNodeType.unknownMediaRectangle
- Unknown media elementsSceneNodeType.line
- Line elementsSceneNodeType.path
- Custom path shapesSceneNodeType.group
- Grouped elements
Import Patterns
Document Sandbox Environment
Copied to your clipboard// In code.js - import constants from express-document-sdkimport { editor, constants, colorUtils } from "express-document-sdk";// Use constants through the constants objectconst newRect = editor.createRectangle();newRect.fill = {type: constants.FillType.color,color: { red: 0.5, green: 0.5, blue: 0.5, alpha: 1 }};
Using Colors with Constants
When working with fill and stroke properties, you'll need to provide Color objects. Use the colorUtils
utility from express-document-sdk
to create colors:
Copied to your clipboardimport { editor, constants, colorUtils } from "express-document-sdk";// Create colors and use with constantsconst rectangle = editor.createRectangle();rectangle.fill = {type: constants.FillType.color, // Constant for type safetycolor: colorUtils.fromHex("#FF0000") // Color from hex string};rectangle.stroke = {type: constants.StrokeType.color,color: colorUtils.fromRGB(0, 0, 1), // Color from RGB valueswidth: 2,position: constants.StrokePosition.inside};
Working with Colors: For comprehensive color creation, conversion, and application examples, see the Use Color Guide.
Communication with UI
Copied to your clipboard// In code.js - expose constants to UI if neededimport { constants } from "express-document-sdk";import addOnSandboxSdk from "add-on-sdk-document-sandbox";addOnSandboxSdk.instance.runtime.exposeApi({getAvailableBlendModes() {return {normal: constants.BlendMode.normal,multiply: constants.BlendMode.multiply,screen: constants.BlendMode.screen,overlay: constants.BlendMode.overlay};}});
Common Patterns
Creating Styled Shapes
Copied to your clipboardimport { editor, constants } from "express-document-sdk";function createStyledRectangle(color, strokeColor) {const rect = editor.createRectangle();// Set fillrect.fill = {type: constants.FillType.color,color: color};// Set strokerect.stroke = {type: constants.StrokeType.color,color: strokeColor,width: 2,position: constants.StrokePosition.inside};// Set blend moderect.blendMode = constants.BlendMode.normal;return rect;}
Text Formatting
Copied to your clipboardimport { editor, constants } from "express-document-sdk";function createFormattedText(content, alignment = constants.TextAlignment.left) {const textNode = editor.createText();textNode.text = content;textNode.textAlignment = alignment;// Apply character stylingconst characterStyles = {fontSize: 24,fontFamily: "Arial",textScriptStyle: constants.TextScriptStyle.none};textNode.setRangeCharacterStyles(0, content.length, characterStyles);return textNode;}
Node Type Checking
Copied to your clipboardimport { editor, constants } from "express-document-sdk";function processSelectedNodes() {const selection = editor.context.selection;selection.forEach(node => {// Type-safe node processingif (node.type === constants.SceneNodeType.rectangle || node.type === constants.SceneNodeType.ellipse) {// Handle shapesif (node.fill?.type === constants.FillType.color) {console.log("Shape has color fill");}} else if (node.type === constants.SceneNodeType.imageRectangle) {// Handle imagesconsole.log("Processing image node");}});}
Common Pitfalls
Environment Confusion
Copied to your clipboard// ❌ Wrong - Document constants not available in UI// In index.html/index.jsimport addOnUISdk from "https://express.adobe.com/static/add-on-sdk/sdk.js";const fillType = addOnUISdk.constants.FillType.color; // Error: FillType is not defined// ✅ Correct - Use in document sandbox only// In code.jsimport { constants } from "express-document-sdk";const fillType = constants.FillType.color; // Works correctly
Missing Type Checks
Copied to your clipboardimport { constants } from "express-document-sdk";// ❌ Risky - assuming node typefunction changeColor(node, color) {node.fill = { type: constants.FillType.color, color }; // May fail on non-fillable nodes}// ✅ Safe - check node type firstfunction changeColor(node, color) {if (node.type === constants.SceneNodeType.rectangle || node.type === constants.SceneNodeType.ellipse) {node.fill = { type: constants.FillType.color, color };}}
Best Practices
- Always use constants instead of string literals for better type safety
- Check node types before applying properties that may not be available
- Use meaningful variable names when working with complex styling
- Group related constants for better code organization
- Document your styling functions with the constants they expect
FAQs
Q: Why can't I access document constants from the UI?
A: Document constants are only available in the Document Sandbox (code.js
) for security isolation. UI and Document environments are separate - use communication APIs to pass data between them.
Q: How do I import document constants?
A: Use import { constants } from "express-document-sdk"
in your code.js
file. Access them as constants.FillType.color
, constants.BlendMode.normal
, etc.
Q: What's the difference between UI SDK constants and Document Sandbox constants?
A: UI SDK constants are for iframe operations (dialogs, exports, events). Document constants are for content creation (fills, strokes, text alignment, node types).
Q: Can I use FillType.gradient
or other fill types?
A: Currently, only FillType.color
is available. Adobe Express may add more fill types in future releases.
Q: How do I check if a node supports fills or strokes?
A: Check the node type first: if (node.type === constants.SceneNodeType.rectangle || node.type === constants.SceneNodeType.ellipse)
before applying fill/stroke properties.
Q: Why does my blend mode not work?
A: Ensure you're applying blend modes to visual nodes and using valid constants like constants.BlendMode.multiply
. Not all nodes support all blend modes.
Q: How do I pass constants from Document Sandbox to UI?
A: Expose them through communication APIs: runtime.exposeApi({ getBlendModes: () => ({ normal: constants.BlendMode.normal }) })
.
Q: What constants should I use for text alignment?
A: Use constants.TextAlignment.left
, constants.TextAlignment.center
, constants.TextAlignment.right
, or constants.TextAlignment.justifyLeft
.
Q: How do I create colors for use with constants?
A: Use colorUtils.fromRGB(r, g, b, alpha)
or colorUtils.fromHex("#RRGGBB")
to create Color objects. Import with: import { colorUtils } from "express-document-sdk"
. See the Use Color Guide for complete examples.
Related Documentation
- Document APIs Reference
- Document APIs Constants
- ColorUtils Reference
- Use Color Guide - Comprehensive color workflow examples
- Add-on UI SDK Constants
- Developer Terminology