moi-computer 0.0.3 → 0.1.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/bunfig.toml +4 -0
- package/client/api/workspaces.ts +277 -6
- package/client/assets/mcp/agentation.png +0 -0
- package/client/assets/mcp/airtable.png +0 -0
- package/client/assets/mcp/apollo.png +0 -0
- package/client/assets/mcp/asana.png +0 -0
- package/client/assets/mcp/box.png +0 -0
- package/client/assets/mcp/chrome.svg +33 -0
- package/client/assets/mcp/clickup.png +0 -0
- package/client/assets/mcp/cloudflare.png +0 -0
- package/client/assets/mcp/context7.png +0 -0
- package/client/assets/mcp/datadog.png +0 -0
- package/client/assets/mcp/dropbox.png +0 -0
- package/client/assets/mcp/elevenlabs.png +0 -0
- package/client/assets/mcp/exa.png +0 -0
- package/client/assets/mcp/firecrawl.png +0 -0
- package/client/assets/mcp/gmail.png +0 -0
- package/client/assets/mcp/googlecalendar.png +0 -0
- package/client/assets/mcp/googledrive.png +0 -0
- package/client/assets/mcp/grafana.png +0 -0
- package/client/assets/mcp/huggingface.png +0 -0
- package/client/assets/mcp/intercom.png +0 -0
- package/client/assets/mcp/monday.png +0 -0
- package/client/assets/mcp/mongodb.png +0 -0
- package/client/assets/mcp/paper.png +0 -0
- package/client/assets/mcp/paypal.png +0 -0
- package/client/assets/mcp/perplexity.png +0 -0
- package/client/assets/mcp/postgresql.png +0 -0
- package/client/assets/mcp/posthog.png +0 -0
- package/client/assets/mcp/puppeteer.png +0 -0
- package/client/assets/mcp/railway.png +0 -0
- package/client/assets/mcp/redis.png +0 -0
- package/client/assets/mcp/salesforce.png +0 -0
- package/client/assets/mcp/shopify.png +0 -0
- package/client/assets/mcp/snowflake.png +0 -0
- package/client/assets/mcp/telegram.png +0 -0
- package/client/assets/mcp/twilio.png +0 -0
- package/client/assets/mcp/zapier.png +0 -0
- package/client/components/App.tsx +7 -1
- package/client/components/ChatInput.tsx +194 -13
- package/client/components/ChatPanel.tsx +116 -47
- package/client/components/ChatPopup.tsx +1 -1
- package/client/components/IconMcp.tsx +21 -0
- package/client/components/McpMenu.tsx +125 -0
- package/client/components/ModelPicker.tsx +147 -0
- package/client/components/Scratchpad.tsx +775 -0
- package/client/components/ShimmerText.tsx +20 -0
- package/client/components/ThreadSelector.tsx +2 -2
- package/client/components/TurnView.tsx +111 -531
- package/client/components/WidgetShell.tsx +1 -1
- package/client/components/WorkspacesPage.tsx +12 -4
- package/client/components/layout/SidebarLayout.tsx +18 -9
- package/client/components/playground/ToolCallsPage.tsx +81 -0
- package/client/components/routes/workspace/[id].tsx +534 -141
- package/client/components/routes/workspace/create.tsx +155 -0
- package/client/components/settings/IconPicker.tsx +590 -0
- package/client/components/settings/SettingsPages.tsx +307 -0
- package/client/components/settings/WorkspaceSettings.tsx +92 -0
- package/client/components/tool-group/CodeBlock.tsx +41 -0
- package/client/components/tool-group/Collapse.tsx +28 -0
- package/client/components/tool-group/ReadImagePreview.tsx +46 -0
- package/client/components/tool-group/SubagentCard.tsx +135 -0
- package/client/components/tool-group/TimelineRow.tsx +147 -0
- package/client/components/tool-group/ToolCallGroup.tsx +231 -0
- package/client/components/tool-group/ToolOutput.tsx +139 -0
- package/client/components/tool-group/detect.ts +93 -0
- package/client/components/tool-group/fixtures.ts +315 -0
- package/client/components/tool-group/format.ts +211 -0
- package/client/components/tool-group/index.ts +9 -0
- package/client/components/ui/dialog.tsx +58 -0
- package/client/components/ui/hover-card.tsx +44 -0
- package/client/components/ui/switch.tsx +23 -0
- package/client/components/ui/tooltip.tsx +54 -0
- package/client/externalize-react.ts +6 -0
- package/client/hooks/useApplet.ts +117 -0
- package/client/hooks/useChat.ts +162 -62
- package/client/hooks/useMeiEvents.ts +9 -2
- package/client/hooks/useStickToBottom.ts +86 -0
- package/client/hooks/useWorkspaceTheme.ts +8 -2
- package/client/index.css +48 -2
- package/client/index.html +9 -5
- package/client/index.tsx +29 -2
- package/client/lib/WorkspaceContext.tsx +5 -8
- package/client/lib/WorkspaceLayoutContext.tsx +30 -10
- package/client/lib/applet-cache.ts +54 -0
- package/client/lib/connection.ts +237 -0
- package/client/lib/flags.ts +10 -0
- package/client/lib/mcp-icons.ts +142 -10
- package/client/lib/preview-turn.ts +37 -0
- package/client/lib/render-icon.ts +173 -0
- package/client/lib/scratch-executor.ts +20 -0
- package/client/lib/uploads.ts +18 -0
- package/client/main.tsx +5 -0
- package/client/store/live.ts +282 -0
- package/client/structured-clone-shim.ts +14 -0
- package/client/theme.css +2 -2
- package/client/vendor/emojibase/en/data.json +1 -0
- package/client/vendor/emojibase/en/messages.json +1 -0
- package/client/vendor/react/development/_impl/react-dom-client.js +15871 -0
- package/client/vendor/react/development/_impl/react-dom.js +223 -0
- package/client/vendor/react/development/_impl/react-jsx-dev-runtime.js +256 -0
- package/client/vendor/react/development/_impl/react-jsx-runtime.js +260 -0
- package/client/vendor/react/development/_impl/react.js +863 -0
- package/client/vendor/react/development/react-dom-client.js +6 -0
- package/client/vendor/react/development/react-dom.js +17 -0
- package/client/vendor/react/development/react-jsx-dev-runtime.js +5 -0
- package/client/vendor/react/development/react-jsx-runtime.js +6 -0
- package/client/vendor/react/development/react.js +47 -0
- package/client/vendor/react/production/_impl/react-dom-client.js +10361 -0
- package/client/vendor/react/production/_impl/react-dom.js +184 -0
- package/client/vendor/react/production/_impl/react-jsx-dev-runtime.js +37 -0
- package/client/vendor/react/production/_impl/react-jsx-runtime.js +60 -0
- package/client/vendor/react/production/_impl/react.js +428 -0
- package/client/vendor/react/production/react-dom-client.js +6 -0
- package/client/vendor/react/production/react-dom.js +17 -0
- package/client/vendor/react/production/react-jsx-dev-runtime.js +5 -0
- package/client/vendor/react/production/react-jsx-runtime.js +6 -0
- package/client/vendor/react/production/react.js +47 -0
- package/dist/agentation-4s0nn45p.png +0 -0
- package/dist/airtable-hsa66v6q.png +0 -0
- package/dist/apollo-2ed5dy2d.png +0 -0
- package/dist/asana-tvgtwbx2.png +0 -0
- package/dist/box-w3n36t5g.png +0 -0
- package/dist/chrome-tspm94rw.svg +33 -0
- package/dist/chunk-kc6e9nd4.css +1 -0
- package/dist/chunk-yf3p7qg7.js +673 -0
- package/dist/clickup-qqrqqvqb.png +0 -0
- package/dist/cloudflare-j9p931v6.png +0 -0
- package/dist/context7-8ht0xjfy.png +0 -0
- package/dist/datadog-70ne3yzt.png +0 -0
- package/dist/dropbox-4m5s9mgr.png +0 -0
- package/dist/elevenlabs-2nzzej1t.png +0 -0
- package/dist/exa-qqj3vr0x.png +0 -0
- package/dist/firecrawl-w2t8nehx.png +0 -0
- package/dist/gmail-sg6a5v3x.png +0 -0
- package/dist/googlecalendar-nqhh2v7r.png +0 -0
- package/dist/googledrive-6s8xyab7.png +0 -0
- package/dist/grafana-1yfsnwr6.png +0 -0
- package/dist/huggingface-kxzaxj83.png +0 -0
- package/dist/index.html +10 -6
- package/dist/intercom-td763jt8.png +0 -0
- package/dist/monday-jdbs4wwf.png +0 -0
- package/dist/mongodb-emzxvhd5.png +0 -0
- package/dist/paper-zbt34fab.png +0 -0
- package/dist/paypal-kwwf7xsc.png +0 -0
- package/dist/perplexity-4y63rxvj.png +0 -0
- package/dist/postgresql-qsspbsr1.png +0 -0
- package/dist/posthog-sphppc4w.png +0 -0
- package/dist/puppeteer-2xjt1a9b.png +0 -0
- package/dist/railway-834de8d2.png +0 -0
- package/dist/redis-spm6w2m1.png +0 -0
- package/dist/salesforce-1mk56v4z.png +0 -0
- package/dist/shopify-90dadaav.png +0 -0
- package/dist/snowflake-hfpykpgy.png +0 -0
- package/dist/telegram-v6tjyrs7.png +0 -0
- package/dist/twilio-dpyjdwpj.png +0 -0
- package/dist/zapier-p67qfch0.png +0 -0
- package/lib/attachment-note.ts +48 -0
- package/lib/claude-adapter.ts +219 -29
- package/lib/format.ts +32 -0
- package/lib/types.ts +314 -6
- package/package.json +10 -5
- package/server/CLAUDE.md +3 -0
- package/server/agent.ts +25 -118
- package/server/api.ts +593 -0
- package/server/applets.ts +396 -0
- package/server/build-applet.ts +580 -0
- package/server/cc-session.ts +611 -0
- package/server/cli-colors.ts +17 -0
- package/server/cli-ui.ts +52 -0
- package/server/cli.ts +964 -72
- package/server/control.ts +166 -19
- package/server/debug.ts +10 -0
- package/server/events.ts +24 -0
- package/server/functions-worker.ts +18 -2
- package/server/functions.ts +59 -12
- package/server/icon.ts +13 -0
- package/server/layout.ts +25 -3
- package/server/mcp.ts +125 -0
- package/server/moi-scaffold.ts +85 -0
- package/server/openclaw-session.ts +41 -10
- package/server/openclaw.ts +22 -0
- package/server/preview.ts +70 -0
- package/server/registry.ts +32 -2
- package/server/scratchpad-executor.ts +380 -0
- package/server/scratchpad-relay.ts +45 -0
- package/server/scratchpad.ts +171 -0
- package/server/skill-version.ts +146 -0
- package/server/skills-template.ts +6 -1
- package/server/state.ts +22 -36
- package/server/static.ts +23 -0
- package/server/status.ts +94 -0
- package/server/thread-config.ts +125 -0
- package/server/uploads.ts +0 -0
- package/server/vendor.ts +63 -0
- package/server/views.ts +0 -0
- package/server/web.ts +108 -253
- package/server/widgets.ts +52 -142
- package/server/workspace-config.ts +37 -0
- package/server/workspace-env.ts +407 -0
- package/server/workspace-init.ts +66 -0
- package/workspace/.claude/skills/moi-workspace/DESIGN.md +109 -0
- package/workspace/.claude/skills/moi-workspace/SCRATCHPAD.md +57 -0
- package/workspace/.claude/skills/moi-workspace/SKILL.md +252 -0
- package/workspace/.claude/skills/moi-workspace/VIEW-DESIGN.md +22 -0
- package/client/hooks/useWidget.ts +0 -72
- package/client/hooks/useWidgetList.ts +0 -30
- package/client/lib/ws.ts +0 -129
- package/client/store/chat.tsx +0 -162
- package/dist/chunk-4615fa69.js +0 -67
- package/dist/chunk-582xmy05.css +0 -1
- package/server/build-widget.ts +0 -343
- package/workspace/.claude/skills/widgets/DESIGN.md +0 -630
- package/workspace/.claude/skills/widgets/SKILL.md +0 -176
|
@@ -0,0 +1,775 @@
|
|
|
1
|
+
import { Fragment, type ReactElement, useCallback, useEffect, useRef, useState } from 'react'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type Editor,
|
|
5
|
+
type StyleProp,
|
|
6
|
+
type TLComponents,
|
|
7
|
+
type TLDefaultColorStyle,
|
|
8
|
+
type TLDefaultDashStyle,
|
|
9
|
+
type TLDefaultFillStyle,
|
|
10
|
+
type TLDefaultSizeStyle,
|
|
11
|
+
type TLEditorSnapshot,
|
|
12
|
+
type TLUiOverrides,
|
|
13
|
+
DefaultColorStyle,
|
|
14
|
+
DefaultDashStyle,
|
|
15
|
+
DefaultFillStyle,
|
|
16
|
+
DefaultFontStyle,
|
|
17
|
+
DefaultHorizontalAlignStyle,
|
|
18
|
+
DefaultSizeStyle,
|
|
19
|
+
DefaultVerticalAlignStyle,
|
|
20
|
+
GeoShapeGeoStyle,
|
|
21
|
+
Tldraw,
|
|
22
|
+
getSnapshot,
|
|
23
|
+
loadSnapshot,
|
|
24
|
+
react
|
|
25
|
+
} from 'tldraw'
|
|
26
|
+
import 'tldraw/tldraw.css'
|
|
27
|
+
import { motion } from 'motion/react'
|
|
28
|
+
import {
|
|
29
|
+
IconArrowUpRight,
|
|
30
|
+
IconEraser,
|
|
31
|
+
IconHandStop,
|
|
32
|
+
IconHighlight,
|
|
33
|
+
IconLine,
|
|
34
|
+
IconPointer2,
|
|
35
|
+
IconSketching,
|
|
36
|
+
IconSquare,
|
|
37
|
+
IconSticker2,
|
|
38
|
+
IconTypography
|
|
39
|
+
} from '@tabler/icons-react'
|
|
40
|
+
|
|
41
|
+
import { cn } from '@/client/lib/cn'
|
|
42
|
+
import { useWorkspaceId } from '@/client/lib/WorkspaceContext'
|
|
43
|
+
import { setScratchExecutor } from '@/client/lib/scratch-executor'
|
|
44
|
+
import { type MeiEvent, useMeiEvent } from '@/client/hooks/useMeiEvents'
|
|
45
|
+
import type { ScratchOp, ScratchOpResult } from '@/lib/types'
|
|
46
|
+
|
|
47
|
+
// Identifies this tab's writes so it can ignore the `scratchpad:updated` echo of
|
|
48
|
+
// its own save (see the MEI reload below). Per page load.
|
|
49
|
+
const ORIGIN_ID = Math.random().toString(36).slice(2)
|
|
50
|
+
|
|
51
|
+
const AUTOSAVE_MS = 500
|
|
52
|
+
|
|
53
|
+
// tldraw license key, inlined at build time from the PUBLIC_TLDRAW_LICENSE_KEY
|
|
54
|
+
// env var (Bun's prefix-based env inlining — see bunfig.toml `[serve.static] env`
|
|
55
|
+
// for dev and scripts/build-client.ts for prod). The key is public by design
|
|
56
|
+
// (domain-scoped, ships in the client bundle). Empty/unset → undefined →
|
|
57
|
+
// tldraw's default unlicensed watermark. See docs/moi-scratchpad.md.
|
|
58
|
+
// Inlining requires the var to be set when the server process starts — the CLI
|
|
59
|
+
// launcher defaults it (server/cli.ts); a bare ref here throws in the browser.
|
|
60
|
+
const LICENSE_KEY = process.env.PUBLIC_TLDRAW_LICENSE_KEY || undefined
|
|
61
|
+
|
|
62
|
+
// Execute a relayed op in this tab. Only `view` is relayed now — rasterizing the
|
|
63
|
+
// canvas to a PNG needs the browser (`editor.toImageDataUrl`); every mutation runs
|
|
64
|
+
// server-side against the disk snapshot (see server/scratchpad-executor.ts), so a
|
|
65
|
+
// non-view op arriving here is unexpected.
|
|
66
|
+
function makeExecutor(editor: Editor) {
|
|
67
|
+
return async (op: ScratchOp): Promise<ScratchOpResult> => {
|
|
68
|
+
if (op.kind !== 'view') {
|
|
69
|
+
throw new Error(`Scratchpad op "${op.kind}" runs on the server, not the browser.`)
|
|
70
|
+
}
|
|
71
|
+
const ids = [...editor.getCurrentPageShapeIds()]
|
|
72
|
+
if (ids.length === 0) throw new Error('Canvas is empty — nothing to view.')
|
|
73
|
+
const { url } = await editor.toImageDataUrl(ids, {
|
|
74
|
+
format: 'png',
|
|
75
|
+
background: true,
|
|
76
|
+
padding: 32
|
|
77
|
+
})
|
|
78
|
+
return { image: url }
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// --- Scratchpad UI shell --------------------------------------------------
|
|
83
|
+
// We replace tldraw's full UI with a curated subset (see docs/moi-scratchpad.md
|
|
84
|
+
// for the prioritization). Backbone v1: a custom vertical tool bar on the left,
|
|
85
|
+
// most menus dropped, pages disabled, export hard-removed, grid on. The default
|
|
86
|
+
// (contextual) style panel and zoom control are kept as-is for now; style
|
|
87
|
+
// trimming is a follow-up.
|
|
88
|
+
|
|
89
|
+
// Drop tldraw's built-in chrome we replace with custom overlays (tool bar, style
|
|
90
|
+
// bar). `null` removes a component entirely. NavigationPanel is kept — we like its
|
|
91
|
+
// native zoom control — but Minimap is nulled, which also strips its toggle button
|
|
92
|
+
// from the panel, leaving just the zoom menu. Kept on defaults: NavigationPanel
|
|
93
|
+
// (zoom), ContextMenu, KeyboardShortcutsDialog.
|
|
94
|
+
const SCRATCH_COMPONENTS: TLComponents = {
|
|
95
|
+
Toolbar: null,
|
|
96
|
+
StylePanel: null,
|
|
97
|
+
Minimap: null,
|
|
98
|
+
MainMenu: null,
|
|
99
|
+
PageMenu: null,
|
|
100
|
+
ActionsMenu: null,
|
|
101
|
+
QuickActions: null,
|
|
102
|
+
HelpMenu: null,
|
|
103
|
+
DebugMenu: null
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Remove dropped tools (kills their shortcuts too) and hard-disable export/print.
|
|
107
|
+
// Deleting an unknown action id is a harmless no-op.
|
|
108
|
+
const SCRATCH_OVERRIDES: TLUiOverrides = {
|
|
109
|
+
tools(_editor, tools) {
|
|
110
|
+
delete tools.frame
|
|
111
|
+
delete tools.laser
|
|
112
|
+
return tools
|
|
113
|
+
},
|
|
114
|
+
actions(_editor, actions) {
|
|
115
|
+
for (const id of ['export-as-svg', 'export-as-png', 'copy-as-svg', 'copy-as-png', 'print']) {
|
|
116
|
+
delete actions[id]
|
|
117
|
+
}
|
|
118
|
+
return actions
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// One page only — disables the page selector and all multi-page UI.
|
|
123
|
+
const SCRATCH_OPTIONS = { maxPages: 1 }
|
|
124
|
+
|
|
125
|
+
type IconC = typeof IconPointer2
|
|
126
|
+
|
|
127
|
+
// The curated left-bar entries, grouped by `sep` dividers. `tool` selects a
|
|
128
|
+
// tldraw tool; `geo` selects the geo tool (rectangle); `menu` is a placeholder
|
|
129
|
+
// (noop for now); `sep` is a divider.
|
|
130
|
+
type ToolEntry =
|
|
131
|
+
| { kind: 'tool'; id: string; label: string; Icon: IconC }
|
|
132
|
+
| { kind: 'geo'; label: string; Icon: IconC }
|
|
133
|
+
| { kind: 'menu'; label: string; Icon: IconC }
|
|
134
|
+
| { kind: 'sep' }
|
|
135
|
+
|
|
136
|
+
const TOOL_ENTRIES: ToolEntry[] = [
|
|
137
|
+
{ kind: 'tool', id: 'select', label: 'Select', Icon: IconPointer2 },
|
|
138
|
+
{ kind: 'tool', id: 'hand', label: 'Hand', Icon: IconHandStop },
|
|
139
|
+
{ kind: 'sep' },
|
|
140
|
+
{ kind: 'tool', id: 'draw', label: 'Pencil', Icon: IconSketching },
|
|
141
|
+
{ kind: 'geo', label: 'Rectangle', Icon: IconSquare },
|
|
142
|
+
{ kind: 'tool', id: 'note', label: 'Sticker', Icon: IconSticker2 },
|
|
143
|
+
{ kind: 'tool', id: 'text', label: 'Text', Icon: IconTypography },
|
|
144
|
+
{ kind: 'tool', id: 'line', label: 'Line', Icon: IconLine },
|
|
145
|
+
{ kind: 'tool', id: 'arrow', label: 'Arrow', Icon: IconArrowUpRight },
|
|
146
|
+
{ kind: 'sep' },
|
|
147
|
+
{ kind: 'tool', id: 'highlight', label: 'Highlighter', Icon: IconHighlight },
|
|
148
|
+
{ kind: 'tool', id: 'eraser', label: 'Eraser', Icon: IconEraser }
|
|
149
|
+
// Temporarily hidden — `menu` kind + IconDots import kept for future use.
|
|
150
|
+
// { kind: "menu", label: "Menu", Icon: IconDots },
|
|
151
|
+
]
|
|
152
|
+
|
|
153
|
+
type ToolButtonProps = {
|
|
154
|
+
label: string
|
|
155
|
+
active: boolean
|
|
156
|
+
Icon: IconC
|
|
157
|
+
onClick: () => void
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function ToolButton({ label, active, Icon, onClick }: ToolButtonProps) {
|
|
161
|
+
return (
|
|
162
|
+
<button
|
|
163
|
+
type="button"
|
|
164
|
+
title={label}
|
|
165
|
+
onClick={onClick}
|
|
166
|
+
className={cn(
|
|
167
|
+
'flex size-9 items-center justify-center rounded-lg text-muted-foreground hover:bg-muted hover:text-foreground',
|
|
168
|
+
active &&
|
|
169
|
+
'bg-primary text-primary-foreground hover:bg-primary hover:text-primary-foreground'
|
|
170
|
+
)}
|
|
171
|
+
>
|
|
172
|
+
<Icon size={20} stroke={1.5} />
|
|
173
|
+
</button>
|
|
174
|
+
)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
type ScratchToolbarProps = { editor: Editor }
|
|
178
|
+
|
|
179
|
+
// tldraw shares style props (dash, size, font…) globally across tools, so a
|
|
180
|
+
// dashed pen would leak into a freshly-picked rect. On each tool activation we
|
|
181
|
+
// pin the styles that tool doesn't expose to fixed values, keeping every tool
|
|
182
|
+
// visually independent.
|
|
183
|
+
function applyToolLocks(editor: Editor, toolId: string) {
|
|
184
|
+
if (toolId === 'geo') {
|
|
185
|
+
// Rects: always a rough (hand-drawn) outline at a consistent stroke — never
|
|
186
|
+
// inherit the pen's dashed/solid or its size.
|
|
187
|
+
editor.setStyleForNextShapes(DefaultDashStyle, 'draw')
|
|
188
|
+
editor.setStyleForNextShapes(DefaultSizeStyle, 'm')
|
|
189
|
+
} else if (toolId === 'note') {
|
|
190
|
+
editor.setStyleForNextShapes(DefaultFontStyle, 'draw')
|
|
191
|
+
editor.setStyleForNextShapes(DefaultHorizontalAlignStyle, 'middle')
|
|
192
|
+
editor.setStyleForNextShapes(DefaultVerticalAlignStyle, 'middle')
|
|
193
|
+
ensurePaletteColor(editor, 'sticker', 'yellow')
|
|
194
|
+
} else if (toolId === 'text') {
|
|
195
|
+
editor.setStyleForNextShapes(DefaultFontStyle, 'draw')
|
|
196
|
+
editor.setStyleForNextShapes(DefaultHorizontalAlignStyle, 'middle')
|
|
197
|
+
editor.setStyleForNextShapes(DefaultVerticalAlignStyle, 'middle')
|
|
198
|
+
} else if (toolId === 'highlight') {
|
|
199
|
+
ensurePaletteColor(editor, 'highlight', 'yellow')
|
|
200
|
+
} else if (toolId === 'arrow') {
|
|
201
|
+
// Arrows don't expose dash — pin it rough to match the hand-drawn look and
|
|
202
|
+
// never inherit a dashed/solid stroke from a previous tool.
|
|
203
|
+
editor.setStyleForNextShapes(DefaultDashStyle, 'draw')
|
|
204
|
+
} else if (toolId === 'draw' || toolId === 'line') {
|
|
205
|
+
// Pen/line only offer rough or dashed — if we're arriving from a rect's
|
|
206
|
+
// 'solid', fall back to rough so a valid dash is always selected.
|
|
207
|
+
const dash = editor.getStyleForNextShape(DefaultDashStyle)
|
|
208
|
+
if (dash !== 'draw' && dash !== 'dashed') {
|
|
209
|
+
editor.setStyleForNextShapes(DefaultDashStyle, 'draw')
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Custom vertical tool bar, rendered as an overlay over the canvas (outside
|
|
215
|
+
// tldraw's own UI layout). Tracks the current tool reactively via `react`.
|
|
216
|
+
function ScratchToolbar({ editor }: ScratchToolbarProps) {
|
|
217
|
+
const [toolId, setToolId] = useState(() => editor.getCurrentToolId())
|
|
218
|
+
|
|
219
|
+
useEffect(() => {
|
|
220
|
+
setToolId(editor.getCurrentToolId())
|
|
221
|
+
return react('scratch current tool', () => setToolId(editor.getCurrentToolId()))
|
|
222
|
+
}, [editor])
|
|
223
|
+
|
|
224
|
+
const activate = (toolId: string) => {
|
|
225
|
+
editor.run(() => {
|
|
226
|
+
applyToolLocks(editor, toolId)
|
|
227
|
+
editor.setCurrentTool(toolId)
|
|
228
|
+
})
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const selectRectangle = () => {
|
|
232
|
+
editor.run(() => {
|
|
233
|
+
editor.setStyleForNextShapes(GeoShapeGeoStyle, 'rectangle')
|
|
234
|
+
applyToolLocks(editor, 'geo')
|
|
235
|
+
editor.setCurrentTool('geo')
|
|
236
|
+
})
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return (
|
|
240
|
+
<div className="absolute top-1/2 left-3 z-10 -translate-y-1/2">
|
|
241
|
+
<motion.div
|
|
242
|
+
initial={{ x: -72, opacity: 0 }}
|
|
243
|
+
animate={{ x: 0, opacity: 1 }}
|
|
244
|
+
transition={{ type: 'spring', bounce: 0.4, duration: 0.55 }}
|
|
245
|
+
className="flex flex-col gap-1 rounded-xl bg-background/95 p-1 shadow-[0_2px_8px_rgba(0,0,0,0.08),0_4px_16px_rgba(0,0,0,0.06),0_0_0_1px_rgba(0,0,0,0.04)] backdrop-blur"
|
|
246
|
+
>
|
|
247
|
+
{TOOL_ENTRIES.map((entry, i) => {
|
|
248
|
+
if (entry.kind === 'sep') {
|
|
249
|
+
return <div key={`sep-${i}`} className="mx-1 my-0.5 h-px bg-border" />
|
|
250
|
+
}
|
|
251
|
+
if (entry.kind === 'geo') {
|
|
252
|
+
return (
|
|
253
|
+
<ToolButton
|
|
254
|
+
key="geo"
|
|
255
|
+
label={entry.label}
|
|
256
|
+
Icon={entry.Icon}
|
|
257
|
+
active={toolId === 'geo'}
|
|
258
|
+
onClick={selectRectangle}
|
|
259
|
+
/>
|
|
260
|
+
)
|
|
261
|
+
}
|
|
262
|
+
if (entry.kind === 'menu') {
|
|
263
|
+
return (
|
|
264
|
+
<ToolButton
|
|
265
|
+
key="menu"
|
|
266
|
+
label={entry.label}
|
|
267
|
+
Icon={entry.Icon}
|
|
268
|
+
active={false}
|
|
269
|
+
onClick={() => {}}
|
|
270
|
+
/>
|
|
271
|
+
)
|
|
272
|
+
}
|
|
273
|
+
return (
|
|
274
|
+
<ToolButton
|
|
275
|
+
key={entry.id}
|
|
276
|
+
label={entry.label}
|
|
277
|
+
Icon={entry.Icon}
|
|
278
|
+
active={toolId === entry.id}
|
|
279
|
+
onClick={() => activate(entry.id)}
|
|
280
|
+
/>
|
|
281
|
+
)
|
|
282
|
+
})}
|
|
283
|
+
</motion.div>
|
|
284
|
+
</div>
|
|
285
|
+
)
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// --- Second bar: per-tool contextual style controls -----------------------
|
|
289
|
+
// Each tool exposes a different subset of controls (color/size/dash/fill). The
|
|
290
|
+
// sub-bar shows the active tool's set, or — when the Select tool has a selection
|
|
291
|
+
// — the union of the selected shapes' sets. Size maps small→'m', large→'xl'.
|
|
292
|
+
// Sticker & text also lock align=center and font=rough (set on mount, not shown).
|
|
293
|
+
|
|
294
|
+
type StyleControl = 'color' | 'size' | 'dash' | 'fill'
|
|
295
|
+
const CONTROL_ORDER: StyleControl[] = ['color', 'size', 'dash', 'fill']
|
|
296
|
+
|
|
297
|
+
// Controls exposed while a creating tool is active.
|
|
298
|
+
const CONTROLS_BY_TOOL: Record<string, StyleControl[]> = {
|
|
299
|
+
draw: ['color', 'size', 'dash'],
|
|
300
|
+
geo: ['color', 'fill'],
|
|
301
|
+
note: ['color', 'size'],
|
|
302
|
+
text: ['color', 'size'],
|
|
303
|
+
line: ['color', 'dash', 'size'],
|
|
304
|
+
highlight: ['color', 'size'],
|
|
305
|
+
arrow: ['color', 'size']
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// Controls exposed per selected shape type (Select tool with a selection).
|
|
309
|
+
const CONTROLS_BY_SHAPE: Record<string, StyleControl[]> = {
|
|
310
|
+
draw: ['color', 'size', 'dash'],
|
|
311
|
+
geo: ['color', 'fill'],
|
|
312
|
+
note: ['color', 'size'],
|
|
313
|
+
text: ['color', 'size'],
|
|
314
|
+
line: ['color', 'dash', 'size'],
|
|
315
|
+
highlight: ['color', 'size'],
|
|
316
|
+
arrow: ['color', 'size']
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// Three contextual color palettes (all subsets of tldraw's color enum):
|
|
320
|
+
// regular (most tools), highlighter-friendly, and sticky-note pastels.
|
|
321
|
+
type Swatch = { value: TLDefaultColorStyle; klass: string }
|
|
322
|
+
type PaletteKind = 'regular' | 'highlight' | 'sticker'
|
|
323
|
+
type Palette = { swatches: Swatch[] }
|
|
324
|
+
|
|
325
|
+
// Swatch display hexes are tldraw's actual light-theme colors, picked to match
|
|
326
|
+
// how each tool paints: regular → stroke `solid`, highlight → `highlightSrgb`,
|
|
327
|
+
// sticker → `noteFill`.
|
|
328
|
+
const PALETTES: Record<PaletteKind, Palette> = {
|
|
329
|
+
regular: {
|
|
330
|
+
swatches: [
|
|
331
|
+
{ value: 'black', klass: 'bg-[#1d1d1d]' },
|
|
332
|
+
{ value: 'red', klass: 'bg-[#e03131]' },
|
|
333
|
+
{ value: 'yellow', klass: 'bg-[#f1ac4b]' },
|
|
334
|
+
{ value: 'green', klass: 'bg-[#099268]' },
|
|
335
|
+
{ value: 'blue', klass: 'bg-[#4465e9]' },
|
|
336
|
+
{ value: 'grey', klass: 'bg-[#9fa8b2]' }
|
|
337
|
+
]
|
|
338
|
+
},
|
|
339
|
+
highlight: {
|
|
340
|
+
swatches: [
|
|
341
|
+
{ value: 'yellow', klass: 'bg-[#fddd00]' },
|
|
342
|
+
{ value: 'green', klass: 'bg-[#00ffc8]' },
|
|
343
|
+
{ value: 'blue', klass: 'bg-[#10acff]' }
|
|
344
|
+
]
|
|
345
|
+
},
|
|
346
|
+
sticker: {
|
|
347
|
+
swatches: [
|
|
348
|
+
{ value: 'yellow', klass: 'bg-[#fed49a]' },
|
|
349
|
+
{ value: 'blue', klass: 'bg-[#8aa3ff]' },
|
|
350
|
+
{ value: 'green', klass: 'bg-[#6fc896]' }
|
|
351
|
+
]
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// Which palette applies right now — by active creating tool, else by the single
|
|
356
|
+
// selected shape type, else regular.
|
|
357
|
+
function paletteKind(editor: Editor): PaletteKind {
|
|
358
|
+
const toolId = editor.getCurrentToolId()
|
|
359
|
+
if (toolId === 'highlight') return 'highlight'
|
|
360
|
+
if (toolId === 'note') return 'sticker'
|
|
361
|
+
if (CONTROLS_BY_TOOL[toolId]) return 'regular'
|
|
362
|
+
|
|
363
|
+
const types = new Set(editor.getSelectedShapeIds().map(id => editor.getShape(id)?.type))
|
|
364
|
+
if (types.size === 1) {
|
|
365
|
+
if (types.has('highlight')) return 'highlight'
|
|
366
|
+
if (types.has('note')) return 'sticker'
|
|
367
|
+
}
|
|
368
|
+
return 'regular'
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// If the next-shape color isn't in `kind`'s palette, snap it to a sensible
|
|
372
|
+
// default so the swatch row always has an active selection.
|
|
373
|
+
function ensurePaletteColor(editor: Editor, kind: PaletteKind, fallback: TLDefaultColorStyle) {
|
|
374
|
+
const values = PALETTES[kind].swatches.map(s => s.value)
|
|
375
|
+
if (!values.includes(editor.getStyleForNextShape(DefaultColorStyle))) {
|
|
376
|
+
editor.setStyleForNextShapes(DefaultColorStyle, fallback)
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// Two sizes only — small→'m', large→'xl'. Each option carries previews for the
|
|
381
|
+
// three size renderings (see sizeGlyphKind): `dot` blob for most tools, `stroke`
|
|
382
|
+
// pen-icon weight for the pencil, `glyph` letter px for text.
|
|
383
|
+
const SIZE_OPTIONS: {
|
|
384
|
+
value: TLDefaultSizeStyle
|
|
385
|
+
label: string
|
|
386
|
+
dot: string
|
|
387
|
+
stroke: number
|
|
388
|
+
glyph: number
|
|
389
|
+
}[] = [
|
|
390
|
+
{ value: 'm', label: 'Small', dot: 'size-1.5', stroke: 1.5, glyph: 14 },
|
|
391
|
+
{ value: 'xl', label: 'Large', dot: 'size-3', stroke: 3, glyph: 22 }
|
|
392
|
+
]
|
|
393
|
+
|
|
394
|
+
// Rough (hand-drawn) vs dashed stroke.
|
|
395
|
+
const DASH_OPTIONS: { value: TLDefaultDashStyle; label: string; preview: string }[] = [
|
|
396
|
+
{ value: 'draw', label: 'Rough', preview: 'border-b-2 border-current' },
|
|
397
|
+
{ value: 'dashed', label: 'Dash', preview: 'border-b-2 border-dashed border-current' }
|
|
398
|
+
]
|
|
399
|
+
|
|
400
|
+
// Three fills: empty, full color, hatch. (tldraw's translucent 'semi' read as
|
|
401
|
+
// "stroke only", so we drop it — 'solid' is the fullest native color fill.)
|
|
402
|
+
const FILL_OPTIONS: { value: TLDefaultFillStyle; label: string; preview: string }[] = [
|
|
403
|
+
// tldraw quirk (see defaultFills): fillStyle 'solid' paints the lighter "semi"
|
|
404
|
+
// color, while 'fill' paints the true solid (body === border color).
|
|
405
|
+
{ value: 'none', label: 'None', preview: 'border border-foreground/50' },
|
|
406
|
+
{ value: 'solid', label: 'Semi', preview: 'border border-foreground/40 bg-foreground/25' },
|
|
407
|
+
{ value: 'fill', label: 'Solid', preview: 'border border-foreground bg-foreground' },
|
|
408
|
+
{
|
|
409
|
+
value: 'pattern',
|
|
410
|
+
label: 'Pattern',
|
|
411
|
+
preview:
|
|
412
|
+
'border border-foreground/40 bg-[repeating-linear-gradient(45deg,currentColor_0,currentColor_1px,transparent_1px,transparent_3px)]'
|
|
413
|
+
}
|
|
414
|
+
]
|
|
415
|
+
|
|
416
|
+
// Read the active value of a style: shared value across the selection, or the
|
|
417
|
+
// next-shape value when nothing is selected.
|
|
418
|
+
function readStyleValue<T>(editor: Editor, style: StyleProp<T>): T | undefined {
|
|
419
|
+
if (editor.getSelectedShapeIds().length > 0) {
|
|
420
|
+
return editor.getSharedStyles().getAsKnownValue(style)
|
|
421
|
+
}
|
|
422
|
+
return editor.getStyleForNextShape(style)
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// Apply to the selection (if any) and make it the default for the next shape.
|
|
426
|
+
function applyStyle<T>(editor: Editor, style: StyleProp<T>, value: T) {
|
|
427
|
+
editor.run(() => {
|
|
428
|
+
editor.setStyleForNextShapes(style, value)
|
|
429
|
+
if (editor.getSelectedShapeIds().length > 0) {
|
|
430
|
+
editor.setStyleForSelectedShapes(style, value)
|
|
431
|
+
}
|
|
432
|
+
})
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// Which controls the sub-bar should show right now: the active creating tool's
|
|
436
|
+
// set, else the union for the current selection, else none (bar hidden).
|
|
437
|
+
function activeControls(editor: Editor): StyleControl[] {
|
|
438
|
+
const toolControls = CONTROLS_BY_TOOL[editor.getCurrentToolId()]
|
|
439
|
+
if (toolControls) return toolControls
|
|
440
|
+
|
|
441
|
+
const ids = editor.getSelectedShapeIds()
|
|
442
|
+
if (ids.length === 0) return []
|
|
443
|
+
const set = new Set<StyleControl>()
|
|
444
|
+
for (const id of ids) {
|
|
445
|
+
const shape = editor.getShape(id)
|
|
446
|
+
if (!shape) continue
|
|
447
|
+
for (const control of CONTROLS_BY_SHAPE[shape.type] ?? []) set.add(control)
|
|
448
|
+
}
|
|
449
|
+
return CONTROL_ORDER.filter(control => set.has(control))
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// How the size control previews itself, matched to what "size" means for the
|
|
453
|
+
// active tool: the pencil's is a stroke weight (pen glyph at varying strokes),
|
|
454
|
+
// text's is a font size (a letter at varying sizes), everything else a plain blob.
|
|
455
|
+
// Keyed off the active tool, or a uniform selection under the Select tool.
|
|
456
|
+
function sizeGlyphKind(editor: Editor): 'pen' | 'letter' | 'dot' {
|
|
457
|
+
const toolId = editor.getCurrentToolId()
|
|
458
|
+
if (toolId === 'draw') return 'pen'
|
|
459
|
+
if (toolId === 'text') return 'letter'
|
|
460
|
+
const ids = editor.getSelectedShapeIds()
|
|
461
|
+
const types = new Set(ids.map(id => editor.getShape(id)?.type))
|
|
462
|
+
if (types.size === 1) {
|
|
463
|
+
if (types.has('draw')) return 'pen'
|
|
464
|
+
if (types.has('text')) return 'letter'
|
|
465
|
+
}
|
|
466
|
+
return 'dot'
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
type StyleBarState = {
|
|
470
|
+
controls: StyleControl[]
|
|
471
|
+
palette: Palette
|
|
472
|
+
color: TLDefaultColorStyle | undefined
|
|
473
|
+
size: TLDefaultSizeStyle | undefined
|
|
474
|
+
sizeGlyph: 'pen' | 'letter' | 'dot'
|
|
475
|
+
dash: TLDefaultDashStyle | undefined
|
|
476
|
+
fill: TLDefaultFillStyle | undefined
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
function readStyleBarState(editor: Editor): StyleBarState {
|
|
480
|
+
return {
|
|
481
|
+
controls: activeControls(editor),
|
|
482
|
+
palette: PALETTES[paletteKind(editor)],
|
|
483
|
+
color: readStyleValue(editor, DefaultColorStyle),
|
|
484
|
+
size: readStyleValue(editor, DefaultSizeStyle),
|
|
485
|
+
sizeGlyph: sizeGlyphKind(editor),
|
|
486
|
+
dash: readStyleValue(editor, DefaultDashStyle),
|
|
487
|
+
fill: readStyleValue(editor, DefaultFillStyle)
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
type ScratchStyleBarProps = { editor: Editor }
|
|
492
|
+
|
|
493
|
+
function ScratchStyleBar({ editor }: ScratchStyleBarProps) {
|
|
494
|
+
const [state, setState] = useState<StyleBarState>(() => readStyleBarState(editor))
|
|
495
|
+
|
|
496
|
+
useEffect(() => {
|
|
497
|
+
setState(readStyleBarState(editor))
|
|
498
|
+
return react('scratch styles', () => setState(readStyleBarState(editor)))
|
|
499
|
+
}, [editor])
|
|
500
|
+
|
|
501
|
+
if (state.controls.length === 0) return null
|
|
502
|
+
|
|
503
|
+
const groups: Record<StyleControl, ReactElement> = {
|
|
504
|
+
color: (
|
|
505
|
+
<div className="flex flex-col items-center gap-2 pt-1">
|
|
506
|
+
{state.palette.swatches.map(c => (
|
|
507
|
+
<button
|
|
508
|
+
key={c.value}
|
|
509
|
+
type="button"
|
|
510
|
+
title={c.value}
|
|
511
|
+
onClick={() => applyStyle(editor, DefaultColorStyle, c.value)}
|
|
512
|
+
className={cn(
|
|
513
|
+
'size-5.5 rounded-full ring-offset-1 ring-offset-background',
|
|
514
|
+
c.klass,
|
|
515
|
+
state.color === c.value ? 'ring-2 ring-primary' : 'ring-1 ring-black/15'
|
|
516
|
+
)}
|
|
517
|
+
/>
|
|
518
|
+
))}
|
|
519
|
+
</div>
|
|
520
|
+
),
|
|
521
|
+
size: (
|
|
522
|
+
<div className="flex flex-col items-center gap-1 text-foreground">
|
|
523
|
+
{SIZE_OPTIONS.map(s => (
|
|
524
|
+
<button
|
|
525
|
+
key={s.value}
|
|
526
|
+
type="button"
|
|
527
|
+
title={s.label}
|
|
528
|
+
onClick={() => applyStyle(editor, DefaultSizeStyle, s.value)}
|
|
529
|
+
className={cn(
|
|
530
|
+
'flex size-7.5 items-center justify-center rounded-full hover:bg-muted',
|
|
531
|
+
state.size === s.value &&
|
|
532
|
+
'bg-muted-foreground/25 text-primary hover:bg-muted-foreground/25'
|
|
533
|
+
)}
|
|
534
|
+
>
|
|
535
|
+
{state.sizeGlyph === 'pen' ? (
|
|
536
|
+
<IconSketching size={18} stroke={s.stroke} />
|
|
537
|
+
) : state.sizeGlyph === 'letter' ? (
|
|
538
|
+
<IconTypography size={s.glyph} stroke={1.5} />
|
|
539
|
+
) : (
|
|
540
|
+
<span className={cn('rounded-full bg-current', s.dot)} />
|
|
541
|
+
)}
|
|
542
|
+
</button>
|
|
543
|
+
))}
|
|
544
|
+
</div>
|
|
545
|
+
),
|
|
546
|
+
dash: (
|
|
547
|
+
<div className="flex flex-col items-center gap-1 text-foreground">
|
|
548
|
+
{DASH_OPTIONS.map(d => (
|
|
549
|
+
<button
|
|
550
|
+
key={d.value}
|
|
551
|
+
type="button"
|
|
552
|
+
title={d.label}
|
|
553
|
+
onClick={() => applyStyle(editor, DefaultDashStyle, d.value)}
|
|
554
|
+
className={cn(
|
|
555
|
+
'flex size-7.5 items-center justify-center rounded-full hover:bg-muted',
|
|
556
|
+
state.dash === d.value &&
|
|
557
|
+
'bg-muted-foreground/25 text-primary hover:bg-muted-foreground/25'
|
|
558
|
+
)}
|
|
559
|
+
>
|
|
560
|
+
<span className={cn('w-3.5', d.preview)} />
|
|
561
|
+
</button>
|
|
562
|
+
))}
|
|
563
|
+
</div>
|
|
564
|
+
),
|
|
565
|
+
fill: (
|
|
566
|
+
<div className="flex flex-col items-center gap-1 text-foreground">
|
|
567
|
+
{FILL_OPTIONS.map(f => (
|
|
568
|
+
<button
|
|
569
|
+
key={f.value}
|
|
570
|
+
type="button"
|
|
571
|
+
title={f.label}
|
|
572
|
+
onClick={() => applyStyle(editor, DefaultFillStyle, f.value)}
|
|
573
|
+
className={cn(
|
|
574
|
+
'flex size-7.5 items-center justify-center rounded-full hover:bg-muted',
|
|
575
|
+
state.fill === f.value &&
|
|
576
|
+
'bg-muted-foreground/25 text-primary hover:bg-muted-foreground/25'
|
|
577
|
+
)}
|
|
578
|
+
>
|
|
579
|
+
<span className={cn('size-4.5 rounded-sm', f.preview)} />
|
|
580
|
+
</button>
|
|
581
|
+
))}
|
|
582
|
+
</div>
|
|
583
|
+
)
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
return (
|
|
587
|
+
<div className="absolute top-1/2 left-15 z-10 flex -translate-y-1/2 flex-col items-center gap-1.5 rounded-full bg-background/95 px-1 py-1 shadow-[0_2px_8px_rgba(0,0,0,0.08),0_4px_16px_rgba(0,0,0,0.06),0_0_0_1px_rgba(0,0,0,0.04)] backdrop-blur">
|
|
588
|
+
{state.controls.map((control, i) => (
|
|
589
|
+
<Fragment key={control}>
|
|
590
|
+
{i > 0 && <div className="h-px w-5 bg-border" />}
|
|
591
|
+
{groups[control]}
|
|
592
|
+
</Fragment>
|
|
593
|
+
))}
|
|
594
|
+
</div>
|
|
595
|
+
)
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
// Hydrate the saved canvas once per workspace from the REST snapshot endpoint.
|
|
599
|
+
// Returns a STABLE snapshot reference (held in a ref, never a fresh object per
|
|
600
|
+
// render): tldraw rebuilds its entire store whenever the `snapshot` prop identity
|
|
601
|
+
// changes, so handing it a new object each render loops it forever — remounting the
|
|
602
|
+
// store and re-fetching fonts/translations endlessly. `document: null` → empty
|
|
603
|
+
// canvas; `loaded` gates the placeholder until the fetch settles.
|
|
604
|
+
function useScratchpadSnapshot(workspaceId: string): {
|
|
605
|
+
loaded: boolean
|
|
606
|
+
snapshot: Partial<TLEditorSnapshot> | undefined
|
|
607
|
+
} {
|
|
608
|
+
const [loaded, setLoaded] = useState(false)
|
|
609
|
+
const snapshot = useRef<Partial<TLEditorSnapshot> | undefined>(undefined)
|
|
610
|
+
|
|
611
|
+
useEffect(() => {
|
|
612
|
+
let cancelled = false
|
|
613
|
+
setLoaded(false)
|
|
614
|
+
snapshot.current = undefined
|
|
615
|
+
fetch(`/api/workspaces/${workspaceId}/scratchpad`)
|
|
616
|
+
.then(r => r.json())
|
|
617
|
+
.then((d: { document: TLEditorSnapshot['document'] | null }) => {
|
|
618
|
+
if (cancelled) return
|
|
619
|
+
if (d?.document) snapshot.current = { document: d.document }
|
|
620
|
+
setLoaded(true)
|
|
621
|
+
})
|
|
622
|
+
.catch(() => {
|
|
623
|
+
if (!cancelled) setLoaded(true)
|
|
624
|
+
})
|
|
625
|
+
return () => {
|
|
626
|
+
cancelled = true
|
|
627
|
+
}
|
|
628
|
+
}, [workspaceId])
|
|
629
|
+
|
|
630
|
+
return { loaded, snapshot: snapshot.current }
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
// The Scratchpad surface: a real tldraw editor, hydrated from and autosaved to
|
|
634
|
+
// `.moi/.scratchpad.json` via REST. One canvas shared by the user and the agent —
|
|
635
|
+
// the agent reaches it through `moi scratch` (relayed ops execute here). See
|
|
636
|
+
// docs/moi-scratchpad.md.
|
|
637
|
+
export function Scratchpad() {
|
|
638
|
+
const workspaceId = useWorkspaceId()
|
|
639
|
+
const editorRef = useRef<Editor | null>(null)
|
|
640
|
+
// The whole scratchpad region (canvas + tool bar + style bar). Focus is driven
|
|
641
|
+
// off whether a pointerdown lands inside this, so clicking a tool keeps the
|
|
642
|
+
// editor focused — see the pointerdown handler in onMount.
|
|
643
|
+
const rootRef = useRef<HTMLDivElement>(null)
|
|
644
|
+
const saveTimer = useRef<ReturnType<typeof setTimeout> | null>(null)
|
|
645
|
+
// Set before a remote `loadSnapshot`; the autosave listener consumes it on the
|
|
646
|
+
// first (throttled) flush so the load doesn't echo back out as a save. (Store
|
|
647
|
+
// listeners fire on the next frame, not synchronously, so a timer-based clear
|
|
648
|
+
// would race — consuming it in the listener is timing-independent.)
|
|
649
|
+
const applyingRemote = useRef(false)
|
|
650
|
+
// Reactive handle to the mounted editor, used to render the custom tool bar.
|
|
651
|
+
const [editor, setEditor] = useState<Editor | null>(null)
|
|
652
|
+
const { loaded, snapshot } = useScratchpadSnapshot(workspaceId)
|
|
653
|
+
|
|
654
|
+
const save = useCallback(() => {
|
|
655
|
+
const editor = editorRef.current
|
|
656
|
+
if (!editor) return
|
|
657
|
+
let document: TLEditorSnapshot['document']
|
|
658
|
+
try {
|
|
659
|
+
document = getSnapshot(editor.store).document
|
|
660
|
+
} catch {
|
|
661
|
+
return
|
|
662
|
+
}
|
|
663
|
+
void fetch(`/api/workspaces/${workspaceId}/scratchpad`, {
|
|
664
|
+
method: 'PUT',
|
|
665
|
+
headers: { 'Content-Type': 'application/json' },
|
|
666
|
+
body: JSON.stringify({ document, origin: ORIGIN_ID })
|
|
667
|
+
}).catch(() => {})
|
|
668
|
+
}, [workspaceId])
|
|
669
|
+
|
|
670
|
+
// A remote save (another tab, or an agent draw landing in another tab) — pull
|
|
671
|
+
// the new snapshot and load it into the live store. Skip our own echo.
|
|
672
|
+
useMeiEvent((e: MeiEvent) => {
|
|
673
|
+
if (e.type !== 'scratchpad:updated' || e.workspaceId !== workspaceId) return
|
|
674
|
+
if (e.origin && e.origin === ORIGIN_ID) return
|
|
675
|
+
if (!editorRef.current) return
|
|
676
|
+
fetch(`/api/workspaces/${workspaceId}/scratchpad`)
|
|
677
|
+
.then(r => r.json())
|
|
678
|
+
.then((d: { document: TLEditorSnapshot['document'] | null }) => {
|
|
679
|
+
const editor = editorRef.current
|
|
680
|
+
if (!editor || !d?.document) return
|
|
681
|
+
applyingRemote.current = true
|
|
682
|
+
loadSnapshot(editor.store, { document: d.document })
|
|
683
|
+
})
|
|
684
|
+
.catch(() => {})
|
|
685
|
+
})
|
|
686
|
+
|
|
687
|
+
const onMount = useCallback(
|
|
688
|
+
(editor: Editor) => {
|
|
689
|
+
editorRef.current = editor
|
|
690
|
+
setEditor(editor)
|
|
691
|
+
// Grid on by default (no toggle in the UI).
|
|
692
|
+
editor.updateInstanceState({ isGridMode: true })
|
|
693
|
+
// Locked defaults that have no UI control: hand-drawn font, centered text.
|
|
694
|
+
editor.run(() => {
|
|
695
|
+
editor.setStyleForNextShapes(DefaultFontStyle, 'draw')
|
|
696
|
+
editor.setStyleForNextShapes(DefaultHorizontalAlignStyle, 'middle')
|
|
697
|
+
editor.setStyleForNextShapes(DefaultVerticalAlignStyle, 'middle')
|
|
698
|
+
// Rectangles start solid-filled. Still user-adjustable via the style bar —
|
|
699
|
+
// applyToolLocks deliberately never re-pins fill, so this is a default, not
|
|
700
|
+
// a lock. ('fill' = the style bar's "Solid"; 'solid' there means "Semi".)
|
|
701
|
+
editor.setStyleForNextShapes(DefaultFillStyle, 'fill')
|
|
702
|
+
})
|
|
703
|
+
const unlisten = editor.store.listen(
|
|
704
|
+
() => {
|
|
705
|
+
if (applyingRemote.current) {
|
|
706
|
+
applyingRemote.current = false
|
|
707
|
+
return
|
|
708
|
+
}
|
|
709
|
+
if (saveTimer.current) clearTimeout(saveTimer.current)
|
|
710
|
+
saveTimer.current = setTimeout(save, AUTOSAVE_MS)
|
|
711
|
+
},
|
|
712
|
+
{ source: 'user', scope: 'document' }
|
|
713
|
+
)
|
|
714
|
+
setScratchExecutor(workspaceId, makeExecutor(editor))
|
|
715
|
+
// Focus management: tldraw only fires keyboard shortcuts while the editor's
|
|
716
|
+
// instance `isFocused` is set, but with `autoFocus={false}` it never flips
|
|
717
|
+
// that on its own. We drive it from where the pointer lands — a pointerdown
|
|
718
|
+
// inside the scratchpad (canvas, tool bar, or style bar, all under rootRef)
|
|
719
|
+
// focuses the editor; one anywhere else (e.g. the chat) blurs it, so hotkeys
|
|
720
|
+
// are live only when the scratchpad is the active surface. Guarded on the
|
|
721
|
+
// current state so a click already inside (e.g. double-click to edit a
|
|
722
|
+
// shape's text) doesn't re-focus the container and steal focus from the
|
|
723
|
+
// text field. Capture phase so we see it regardless of downstream handlers.
|
|
724
|
+
const onPointerDown = (e: PointerEvent) => {
|
|
725
|
+
const target = e.target as Node | null
|
|
726
|
+
const inside = !!target && !!rootRef.current?.contains(target)
|
|
727
|
+
if (inside) {
|
|
728
|
+
if (!editor.getIsFocused()) editor.focus()
|
|
729
|
+
} else if (editor.getIsFocused()) {
|
|
730
|
+
editor.blur()
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
const doc = editor.getContainerDocument()
|
|
734
|
+
doc.addEventListener('pointerdown', onPointerDown, true)
|
|
735
|
+
// First open: grab focus so the user can reach for the keyboard right away.
|
|
736
|
+
// `focus()` both sets `isFocused` and DOM-focuses the canvas container, so it
|
|
737
|
+
// becomes the active element — clearing tldraw's secondary guard that mutes
|
|
738
|
+
// shortcuts while an input/textarea (e.g. the chat) holds focus.
|
|
739
|
+
editor.focus()
|
|
740
|
+
return () => {
|
|
741
|
+
unlisten()
|
|
742
|
+
doc.removeEventListener('pointerdown', onPointerDown, true)
|
|
743
|
+
if (saveTimer.current) clearTimeout(saveTimer.current)
|
|
744
|
+
setScratchExecutor(workspaceId, null)
|
|
745
|
+
setEditor(null)
|
|
746
|
+
editorRef.current = null
|
|
747
|
+
}
|
|
748
|
+
},
|
|
749
|
+
[workspaceId, save]
|
|
750
|
+
)
|
|
751
|
+
|
|
752
|
+
if (!loaded) return <div className="min-h-0 flex-1 bg-muted/40" />
|
|
753
|
+
|
|
754
|
+
return (
|
|
755
|
+
<div ref={rootRef} className="relative min-h-0 flex-1 overflow-hidden">
|
|
756
|
+
<div className="absolute inset-0">
|
|
757
|
+
<Tldraw
|
|
758
|
+
snapshot={snapshot}
|
|
759
|
+
licenseKey={LICENSE_KEY}
|
|
760
|
+
onMount={onMount}
|
|
761
|
+
components={SCRATCH_COMPONENTS}
|
|
762
|
+
overrides={SCRATCH_OVERRIDES}
|
|
763
|
+
options={SCRATCH_OPTIONS}
|
|
764
|
+
// Hand focus control entirely to us: tldraw's own `autoFocus` only seeds
|
|
765
|
+
// the `isFocused` flag (hotkeys would fire window-wide, even from the
|
|
766
|
+
// chat) without ever DOM-focusing the canvas. Instead onMount focuses
|
|
767
|
+
// the editor on open and a pointerdown handler toggles it thereafter.
|
|
768
|
+
autoFocus={false}
|
|
769
|
+
/>
|
|
770
|
+
</div>
|
|
771
|
+
{editor && <ScratchToolbar editor={editor} />}
|
|
772
|
+
{editor && <ScratchStyleBar editor={editor} />}
|
|
773
|
+
</div>
|
|
774
|
+
)
|
|
775
|
+
}
|