feat(editor): support connector dom renderer (#12505)

### TL;DR

Added DOM-based renderer for connector elements in the AFFiNE editor.

### What changed?

- Created a new DOM-based renderer for connector elements that uses SVG
for rendering
- Implemented `ConnectorDomRendererExtension` to register the DOM
renderer for connector elements
- Added support for rendering connector paths, endpoints (arrows,
triangles, circles, diamonds), stroke styles, and labels
- Registered the new DOM renderer extension in the connector view setup
- Added comprehensive tests to verify DOM rendering functionality

### How to test?

1. Enable the DOM renderer flag in the editor
2. Create connector elements between shapes or with fixed positions
3. Verify that connectors render correctly with different styles:
   - Try different stroke styles (solid, dashed)
   - Test various endpoint styles (Arrow, Triangle, Circle, Diamond)
   - Add text labels to connectors
4. Check that connectors update properly when connected elements move
5. Verify that connectors are removed when deleted

### Why make this change?

The DOM-based renderer provides an alternative to the Canvas-based
renderer, offering better accessibility and potentially improved
performance for certain use cases. This implementation allows connectors
to be rendered as SVG elements within the DOM, which can be more easily
inspected, styled with CSS, and interacted with by assistive
technologies.
This commit is contained in:
Yifeng Wang
2025-06-23 11:59:45 +08:00
committed by GitHub
parent 12fce1f21a
commit 76568bae9f
9 changed files with 786 additions and 15 deletions

View File

@@ -0,0 +1,73 @@
import { describe, expect, test } from 'vitest';
import { SVGPathBuilder, SVGShapeBuilder } from '../gfx/svg-path.js';
describe('SVGPathBuilder', () => {
test('should build a simple path', () => {
const pathBuilder = new SVGPathBuilder();
const result = pathBuilder.moveTo(10, 20).lineTo(30, 40).build();
expect(result).toBe('M 10 20 L 30 40');
});
test('should build a path with curves', () => {
const pathBuilder = new SVGPathBuilder();
const result = pathBuilder
.moveTo(0, 0)
.curveTo(10, 0, 10, 10, 20, 10)
.build();
expect(result).toBe('M 0 0 C 10 0 10 10 20 10');
});
test('should build a closed path', () => {
const pathBuilder = new SVGPathBuilder();
const result = pathBuilder
.moveTo(0, 0)
.lineTo(10, 0)
.lineTo(5, 10)
.closePath()
.build();
expect(result).toBe('M 0 0 L 10 0 L 5 10 Z');
});
test('should clear commands', () => {
const pathBuilder = new SVGPathBuilder();
pathBuilder.moveTo(10, 20).lineTo(30, 40);
pathBuilder.clear();
const result = pathBuilder.moveTo(0, 0).build();
expect(result).toBe('M 0 0');
});
});
describe('SVGShapeBuilder', () => {
test('should generate diamond polygon points', () => {
const result = SVGShapeBuilder.diamond(100, 80, 2);
expect(result).toBe('50,1 99,40 50,79 1,40');
});
test('should generate triangle polygon points', () => {
const result = SVGShapeBuilder.triangle(100, 80, 2);
expect(result).toBe('50,1 99,79 1,79');
});
test('should generate diamond path', () => {
const result = SVGShapeBuilder.diamondPath(100, 80, 2);
expect(result).toBe('M 50 1 L 99 40 L 50 79 L 1 40 Z');
});
test('should generate triangle path', () => {
const result = SVGShapeBuilder.trianglePath(100, 80, 2);
expect(result).toBe('M 50 1 L 99 79 L 1 79 Z');
});
test('should handle zero stroke width', () => {
const diamondResult = SVGShapeBuilder.diamond(100, 80, 0);
expect(diamondResult).toBe('50,0 100,40 50,80 0,40');
const triangleResult = SVGShapeBuilder.triangle(100, 80, 0);
expect(triangleResult).toBe('50,0 100,80 0,80');
});
});

View File

@@ -4,4 +4,5 @@ export * from './math.js';
export * from './model/index.js';
export * from './perfect-freehand/index.js';
export * from './polyline.js';
export * from './svg-path.js';
export * from './xywh.js';

View File

@@ -0,0 +1,160 @@
interface PathCommand {
command: string;
coordinates: number[];
}
/**
* A utility class for building SVG path strings using command-based API.
* Supports moveTo, lineTo, curveTo operations and can build complete path strings.
*/
export class SVGPathBuilder {
private commands: PathCommand[] = [];
/**
* Move to a specific point without drawing
*/
moveTo(x: number, y: number): this {
this.commands.push({
command: 'M',
coordinates: [x, y],
});
return this;
}
/**
* Draw a line to a specific point
*/
lineTo(x: number, y: number): this {
this.commands.push({
command: 'L',
coordinates: [x, y],
});
return this;
}
/**
* Draw a cubic Bézier curve
*/
curveTo(
cp1x: number,
cp1y: number,
cp2x: number,
cp2y: number,
x: number,
y: number
): this {
this.commands.push({
command: 'C',
coordinates: [cp1x, cp1y, cp2x, cp2y, x, y],
});
return this;
}
/**
* Close the current path
*/
closePath(): this {
this.commands.push({
command: 'Z',
coordinates: [],
});
return this;
}
/**
* Build the complete SVG path string
*/
build(): string {
const pathSegments = this.commands.map(cmd => {
const coords = cmd.coordinates.join(' ');
return coords ? `${cmd.command} ${coords}` : cmd.command;
});
return pathSegments.join(' ');
}
/**
* Clear all commands and reset the builder
*/
clear(): this {
this.commands = [];
return this;
}
}
/**
* Create SVG polygon points string for common shapes
*/
export class SVGShapeBuilder {
/**
* Generate diamond (rhombus) polygon points
*/
static diamond(
width: number,
height: number,
strokeWidth: number = 0
): string {
const halfStroke = strokeWidth / 2;
return [
`${width / 2},${halfStroke}`,
`${width - halfStroke},${height / 2}`,
`${width / 2},${height - halfStroke}`,
`${halfStroke},${height / 2}`,
].join(' ');
}
/**
* Generate triangle polygon points
*/
static triangle(
width: number,
height: number,
strokeWidth: number = 0
): string {
const halfStroke = strokeWidth / 2;
return [
`${width / 2},${halfStroke}`,
`${width - halfStroke},${height - halfStroke}`,
`${halfStroke},${height - halfStroke}`,
].join(' ');
}
/**
* Generate diamond path using SVGPathBuilder
*/
static diamondPath(
width: number,
height: number,
strokeWidth: number = 0
): string {
const halfStroke = strokeWidth / 2;
const pathBuilder = new SVGPathBuilder();
return pathBuilder
.moveTo(width / 2, halfStroke)
.lineTo(width - halfStroke, height / 2)
.lineTo(width / 2, height - halfStroke)
.lineTo(halfStroke, height / 2)
.closePath()
.build();
}
/**
* Generate triangle path using SVGPathBuilder
*/
static trianglePath(
width: number,
height: number,
strokeWidth: number = 0
): string {
const halfStroke = strokeWidth / 2;
const pathBuilder = new SVGPathBuilder();
return pathBuilder
.moveTo(width / 2, halfStroke)
.lineTo(width - halfStroke, height - halfStroke)
.lineTo(halfStroke, height - halfStroke)
.closePath()
.build();
}
}