@bendyline/squisq-editor-react 1.5.2 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (165) hide show
  1. package/dist/index.d.ts +757 -20
  2. package/dist/index.js +16910 -6844
  3. package/dist/index.js.map +1 -1
  4. package/package.json +4 -4
  5. package/src/BlockCardView.tsx +121 -0
  6. package/src/BlockPreviewPanel.tsx +69 -0
  7. package/src/BlockPropertiesPopover.tsx +191 -0
  8. package/src/EditorContext.tsx +143 -0
  9. package/src/EditorShell.tsx +200 -120
  10. package/src/FolderView.tsx +131 -0
  11. package/src/Icon.tsx +26 -0
  12. package/src/ImageEditor.tsx +69 -22
  13. package/src/OutlinePanel.tsx +38 -3
  14. package/src/PlainHtmlPreview.tsx +30 -3
  15. package/src/PreviewControls.tsx +180 -8
  16. package/src/RawEditor.tsx +216 -29
  17. package/src/RecorderEntry.tsx +9 -16
  18. package/src/TemplateAnnotation.ts +44 -0
  19. package/src/TemplatePicker.tsx +329 -54
  20. package/src/ThemeCustomizerPanel.tsx +30 -336
  21. package/src/ThemePicker.tsx +112 -3
  22. package/src/TimelineBlockPreview.tsx +37 -0
  23. package/src/TimelineTrack.tsx +671 -0
  24. package/src/Toolbar.tsx +528 -174
  25. package/src/Tooltip.tsx +22 -4
  26. package/src/TransitionPicker.tsx +351 -0
  27. package/src/VersionHistoryPanel.tsx +61 -31
  28. package/src/ViewMenuPanel.tsx +17 -14
  29. package/src/WysiwygEditor.tsx +161 -65
  30. package/src/__tests__/blockProperties.test.ts +92 -0
  31. package/src/__tests__/blockRange.test.ts +105 -0
  32. package/src/__tests__/buildPreviewDocTransition.test.ts +73 -0
  33. package/src/__tests__/createShapeLayer.test.ts +46 -0
  34. package/src/__tests__/drawingShapeRoundTrip.test.ts +49 -0
  35. package/src/__tests__/embeddedMedia.test.ts +48 -0
  36. package/src/__tests__/headingTransition.test.ts +138 -0
  37. package/src/__tests__/layoutChildRoundTrip.test.ts +71 -0
  38. package/src/__tests__/plainHtmlPreview.test.tsx +10 -8
  39. package/src/__tests__/recorderMediaInsert.test.ts +86 -0
  40. package/src/__tests__/templateAnnotationRoundTrip.test.ts +18 -0
  41. package/src/__tests__/templatePickerMetadata.test.ts +32 -0
  42. package/src/__tests__/timelineSource.test.ts +134 -0
  43. package/src/__tests__/tiptapBridge.test.ts +92 -0
  44. package/src/__tests__/tiptapBridgeConformance.test.ts +47 -0
  45. package/src/__tests__/tooltip.test.tsx +72 -0
  46. package/src/__tests__/transitionCatalog.test.ts +64 -0
  47. package/src/__tests__/useBlockNavigator.test.tsx +67 -0
  48. package/src/__tests__/useMediaRecorder.test.ts +24 -0
  49. package/src/__tests__/useTimelineClock.test.ts +21 -0
  50. package/src/blockProperties.ts +88 -0
  51. package/src/blockRange.ts +132 -0
  52. package/src/buildPreviewDoc.ts +98 -9
  53. package/src/customTemplates/AddBin.tsx +126 -0
  54. package/src/customTemplates/CustomLayoutManager.tsx +233 -0
  55. package/src/customTemplates/CustomTemplateContext.tsx +182 -0
  56. package/src/customTemplates/LayerToolbar.tsx +580 -0
  57. package/src/customTemplates/ShapeGlyph.tsx +47 -0
  58. package/src/customTemplates/TemplateDesigner.tsx +430 -0
  59. package/src/customTemplates/__tests__/library.test.ts +88 -0
  60. package/src/customTemplates/__tests__/normalizePositions.test.ts +109 -0
  61. package/src/customTemplates/__tests__/shapeDefs.test.ts +49 -0
  62. package/src/customTemplates/__tests__/useMemoryLayerAdapter.test.ts +95 -0
  63. package/src/customTemplates/designer.css +673 -0
  64. package/src/customTemplates/index.ts +31 -0
  65. package/src/customTemplates/library.ts +97 -0
  66. package/src/customTemplates/normalizePositions.ts +75 -0
  67. package/src/customTemplates/shapeDefs.ts +131 -0
  68. package/src/customTemplates/thumbnail.tsx +63 -0
  69. package/src/customTemplates/tokenDefs.ts +60 -0
  70. package/src/customTemplates/useDocCustomTemplates.ts +52 -0
  71. package/src/customTemplates/useMemoryLayerAdapter.ts +123 -0
  72. package/src/customThemes/CustomThemeContext.tsx +179 -0
  73. package/src/customThemes/CustomThemeDialog.tsx +286 -0
  74. package/src/customThemes/__tests__/CustomThemeContext.test.tsx +64 -0
  75. package/src/customThemes/__tests__/CustomThemeDialog.test.tsx +47 -0
  76. package/src/customThemes/__tests__/customThemeLibrary.test.ts +51 -0
  77. package/src/customThemes/customThemeLibrary.ts +97 -0
  78. package/src/customThemes/index.ts +31 -0
  79. package/src/customThemes/themeControls.tsx +229 -0
  80. package/src/customThemes/themeDraft.ts +272 -0
  81. package/src/customThemes/useDocCustomThemes.ts +49 -0
  82. package/src/diagram/DiagramCanvas.tsx +240 -0
  83. package/src/diagram/DiagramExtension.ts +209 -0
  84. package/src/diagram/DiagramMaximizedOverlay.tsx +46 -0
  85. package/src/diagram/DiagramWidget.tsx +270 -0
  86. package/src/diagram/diagramCommands.ts +604 -0
  87. package/src/diagram/diagramConstants.ts +17 -0
  88. package/src/diagram/useDiagramData.ts +126 -0
  89. package/src/embeddedMedia.ts +78 -0
  90. package/src/frontmatter.ts +29 -0
  91. package/src/headingTransition.ts +231 -0
  92. package/src/imageEditor/CanvasSurface.tsx +383 -88
  93. package/src/imageEditor/PropertiesPanel.tsx +47 -1
  94. package/src/imageEditor/Toolbar.tsx +229 -16
  95. package/src/imageEditor/createShapeLayer.ts +280 -0
  96. package/src/imageEditor/icons.tsx +34 -114
  97. package/src/imageEditor/image-editor.css +54 -5
  98. package/src/imageEditor/state.ts +23 -3
  99. package/src/index.ts +77 -0
  100. package/src/recorder/RecorderModal.tsx +120 -53
  101. package/src/recorder/RecorderPanel.tsx +2 -26
  102. package/src/recorder/hooks/useMediaRecorder.ts +17 -2
  103. package/src/recorder/insertMediaBlock.ts +30 -0
  104. package/src/resolveBlockVisual.ts +33 -0
  105. package/src/scene/Scene.tsx +540 -0
  106. package/src/scene/SceneBlockExtension.ts +198 -0
  107. package/src/scene/SceneBlockToolbar.tsx +201 -0
  108. package/src/scene/SceneBlockWidget.tsx +434 -0
  109. package/src/scene/ScenePropsBar.tsx +85 -0
  110. package/src/scene/SceneSelection.tsx +107 -0
  111. package/src/scene/SceneViewport.tsx +102 -0
  112. package/src/scene/ShapePalette.tsx +181 -0
  113. package/src/scene/__tests__/DiagramAdapter.test.ts +56 -0
  114. package/src/scene/__tests__/bezierEdit.test.ts +85 -0
  115. package/src/scene/__tests__/blockLayers.test.ts +57 -0
  116. package/src/scene/__tests__/shapeLayers.test.ts +106 -0
  117. package/src/scene/__tests__/useSceneHitTest.test.ts +90 -0
  118. package/src/scene/__tests__/useScenePanZoom.test.ts +103 -0
  119. package/src/scene/adapters/DiagramAdapter.ts +168 -0
  120. package/src/scene/adapters/DrawingAdapter.ts +415 -0
  121. package/src/scene/adapters/LayoutAdapter.ts +310 -0
  122. package/src/scene/adapters/blockLayers.ts +159 -0
  123. package/src/scene/commands/SceneCommand.ts +70 -0
  124. package/src/scene/commands/drawingCommands.ts +318 -0
  125. package/src/scene/commands/layoutCommands.ts +301 -0
  126. package/src/scene/hooks/useSceneHitTest.ts +105 -0
  127. package/src/scene/hooks/useScenePanZoom.ts +147 -0
  128. package/src/scene/hooks/useSceneSelection.ts +62 -0
  129. package/src/scene/index.ts +95 -0
  130. package/src/scene/layers/DiagramEdges.tsx +127 -0
  131. package/src/scene/layers/edgeGeometry.ts +77 -0
  132. package/src/scene/layers/nodeCard.tsx +145 -0
  133. package/src/scene/layers/renderLayer.tsx +70 -0
  134. package/src/scene/layers/shapeLayers.ts +201 -0
  135. package/src/scene/paths/bezierEdit.ts +208 -0
  136. package/src/scene/scene.css +649 -0
  137. package/src/scene/text/SceneTextOverlay.tsx +161 -0
  138. package/src/scene/text/sceneTextChannel.ts +40 -0
  139. package/src/scene/text/sceneTextConfig.ts +27 -0
  140. package/src/scene/text/sceneTiptap.ts +36 -0
  141. package/src/scene/text/useSceneTextEditing.ts +39 -0
  142. package/src/scene/tools/ConnectTool.ts +111 -0
  143. package/src/scene/tools/DrawingConnectTool.ts +161 -0
  144. package/src/scene/tools/PathTool.ts +158 -0
  145. package/src/scene/tools/PlaceTool.ts +47 -0
  146. package/src/scene/tools/SceneTool.ts +75 -0
  147. package/src/scene/tools/SelectTool.ts +284 -0
  148. package/src/scene/tools/ShapeTool.ts +144 -0
  149. package/src/scene/tools/TextTool.ts +72 -0
  150. package/src/scene/tools/TokenTool.ts +95 -0
  151. package/src/scene/tools/createDrawShapeTool.ts +82 -0
  152. package/src/styles/diagram.css +183 -0
  153. package/src/styles/editor.css +1615 -203
  154. package/src/styles/folder-view.css +210 -0
  155. package/src/styles/image-edit-affordance.css +2 -2
  156. package/src/styles/index.css +4 -0
  157. package/src/timelineSource.ts +244 -0
  158. package/src/tiptapBridge.ts +115 -34
  159. package/src/tooltipPlacement.ts +13 -0
  160. package/src/transitionCatalog.ts +159 -0
  161. package/src/types/monaco-shims.d.ts +10 -0
  162. package/src/useBlockNavigator.ts +153 -0
  163. package/src/useMonacoLoader.ts +105 -0
  164. package/src/useTimelineClock.ts +76 -0
  165. package/src/utils/dropUtils.ts +1 -1
@@ -0,0 +1,144 @@
1
+ /**
2
+ * ShapeTool — drag to draw a rectangle or circle as a new ShapeLayer.
3
+ *
4
+ * Exposed as a factory so a host can pre-bind the shape kind and
5
+ * stroke/fill defaults. Returns a fresh SceneTool instance — the
6
+ * factory pattern lets multiple Scene instances coexist without
7
+ * sharing module-level drag state.
8
+ */
9
+
10
+ import { createElement, type JSX } from 'react';
11
+ import type { ShapeLayer } from '@bendyline/squisq/schemas';
12
+ import type { SceneTool } from './SceneTool';
13
+
14
+ interface ShapeToolOptions {
15
+ /** Shape kind. Default 'rect'. */
16
+ shape?: 'rect' | 'circle' | 'line';
17
+ /** Stroke color. Default '#1e293b'. */
18
+ stroke?: string;
19
+ /** Fill color (or 'none' for an outline-only shape). Default 'none'. */
20
+ fill?: string;
21
+ /** Stroke width in viewport units. Default 2. */
22
+ strokeWidth?: number;
23
+ /** Corner radius for rects. Default 6. */
24
+ borderRadius?: number;
25
+ /** Override the tool id (default 'shape'). */
26
+ id?: string;
27
+ /** Override the tool label. */
28
+ label?: string;
29
+ /** Keyboard shortcut (single char, lowercase). */
30
+ shortcut?: string;
31
+ }
32
+
33
+ export function createShapeTool(options: ShapeToolOptions = {}): SceneTool {
34
+ const shape = options.shape ?? 'rect';
35
+ const stroke = options.stroke ?? '#1e293b';
36
+ const fill = options.fill ?? 'none';
37
+ const strokeWidth = options.strokeWidth ?? 2;
38
+ const borderRadius = options.borderRadius ?? 6;
39
+
40
+ interface DragState {
41
+ startV: { x: number; y: number };
42
+ currentV: { x: number; y: number };
43
+ }
44
+ let drag: DragState | null = null;
45
+
46
+ function pointer(e: React.PointerEvent): { sx: number; sy: number } {
47
+ const rect = (e.currentTarget as Element).getBoundingClientRect();
48
+ return { sx: e.clientX - rect.left, sy: e.clientY - rect.top };
49
+ }
50
+
51
+ return {
52
+ id: options.id ?? `shape-${shape}`,
53
+ label: options.label ?? `${shape[0].toUpperCase()}${shape.slice(1)}`,
54
+ cursor: 'crosshair',
55
+ shortcut: options.shortcut,
56
+
57
+ onPointerDown(e, ctx) {
58
+ if (e.button !== 0) return;
59
+ const { sx, sy } = pointer(e);
60
+ const v = ctx.screenToViewport(sx, sy);
61
+ drag = { startV: v, currentV: v };
62
+ (e.currentTarget as Element).setPointerCapture?.(e.pointerId);
63
+ },
64
+
65
+ onPointerMove(e, ctx) {
66
+ if (!drag) return;
67
+ const { sx, sy } = pointer(e);
68
+ drag.currentV = ctx.screenToViewport(sx, sy);
69
+ },
70
+
71
+ onPointerUp(e, ctx) {
72
+ if (!drag) return;
73
+ const box = boxFromCorners(drag.startV, drag.currentV);
74
+ drag = null;
75
+ (e.currentTarget as Element).releasePointerCapture?.(e.pointerId);
76
+ // Ignore micro-drags that are probably an accidental click.
77
+ if (box.width < 4 || box.height < 4) return;
78
+ const layer: ShapeLayer = {
79
+ id: `shape-${Date.now().toString(36)}-${Math.floor(Math.random() * 1e6).toString(36)}`,
80
+ type: 'shape',
81
+ position: { x: box.x, y: box.y, width: box.width, height: box.height },
82
+ content: {
83
+ shape,
84
+ fill,
85
+ stroke,
86
+ strokeWidth,
87
+ ...(shape === 'rect' ? { borderRadius } : {}),
88
+ },
89
+ };
90
+ ctx.dispatch({ kind: 'addLayer', layer });
91
+ // Switch back to Select so the user can immediately move the new shape.
92
+ ctx.setActiveTool?.('select');
93
+ },
94
+
95
+ renderOverlay(): JSX.Element | null {
96
+ if (!drag) return null;
97
+ const box = boxFromCorners(drag.startV, drag.currentV);
98
+ if (shape === 'circle') {
99
+ const r = Math.min(box.width, box.height) / 2;
100
+ return createElement('circle', {
101
+ key: 'shape-preview',
102
+ cx: box.x + box.width / 2,
103
+ cy: box.y + box.height / 2,
104
+ r,
105
+ className: 'squisq-scene-connect-preview',
106
+ fill: 'none',
107
+ });
108
+ }
109
+ if (shape === 'line') {
110
+ return createElement('line', {
111
+ key: 'shape-preview',
112
+ x1: drag.startV.x,
113
+ y1: drag.startV.y,
114
+ x2: drag.currentV.x,
115
+ y2: drag.currentV.y,
116
+ className: 'squisq-scene-connect-preview',
117
+ });
118
+ }
119
+ return createElement('rect', {
120
+ key: 'shape-preview',
121
+ x: box.x,
122
+ y: box.y,
123
+ width: box.width,
124
+ height: box.height,
125
+ rx: borderRadius,
126
+ ry: borderRadius,
127
+ className: 'squisq-scene-connect-preview',
128
+ fill: 'none',
129
+ });
130
+ },
131
+ };
132
+ }
133
+
134
+ function boxFromCorners(
135
+ a: { x: number; y: number },
136
+ b: { x: number; y: number },
137
+ ): { x: number; y: number; width: number; height: number } {
138
+ return {
139
+ x: Math.min(a.x, b.x),
140
+ y: Math.min(a.y, b.y),
141
+ width: Math.abs(b.x - a.x),
142
+ height: Math.abs(b.y - a.y),
143
+ };
144
+ }
@@ -0,0 +1,72 @@
1
+ /**
2
+ * TextTool — click to place a new TextLayer; the host can hook the
3
+ * `prompt` option to swap the default `window.prompt` for an inline
4
+ * editing UI (Tiptap, contenteditable, etc.) when one is available.
5
+ */
6
+
7
+ import type { TextLayer } from '@bendyline/squisq/schemas';
8
+ import type { SceneTool } from './SceneTool';
9
+
10
+ interface TextToolOptions {
11
+ /** Default font size in viewport units. Default 24. */
12
+ fontSize?: number;
13
+ /** Default text color. Default '#1e293b'. */
14
+ color?: string;
15
+ /** Default text alignment. Default 'left'. */
16
+ textAlign?: 'left' | 'center' | 'right';
17
+ /**
18
+ * Prompt the user for the text content. Returns null to cancel.
19
+ * Default uses `window.prompt`.
20
+ */
21
+ prompt?: () => string | null;
22
+ /** Override id / label / shortcut. */
23
+ id?: string;
24
+ label?: string;
25
+ shortcut?: string;
26
+ }
27
+
28
+ export function createTextTool(options: TextToolOptions = {}): SceneTool {
29
+ const fontSize = options.fontSize ?? 24;
30
+ const color = options.color ?? '#1e293b';
31
+ const textAlign = options.textAlign ?? 'left';
32
+ const prompt = options.prompt ?? (() => window.prompt('Text:'));
33
+
34
+ return {
35
+ id: options.id ?? 'text',
36
+ label: options.label ?? 'Text',
37
+ cursor: 'text',
38
+ shortcut: options.shortcut ?? 't',
39
+
40
+ onPointerDown(e, ctx) {
41
+ if (e.button !== 0) return;
42
+ const rect = (e.currentTarget as Element).getBoundingClientRect();
43
+ const sx = e.clientX - rect.left;
44
+ const sy = e.clientY - rect.top;
45
+ const v = ctx.screenToViewport(sx, sy);
46
+ const text = prompt();
47
+ if (text == null || text.length === 0) {
48
+ ctx.setActiveTool?.('select');
49
+ return;
50
+ }
51
+ const layer: TextLayer = {
52
+ id: `text-${Date.now().toString(36)}-${Math.floor(Math.random() * 1e6).toString(36)}`,
53
+ type: 'text',
54
+ position: {
55
+ x: v.x,
56
+ y: v.y,
57
+ // No explicit width — hit-test will treat the bounding box as
58
+ // measured at render time. Provide a generous default so the
59
+ // hit-test stub finds something.
60
+ width: Math.max(fontSize * text.length * 0.6, 80),
61
+ height: fontSize * 1.4,
62
+ },
63
+ content: {
64
+ text,
65
+ style: { fontSize, color, textAlign },
66
+ },
67
+ };
68
+ ctx.dispatch({ kind: 'addLayer', layer });
69
+ ctx.setActiveTool?.('select');
70
+ },
71
+ };
72
+ }
@@ -0,0 +1,95 @@
1
+ /**
2
+ * TokenTool — pointer-down on the canvas drops a pre-filled
3
+ * placeholder layer at the click position. Used by the template
4
+ * designer's Add bin so the user can place `{title}`,
5
+ * `{content}`, `{children}`, or `{image:N}` in a single click instead
6
+ * of typing the placeholder string into a generic TextTool.
7
+ *
8
+ * Factory pattern (`createTokenTool`) lets the palette build a
9
+ * separate tool instance per token, each with its own id / label /
10
+ * default layer shape.
11
+ */
12
+
13
+ import type { ImageLayer, TextLayer } from '@bendyline/squisq/schemas';
14
+ import type { SceneTool } from './SceneTool';
15
+ import { createPlaceTool } from './PlaceTool';
16
+
17
+ export type TokenKind = 'text' | 'image';
18
+
19
+ export interface TokenToolOptions {
20
+ /** Stable tool id (e.g. `'token-title'`). */
21
+ id: string;
22
+ /** Label shown in the toolbar / palette button. */
23
+ label: string;
24
+ /** Optional keyboard shortcut. */
25
+ shortcut?: string;
26
+ /**
27
+ * The placeholder string to substitute at render time, e.g.
28
+ * `'{title}'` or `'{image:0}'`. The designer drops this literal
29
+ * value into the new layer; the resolver replaces it later.
30
+ */
31
+ token: string;
32
+ /**
33
+ * Which layer kind to insert. `'text'` creates a TextLayer with
34
+ * `content.text = token`; `'image'` creates an ImageLayer with
35
+ * `content.src = token`.
36
+ */
37
+ kind: TokenKind;
38
+ /** Default font size for text tokens. Default 36. */
39
+ fontSize?: number;
40
+ /** Default text color for text tokens. Default '#0f172a'. */
41
+ color?: string;
42
+ /** Default width (pixels) for the placed layer. Default 480. */
43
+ width?: number;
44
+ /** Default height (pixels) for the placed layer. Default for text: 72; for image: 320. */
45
+ height?: number;
46
+ }
47
+
48
+ /**
49
+ * Build a placeholder layer for `options` with its top-left corner at
50
+ * the given viewport point. Shared by the click-to-place TokenTool and
51
+ * the template designer's drag-and-drop drop handler so both produce
52
+ * identical layers.
53
+ */
54
+ export function buildTokenLayer(
55
+ options: TokenToolOptions,
56
+ point: { x: number; y: number },
57
+ ): TextLayer | ImageLayer {
58
+ const width = options.width ?? 480;
59
+ const height = options.height ?? (options.kind === 'image' ? 320 : 72);
60
+ const fontSize = options.fontSize ?? 36;
61
+ const color = options.color ?? '#0f172a';
62
+ const id = nextLayerId(options.kind);
63
+ if (options.kind === 'text') {
64
+ return {
65
+ id,
66
+ type: 'text',
67
+ position: { x: point.x, y: point.y, width, height },
68
+ content: {
69
+ text: options.token,
70
+ style: { fontSize, color, textAlign: 'left' },
71
+ },
72
+ };
73
+ }
74
+ return {
75
+ id,
76
+ type: 'image',
77
+ position: { x: point.x, y: point.y, width, height },
78
+ content: { src: options.token, alt: 'Placeholder image', fit: 'cover' },
79
+ };
80
+ }
81
+
82
+ export function createTokenTool(options: TokenToolOptions): SceneTool {
83
+ return createPlaceTool({
84
+ id: options.id,
85
+ label: options.label,
86
+ shortcut: options.shortcut,
87
+ build: (point) => buildTokenLayer(options, point),
88
+ });
89
+ }
90
+
91
+ let counter = 0;
92
+ function nextLayerId(kind: TokenKind): string {
93
+ counter += 1;
94
+ return `${kind}-token-${Date.now().toString(36)}-${counter}`;
95
+ }
@@ -0,0 +1,82 @@
1
+ /**
2
+ * createDrawShapeTool — drag to draw the currently-pending shape kind.
3
+ *
4
+ * Generalizes `ShapeTool` to any kind in the drawing vocabulary. The pending
5
+ * kind is read on demand (set by the ShapePalette), and committing calls back
6
+ * to the host with the kind + bounding box so the adapter can insert a
7
+ * `{[kind …]}` heading. The live preview renders the actual shape geometry.
8
+ */
9
+
10
+ import { createElement, type JSX } from 'react';
11
+ import { shapePath } from '@bendyline/squisq/doc';
12
+ import type { SceneTool } from './SceneTool';
13
+
14
+ export interface DrawShapeOptions {
15
+ /** The kind to draw, or null when none is pending (tool is inert). */
16
+ getKind: () => string | null;
17
+ /** Commit a drawn shape. */
18
+ onDraw: (kind: string, box: { x: number; y: number; width: number; height: number }) => void;
19
+ }
20
+
21
+ export function createDrawShapeTool(options: DrawShapeOptions): SceneTool {
22
+ let drag: { start: { x: number; y: number }; current: { x: number; y: number } } | null = null;
23
+
24
+ function pointer(e: React.PointerEvent): { sx: number; sy: number } {
25
+ const rect = (e.currentTarget as Element).getBoundingClientRect();
26
+ return { sx: e.clientX - rect.left, sy: e.clientY - rect.top };
27
+ }
28
+
29
+ function box(): { x: number; y: number; width: number; height: number } {
30
+ const a = drag!.start;
31
+ const b = drag!.current;
32
+ return {
33
+ x: Math.min(a.x, b.x),
34
+ y: Math.min(a.y, b.y),
35
+ width: Math.abs(b.x - a.x),
36
+ height: Math.abs(b.y - a.y),
37
+ };
38
+ }
39
+
40
+ return {
41
+ id: 'draw',
42
+ label: 'Shape',
43
+ cursor: 'crosshair',
44
+
45
+ onPointerDown(e, ctx) {
46
+ if (e.button !== 0 || !options.getKind()) return;
47
+ const { sx, sy } = pointer(e);
48
+ const v = ctx.screenToViewport(sx, sy);
49
+ drag = { start: v, current: v };
50
+ (e.currentTarget as Element).setPointerCapture?.(e.pointerId);
51
+ },
52
+
53
+ onPointerMove(e, ctx) {
54
+ if (!drag) return;
55
+ const { sx, sy } = pointer(e);
56
+ drag.current = ctx.screenToViewport(sx, sy);
57
+ },
58
+
59
+ onPointerUp(e, ctx) {
60
+ if (!drag) return;
61
+ const b = box();
62
+ const kind = options.getKind();
63
+ drag = null;
64
+ (e.currentTarget as Element).releasePointerCapture?.(e.pointerId);
65
+ if (kind && b.width >= 4 && b.height >= 4) options.onDraw(kind, b);
66
+ ctx.setActiveTool?.('select');
67
+ },
68
+
69
+ renderOverlay(): JSX.Element | null {
70
+ if (!drag) return null;
71
+ const b = box();
72
+ const kind = options.getKind();
73
+ const d = kind ? shapePath(kind, b.x, b.y, b.width, b.height) : null;
74
+ return createElement('path', {
75
+ key: 'draw-preview',
76
+ d: d ?? `M ${b.x} ${b.y} h ${b.width} v ${b.height} h ${-b.width} Z`,
77
+ className: 'squisq-scene-connect-preview',
78
+ fill: 'none',
79
+ });
80
+ },
81
+ };
82
+ }
@@ -0,0 +1,183 @@
1
+ /**
2
+ * Diagram editor styles.
3
+ *
4
+ * `.squisq-diagram-widget-host`: the wrapper ProseMirror inserts after a
5
+ * `### Foo {[diagram]}` heading. Hosts a React-Flow canvas at a fixed
6
+ * inline height; flipping the `maximized` state portals the canvas out
7
+ * to an overlay (`.squisq-diagram-maximized-overlay`) instead.
8
+ *
9
+ * `[data-squisq-diagram-child]`: ProseMirror marks each direct sub-heading
10
+ * of a diagram parent. CSS hides those headings (and the body content
11
+ * between them) so the diagram canvas is the only visual representation
12
+ * of the section in the WYSIWYG view. Authors can still edit them
13
+ * through the canvas, or via the Raw / Linear views.
14
+ */
15
+
16
+ .squisq-diagram-widget-host {
17
+ margin: 0.75rem 0 1.25rem;
18
+ }
19
+
20
+ .squisq-diagram-inline {
21
+ position: relative;
22
+ width: 100%;
23
+ height: 420px;
24
+ border: 1px solid var(--squisq-border, #e2e8f0);
25
+ border-radius: 8px;
26
+ overflow: hidden;
27
+ background: var(--squisq-surface, #ffffff);
28
+ }
29
+
30
+ .squisq-diagram-canvas {
31
+ position: relative;
32
+ width: 100%;
33
+ height: 100%;
34
+ }
35
+
36
+ /* Drag bar along the bottom edge — pins the canvas to a fixed pixel
37
+ * height (persisted as `height=` on the `{[diagram …]}` heading). Sits
38
+ * above the Scene so the gesture is captured by the handle, not the
39
+ * canvas. Double-click reverts to the default height. */
40
+ .squisq-diagram-resize-handle {
41
+ position: absolute;
42
+ left: 0;
43
+ right: 0;
44
+ bottom: 0;
45
+ height: 12px;
46
+ display: flex;
47
+ align-items: center;
48
+ justify-content: center;
49
+ cursor: ns-resize;
50
+ z-index: 11;
51
+ touch-action: none;
52
+ }
53
+ .squisq-diagram-resize-handle::before {
54
+ content: '';
55
+ width: 44px;
56
+ height: 4px;
57
+ border-radius: 2px;
58
+ background: var(--squisq-border, #cbd5e1);
59
+ transition: background 0.12s;
60
+ }
61
+ .squisq-diagram-resize-handle:hover::before {
62
+ background: var(--squisq-text-muted, #94a3b8);
63
+ }
64
+
65
+ .squisq-diagram-canvas .react-flow {
66
+ width: 100%;
67
+ height: 100%;
68
+ }
69
+
70
+ .squisq-diagram-empty {
71
+ position: absolute;
72
+ inset: 0;
73
+ display: flex;
74
+ flex-direction: column;
75
+ align-items: center;
76
+ justify-content: center;
77
+ gap: 8px;
78
+ pointer-events: none;
79
+ text-align: center;
80
+ padding: 24px;
81
+ }
82
+
83
+ .squisq-diagram-empty-title {
84
+ font-size: 14px;
85
+ font-weight: 600;
86
+ color: var(--squisq-text, #1e293b);
87
+ }
88
+
89
+ .squisq-diagram-empty-hint {
90
+ font-size: 12px;
91
+ color: var(--squisq-text-muted, #64748b);
92
+ max-width: 320px;
93
+ line-height: 1.45;
94
+ }
95
+
96
+ .squisq-diagram-empty-btn {
97
+ pointer-events: auto;
98
+ margin-top: 4px;
99
+ padding: 6px 12px;
100
+ font-size: 12px;
101
+ font-weight: 500;
102
+ color: #ffffff;
103
+ background: #6366f1;
104
+ border: none;
105
+ border-radius: 6px;
106
+ cursor: pointer;
107
+ transition: background 0.1s;
108
+ }
109
+
110
+ .squisq-diagram-empty-btn:hover {
111
+ background: #4f46e5;
112
+ }
113
+
114
+ .squisq-diagram-maximize-btn {
115
+ position: absolute;
116
+ top: 8px;
117
+ right: 8px;
118
+ z-index: 10;
119
+ width: 32px;
120
+ height: 32px;
121
+ display: inline-flex;
122
+ align-items: center;
123
+ justify-content: center;
124
+ background: var(--squisq-surface, #ffffff);
125
+ border: 1px solid var(--squisq-border, #e2e8f0);
126
+ border-radius: 6px;
127
+ cursor: pointer;
128
+ font-size: 16px;
129
+ line-height: 1;
130
+ color: var(--squisq-text, #1e293b);
131
+ box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
132
+ }
133
+ .squisq-diagram-maximize-btn:hover {
134
+ background: var(--squisq-surface-hover, #f8fafc);
135
+ }
136
+
137
+ .squisq-diagram-maximized-overlay {
138
+ position: fixed;
139
+ inset: 0;
140
+ z-index: 9999;
141
+ background: rgba(15, 23, 42, 0.6);
142
+ display: flex;
143
+ align-items: stretch;
144
+ justify-content: stretch;
145
+ }
146
+
147
+ .squisq-diagram-maximized-content {
148
+ position: relative;
149
+ flex: 1;
150
+ margin: 24px;
151
+ background: var(--squisq-surface, #ffffff);
152
+ border-radius: 12px;
153
+ overflow: hidden;
154
+ box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
155
+ }
156
+
157
+ .squisq-diagram-maximized-content .squisq-diagram-canvas {
158
+ width: 100%;
159
+ height: 100%;
160
+ }
161
+
162
+ /* Child headings owned by a diagram are hidden in the WYSIWYG.
163
+ * They still exist in the doc (and in markdown) — the diagram canvas
164
+ * is the editing surface for them. */
165
+ [data-squisq-diagram-child='true'] {
166
+ display: none;
167
+ }
168
+
169
+ .squisq-diagram-inline-placeholder {
170
+ /* Reserved space so the document layout doesn't jump when the canvas
171
+ * portals into the maximize overlay. */
172
+ height: 420px;
173
+ border: 1px dashed var(--squisq-border, #e2e8f0);
174
+ border-radius: 8px;
175
+ display: flex;
176
+ align-items: center;
177
+ justify-content: center;
178
+ color: var(--squisq-text-muted, #64748b);
179
+ font-size: 14px;
180
+ }
181
+ .squisq-diagram-inline-placeholder::before {
182
+ content: 'Diagram open in full screen — press Esc to return';
183
+ }