96 lines
3.0 KiB
TypeScript
96 lines
3.0 KiB
TypeScript
|
|
/**
|
|||
|
|
* core-drawing – Built-in V2-Plugin (Task A3 Pilot).
|
|||
|
|
*
|
|||
|
|
* Werkzeuge: line (Migrations-Pilot aus dem Legacy-Switch).
|
|||
|
|
* Elemente werden über CADDocument committed → kollaborativ korrektes Undo.
|
|||
|
|
*
|
|||
|
|
* Sitzungszustand (Startpunkt der aktuellen Linie) lebt als Modul-Member,
|
|||
|
|
* NICHT in options: Options sind geteilter UI-Store-Zustand.
|
|||
|
|
*/
|
|||
|
|
import type { PluginV2, ToolExtensionV2, FullToolContext } from '../../types';
|
|||
|
|
import type { CADElement } from '../../../types/cad.types';
|
|||
|
|
import type { Pt } from '../../../tools/modification/geometry';
|
|||
|
|
|
|||
|
|
const uid = (prefix: string): string =>
|
|||
|
|
`${prefix}_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 9)}`;
|
|||
|
|
|
|||
|
|
/** Startpunkt der laufenden Linie (nur innerhalb einer down→up-Sitzung gesetzt). */
|
|||
|
|
let lineStart: Pt | null = null;
|
|||
|
|
|
|||
|
|
function buildLine(start: Pt, end: Pt, ctx: FullToolContext): CADElement {
|
|||
|
|
const stroke = (ctx.options.strokeColor as string | undefined) ?? '#e0e0f0';
|
|||
|
|
const layerId = (ctx.options.layerId as string | undefined) ?? 'layer-0';
|
|||
|
|
return {
|
|||
|
|
id: uid('line'),
|
|||
|
|
type: 'line',
|
|||
|
|
layerId,
|
|||
|
|
x: (start.x + end.x) / 2,
|
|||
|
|
y: (start.y + end.y) / 2,
|
|||
|
|
width: Math.abs(end.x - start.x),
|
|||
|
|
height: Math.abs(end.y - start.y),
|
|||
|
|
properties: { x1: start.x, y1: start.y, x2: end.x, y2: end.y, stroke, strokeWidth: 1 },
|
|||
|
|
} as unknown as CADElement;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* line-Werkzeug: down setzt Startpunkt, move zeigt Live-Vorschau,
|
|||
|
|
* up committet via doc.transact (1 Linie = 1 Undo-Schritt).
|
|||
|
|
*/
|
|||
|
|
export const lineTool: ToolExtensionV2 = {
|
|||
|
|
manifest: {
|
|||
|
|
id: 'line',
|
|||
|
|
label: 'Linie',
|
|||
|
|
icon: '\u2571',
|
|||
|
|
ribbonTab: 'canvas',
|
|||
|
|
tags: ['basic'],
|
|||
|
|
description: 'Zeichnet eine Linie (Klick Start, Klick Ende)',
|
|||
|
|
},
|
|||
|
|
optionsSchema: [
|
|||
|
|
{ key: 'strokeColor', label: 'Farbe', type: 'color' },
|
|||
|
|
],
|
|||
|
|
handlers: {
|
|||
|
|
down(e, ctx) {
|
|||
|
|
lineStart = e.world;
|
|||
|
|
ctx.setStatus(`Linie ab (${lineStart.x.toFixed(0)}, ${lineStart.y.toFixed(0)}) – Ende wählen`);
|
|||
|
|
},
|
|||
|
|
move(e, ctx) {
|
|||
|
|
if (!lineStart) return;
|
|||
|
|
ctx.setPreview?.(buildLine(lineStart, e.world, ctx));
|
|||
|
|
},
|
|||
|
|
up(e, ctx) {
|
|||
|
|
const c = ctx as FullToolContext;
|
|||
|
|
if (!lineStart || !c.doc) {
|
|||
|
|
lineStart = null;
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
const el = buildLine(lineStart, e.world, c);
|
|||
|
|
c.doc.transact(() => {
|
|||
|
|
// addElement öffnet inneres transact — Yjs verschachtelt sie → EIN Undo-Schritt
|
|||
|
|
c.doc!.addElement(el);
|
|||
|
|
});
|
|||
|
|
lineStart = null;
|
|||
|
|
ctx.setPreview?.(null);
|
|||
|
|
ctx.setStatus(`Linie erstellt (${el.id})`);
|
|||
|
|
},
|
|||
|
|
cancel(ctx) {
|
|||
|
|
lineStart = null;
|
|||
|
|
ctx.setPreview?.(null);
|
|||
|
|
ctx.setStatus('Linie abgebrochen');
|
|||
|
|
},
|
|||
|
|
},
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
/** V2-Plugin: Kern-Zeichenwerkzeuge (Pilot: nur line). */
|
|||
|
|
export const coreDrawingPlugin: PluginV2 = {
|
|||
|
|
manifest: {
|
|||
|
|
id: 'core-drawing',
|
|||
|
|
name: 'Zeichnen (Kern)',
|
|||
|
|
version: '1.0.0',
|
|||
|
|
author: 'web-cad team',
|
|||
|
|
description: 'Grundlegende Zeichenwerkzeuge (V2-Pilot: Linie).',
|
|||
|
|
category: 'tools',
|
|||
|
|
enabledByDefault: true,
|
|||
|
|
},
|
|||
|
|
tools: [lineTool],
|
|||
|
|
};
|