@bendyline/squisq-editor-react 1.6.2 → 2.0.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.
- package/README.md +12 -3
- package/dist/index.d.ts +1229 -179
- package/dist/index.js +18017 -11231
- package/dist/index.js.map +1 -1
- package/dist/monaco.d.ts +1 -0
- package/dist/monaco.js +4 -0
- package/dist/monaco.js.map +1 -0
- package/dist/styles/index.css +1471 -245
- package/package.json +9 -5
- package/src/BlockPropertiesPopover.tsx +30 -15
- package/src/DocumentSettingsDialog.tsx +13 -21
- package/src/EditorContext.tsx +199 -42
- package/src/EditorShell.tsx +348 -260
- package/src/ImageEditor.tsx +22 -1
- package/src/InlinePreviewGutter.tsx +16 -8
- package/src/MediaBin.tsx +107 -14
- package/src/OutlinePanel.tsx +202 -7
- package/src/PlainHtmlPreview.tsx +40 -1
- package/src/PreviewControls.tsx +309 -69
- package/src/PreviewPanel.tsx +312 -78
- package/src/RawEditor.tsx +13 -2
- package/src/StatusBar.tsx +5 -2
- package/src/TemplateAnnotation.ts +12 -3
- package/src/TemplatePicker.tsx +75 -21
- package/src/ThemeCustomizerPanel.tsx +8 -0
- package/src/ThemePicker.tsx +10 -5
- package/src/TimelineBlockPreview.tsx +1 -1
- package/src/Toolbar.tsx +202 -41
- package/src/TransitionPicker.tsx +36 -4
- package/src/ViewMenuPanel.tsx +12 -14
- package/src/WysiwygEditor.tsx +124 -91
- package/src/__tests__/blockPropertiesPopoverTheme.test.tsx +33 -0
- package/src/__tests__/blockTagActivity.test.ts +183 -0
- package/src/__tests__/buildDocumentPreviewMarkdown.test.ts +75 -0
- package/src/__tests__/buildPreviewDocContent.test.ts +48 -0
- package/src/__tests__/buildPreviewDocTransition.test.ts +21 -1
- package/src/__tests__/canvasSurfaceTextEditing.test.tsx +60 -0
- package/src/__tests__/customLayoutManagerTheme.test.tsx +34 -0
- package/src/__tests__/documentSettingsDialog.test.tsx +29 -1
- package/src/__tests__/editorScrollHandoff.test.tsx +102 -0
- package/src/__tests__/editorScrollSync.test.ts +65 -0
- package/src/__tests__/editorShellProps.test.tsx +123 -1
- package/src/__tests__/emojiPicker.test.tsx +2 -42
- package/src/__tests__/headingTransition.test.ts +0 -12
- package/src/__tests__/imageEditAffordance.test.tsx +5 -1
- package/src/__tests__/imageEditModalContract.test.tsx +119 -0
- package/src/__tests__/imageEditorShell.test.tsx +15 -1
- package/src/__tests__/jsonEditor.test.tsx +56 -1
- package/src/__tests__/layersPanel.test.tsx +75 -5
- package/src/__tests__/majorApiTypes.test.ts +26 -0
- package/src/__tests__/mediaBinDrop.test.tsx +90 -0
- package/src/__tests__/narrationSave.test.ts +148 -0
- package/src/__tests__/outlinePanel.test.tsx +93 -3
- package/src/__tests__/outlineSource.test.ts +217 -0
- package/src/__tests__/plainHtmlPreview.test.tsx +17 -1
- package/src/__tests__/presentationMode.test.tsx +290 -0
- package/src/__tests__/previewControls.test.tsx +218 -5
- package/src/__tests__/previewPanelAudioFailure.test.tsx +46 -0
- package/src/__tests__/previewPanelPresentation.test.tsx +126 -0
- package/src/__tests__/previewPanelTransforms.test.tsx +117 -0
- package/src/__tests__/rawEditorModelIsolation.test.ts +19 -0
- package/src/__tests__/resolveBlockVisual.test.ts +43 -0
- package/src/__tests__/statusBar.test.tsx +27 -0
- package/src/__tests__/teleprompter.test.tsx +190 -0
- package/src/__tests__/templateAnnotationRoundTrip.test.ts +26 -0
- package/src/__tests__/templatePickerPortal.test.tsx +62 -0
- package/src/__tests__/tiptapBridge.test.ts +23 -0
- package/src/__tests__/tiptapCodeBlockRoundTrip.test.ts +109 -0
- package/src/__tests__/transformStyleId.test.ts +13 -0
- package/src/__tests__/useImageEditor.test.tsx +67 -0
- package/src/__tests__/useMediaRecorder.test.ts +75 -0
- package/src/__tests__/viewMenuPanel.test.tsx +130 -0
- package/src/__tests__/wysiwygImageUpload.test.ts +69 -0
- package/src/__tests__/wysiwygTemplateBadgeFocus.test.tsx +56 -0
- package/src/asciiDiagram/AsciiDiagramExtension.ts +343 -0
- package/src/asciiDiagram/AsciiDiagramWidget.tsx +224 -0
- package/src/asciiDiagram/RepairableDiagramExtension.ts +200 -0
- package/src/asciiDiagram/__tests__/AsciiDiagramExtension.test.ts +145 -0
- package/src/asciiDiagram/__tests__/RepairableDiagramExtension.test.ts +159 -0
- package/src/asciiDiagram/__tests__/asciiDiagramCommands.test.ts +227 -0
- package/src/asciiDiagram/__tests__/asciiDiagramOps.test.ts +114 -0
- package/src/asciiDiagram/__tests__/asciiPaste.test.ts +43 -0
- package/src/asciiDiagram/asciiDiagramCommands.ts +148 -0
- package/src/asciiDiagram/asciiDiagramData.ts +116 -0
- package/src/asciiDiagram/asciiDiagramOps.ts +219 -0
- package/src/asciiDiagram/asciiPaste.ts +19 -0
- package/src/blockTagActivity.ts +233 -0
- package/src/buildDocumentPreviewMarkdown.ts +168 -0
- package/src/buildPreviewDoc.ts +19 -12
- package/src/customTemplates/CustomLayoutManager.tsx +3 -2
- package/src/customTemplates/CustomTemplateContext.tsx +6 -0
- package/src/customTemplates/TemplateDesigner.tsx +7 -4
- package/src/customTemplates/__tests__/library.test.ts +8 -0
- package/src/customTemplates/designer.css +158 -99
- package/src/customTemplates/library.ts +16 -3
- package/src/customThemes/CustomThemeContext.tsx +6 -0
- package/src/customThemes/CustomThemeDialog.tsx +35 -5
- package/src/customThemes/ImportThemeSection.tsx +178 -0
- package/src/customThemes/__tests__/customThemeLibrary.test.ts +9 -0
- package/src/customThemes/__tests__/importThemeSection.test.tsx +192 -0
- package/src/customThemes/customThemeLibrary.ts +7 -4
- package/src/customThemes/index.ts +7 -1
- package/src/customThemes/themeDraft.ts +23 -7
- package/src/diagram/DiagramCanvas.tsx +9 -4
- package/src/diagram/types.ts +35 -0
- package/src/editorScrollSync.ts +73 -0
- package/src/emojiData.ts +3 -23
- package/src/headingTransition.ts +0 -15
- package/src/imageEditor/CanvasSurface.tsx +29 -23
- package/src/imageEditor/LayersPanel.tsx +78 -3
- package/src/imageEditor/Toolbar.tsx +5 -1
- package/src/imageEditor/icons.tsx +4 -0
- package/src/imageEditor/image-editor.css +75 -0
- package/src/imageEditor/layers/SelectionHandles.tsx +1 -1
- package/src/imageEditor/useImageEditor.ts +71 -14
- package/src/index.ts +191 -19
- package/src/jsonEditor/JsonEditorContext.tsx +12 -6
- package/src/jsonEditor/RenderNode.tsx +24 -3
- package/src/jsonEditor/editors.tsx +86 -19
- package/src/mediaEntries.ts +12 -0
- package/src/monaco.ts +35 -0
- package/src/monacoWorkers.ts +89 -0
- package/src/outlineSource.ts +171 -0
- package/src/presentation/PresentationMode.tsx +596 -0
- package/src/rawEditorIsolation.ts +18 -0
- package/src/recorder/hooks/useMediaRecorder.ts +97 -53
- package/src/resolveBlockVisual.ts +10 -4
- package/src/scene/Scene.tsx +48 -18
- package/src/scene/SceneBlockExtension.ts +17 -10
- package/src/scene/SceneBlockWidget.tsx +5 -1
- package/src/scene/SceneViewport.tsx +7 -3
- package/src/scene/ShapePalette.tsx +17 -2
- package/src/scene/__tests__/DiagramAdapter.test.ts +86 -0
- package/src/scene/__tests__/SceneBlockExtension.test.ts +33 -0
- package/src/scene/__tests__/sceneIsolation.test.tsx +93 -0
- package/src/scene/adapters/DiagramAdapter.ts +12 -101
- package/src/scene/commands/SceneCommand.ts +4 -1
- package/src/scene/index.ts +1 -6
- package/src/scene/layers/DiagramEdges.tsx +21 -7
- package/src/scene/layers/edgeGeometry.ts +9 -1
- package/src/scene/layers/nodeCard.tsx +27 -8
- package/src/scene/scene.css +5 -2
- package/src/scene/text/SceneTextOverlay.tsx +5 -5
- package/src/scene/text/sceneTextChannel.ts +26 -20
- package/src/scene/text/sceneTextConfig.ts +4 -0
- package/src/scene/tools/ConnectTool.ts +13 -4
- package/src/scene/tools/SceneTool.ts +8 -0
- package/src/scene/tools/SelectTool.ts +34 -18
- package/src/scene/tools/ShapeTool.ts +2 -3
- package/src/styles/ascii-diagram.css +79 -0
- package/src/styles/ascii-timeline.css +499 -0
- package/src/styles/editor.css +706 -120
- package/src/styles/index.css +3 -0
- package/src/styles/tree-view.css +139 -0
- package/src/teleprompter/TeleprompterControls.tsx +218 -0
- package/src/teleprompter/TeleprompterSelfView.tsx +22 -0
- package/src/teleprompter/TeleprompterSurface.tsx +267 -0
- package/src/teleprompter/TeleprompterView.tsx +338 -0
- package/src/teleprompter/canvasRenderer.ts +161 -0
- package/src/teleprompter/floatingWindow.ts +352 -0
- package/src/teleprompter/index.ts +61 -0
- package/src/teleprompter/pcmWorklet.ts +62 -0
- package/src/teleprompter/recording/insertPreamble.ts +80 -0
- package/src/teleprompter/recording/narrationSave.ts +134 -0
- package/src/teleprompter/recording/useNarrationRecorder.ts +367 -0
- package/src/teleprompter/scrollModel.ts +85 -0
- package/src/teleprompter/teleprompterTheme.ts +303 -0
- package/src/teleprompter/types.ts +38 -0
- package/src/teleprompter/useFloatingWindow.ts +74 -0
- package/src/teleprompter/useMicAnalysis.ts +211 -0
- package/src/teleprompter/useTeleprompter.ts +421 -0
- package/src/templateContentPreviewResolver.ts +12 -5
- package/src/timeline/TimelineEditorWidget.tsx +702 -0
- package/src/timeline/TimelineViewExtension.ts +252 -0
- package/src/timeline/__tests__/TimelineEditorWidget.test.tsx +355 -0
- package/src/timeline/__tests__/TimelineViewExtension.test.ts +163 -0
- package/src/timeline/__tests__/timelineCommands.test.ts +327 -0
- package/src/timeline/__tests__/timelineOps.test.ts +222 -0
- package/src/timeline/__tests__/timelinePaste.test.ts +34 -0
- package/src/timeline/timelineCommands.ts +367 -0
- package/src/timeline/timelineData.ts +54 -0
- package/src/timeline/timelineOps.ts +277 -0
- package/src/timeline/timelinePaste.ts +8 -0
- package/src/tiptapBridge.ts +24 -16
- package/src/transformStyleId.ts +17 -0
- package/src/treeview/TreeOutlineWidget.tsx +240 -0
- package/src/treeview/TreeViewExtension.ts +250 -0
- package/src/treeview/__tests__/TreeViewExtension.test.ts +122 -0
- package/src/treeview/__tests__/treeOps.test.ts +125 -0
- package/src/treeview/__tests__/treePaste.test.ts +40 -0
- package/src/treeview/__tests__/treeViewCommands.test.ts +163 -0
- package/src/treeview/treeOps.ts +172 -0
- package/src/treeview/treePaste.ts +13 -0
- package/src/treeview/treeViewCommands.ts +94 -0
- package/src/treeview/treeViewData.ts +41 -0
- package/src/useMonacoLoader.ts +11 -34
- package/src/wysiwygImageUpload.ts +113 -0
- package/src/diagram/DiagramExtension.ts +0 -209
- package/src/diagram/DiagramWidget.tsx +0 -272
- package/src/diagram/useDiagramData.ts +0 -126
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AsciiDiagramWidget — the interactive canvas mounted below a hidden
|
|
3
|
+
* ASCII-diagram code fence in the WYSIWYG view.
|
|
4
|
+
*
|
|
5
|
+
* Mirrors the legacy heading-based DiagramWidget's chrome (shared toolbar,
|
|
6
|
+
* maximize overlay, height handle) but reads/writes the FENCE: data comes
|
|
7
|
+
* from `useAsciiDiagramData` (live re-parse), edits go through
|
|
8
|
+
* `applyAsciiDiagramCommand` (op → render → verify → single-transaction
|
|
9
|
+
* text replace).
|
|
10
|
+
*
|
|
11
|
+
* The height handle is session-local by design: a fence has no annotation
|
|
12
|
+
* surface to persist a `height=` on, and writing chrome state into the art
|
|
13
|
+
* would violate "the fence is the diagram".
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { useCallback, useMemo, useRef, useState } from 'react';
|
|
17
|
+
import type { Editor } from '@tiptap/react';
|
|
18
|
+
import { DiagramCanvas, type DiagramCommand } from '../diagram/DiagramCanvas';
|
|
19
|
+
import { DIAGRAM_TOOLS, DIAGRAM_VIEWPORT } from '../diagram/diagramConstants';
|
|
20
|
+
import { DiagramMaximizedOverlay } from '../diagram/DiagramMaximizedOverlay';
|
|
21
|
+
import { SceneBlockToolbar, type SceneBlockAction } from '../scene/SceneBlockToolbar';
|
|
22
|
+
import { SceneSideToolbar } from '../scene/SceneSideToolbar';
|
|
23
|
+
import { nodeIdFromCardLayerId, NODE_WIDTH, NODE_HEIGHT } from '../scene';
|
|
24
|
+
import { Icon } from '../Icon';
|
|
25
|
+
import { useAsciiDiagramData } from './asciiDiagramData';
|
|
26
|
+
import { applyAsciiDiagramCommand } from './asciiDiagramCommands';
|
|
27
|
+
import { isAsciiSourceVisible, toggleAsciiSource } from './AsciiDiagramExtension';
|
|
28
|
+
import type { SceneTextChannel } from '../scene/text/sceneTextChannel';
|
|
29
|
+
|
|
30
|
+
/** Smallest height the canvas can be dragged to (px). */
|
|
31
|
+
const MIN_DIAGRAM_HEIGHT = 160;
|
|
32
|
+
const DEFAULT_DIAGRAM_HEIGHT = 420;
|
|
33
|
+
|
|
34
|
+
interface AsciiDiagramWidgetProps {
|
|
35
|
+
editor: Editor;
|
|
36
|
+
/** Session id from the extension's position registry. */
|
|
37
|
+
blockId: string;
|
|
38
|
+
/** Position at widget-creation time — display-only fallback. */
|
|
39
|
+
fallbackPos: number;
|
|
40
|
+
/** Host element used for portal targeting by the maximize overlay. */
|
|
41
|
+
host?: HTMLElement | null;
|
|
42
|
+
textChannel?: SceneTextChannel;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function AsciiDiagramWidget({
|
|
46
|
+
editor,
|
|
47
|
+
blockId,
|
|
48
|
+
host,
|
|
49
|
+
textChannel,
|
|
50
|
+
}: AsciiDiagramWidgetProps) {
|
|
51
|
+
const view = useAsciiDiagramData(editor, blockId);
|
|
52
|
+
const [maximized, setMaximized] = useState(false);
|
|
53
|
+
const [height, setHeight] = useState<number | null>(null);
|
|
54
|
+
const [dragHeight, setDragHeight] = useState<number | null>(null);
|
|
55
|
+
const inlineRef = useRef<HTMLDivElement>(null);
|
|
56
|
+
const effectiveHeight = dragHeight ?? height;
|
|
57
|
+
|
|
58
|
+
const dispatch = useCallback(
|
|
59
|
+
(cmd: DiagramCommand) => {
|
|
60
|
+
applyAsciiDiagramCommand(editor, blockId, cmd);
|
|
61
|
+
},
|
|
62
|
+
[editor, blockId],
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
const onResizeStart = useCallback(
|
|
66
|
+
(e: React.PointerEvent) => {
|
|
67
|
+
e.preventDefault();
|
|
68
|
+
e.stopPropagation();
|
|
69
|
+
const startY = e.clientY;
|
|
70
|
+
const startHeight =
|
|
71
|
+
inlineRef.current?.getBoundingClientRect().height ??
|
|
72
|
+
effectiveHeight ??
|
|
73
|
+
DEFAULT_DIAGRAM_HEIGHT;
|
|
74
|
+
const heightAt = (clientY: number) =>
|
|
75
|
+
Math.max(MIN_DIAGRAM_HEIGHT, Math.round(startHeight + (clientY - startY)));
|
|
76
|
+
const prevCursor = document.body.style.cursor;
|
|
77
|
+
const prevSelect = document.body.style.userSelect;
|
|
78
|
+
document.body.style.cursor = 'ns-resize';
|
|
79
|
+
document.body.style.userSelect = 'none';
|
|
80
|
+
const onMove = (ev: PointerEvent) => setDragHeight(heightAt(ev.clientY));
|
|
81
|
+
const onUp = (ev: PointerEvent) => {
|
|
82
|
+
window.removeEventListener('pointermove', onMove);
|
|
83
|
+
window.removeEventListener('pointerup', onUp);
|
|
84
|
+
document.body.style.cursor = prevCursor;
|
|
85
|
+
document.body.style.userSelect = prevSelect;
|
|
86
|
+
setDragHeight(null);
|
|
87
|
+
setHeight(heightAt(ev.clientY));
|
|
88
|
+
};
|
|
89
|
+
window.addEventListener('pointermove', onMove);
|
|
90
|
+
window.addEventListener('pointerup', onUp);
|
|
91
|
+
},
|
|
92
|
+
[effectiveHeight],
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
const onResizeReset = useCallback(() => {
|
|
96
|
+
setDragHeight(null);
|
|
97
|
+
setHeight(null);
|
|
98
|
+
}, []);
|
|
99
|
+
|
|
100
|
+
// ── Shared toolbar (above the canvas) ────────────────────
|
|
101
|
+
const [activeToolId, setActiveToolId] = useState<string>('select');
|
|
102
|
+
const [selectedIds, setSelectedIds] = useState<ReadonlySet<string>>(() => new Set());
|
|
103
|
+
const selectedNodeIds = useMemo(
|
|
104
|
+
() =>
|
|
105
|
+
[...selectedIds]
|
|
106
|
+
.map((id) => nodeIdFromCardLayerId(id))
|
|
107
|
+
.filter((id): id is string => id != null),
|
|
108
|
+
[selectedIds],
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
const sourceVisible = isAsciiSourceVisible(editor, blockId);
|
|
112
|
+
|
|
113
|
+
if (!view) return null;
|
|
114
|
+
|
|
115
|
+
const actions: SceneBlockAction[] = [
|
|
116
|
+
{
|
|
117
|
+
id: 'add-node',
|
|
118
|
+
label: 'Node',
|
|
119
|
+
icon: <Icon icon="fa-solid fa-plus" />,
|
|
120
|
+
title: 'Add node',
|
|
121
|
+
onClick: () =>
|
|
122
|
+
dispatch({
|
|
123
|
+
kind: 'addNode',
|
|
124
|
+
x: DIAGRAM_VIEWPORT.width / 2 - NODE_WIDTH / 2,
|
|
125
|
+
y: DIAGRAM_VIEWPORT.height / 2 - NODE_HEIGHT / 2,
|
|
126
|
+
}),
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
id: 'delete',
|
|
130
|
+
label: 'Delete',
|
|
131
|
+
icon: <Icon icon="fa-solid fa-trash" />,
|
|
132
|
+
title: 'Delete selected node',
|
|
133
|
+
danger: true,
|
|
134
|
+
disabled: selectedNodeIds.length === 0,
|
|
135
|
+
onClick: () => selectedNodeIds.forEach((id) => dispatch({ kind: 'removeNode', nodeId: id })),
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
id: 'ascii-source',
|
|
139
|
+
label: 'Source',
|
|
140
|
+
icon: <Icon icon="fa-solid fa-code" />,
|
|
141
|
+
title: sourceVisible ? 'Hide ASCII source' : 'Show ASCII source',
|
|
142
|
+
onClick: () => toggleAsciiSource(editor, blockId),
|
|
143
|
+
},
|
|
144
|
+
];
|
|
145
|
+
|
|
146
|
+
const toolbar = (
|
|
147
|
+
<SceneBlockToolbar
|
|
148
|
+
tools={DIAGRAM_TOOLS}
|
|
149
|
+
activeToolId={activeToolId}
|
|
150
|
+
onSelectTool={setActiveToolId}
|
|
151
|
+
actions={actions}
|
|
152
|
+
/>
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
const warnings =
|
|
156
|
+
view.warnings.length > 0 ? (
|
|
157
|
+
<details className="squisq-ascii-diagram-warnings">
|
|
158
|
+
<summary>
|
|
159
|
+
{view.warnings.length} parser note{view.warnings.length === 1 ? '' : 's'}
|
|
160
|
+
</summary>
|
|
161
|
+
<ul>
|
|
162
|
+
{view.warnings.map((w, i) => (
|
|
163
|
+
<li key={i}>{w}</li>
|
|
164
|
+
))}
|
|
165
|
+
</ul>
|
|
166
|
+
</details>
|
|
167
|
+
) : null;
|
|
168
|
+
|
|
169
|
+
const canvas = (
|
|
170
|
+
<DiagramCanvas
|
|
171
|
+
textChannel={textChannel}
|
|
172
|
+
nodes={view.nodes}
|
|
173
|
+
edges={view.edges}
|
|
174
|
+
onCommand={dispatch}
|
|
175
|
+
showMaximize
|
|
176
|
+
maximized={maximized}
|
|
177
|
+
onToggleMaximize={() => setMaximized((m) => !m)}
|
|
178
|
+
activeToolId={activeToolId}
|
|
179
|
+
onActiveToolIdChange={setActiveToolId}
|
|
180
|
+
onSelectionChange={setSelectedIds}
|
|
181
|
+
/>
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
if (maximized) {
|
|
185
|
+
return (
|
|
186
|
+
<div
|
|
187
|
+
className="squisq-diagram-inline-placeholder"
|
|
188
|
+
style={effectiveHeight != null ? { height: effectiveHeight } : undefined}
|
|
189
|
+
>
|
|
190
|
+
<DiagramMaximizedOverlay host={host ?? null} onClose={() => setMaximized(false)}>
|
|
191
|
+
<div className="squisq-scene-block-max">
|
|
192
|
+
{canvas}
|
|
193
|
+
<div className="squisq-scene-side-toolbar">{toolbar}</div>
|
|
194
|
+
</div>
|
|
195
|
+
</DiagramMaximizedOverlay>
|
|
196
|
+
</div>
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return (
|
|
201
|
+
<div className="squisq-scene-shell">
|
|
202
|
+
<SceneSideToolbar>{toolbar}</SceneSideToolbar>
|
|
203
|
+
<div
|
|
204
|
+
className="squisq-diagram-inline"
|
|
205
|
+
ref={inlineRef}
|
|
206
|
+
style={effectiveHeight != null ? { height: effectiveHeight } : undefined}
|
|
207
|
+
>
|
|
208
|
+
{canvas}
|
|
209
|
+
<div
|
|
210
|
+
className="squisq-diagram-resize-handle"
|
|
211
|
+
onPointerDown={onResizeStart}
|
|
212
|
+
onDoubleClick={onResizeReset}
|
|
213
|
+
role="separator"
|
|
214
|
+
aria-orientation="horizontal"
|
|
215
|
+
aria-label="Resize diagram height"
|
|
216
|
+
title="Drag to resize · double-click to reset"
|
|
217
|
+
/>
|
|
218
|
+
</div>
|
|
219
|
+
{warnings}
|
|
220
|
+
</div>
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export default AsciiDiagramWidget;
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RepairableDiagramExtension — mounts an inline "Repair as diagram" button on
|
|
3
|
+
* code fences that hold BROKEN box-and-line art.
|
|
4
|
+
*
|
|
5
|
+
* The three states of a box-drawing fence partition cleanly:
|
|
6
|
+
* - a clean diagram → AsciiDiagramExtension mounts the canvas;
|
|
7
|
+
* - a file tree / outline → TreeViewExtension mounts the outline;
|
|
8
|
+
* - broken box art → neither claims it (it renders as a faithful
|
|
9
|
+
* code block), and THIS extension offers a button
|
|
10
|
+
* to reconstruct it via `repairAsciiDiagram`.
|
|
11
|
+
*
|
|
12
|
+
* Unlike the diagram/tree extensions it does NOT hide the fence or mount a
|
|
13
|
+
* persistent interactive widget — the code stays visible and editable, and a
|
|
14
|
+
* single click rewrites the fence to clean `diagram`-tagged art (which the
|
|
15
|
+
* AsciiDiagramExtension then turns into a canvas). So the position registry
|
|
16
|
+
* only needs to survive until the click; it mirrors the other extensions'
|
|
17
|
+
* mapping so the button always resolves the right fence.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { Extension } from '@tiptap/core';
|
|
21
|
+
import { Plugin, PluginKey } from '@tiptap/pm/state';
|
|
22
|
+
import type { Transaction } from '@tiptap/pm/state';
|
|
23
|
+
import { Decoration, DecorationSet } from '@tiptap/pm/view';
|
|
24
|
+
import type { Node as PMNode } from '@tiptap/pm/model';
|
|
25
|
+
import type { Editor } from '@tiptap/react';
|
|
26
|
+
import { isEligibleAsciiFenceLang, isRepairableDiagram } from '@bendyline/squisq/doc';
|
|
27
|
+
|
|
28
|
+
export interface RepairableBlockEntry {
|
|
29
|
+
/** Synthetic session id (`repair-N`), stable across edits for one block. */
|
|
30
|
+
id: string;
|
|
31
|
+
/** Document position of the codeBlock node at the current state. */
|
|
32
|
+
pos: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface RepairablePluginState {
|
|
36
|
+
entries: RepairableBlockEntry[];
|
|
37
|
+
decorations: DecorationSet;
|
|
38
|
+
seq: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const REPAIRABLE_KEY = new PluginKey<RepairablePluginState>('squisq-repairable-diagram');
|
|
42
|
+
|
|
43
|
+
const cache = new WeakMap<PMNode, boolean>();
|
|
44
|
+
|
|
45
|
+
function fenceLangOf(node: PMNode): string | null {
|
|
46
|
+
const lang = (node.attrs as { language?: string | null }).language;
|
|
47
|
+
return typeof lang === 'string' ? lang : null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** True when a codeBlock holds broken box art worth offering to repair. */
|
|
51
|
+
export function isRepairableFence(node: PMNode): boolean {
|
|
52
|
+
if (node.type.name !== 'codeBlock') return false;
|
|
53
|
+
const cached = cache.get(node);
|
|
54
|
+
if (cached !== undefined) return cached;
|
|
55
|
+
const result =
|
|
56
|
+
isEligibleAsciiFenceLang(fenceLangOf(node)) && isRepairableDiagram(node.textContent);
|
|
57
|
+
cache.set(node, result);
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Resolve a registered block's current doc position (null when gone). */
|
|
62
|
+
export function findRepairableBlockPos(editor: Editor, blockId: string): number | null {
|
|
63
|
+
return REPAIRABLE_KEY.getState(editor.state)?.entries.find((e) => e.id === blockId)?.pos ?? null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Handler the mounted button invokes; wired by the host via extension options. */
|
|
67
|
+
export interface RepairableDiagramExtensionOptions {
|
|
68
|
+
enabled?: boolean;
|
|
69
|
+
/** Invoked when a repair button is clicked, with the block's synthetic id. */
|
|
70
|
+
onRepair?: (editor: Editor, blockId: string) => void;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function buildDecorations(
|
|
74
|
+
doc: PMNode,
|
|
75
|
+
entries: RepairableBlockEntry[],
|
|
76
|
+
editor: Editor,
|
|
77
|
+
onRepair?: (editor: Editor, blockId: string) => void,
|
|
78
|
+
): DecorationSet {
|
|
79
|
+
const decos: Decoration[] = [];
|
|
80
|
+
for (const entry of entries) {
|
|
81
|
+
const node = doc.nodeAt(entry.pos);
|
|
82
|
+
if (!node || node.type.name !== 'codeBlock') continue;
|
|
83
|
+
const blockId = entry.id;
|
|
84
|
+
decos.push(
|
|
85
|
+
Decoration.widget(
|
|
86
|
+
entry.pos + node.nodeSize,
|
|
87
|
+
() => {
|
|
88
|
+
const host = document.createElement('div');
|
|
89
|
+
host.className = 'squisq-repair-banner';
|
|
90
|
+
host.contentEditable = 'false';
|
|
91
|
+
const label = document.createElement('span');
|
|
92
|
+
label.className = 'squisq-repair-banner-text';
|
|
93
|
+
label.textContent = 'This looks like a diagram that didn’t render cleanly.';
|
|
94
|
+
const btn = document.createElement('button');
|
|
95
|
+
btn.type = 'button';
|
|
96
|
+
btn.className = 'squisq-repair-banner-btn';
|
|
97
|
+
btn.textContent = '⟳ Repair as diagram';
|
|
98
|
+
const stop = (e: Event) => e.stopPropagation();
|
|
99
|
+
host.addEventListener('mousedown', stop);
|
|
100
|
+
btn.addEventListener('mousedown', stop);
|
|
101
|
+
btn.addEventListener('click', (e) => {
|
|
102
|
+
e.preventDefault();
|
|
103
|
+
e.stopPropagation();
|
|
104
|
+
onRepair?.(editor, blockId);
|
|
105
|
+
});
|
|
106
|
+
host.appendChild(label);
|
|
107
|
+
host.appendChild(btn);
|
|
108
|
+
return host;
|
|
109
|
+
},
|
|
110
|
+
{ side: 1, ignoreSelection: true, key: `squisq-repair-${entry.id}` },
|
|
111
|
+
),
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
return DecorationSet.create(doc, decos);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function applyState(
|
|
118
|
+
tr: Transaction,
|
|
119
|
+
prev: RepairablePluginState,
|
|
120
|
+
editor: Editor,
|
|
121
|
+
doc: PMNode,
|
|
122
|
+
onRepair?: (editor: Editor, blockId: string) => void,
|
|
123
|
+
): RepairablePluginState {
|
|
124
|
+
if (!tr.docChanged) return prev;
|
|
125
|
+
|
|
126
|
+
const mapped = new Map<number, string>();
|
|
127
|
+
const claimed = new Set<string>();
|
|
128
|
+
for (const entry of prev.entries) {
|
|
129
|
+
const result = tr.mapping.mapResult(entry.pos, 1);
|
|
130
|
+
if (!result.deleted) {
|
|
131
|
+
mapped.set(result.pos, entry.id);
|
|
132
|
+
claimed.add(entry.id);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
for (const entry of prev.entries) {
|
|
136
|
+
if (claimed.has(entry.id)) continue;
|
|
137
|
+
const result = tr.mapping.mapResult(entry.pos, 1);
|
|
138
|
+
if (mapped.has(result.pos)) continue;
|
|
139
|
+
if (doc.nodeAt(result.pos)?.type.name === 'codeBlock') {
|
|
140
|
+
mapped.set(result.pos, entry.id);
|
|
141
|
+
claimed.add(entry.id);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
let seq = prev.seq;
|
|
146
|
+
const entries: RepairableBlockEntry[] = [];
|
|
147
|
+
doc.descendants((node, pos) => {
|
|
148
|
+
if (node.type.name !== 'codeBlock') return;
|
|
149
|
+
if (!isRepairableFence(node)) return false;
|
|
150
|
+
entries.push({ id: mapped.get(pos) ?? `repair-${++seq}`, pos });
|
|
151
|
+
return false;
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
return { entries, seq, decorations: buildDecorations(doc, entries, editor, onRepair) };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export const RepairableDiagramExtension = Extension.create<RepairableDiagramExtensionOptions>({
|
|
158
|
+
name: 'squisqRepairableDiagram',
|
|
159
|
+
|
|
160
|
+
addOptions() {
|
|
161
|
+
return { enabled: true };
|
|
162
|
+
},
|
|
163
|
+
|
|
164
|
+
addProseMirrorPlugins() {
|
|
165
|
+
const editor = this.editor as Editor;
|
|
166
|
+
if (this.options.enabled === false) return [];
|
|
167
|
+
const onRepair = this.options.onRepair;
|
|
168
|
+
|
|
169
|
+
return [
|
|
170
|
+
new Plugin<RepairablePluginState>({
|
|
171
|
+
key: REPAIRABLE_KEY,
|
|
172
|
+
state: {
|
|
173
|
+
init: (_config, state) => {
|
|
174
|
+
let seq = 0;
|
|
175
|
+
const entries: RepairableBlockEntry[] = [];
|
|
176
|
+
state.doc.descendants((node, pos) => {
|
|
177
|
+
if (node.type.name !== 'codeBlock') return;
|
|
178
|
+
if (!isRepairableFence(node)) return false;
|
|
179
|
+
entries.push({ id: `repair-${++seq}`, pos });
|
|
180
|
+
return false;
|
|
181
|
+
});
|
|
182
|
+
return {
|
|
183
|
+
entries,
|
|
184
|
+
seq,
|
|
185
|
+
decorations: buildDecorations(state.doc, entries, editor, onRepair),
|
|
186
|
+
};
|
|
187
|
+
},
|
|
188
|
+
apply: (tr, prev, _old, newState) => applyState(tr, prev, editor, newState.doc, onRepair),
|
|
189
|
+
},
|
|
190
|
+
props: {
|
|
191
|
+
decorations(state) {
|
|
192
|
+
return this.getState(state)?.decorations ?? DecorationSet.empty;
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
}),
|
|
196
|
+
];
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
export default RepairableDiagramExtension;
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Registry / decoration behavior of AsciiDiagramExtension: stable block
|
|
3
|
+
* identity across unrelated edits AND self-inflicted fence rewrites — the
|
|
4
|
+
* property that keeps the mounted canvas (pan/zoom) alive.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { Editor } from '@tiptap/core';
|
|
8
|
+
import StarterKit from '@tiptap/starter-kit';
|
|
9
|
+
import { afterEach, beforeAll, describe, expect, it } from 'vitest';
|
|
10
|
+
import { markdownToTiptap } from '../../tiptapBridge';
|
|
11
|
+
import { HeadingWithTemplate } from '../../TemplateAnnotation';
|
|
12
|
+
import {
|
|
13
|
+
AsciiDiagramExtension,
|
|
14
|
+
ASCII_DIAGRAM_KEY,
|
|
15
|
+
findAsciiDiagramBlockPos,
|
|
16
|
+
isAsciiSourceVisible,
|
|
17
|
+
toggleAsciiSource,
|
|
18
|
+
} from '../AsciiDiagramExtension';
|
|
19
|
+
import { replaceAsciiFenceText } from '../asciiDiagramCommands';
|
|
20
|
+
|
|
21
|
+
beforeAll(() => {
|
|
22
|
+
if (typeof globalThis.ResizeObserver === 'undefined') {
|
|
23
|
+
class ResizeObserverStub {
|
|
24
|
+
observe(): void {}
|
|
25
|
+
unobserve(): void {}
|
|
26
|
+
disconnect(): void {}
|
|
27
|
+
}
|
|
28
|
+
globalThis.ResizeObserver = ResizeObserverStub as unknown as typeof ResizeObserver;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const ART_A = [
|
|
33
|
+
'┌────────┐',
|
|
34
|
+
'│ Alpha │',
|
|
35
|
+
'└───┬────┘',
|
|
36
|
+
' │',
|
|
37
|
+
' ▼',
|
|
38
|
+
'┌────────┐',
|
|
39
|
+
'│ Beta │',
|
|
40
|
+
'└────────┘',
|
|
41
|
+
].join('\n');
|
|
42
|
+
|
|
43
|
+
const ART_B = [
|
|
44
|
+
'+-------+ +-------+',
|
|
45
|
+
'| Left | --> | Right |',
|
|
46
|
+
'+-------+ +-------+',
|
|
47
|
+
].join('\n');
|
|
48
|
+
|
|
49
|
+
let editors: Editor[] = [];
|
|
50
|
+
|
|
51
|
+
function makeEditor(md: string, options?: { enabled?: boolean }): Editor {
|
|
52
|
+
const editor = new Editor({
|
|
53
|
+
extensions: [
|
|
54
|
+
StarterKit.configure({
|
|
55
|
+
heading: false,
|
|
56
|
+
codeBlock: { HTMLAttributes: { class: 'squisq-code-block' } },
|
|
57
|
+
}),
|
|
58
|
+
HeadingWithTemplate.configure({ levels: [1, 2, 3, 4, 5, 6] }),
|
|
59
|
+
options ? AsciiDiagramExtension.configure(options) : AsciiDiagramExtension,
|
|
60
|
+
],
|
|
61
|
+
content: markdownToTiptap(md),
|
|
62
|
+
});
|
|
63
|
+
editors.push(editor);
|
|
64
|
+
return editor;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
afterEach(() => {
|
|
68
|
+
for (const e of editors) e.destroy();
|
|
69
|
+
editors = [];
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
function entriesOf(editor: Editor) {
|
|
73
|
+
return ASCII_DIAGRAM_KEY.getState(editor.state)?.entries ?? [];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
describe('AsciiDiagramExtension registry', () => {
|
|
77
|
+
it('registers each qualifying fence with a distinct id', () => {
|
|
78
|
+
const editor = makeEditor(
|
|
79
|
+
'Intro paragraph.\n\n```\n' + ART_A + '\n```\n\nBetween.\n\n```\n' + ART_B + '\n```\n',
|
|
80
|
+
);
|
|
81
|
+
const entries = entriesOf(editor);
|
|
82
|
+
expect(entries).toHaveLength(2);
|
|
83
|
+
expect(entries[0].id).not.toBe(entries[1].id);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('ignores fences with a real language and non-diagram fences', () => {
|
|
87
|
+
const editor = makeEditor(
|
|
88
|
+
'```js\n' + ART_A + '\n```\n\n```\nplain code here\nmore\nlines\n```\n',
|
|
89
|
+
);
|
|
90
|
+
expect(entriesOf(editor)).toHaveLength(0);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('keeps ids stable when content above changes (positions remap)', () => {
|
|
94
|
+
const editor = makeEditor('First.\n\n```\n' + ART_A + '\n```\n');
|
|
95
|
+
const [before] = entriesOf(editor);
|
|
96
|
+
editor.commands.insertContentAt(0, '<p>inserted above</p>');
|
|
97
|
+
const [after] = entriesOf(editor);
|
|
98
|
+
expect(after.id).toBe(before.id);
|
|
99
|
+
expect(after.pos).toBeGreaterThan(before.pos);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('keeps the id across a self-inflicted fence rewrite', () => {
|
|
103
|
+
const editor = makeEditor('```\n' + ART_A + '\n```\n');
|
|
104
|
+
const [before] = entriesOf(editor);
|
|
105
|
+
const rewritten = ART_A.replace('Beta', 'Delta');
|
|
106
|
+
expect(replaceAsciiFenceText(editor, before.pos, rewritten)).toBe(true);
|
|
107
|
+
const [after] = entriesOf(editor);
|
|
108
|
+
expect(after.id).toBe(before.id);
|
|
109
|
+
expect(findAsciiDiagramBlockPos(editor, before.id)).toBe(after.pos);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('drops a block whose fence no longer parses at all', () => {
|
|
113
|
+
const editor = makeEditor('```\n' + ART_A + '\n```\n');
|
|
114
|
+
const [entry] = entriesOf(editor);
|
|
115
|
+
replaceAsciiFenceText(editor, entry.pos, 'no boxes here\njust text\nlines');
|
|
116
|
+
expect(entriesOf(editor)).toHaveLength(0);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('hysteresis: a registered block degraded to ONE box stays interactive', () => {
|
|
120
|
+
const editor = makeEditor('```\n' + ART_A + '\n```\n');
|
|
121
|
+
const [entry] = entriesOf(editor);
|
|
122
|
+
const oneBox = '┌────────┐\n│ Alpha │\n└────────┘';
|
|
123
|
+
replaceAsciiFenceText(editor, entry.pos, oneBox);
|
|
124
|
+
const after = entriesOf(editor);
|
|
125
|
+
expect(after).toHaveLength(1);
|
|
126
|
+
expect(after[0].id).toBe(entry.id);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('toggleAsciiSource flips visibility without touching the doc', () => {
|
|
130
|
+
const editor = makeEditor('```\n' + ART_A + '\n```\n');
|
|
131
|
+
const [entry] = entriesOf(editor);
|
|
132
|
+
const json = JSON.stringify(editor.state.doc.toJSON());
|
|
133
|
+
expect(isAsciiSourceVisible(editor, entry.id)).toBe(false);
|
|
134
|
+
toggleAsciiSource(editor, entry.id);
|
|
135
|
+
expect(isAsciiSourceVisible(editor, entry.id)).toBe(true);
|
|
136
|
+
toggleAsciiSource(editor, entry.id);
|
|
137
|
+
expect(isAsciiSourceVisible(editor, entry.id)).toBe(false);
|
|
138
|
+
expect(JSON.stringify(editor.state.doc.toJSON())).toBe(json);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('enabled: false disables the plugin entirely', () => {
|
|
142
|
+
const editor = makeEditor('```\n' + ART_A + '\n```\n', { enabled: false });
|
|
143
|
+
expect(ASCII_DIAGRAM_KEY.getState(editor.state)).toBeUndefined();
|
|
144
|
+
});
|
|
145
|
+
});
|