moi-computer 0.8.1-next.0 → 0.9.1-next.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/client/assets/hermes.png +0 -0
- package/client/components/shared/IconGhost.tsx +15 -18
- package/client/components/ui/button.tsx +2 -2
- package/client/components/ui/hover-card.tsx +1 -1
- package/client/components/ui/popover.tsx +3 -1
- package/client/features/chat/ChatEmptyState.tsx +25 -7
- package/client/features/chat/ChatPanel.tsx +70 -28
- package/client/features/chat/ChatPopup.tsx +3 -2
- package/client/features/chat/ChatPromptBubbles.tsx +1 -1
- package/client/features/chat/MarkdownContent.tsx +1 -1
- package/client/features/chat/attachment-staging.ts +153 -0
- package/client/features/chat/chat-send.ts +55 -0
- package/client/features/chat/chat-store.ts +32 -8
- package/client/features/chat/composer/ChatComposer.tsx +190 -77
- package/client/features/chat/{ModelPicker.tsx → composer/ModelPicker.tsx} +29 -25
- package/client/features/chat/composer/banners/ComposerBanner.tsx +1 -1
- package/client/features/chat/composer/banners/WorkspaceAgentAvailabilityBanner.tsx +4 -13
- package/client/features/chat/{model-order.ts → composer/model-order.ts} +16 -0
- package/client/features/chat/composer/useWorkspaceComposerState.tsx +9 -4
- package/client/features/chat/tool-group/format/claude.ts +48 -0
- package/client/features/chat/tool-group/format/codex.ts +126 -0
- package/client/features/chat/tool-group/format/hermes.ts +68 -0
- package/client/features/chat/tool-group/format/index.ts +34 -0
- package/client/features/chat/tool-group/format/mcp.ts +64 -0
- package/client/features/chat/tool-group/format/openclaw.ts +116 -0
- package/client/features/chat/tool-group/format/shared.ts +49 -0
- package/client/features/chat/useChat.ts +5 -10
- package/client/features/dev/ChatStatesPage.tsx +40 -2
- package/client/features/drawings/DrawingLayer.tsx +49 -0
- package/client/features/drawings/DrawingToolbar.tsx +95 -0
- package/client/features/drawings/annotation-availability.ts +10 -0
- package/client/features/drawings/capture-element.ts +45 -0
- package/client/features/drawings/useChatAnnotation.ts +222 -0
- package/client/features/drawings/useDrawingLayer.ts +758 -0
- package/client/features/drawings/useViewBuilderSketch.ts +151 -0
- package/client/features/home/workspace-presentation.tsx +6 -2
- package/client/features/home/workspace-setup/WorkspaceAgentStep.tsx +12 -6
- package/client/features/scratchpad/ScratchpadControls.tsx +19 -5
- package/client/features/views/ViewBuilder.tsx +224 -0
- package/client/features/views/api.ts +1 -0
- package/client/features/views/useViewBuilderActions.ts +24 -3
- package/client/features/views/useViewBuilderDrafts.ts +66 -0
- package/client/features/widgets/Widgets.tsx +23 -39
- package/client/features/workspace/CustomizePanel.tsx +1 -1
- package/client/features/workspace/WorkspaceScreen.tsx +157 -57
- package/client/features/workspace/WorkspaceTabs.tsx +3 -3
- package/client/features/workspace/useFitsSplitLayout.ts +14 -8
- package/client/index.css +8 -0
- package/client/store/ui.ts +19 -0
- package/client/theme.css +33 -8
- package/dist/chunk-3becmq75.js +81 -0
- package/dist/{chunk-kkj86722.js → chunk-6w6j9g4b.js} +78 -78
- package/dist/{chunk-xeyxk7ep.js → chunk-8ew39cvq.js} +4 -4
- package/dist/chunk-c8gtrjwp.js +1 -0
- package/dist/{chunk-2vaafjp4.js → chunk-pp62fnxh.js} +1 -1
- package/dist/{chunk-35fq49qa.js → chunk-sxc24vfp.js} +1 -1
- package/dist/{chunk-xzx7c9hq.js → chunk-v964b5g7.js} +1 -1
- package/dist/chunk-zx33ts9d.css +1 -0
- package/dist/hermes-pznd63x9.png +0 -0
- package/dist/index.html +1 -1
- package/lib/format.ts +21 -7
- package/lib/themes.ts +8 -8
- package/lib/types.ts +8 -1
- package/lib/workspace-types.ts +2 -1
- package/package.json +6 -4
- package/server/api.ts +21 -2
- package/server/bundler/build-applet.ts +35 -44
- package/server/cli-env.ts +2 -2
- package/server/cli.ts +227 -56
- package/server/constants.ts +8 -0
- package/server/control-client.ts +124 -0
- package/server/control.ts +2 -2
- package/server/harness/README.md +74 -23
- package/server/harness/acp/adapter.ts +220 -0
- package/server/harness/acp/archived.ts +88 -0
- package/server/harness/acp/client.ts +328 -0
- package/server/harness/acp/discovery.ts +130 -0
- package/server/harness/acp/run-durations.ts +68 -0
- package/server/harness/acp/session.ts +788 -0
- package/server/harness/acp/wire.ts +132 -0
- package/server/harness/executable.ts +15 -1
- package/server/harness/hermes/NOTES.md +815 -0
- package/server/harness/hermes/discovery.ts +155 -0
- package/server/harness/hermes/index.ts +155 -0
- package/server/harness/hermes/models.ts +79 -0
- package/server/harness/openclaw/NOTES.md +196 -94
- package/server/harness/openclaw/adapter.ts +28 -55
- package/server/harness/openclaw/compat.ts +4 -8
- package/server/harness/openclaw/session.ts +257 -238
- package/server/harness/openclaw/strip.ts +10 -34
- package/server/harness/openclaw/thinking.ts +5 -53
- package/server/harness/registry.ts +3 -1
- package/server/service.ts +2 -2
- package/server/view-builders.ts +4 -2
- package/server/workspace-import.ts +30 -1
- package/workspace/.claude/skills/moi-workspace/SKILL.md +6 -6
- package/client/features/chat/tool-group/format.ts +0 -359
- package/client/features/views/ViewBuilderTab.tsx +0 -152
- package/dist/chunk-gny990qx.css +0 -1
- package/dist/chunk-gt7n06ef.js +0 -1
- package/dist/chunk-n7pga5x3.js +0 -80
- /package/workspace/.claude/skills/moi-workspace/{DESIGN.md → references/DESIGN.md} +0 -0
- /package/workspace/.claude/skills/moi-workspace/{SCRATCHPAD.md → references/SCRATCHPAD.md} +0 -0
|
@@ -0,0 +1,758 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useCallback,
|
|
3
|
+
useEffect,
|
|
4
|
+
useLayoutEffect,
|
|
5
|
+
useMemo,
|
|
6
|
+
useRef,
|
|
7
|
+
useState,
|
|
8
|
+
useSyncExternalStore
|
|
9
|
+
} from 'react'
|
|
10
|
+
import type {
|
|
11
|
+
KeyboardEvent as ReactKeyboardEvent,
|
|
12
|
+
PointerEvent as ReactPointerEvent,
|
|
13
|
+
RefObject
|
|
14
|
+
} from 'react'
|
|
15
|
+
|
|
16
|
+
import { toast } from '@/client/components/ui/toast'
|
|
17
|
+
|
|
18
|
+
import type { DrawingCanvasPointerProps, DrawingLayerProps } from './DrawingLayer'
|
|
19
|
+
import { captureElement, drawingCaptureScale } from './capture-element'
|
|
20
|
+
|
|
21
|
+
export type DrawingPoint = {
|
|
22
|
+
x: number
|
|
23
|
+
y: number
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type DrawingStroke = {
|
|
27
|
+
points: DrawingPoint[]
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type DrawingHistory = {
|
|
31
|
+
past: DrawingStroke[][]
|
|
32
|
+
present: DrawingStroke[]
|
|
33
|
+
future: DrawingStroke[][]
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export type DrawingHistoryAction =
|
|
37
|
+
| { type: 'commit'; stroke: DrawingStroke }
|
|
38
|
+
| { type: 'undo' }
|
|
39
|
+
| { type: 'redo' }
|
|
40
|
+
| { type: 'clear' }
|
|
41
|
+
|
|
42
|
+
export const EMPTY_DRAWING_HISTORY: DrawingHistory = {
|
|
43
|
+
past: [],
|
|
44
|
+
present: [],
|
|
45
|
+
future: []
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function drawingHistoryReducer(
|
|
49
|
+
history: DrawingHistory,
|
|
50
|
+
action: DrawingHistoryAction
|
|
51
|
+
): DrawingHistory {
|
|
52
|
+
if (action.type === 'commit') {
|
|
53
|
+
return {
|
|
54
|
+
past: [...history.past, history.present],
|
|
55
|
+
present: [...history.present, action.stroke],
|
|
56
|
+
future: []
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (action.type === 'undo') {
|
|
61
|
+
const previous = history.past.at(-1)
|
|
62
|
+
if (!previous) return history
|
|
63
|
+
return {
|
|
64
|
+
past: history.past.slice(0, -1),
|
|
65
|
+
present: previous,
|
|
66
|
+
future: [history.present, ...history.future]
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (action.type === 'redo') {
|
|
71
|
+
const next = history.future[0]
|
|
72
|
+
if (!next) return history
|
|
73
|
+
return {
|
|
74
|
+
past: [...history.past, history.present],
|
|
75
|
+
present: next,
|
|
76
|
+
future: history.future.slice(1)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (history.present.length === 0) return history
|
|
81
|
+
return {
|
|
82
|
+
past: [...history.past, history.present],
|
|
83
|
+
present: [],
|
|
84
|
+
future: []
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function pointOnCanvas(
|
|
89
|
+
canvas: HTMLCanvasElement,
|
|
90
|
+
clientX: number,
|
|
91
|
+
clientY: number
|
|
92
|
+
): DrawingPoint {
|
|
93
|
+
const bounds = canvas.getBoundingClientRect()
|
|
94
|
+
return {
|
|
95
|
+
x: ((clientX - bounds.left) / bounds.width) * canvas.width,
|
|
96
|
+
y: ((clientY - bounds.top) / bounds.height) * canvas.height
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function coalescedPointsOnCanvas(canvas: HTMLCanvasElement, event: PointerEvent): DrawingPoint[] {
|
|
101
|
+
const coalesced = event.getCoalescedEvents?.() ?? []
|
|
102
|
+
const samples = coalesced.length > 0 ? coalesced : [event]
|
|
103
|
+
return samples.map(sample => pointOnCanvas(canvas, sample.clientX, sample.clientY))
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
type DrawingLayerSession = {
|
|
107
|
+
id: number
|
|
108
|
+
snapshot: HTMLCanvasElement
|
|
109
|
+
strokeColor: string
|
|
110
|
+
haloColor: string
|
|
111
|
+
targetWidth: number
|
|
112
|
+
targetHeight: number
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
type DrawingExport = {
|
|
116
|
+
sessionId: number
|
|
117
|
+
historyVersion: number
|
|
118
|
+
blob: Blob | null
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
type PendingDrawingExport = Omit<DrawingExport, 'blob'> & {
|
|
122
|
+
promise: Promise<Blob | null>
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
type UseDrawingLayerOptions = {
|
|
126
|
+
mode?: 'annotation' | 'sketch'
|
|
127
|
+
onChange?: (blob: Blob | null) => void
|
|
128
|
+
onEditingStart?: () => void
|
|
129
|
+
onFinish?: (blob: Blob | null) => void | Promise<void>
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Undo/redo/clear availability. Deliberately NOT part of the controls object:
|
|
133
|
+
// it changes on every stroke commit, so components that need it subscribe via
|
|
134
|
+
// useDrawingHistoryState and re-render alone — a pen-up must not re-render
|
|
135
|
+
// the whole screen that hosts the hook.
|
|
136
|
+
export type DrawingHistoryState = {
|
|
137
|
+
canUndo: boolean
|
|
138
|
+
canRedo: boolean
|
|
139
|
+
hasStrokes: boolean
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const EMPTY_HISTORY_STATE: DrawingHistoryState = {
|
|
143
|
+
canUndo: false,
|
|
144
|
+
canRedo: false,
|
|
145
|
+
hasStrokes: false
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export type DrawingControls = {
|
|
149
|
+
active: boolean
|
|
150
|
+
visible: boolean
|
|
151
|
+
starting: boolean
|
|
152
|
+
finishing: boolean
|
|
153
|
+
open: () => Promise<boolean>
|
|
154
|
+
undo: () => void
|
|
155
|
+
redo: () => void
|
|
156
|
+
clear: () => void
|
|
157
|
+
flush: () => Promise<Blob | null>
|
|
158
|
+
deactivate: () => Promise<Blob | null>
|
|
159
|
+
finish: () => Promise<Blob | null>
|
|
160
|
+
cancel: () => Promise<void>
|
|
161
|
+
subscribeHistory: (listener: () => void) => () => void
|
|
162
|
+
getHistoryState: () => DrawingHistoryState
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Subscribe to the per-stroke history flags from the component that renders
|
|
166
|
+
// them (the toolbar), keeping stroke commits out of the host screen's renders.
|
|
167
|
+
export function useDrawingHistoryState(controls: DrawingControls): DrawingHistoryState {
|
|
168
|
+
return useSyncExternalStore(
|
|
169
|
+
controls.subscribeHistory,
|
|
170
|
+
controls.getHistoryState,
|
|
171
|
+
controls.getHistoryState
|
|
172
|
+
)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export type DrawingController = {
|
|
176
|
+
targetRef: RefObject<HTMLDivElement | null>
|
|
177
|
+
controls: DrawingControls
|
|
178
|
+
layerProps: DrawingLayerProps
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function drawPath(
|
|
182
|
+
context: CanvasRenderingContext2D,
|
|
183
|
+
stroke: DrawingStroke,
|
|
184
|
+
color: string,
|
|
185
|
+
width: number
|
|
186
|
+
): void {
|
|
187
|
+
const first = stroke.points[0]
|
|
188
|
+
if (!first) return
|
|
189
|
+
|
|
190
|
+
context.beginPath()
|
|
191
|
+
context.lineCap = 'round'
|
|
192
|
+
context.lineJoin = 'round'
|
|
193
|
+
context.lineWidth = width
|
|
194
|
+
context.strokeStyle = color
|
|
195
|
+
context.moveTo(first.x, first.y)
|
|
196
|
+
for (let index = 1; index < stroke.points.length; index++) {
|
|
197
|
+
const point = stroke.points[index]
|
|
198
|
+
context.lineTo(point.x, point.y)
|
|
199
|
+
}
|
|
200
|
+
if (stroke.points.length === 1) context.lineTo(first.x + 0.01, first.y + 0.01)
|
|
201
|
+
context.stroke()
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function drawDrawingStrokes(
|
|
205
|
+
context: CanvasRenderingContext2D,
|
|
206
|
+
width: number,
|
|
207
|
+
height: number,
|
|
208
|
+
session: DrawingLayerSession,
|
|
209
|
+
strokes: DrawingStroke[],
|
|
210
|
+
draft: DrawingStroke | null = null
|
|
211
|
+
): void {
|
|
212
|
+
const innerWidth = Math.max(3, Math.min(width, height) * 0.005)
|
|
213
|
+
const outerWidth = innerWidth + Math.max(2, innerWidth * 0.75)
|
|
214
|
+
const visible = draft ? [...strokes, draft] : strokes
|
|
215
|
+
for (const stroke of visible) drawPath(context, stroke, session.haloColor, outerWidth)
|
|
216
|
+
for (const stroke of visible) drawPath(context, stroke, session.strokeColor, innerWidth)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function renderDrawingLayer(
|
|
220
|
+
canvas: HTMLCanvasElement,
|
|
221
|
+
session: DrawingLayerSession,
|
|
222
|
+
strokes: DrawingStroke[],
|
|
223
|
+
draft: DrawingStroke | null = null
|
|
224
|
+
): void {
|
|
225
|
+
const context = canvas.getContext('2d')
|
|
226
|
+
if (!context) return
|
|
227
|
+
|
|
228
|
+
context.clearRect(0, 0, canvas.width, canvas.height)
|
|
229
|
+
drawDrawingStrokes(context, canvas.width, canvas.height, session, strokes, draft)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function renderDrawingExport(
|
|
233
|
+
canvas: HTMLCanvasElement,
|
|
234
|
+
session: DrawingLayerSession,
|
|
235
|
+
strokes: DrawingStroke[]
|
|
236
|
+
): void {
|
|
237
|
+
const context = canvas.getContext('2d')
|
|
238
|
+
if (!context) return
|
|
239
|
+
|
|
240
|
+
context.clearRect(0, 0, canvas.width, canvas.height)
|
|
241
|
+
context.drawImage(session.snapshot, 0, 0, canvas.width, canvas.height)
|
|
242
|
+
drawDrawingStrokes(context, canvas.width, canvas.height, session, strokes)
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function canvasBlob(canvas: HTMLCanvasElement): Promise<Blob> {
|
|
246
|
+
return new Promise((resolve, reject) => {
|
|
247
|
+
canvas.toBlob(blob => {
|
|
248
|
+
if (blob) resolve(blob)
|
|
249
|
+
else reject(new Error('Could not export drawing'))
|
|
250
|
+
}, 'image/png')
|
|
251
|
+
})
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function createBlankSession(
|
|
255
|
+
target: HTMLDivElement,
|
|
256
|
+
canvas: HTMLCanvasElement,
|
|
257
|
+
id: number
|
|
258
|
+
): DrawingLayerSession | null {
|
|
259
|
+
const bounds = target.getBoundingClientRect()
|
|
260
|
+
if (bounds.width <= 0 || bounds.height <= 0) return null
|
|
261
|
+
|
|
262
|
+
const styles = getComputedStyle(target)
|
|
263
|
+
const scale = drawingCaptureScale(bounds.width, bounds.height, window.devicePixelRatio)
|
|
264
|
+
const width = Math.max(1, Math.round(bounds.width * scale))
|
|
265
|
+
const height = Math.max(1, Math.round(bounds.height * scale))
|
|
266
|
+
const snapshot = document.createElement('canvas')
|
|
267
|
+
snapshot.width = width
|
|
268
|
+
snapshot.height = height
|
|
269
|
+
const background = styles.getPropertyValue('--background').trim() || '#ffffff'
|
|
270
|
+
const context = snapshot.getContext('2d')
|
|
271
|
+
if (context) {
|
|
272
|
+
context.fillStyle = background
|
|
273
|
+
context.fillRect(0, 0, width, height)
|
|
274
|
+
}
|
|
275
|
+
canvas.width = width
|
|
276
|
+
canvas.height = height
|
|
277
|
+
|
|
278
|
+
return {
|
|
279
|
+
id,
|
|
280
|
+
snapshot,
|
|
281
|
+
strokeColor: styles.getPropertyValue('--primary').trim() || '#2563eb',
|
|
282
|
+
haloColor: background,
|
|
283
|
+
targetWidth: bounds.width,
|
|
284
|
+
targetHeight: bounds.height
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function isEditableTarget(target: EventTarget | null): boolean {
|
|
289
|
+
return (
|
|
290
|
+
target instanceof HTMLInputElement ||
|
|
291
|
+
target instanceof HTMLTextAreaElement ||
|
|
292
|
+
(target instanceof HTMLElement && target.isContentEditable)
|
|
293
|
+
)
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export function useDrawingLayer({
|
|
297
|
+
mode = 'annotation',
|
|
298
|
+
onChange,
|
|
299
|
+
onEditingStart,
|
|
300
|
+
onFinish
|
|
301
|
+
}: UseDrawingLayerOptions = {}): DrawingController {
|
|
302
|
+
const targetRef = useRef<HTMLDivElement>(null)
|
|
303
|
+
const canvasRef = useRef<HTMLCanvasElement>(null)
|
|
304
|
+
const [session, setSession] = useState<DrawingLayerSession | null>(null)
|
|
305
|
+
const sessionRef = useRef(session)
|
|
306
|
+
const [editing, setEditing] = useState(false)
|
|
307
|
+
const editingRef = useRef(false)
|
|
308
|
+
const historyRef = useRef<DrawingHistory>(EMPTY_DRAWING_HISTORY)
|
|
309
|
+
const historyStateRef = useRef<DrawingHistoryState>(EMPTY_HISTORY_STATE)
|
|
310
|
+
const historyListenersRef = useRef(new Set<() => void>())
|
|
311
|
+
const historyVersionRef = useRef(0)
|
|
312
|
+
const draftRef = useRef<DrawingStroke | null>(null)
|
|
313
|
+
const pointerIdRef = useRef<number | null>(null)
|
|
314
|
+
const frameRef = useRef(0)
|
|
315
|
+
const lifecycleRevisionRef = useRef(0)
|
|
316
|
+
const latestExportRef = useRef<DrawingExport | null>(null)
|
|
317
|
+
const pendingExportRef = useRef<PendingDrawingExport | null>(null)
|
|
318
|
+
const [starting, setStarting] = useState(false)
|
|
319
|
+
const startingRef = useRef(false)
|
|
320
|
+
const [finishing, setFinishing] = useState(false)
|
|
321
|
+
const finishingRef = useRef(false)
|
|
322
|
+
const onChangeRef = useRef(onChange)
|
|
323
|
+
const onEditingStartRef = useRef(onEditingStart)
|
|
324
|
+
const onFinishRef = useRef(onFinish)
|
|
325
|
+
onChangeRef.current = onChange
|
|
326
|
+
onEditingStartRef.current = onEditingStart
|
|
327
|
+
onFinishRef.current = onFinish
|
|
328
|
+
|
|
329
|
+
// History flags live outside React state (see DrawingHistoryState): only
|
|
330
|
+
// subscribed components re-render on a stroke commit, not the host screen.
|
|
331
|
+
const publishHistory = useCallback(() => {
|
|
332
|
+
const history = historyRef.current
|
|
333
|
+
const previous = historyStateRef.current
|
|
334
|
+
const next: DrawingHistoryState = {
|
|
335
|
+
canUndo: history.past.length > 0,
|
|
336
|
+
canRedo: history.future.length > 0,
|
|
337
|
+
hasStrokes: history.present.length > 0
|
|
338
|
+
}
|
|
339
|
+
if (
|
|
340
|
+
next.canUndo === previous.canUndo &&
|
|
341
|
+
next.canRedo === previous.canRedo &&
|
|
342
|
+
next.hasStrokes === previous.hasStrokes
|
|
343
|
+
) {
|
|
344
|
+
return
|
|
345
|
+
}
|
|
346
|
+
historyStateRef.current = next
|
|
347
|
+
for (const listener of historyListenersRef.current) listener()
|
|
348
|
+
}, [])
|
|
349
|
+
|
|
350
|
+
const subscribeHistory = useCallback((listener: () => void) => {
|
|
351
|
+
historyListenersRef.current.add(listener)
|
|
352
|
+
return () => {
|
|
353
|
+
historyListenersRef.current.delete(listener)
|
|
354
|
+
}
|
|
355
|
+
}, [])
|
|
356
|
+
|
|
357
|
+
const getHistoryState = useCallback(() => historyStateRef.current, [])
|
|
358
|
+
|
|
359
|
+
const redraw = useCallback((strokes: DrawingStroke[], draft: DrawingStroke | null = null) => {
|
|
360
|
+
if (frameRef.current) {
|
|
361
|
+
cancelAnimationFrame(frameRef.current)
|
|
362
|
+
frameRef.current = 0
|
|
363
|
+
}
|
|
364
|
+
const canvas = canvasRef.current
|
|
365
|
+
const current = sessionRef.current
|
|
366
|
+
if (canvas && current) renderDrawingLayer(canvas, current, strokes, draft)
|
|
367
|
+
}, [])
|
|
368
|
+
|
|
369
|
+
// Pointer moves arrive faster than frames paint. Batching their redraws to
|
|
370
|
+
// one per frame keeps the cost of a move at "append points", so the surface
|
|
371
|
+
// stays smooth as strokes accumulate.
|
|
372
|
+
const scheduleRedraw = useCallback(() => {
|
|
373
|
+
if (frameRef.current) return
|
|
374
|
+
frameRef.current = requestAnimationFrame(() => {
|
|
375
|
+
frameRef.current = 0
|
|
376
|
+
redraw(historyRef.current.present, draftRef.current)
|
|
377
|
+
})
|
|
378
|
+
}, [redraw])
|
|
379
|
+
|
|
380
|
+
const exportCommitted = useCallback(
|
|
381
|
+
(
|
|
382
|
+
current: DrawingLayerSession,
|
|
383
|
+
strokes: DrawingStroke[],
|
|
384
|
+
historyVersion: number
|
|
385
|
+
): Promise<Blob | null> => {
|
|
386
|
+
const cached = latestExportRef.current
|
|
387
|
+
if (cached?.sessionId === current.id && cached.historyVersion === historyVersion) {
|
|
388
|
+
return Promise.resolve(cached.blob)
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const pending = pendingExportRef.current
|
|
392
|
+
if (pending?.sessionId === current.id && pending.historyVersion === historyVersion) {
|
|
393
|
+
return pending.promise
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
if (strokes.length === 0) {
|
|
397
|
+
latestExportRef.current = { sessionId: current.id, historyVersion, blob: null }
|
|
398
|
+
onChangeRef.current?.(null)
|
|
399
|
+
return Promise.resolve(null)
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const exportCanvas = document.createElement('canvas')
|
|
403
|
+
exportCanvas.width = current.snapshot.width
|
|
404
|
+
exportCanvas.height = current.snapshot.height
|
|
405
|
+
renderDrawingExport(exportCanvas, current, strokes)
|
|
406
|
+
|
|
407
|
+
const promise = canvasBlob(exportCanvas)
|
|
408
|
+
.then(blob => {
|
|
409
|
+
if (
|
|
410
|
+
sessionRef.current?.id !== current.id ||
|
|
411
|
+
historyVersionRef.current !== historyVersion
|
|
412
|
+
) {
|
|
413
|
+
return blob
|
|
414
|
+
}
|
|
415
|
+
latestExportRef.current = { sessionId: current.id, historyVersion, blob }
|
|
416
|
+
onChangeRef.current?.(blob)
|
|
417
|
+
return blob
|
|
418
|
+
})
|
|
419
|
+
.catch(() => {
|
|
420
|
+
// Never fall back to an earlier export: a stale image that doesn't
|
|
421
|
+
// match the canvas is worse than no image. Surface the failure only
|
|
422
|
+
// if this export is still the current one.
|
|
423
|
+
if (
|
|
424
|
+
sessionRef.current?.id === current.id &&
|
|
425
|
+
historyVersionRef.current === historyVersion
|
|
426
|
+
) {
|
|
427
|
+
toast.add({ title: 'Couldn’t export the drawing', type: 'error' })
|
|
428
|
+
}
|
|
429
|
+
return null
|
|
430
|
+
})
|
|
431
|
+
.finally(() => {
|
|
432
|
+
if (pendingExportRef.current?.promise === promise) pendingExportRef.current = null
|
|
433
|
+
})
|
|
434
|
+
|
|
435
|
+
pendingExportRef.current = { sessionId: current.id, historyVersion, promise }
|
|
436
|
+
return promise
|
|
437
|
+
},
|
|
438
|
+
[]
|
|
439
|
+
)
|
|
440
|
+
|
|
441
|
+
const cancelStroke = useCallback(() => {
|
|
442
|
+
if (!draftRef.current) return
|
|
443
|
+
const canvas = canvasRef.current
|
|
444
|
+
const pointerId = pointerIdRef.current
|
|
445
|
+
if (canvas && pointerId !== null && canvas.hasPointerCapture(pointerId)) {
|
|
446
|
+
canvas.releasePointerCapture(pointerId)
|
|
447
|
+
}
|
|
448
|
+
draftRef.current = null
|
|
449
|
+
pointerIdRef.current = null
|
|
450
|
+
redraw(historyRef.current.present)
|
|
451
|
+
}, [redraw])
|
|
452
|
+
|
|
453
|
+
const apply = useCallback(
|
|
454
|
+
(action: DrawingHistoryAction) => {
|
|
455
|
+
const current = sessionRef.current
|
|
456
|
+
if (!current || finishingRef.current) return
|
|
457
|
+
const next = drawingHistoryReducer(historyRef.current, action)
|
|
458
|
+
if (next === historyRef.current) return
|
|
459
|
+
historyRef.current = next
|
|
460
|
+
publishHistory()
|
|
461
|
+
draftRef.current = null
|
|
462
|
+
const historyVersion = ++historyVersionRef.current
|
|
463
|
+
redraw(next.present)
|
|
464
|
+
void exportCommitted(current, next.present, historyVersion)
|
|
465
|
+
},
|
|
466
|
+
[exportCommitted, publishHistory, redraw]
|
|
467
|
+
)
|
|
468
|
+
|
|
469
|
+
const installSession = useCallback(
|
|
470
|
+
(next: DrawingLayerSession) => {
|
|
471
|
+
historyRef.current = EMPTY_DRAWING_HISTORY
|
|
472
|
+
historyVersionRef.current = 0
|
|
473
|
+
latestExportRef.current = null
|
|
474
|
+
pendingExportRef.current = null
|
|
475
|
+
draftRef.current = null
|
|
476
|
+
pointerIdRef.current = null
|
|
477
|
+
publishHistory()
|
|
478
|
+
sessionRef.current = next
|
|
479
|
+
setSession(next)
|
|
480
|
+
},
|
|
481
|
+
[publishHistory]
|
|
482
|
+
)
|
|
483
|
+
|
|
484
|
+
const beginEditing = useCallback(() => {
|
|
485
|
+
if (editingRef.current) return
|
|
486
|
+
editingRef.current = true
|
|
487
|
+
setEditing(true)
|
|
488
|
+
onEditingStartRef.current?.()
|
|
489
|
+
}, [])
|
|
490
|
+
|
|
491
|
+
const open = useCallback(async (): Promise<boolean> => {
|
|
492
|
+
const target = targetRef.current
|
|
493
|
+
if (!target || sessionRef.current || startingRef.current) return false
|
|
494
|
+
|
|
495
|
+
const revision = ++lifecycleRevisionRef.current
|
|
496
|
+
startingRef.current = true
|
|
497
|
+
setStarting(true)
|
|
498
|
+
try {
|
|
499
|
+
const snapshot = await captureElement(target)
|
|
500
|
+
if (revision !== lifecycleRevisionRef.current || targetRef.current !== target) return false
|
|
501
|
+
|
|
502
|
+
const bounds = target.getBoundingClientRect()
|
|
503
|
+
const styles = getComputedStyle(target)
|
|
504
|
+
const next: DrawingLayerSession = {
|
|
505
|
+
id: revision,
|
|
506
|
+
snapshot,
|
|
507
|
+
strokeColor: styles.getPropertyValue('--primary').trim() || '#2563eb',
|
|
508
|
+
haloColor: styles.getPropertyValue('--background').trim() || '#ffffff',
|
|
509
|
+
targetWidth: bounds.width,
|
|
510
|
+
targetHeight: bounds.height
|
|
511
|
+
}
|
|
512
|
+
installSession(next)
|
|
513
|
+
beginEditing()
|
|
514
|
+
return true
|
|
515
|
+
} finally {
|
|
516
|
+
if (revision === lifecycleRevisionRef.current) {
|
|
517
|
+
startingRef.current = false
|
|
518
|
+
setStarting(false)
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}, [beginEditing, installSession])
|
|
522
|
+
|
|
523
|
+
const cancel = useCallback((): Promise<void> => {
|
|
524
|
+
const current = sessionRef.current
|
|
525
|
+
lifecycleRevisionRef.current += 1
|
|
526
|
+
startingRef.current = false
|
|
527
|
+
finishingRef.current = false
|
|
528
|
+
setStarting(false)
|
|
529
|
+
setFinishing(false)
|
|
530
|
+
editingRef.current = false
|
|
531
|
+
setEditing(false)
|
|
532
|
+
cancelStroke()
|
|
533
|
+
setSession(null)
|
|
534
|
+
historyRef.current = EMPTY_DRAWING_HISTORY
|
|
535
|
+
publishHistory()
|
|
536
|
+
const pending = pendingExportRef.current
|
|
537
|
+
if (!current || pending?.sessionId !== current.id) {
|
|
538
|
+
sessionRef.current = null
|
|
539
|
+
return Promise.resolve()
|
|
540
|
+
}
|
|
541
|
+
return pending.promise.then(() => {
|
|
542
|
+
if (sessionRef.current?.id === current.id) sessionRef.current = null
|
|
543
|
+
})
|
|
544
|
+
}, [cancelStroke, publishHistory])
|
|
545
|
+
|
|
546
|
+
const flush = useCallback(async (): Promise<Blob | null> => {
|
|
547
|
+
const current = sessionRef.current
|
|
548
|
+
if (!current) return null
|
|
549
|
+
cancelStroke()
|
|
550
|
+
return exportCommitted(current, historyRef.current.present, historyVersionRef.current)
|
|
551
|
+
}, [cancelStroke, exportCommitted])
|
|
552
|
+
|
|
553
|
+
const deactivate = useCallback(async (): Promise<Blob | null> => {
|
|
554
|
+
editingRef.current = false
|
|
555
|
+
setEditing(false)
|
|
556
|
+
return flush()
|
|
557
|
+
}, [flush])
|
|
558
|
+
|
|
559
|
+
const finish = useCallback(async (): Promise<Blob | null> => {
|
|
560
|
+
const current = sessionRef.current
|
|
561
|
+
if (!current || finishingRef.current) return null
|
|
562
|
+
|
|
563
|
+
finishingRef.current = true
|
|
564
|
+
setFinishing(true)
|
|
565
|
+
cancelStroke()
|
|
566
|
+
const historyVersion = historyVersionRef.current
|
|
567
|
+
const strokes = historyRef.current.present
|
|
568
|
+
let blob: Blob | null = null
|
|
569
|
+
if (strokes.length > 0 || historyVersion > 0) {
|
|
570
|
+
blob = await exportCommitted(current, strokes, historyVersion)
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
if (sessionRef.current?.id !== current.id) return blob
|
|
574
|
+
try {
|
|
575
|
+
await onFinishRef.current?.(blob)
|
|
576
|
+
} finally {
|
|
577
|
+
if (sessionRef.current?.id === current.id) {
|
|
578
|
+
lifecycleRevisionRef.current += 1
|
|
579
|
+
editingRef.current = false
|
|
580
|
+
setEditing(false)
|
|
581
|
+
sessionRef.current = null
|
|
582
|
+
setSession(null)
|
|
583
|
+
finishingRef.current = false
|
|
584
|
+
setFinishing(false)
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
return blob
|
|
588
|
+
}, [cancelStroke, exportCommitted])
|
|
589
|
+
|
|
590
|
+
const undo = useCallback(() => apply({ type: 'undo' }), [apply])
|
|
591
|
+
const redo = useCallback(() => apply({ type: 'redo' }), [apply])
|
|
592
|
+
const clear = useCallback(() => apply({ type: 'clear' }), [apply])
|
|
593
|
+
|
|
594
|
+
// Everything returned from here down is identity-stable across renders
|
|
595
|
+
// (callbacks hold state in refs), so hosts can memoize children that take
|
|
596
|
+
// these objects as props. Identities change only with the session lifecycle.
|
|
597
|
+
const pointerProps: DrawingCanvasPointerProps = useMemo(
|
|
598
|
+
() => ({
|
|
599
|
+
onPointerDown: (event: ReactPointerEvent<HTMLCanvasElement>) => {
|
|
600
|
+
if (
|
|
601
|
+
finishingRef.current ||
|
|
602
|
+
!event.isPrimary ||
|
|
603
|
+
(event.pointerType === 'mouse' && event.button !== 0)
|
|
604
|
+
) {
|
|
605
|
+
return
|
|
606
|
+
}
|
|
607
|
+
if (!sessionRef.current && mode === 'sketch') {
|
|
608
|
+
const target = targetRef.current
|
|
609
|
+
if (!target) return
|
|
610
|
+
const next = createBlankSession(
|
|
611
|
+
target,
|
|
612
|
+
event.currentTarget,
|
|
613
|
+
++lifecycleRevisionRef.current
|
|
614
|
+
)
|
|
615
|
+
if (!next) return
|
|
616
|
+
installSession(next)
|
|
617
|
+
}
|
|
618
|
+
if (!sessionRef.current) return
|
|
619
|
+
beginEditing()
|
|
620
|
+
pointerIdRef.current = event.pointerId
|
|
621
|
+
event.currentTarget.setPointerCapture(event.pointerId)
|
|
622
|
+
draftRef.current = {
|
|
623
|
+
points: [pointOnCanvas(event.currentTarget, event.clientX, event.clientY)]
|
|
624
|
+
}
|
|
625
|
+
redraw(historyRef.current.present, draftRef.current)
|
|
626
|
+
},
|
|
627
|
+
onPointerMove: (event: ReactPointerEvent<HTMLCanvasElement>) => {
|
|
628
|
+
if (event.pointerId !== pointerIdRef.current || !draftRef.current) return
|
|
629
|
+
// Appended in place: rebuilding the array here made a long stroke
|
|
630
|
+
// quadratic in its own point count.
|
|
631
|
+
draftRef.current.points.push(
|
|
632
|
+
...coalescedPointsOnCanvas(event.currentTarget, event.nativeEvent)
|
|
633
|
+
)
|
|
634
|
+
scheduleRedraw()
|
|
635
|
+
},
|
|
636
|
+
onPointerUp: (event: ReactPointerEvent<HTMLCanvasElement>) => {
|
|
637
|
+
if (event.pointerId !== pointerIdRef.current || !draftRef.current) return
|
|
638
|
+
const stroke = draftRef.current
|
|
639
|
+
stroke.points.push(...coalescedPointsOnCanvas(event.currentTarget, event.nativeEvent))
|
|
640
|
+
draftRef.current = null
|
|
641
|
+
pointerIdRef.current = null
|
|
642
|
+
if (event.currentTarget.hasPointerCapture(event.pointerId)) {
|
|
643
|
+
event.currentTarget.releasePointerCapture(event.pointerId)
|
|
644
|
+
}
|
|
645
|
+
apply({ type: 'commit', stroke })
|
|
646
|
+
},
|
|
647
|
+
onPointerCancel: (event: ReactPointerEvent<HTMLCanvasElement>) => {
|
|
648
|
+
if (event.pointerId === pointerIdRef.current) cancelStroke()
|
|
649
|
+
}
|
|
650
|
+
}),
|
|
651
|
+
[apply, beginEditing, cancelStroke, installSession, mode, redraw, scheduleRedraw]
|
|
652
|
+
)
|
|
653
|
+
|
|
654
|
+
const onKeyDown = useCallback(
|
|
655
|
+
(event: ReactKeyboardEvent<HTMLDivElement>) => {
|
|
656
|
+
if (isEditableTarget(event.target)) return
|
|
657
|
+
if (event.key === 'Escape' && mode === 'annotation') {
|
|
658
|
+
event.preventDefault()
|
|
659
|
+
void finish()
|
|
660
|
+
return
|
|
661
|
+
}
|
|
662
|
+
if (!(event.metaKey || event.ctrlKey) || event.key.toLowerCase() !== 'z') return
|
|
663
|
+
event.preventDefault()
|
|
664
|
+
if (event.shiftKey) redo()
|
|
665
|
+
else undo()
|
|
666
|
+
},
|
|
667
|
+
[finish, mode, redo, undo]
|
|
668
|
+
)
|
|
669
|
+
|
|
670
|
+
useLayoutEffect(() => {
|
|
671
|
+
if (!session) return
|
|
672
|
+
redraw(historyRef.current.present, draftRef.current)
|
|
673
|
+
if (editing) canvasRef.current?.focus()
|
|
674
|
+
}, [editing, redraw, session])
|
|
675
|
+
|
|
676
|
+
useEffect(() => {
|
|
677
|
+
if (!session || mode === 'sketch') return
|
|
678
|
+
const target = targetRef.current
|
|
679
|
+
if (!target) {
|
|
680
|
+
void cancel()
|
|
681
|
+
return
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
const observer = new ResizeObserver(entries => {
|
|
685
|
+
const bounds = entries[0]?.contentRect
|
|
686
|
+
if (!bounds) return
|
|
687
|
+
if (
|
|
688
|
+
Math.abs(bounds.width - session.targetWidth) > 1 ||
|
|
689
|
+
Math.abs(bounds.height - session.targetHeight) > 1
|
|
690
|
+
) {
|
|
691
|
+
void cancel()
|
|
692
|
+
}
|
|
693
|
+
})
|
|
694
|
+
observer.observe(target)
|
|
695
|
+
return () => observer.disconnect()
|
|
696
|
+
}, [cancel, mode, session])
|
|
697
|
+
|
|
698
|
+
useEffect(
|
|
699
|
+
() => () => {
|
|
700
|
+
lifecycleRevisionRef.current += 1
|
|
701
|
+
editingRef.current = false
|
|
702
|
+
sessionRef.current = null
|
|
703
|
+
if (frameRef.current) cancelAnimationFrame(frameRef.current)
|
|
704
|
+
},
|
|
705
|
+
[]
|
|
706
|
+
)
|
|
707
|
+
|
|
708
|
+
const controls: DrawingControls = useMemo(
|
|
709
|
+
() => ({
|
|
710
|
+
active: editing,
|
|
711
|
+
visible: mode === 'sketch' || session !== null,
|
|
712
|
+
starting,
|
|
713
|
+
finishing,
|
|
714
|
+
open,
|
|
715
|
+
undo,
|
|
716
|
+
redo,
|
|
717
|
+
clear,
|
|
718
|
+
flush,
|
|
719
|
+
deactivate,
|
|
720
|
+
finish,
|
|
721
|
+
cancel,
|
|
722
|
+
subscribeHistory,
|
|
723
|
+
getHistoryState
|
|
724
|
+
}),
|
|
725
|
+
[
|
|
726
|
+
editing,
|
|
727
|
+
mode,
|
|
728
|
+
session,
|
|
729
|
+
starting,
|
|
730
|
+
finishing,
|
|
731
|
+
open,
|
|
732
|
+
undo,
|
|
733
|
+
redo,
|
|
734
|
+
clear,
|
|
735
|
+
flush,
|
|
736
|
+
deactivate,
|
|
737
|
+
finish,
|
|
738
|
+
cancel,
|
|
739
|
+
subscribeHistory,
|
|
740
|
+
getHistoryState
|
|
741
|
+
]
|
|
742
|
+
)
|
|
743
|
+
|
|
744
|
+
const layerProps: DrawingLayerProps = useMemo(
|
|
745
|
+
() => ({
|
|
746
|
+
visible: controls.visible,
|
|
747
|
+
editing: controls.active,
|
|
748
|
+
canvasRef,
|
|
749
|
+
width: session?.snapshot.width ?? 1,
|
|
750
|
+
height: session?.snapshot.height ?? 1,
|
|
751
|
+
pointerProps,
|
|
752
|
+
onKeyDown
|
|
753
|
+
}),
|
|
754
|
+
[controls.active, controls.visible, session, pointerProps, onKeyDown]
|
|
755
|
+
)
|
|
756
|
+
|
|
757
|
+
return useMemo(() => ({ targetRef, controls, layerProps }), [controls, layerProps])
|
|
758
|
+
}
|