Skip to content

Commit 4d8b69c

Browse files
committed
fix(editor): cut and copy every selection, and keep the import scan within engine limits
- The context menu's Cut and Copy take every selected passage, and Cut deletes only after the clipboard write succeeds, so nothing is removed that never reached it. - The import scan sees require?.() calls and walks array children one push at a time, so a 100,000-element literal stays under Bun's argument limit. - The Code tab e2e seeds the clipboard before pasting into the read-only view.
1 parent 3bae054 commit 4d8b69c

4 files changed

Lines changed: 32 additions & 11 deletions

File tree

‎apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/text-editor.tsx‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,18 @@ export const TextEditor = memo(function TextEditor({
285285
return false
286286
}
287287

288+
/** Every selected passage, joined by line breaks as a multi-cursor copy is. */
289+
const selectedText = (): string => {
290+
const editor = editorRef.current
291+
if (!editor) return ''
292+
const text = editor.getText()
293+
return editor
294+
.getSelectionRanges()
295+
.filter(({ start, end }) => start < end)
296+
.map(({ start, end }) => text.slice(start, end))
297+
.join('\n')
298+
}
299+
288300
const handleEditorPasteCapture = (event: ReactClipboardEvent<HTMLDivElement>) => {
289301
const pastedText = event.clipboardData.getData('text/plain')
290302
if (!pastedText || admitsPaste(pastedText)) return
@@ -442,17 +454,19 @@ export const TextEditor = memo(function TextEditor({
442454
closeContextMenu()
443455
}}
444456
onCut={() => {
445-
const selection = editorRef.current?.getSelection()
446-
if (selection) {
447-
navigator.clipboard.writeText(selection.text).catch(() => {})
448-
editorRef.current?.replaceSelection('')
449-
}
450457
closeContextMenu()
458+
const text = selectedText()
459+
if (!text) return
460+
/* Delete only once the text is safely on the clipboard. */
461+
navigator.clipboard
462+
.writeText(text)
463+
.then(() => editorRef.current?.replaceSelection(''))
464+
.catch(() => {})
451465
}}
452466
onCopy={() => {
453-
const selection = editorRef.current?.getSelection()
454-
if (selection) navigator.clipboard.writeText(selection.text).catch(() => {})
455467
closeContextMenu()
468+
const text = selectedText()
469+
if (text) navigator.clipboard.writeText(text).catch(() => {})
456470
}}
457471
onCopyAll={() => {
458472
navigator.clipboard.writeText(editorRef.current?.getText() ?? '').catch(() => {})

‎apps/sim/lib/function-execution/execute-request.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@ describe('Function execution request', () => {
846846

847847
it.each([
848848
['a require call', 'const path = require("node:path")\nreturn path.sep', 1],
849+
['an optional require call', 'const path = require?.("node:path")\nreturn path.sep', 1],
849850
['code that does not parse', 'import path from "node:path"\nreturn (', 0],
850851
[
851852
'code whose only error a parser can recover from',

‎apps/sim/lib/function-execution/execute-request.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,10 @@ function forEachSyntaxNode(root: Node, visit: (node: Node) => void): void {
399399
visit(node)
400400
for (const key of VISITOR_KEYS[node.type] ?? []) {
401401
const child: unknown = Reflect.get(node, key)
402-
if (Array.isArray(child)) pending.push(...child.filter(isSyntaxNode))
403-
else if (isSyntaxNode(child)) pending.push(child)
402+
/* One push per child: spreading a 100,000-element array literal exceeds Bun's argument limit. */
403+
if (Array.isArray(child)) {
404+
for (const item of child) if (isSyntaxNode(item)) pending.push(item)
405+
} else if (isSyntaxNode(child)) pending.push(child)
404406
}
405407
}
406408
}
@@ -428,7 +430,7 @@ function extractJavaScriptImports(code: string): {
428430
forEachSyntaxNode(program, (node) => {
429431
if (node.type === 'Identifier') identifierNames.add(node.name)
430432
if (
431-
node.type === 'CallExpression' &&
433+
(node.type === 'CallExpression' || node.type === 'OptionalCallExpression') &&
432434
node.callee.type === 'Identifier' &&
433435
node.callee.name === 'require'
434436
) {

‎apps/sim/scripts/test-workflow-code-ui-e2e.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,10 @@ async function visitWorkflow(page: Page, origin: URL) {
303303
/** A page in a fresh browser context signed in to `origin`. */
304304
async function signedInPage(origin: URL): Promise<Page> {
305305
assert(browser, 'The browser must be launched first')
306-
const context = await browser.newContext({ viewport: { width: 1440, height: 1000 } })
306+
const context = await browser.newContext({
307+
viewport: { width: 1440, height: 1000 },
308+
permissions: ['clipboard-read', 'clipboard-write'],
309+
})
307310
await context.addCookies([
308311
{ name: 'better-auth.session_token', value: cookie, url: origin.origin },
309312
])
@@ -365,6 +368,7 @@ try {
365368
await page.keyboard.type('typed into a read-only view')
366369
await page.keyboard.press('Enter')
367370
await page.keyboard.press('Backspace')
371+
await page.evaluate(() => navigator.clipboard.writeText('pasted into a read-only view'))
368372
await page.keyboard.press('ControlOrMeta+v')
369373
// The editor paints edits on the next animation frame; two frames cover any queued render.
370374
await page.evaluate(

0 commit comments

Comments
 (0)