react-design-editor

repository·master·Indexed 23 days ago

https://github.com/salgum1114/react-design-editor

A React module for building design tools, featuring an Image Editor and a Business Process Modelling (BPM) tool for workflows and flowcharts. Built with React.js, ant.design, and fabric.js, it provides a core Canvas component and a Handler API for manipulating objects, managing undo/redo history, and exporting/importing JSON data. Supports specialized editor modes including imagemap, workflow, hexgrid, and fiber.

Tokens
8K
Snippets
8
Records
54
Agent score
82%

What's inside react-design-editor

  1. Understand Ant Design v6 and React compatibility requirements

    master

    The project currently targets antd@6. Because antd@6 requires React >= 18, the package metadata has been updated to support React 18.x and 19.x.

    Note: If your environment requires React 16 or 17, this version of the project is incompatible as it cannot support antd@6 while maintaining those older React versions.

  2. Use the antd compatibility layer for legacy component patterns

    master

    The project includes a compatibility layer located at src/antd-compat.tsx (aliased in vite.config.ts) to allow legacy Ant Design v3 patterns to function with antd@6.

    If you are extending the codebase or maintaining existing components, be aware that the following mappings are handled by the compatibility layer:

    Legacy PatternModern Mapping (antd@6)
    Form.create()Modern Form internals
    getFieldDecorator()Form.Item
    validateFields, resetFields, getFieldsErrorNewer Form API
    Modal visibleopen
    Modal bodyStyle / maskStylesemantic styles
    Tooltip / Popover visibleopen (or equivalent)
    Tabs.TabPane childrenitems prop
    Tabs tabPositiontabPlacement
  3. Use the Canvas component

    master

    The Canvas component is the core of the editor. You can control it using a ref of type CanvasInstance. The canvasRef.current.handler provides an API to manipulate the canvas, such as adding objects, exporting/importing JSON, and managing undo/redo history.

    Key Props:

    • ref: A CanvasInstance ref to access the handler.
    • style: Must provide a visible height (especially for responsive sizing).
    • canvasOption: Configuration for the canvas (e.g., backgroundColor).
    • workareaOption: Configuration for the drawing area (e.g., width, height, backgroundColor).
    • canvasActions: Enables features like clipboard and transaction (undo/redo).
    • onSelect: Callback triggered when an object is selected.
    import { useRef } from 'react';
    import { Canvas, type CanvasInstance } from 'react-design-editor';
    import 'react-design-editor/react-design-editor.css';
    
    export default function DesignCanvas() {
    	const canvasRef = useRef<CanvasInstance | null>(null);
    
    	const addRectangle = () => {
    		canvasRef.current?.handler.add({
    			type: 'rect',
    			name: 'Rectangle',
    			width: 160,
    			height: 90,
    			fill: '#5ee0bd',
    			rx: 8,
    			ry: 8,
    		});
    	};
    
    	const saveCanvas = () => {
    		const objects = canvasRef.current?.handler.exportJSON() ?? [];
    		localStorage.setItem('design', JSON.stringify(objects));
    	};
    
    	const loadCanvas = async () => {
    		const saved = localStorage.getItem('design');
    		if (saved) {
    			await canvasRef.current?.handler.importJSON(JSON.parse(saved));
    		}
    	};
    
    	return (
    		<div>
    			<button type="button" onClick={addRectangle}>Add rectangle</button>
    			<button type="button" onClick={saveCanvas}>Save</button>
    			<button type="button" onClick={loadCanvas}>Load</button>
    
    			<Canvas
    				ref={canvasRef}
    				style={{ width: '100%', height: 600 }}
    				canvasOption={{ backgroundColor: '#f4f7f9' }}
    				workareaOption={
    					{
    					width: 800,
    						height: 500,
    						backgroundColor: '#ffffff',
    					},
    				}
    				canvasActions={
    					{
    						clipboard: true,
    							transaction: true,
    					},
    				}
    				onSelect={object => {
    					console.log('Selected object:', object);
    				}},
    			/>
    		</div>
    	);
    }
  4. Install react-design-editor

    master

    Install the package using npm or yarn, and ensure you import the bundled CSS file in your application entry point to apply the editor's styles.

    npm install react-design-editor
    # or
    yarn add react-design-editor
    import 'react-design-editor/react-design-editor.css';
  5. Understand the ExtendedFont data structure

    master

    When using getFontsExtended() or getFontsExtendedSync(), the library returns an array of ExtendedFont objects. This structure groups multiple font files (sub-families) under a single family name.

    An ExtendedFont object contains:

    • family: The primary font family name.
    • systemFont: A boolean indicating if the font is a system-installed font.
    • subFamilies: An array of strings representing the different sub-families (e.g., ['Regular', 'Bold', 'Italic']).
    • files: A mapping where keys are sub-family names and values are the absolute file paths.
    • postscriptNames: A mapping where keys are sub-family names and values are the PostScript names.
  6. Manipulate the canvas via CanvasInstance handler

    master

    Once you have a reference to the Canvas component via useRef<CanvasInstance>, you can use canvasRef.current.handler to perform the following tasks:

    • Add objects: Use .add({ type, ...options }) to insert elements like 'rect'.
    • Export data: Use .exportJSON() to get a JSON representation of the canvas objects.
    • Import data: Use .importJSON(data) to load previously saved JSON data.
    • Undo/Redo: Controlled via the transaction: true option in canvasActions.
    • Clipboard: Controlled via the clipboard: true option in canvasActions.
  7. Configure SystemFontsOptions

    master

    The SystemFontsOptions object allows you to control how the SystemFonts class scans for files and filters results.

    KeyTypeDefaultDescription
    ignoreSystemFontsbooleanfalseIf true, only fonts located in customDirs are returned.
    customDirsstring[][]An array of folder paths to be scanned for fonts in addition to standard system paths.
  8. Configure Blend Color Filter

    master

    The BlendColorFilter allows you to blend a specific color with the image using various modes.

    Options:

    • color (string): The color to blend (e.g., #ff0000). Defaults to #000000.
    • mode (string): The blend mode. Supported values: 'multiply', 'add', 'difference', 'screen', 'subtract', 'darken', 'lighten', 'overlay', 'exclusion', 'tint'.
    • alpha (number): The opacity of the blend.