-
Notifications
You must be signed in to change notification settings - Fork 5.8k
fix TextVariableEditor layout and add support for multi line paste #18721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
neo773
wants to merge
8
commits into
main
Choose a base branch
from
fix-text-variable-editor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
568eeab
fix TextVariableEditor layout and add support for multi line paste
neo773 765ccf3
Update useTextVariableEditor.ts
neo773 b03d63e
add tests
neo773 33dd293
fix lint
neo773 243ecd1
fix
neo773 0ba5965
Merge remote-tracking branch 'origin/main' into fix-text-variable-editor
charlesBochet 27590a0
fix SVG icon sizing broken in Chrome 142 due to var() in attributes
charlesBochet 8f791aa
Revert "fix SVG icon sizing broken in Chrome 142 due to var() in attr…
charlesBochet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
...es/object-record/record-field/ui/form-types/hooks/__tests__/useTextVariableEditor.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| import { renderHook } from '@testing-library/react'; | ||
| import { Fragment, Slice } from '@tiptap/pm/model'; | ||
| import type { Editor } from '@tiptap/react'; | ||
|
|
||
| import { parseEditorContent } from '@/workflow/workflow-variables/utils/parseEditorContent'; | ||
| import { useTextVariableEditor } from '@/object-record/record-field/ui/form-types/hooks/useTextVariableEditor'; | ||
|
|
||
| const mockPasteEvent = (text: string) => | ||
| ({ | ||
| clipboardData: { | ||
| getData: (type: string) => (type === 'text/plain' ? text : ''), | ||
| }, | ||
| }) as unknown as ClipboardEvent; | ||
|
|
||
| const paste = (editor: Editor, text: string): boolean => { | ||
| const handlePaste = editor.view.props.handlePaste!; | ||
| const { schema } = editor.view.state; | ||
| const slice = text | ||
| ? new Slice(Fragment.from(schema.text(text)), 0, 0) | ||
| : Slice.empty; | ||
|
|
||
| return handlePaste(editor.view, mockPasteEvent(text), slice) as boolean; | ||
| }; | ||
|
|
||
| const content = (editor: Editor) => parseEditorContent(editor.getJSON()); | ||
|
|
||
| const countHardBreaks = (editor: Editor) => | ||
| editor | ||
| .getJSON() | ||
| .content?.[0]?.content?.filter( | ||
| (n: { type: string }) => n.type === 'hardBreak', | ||
| )?.length ?? 0; | ||
|
|
||
| const setup = ( | ||
| opts: Partial<{ | ||
| multiline: boolean; | ||
| readonly: boolean; | ||
| defaultValue: string | null; | ||
| }> = {}, | ||
| ) => { | ||
| const onUpdate = jest.fn(); | ||
| const { result, unmount } = renderHook(() => | ||
| useTextVariableEditor({ | ||
| placeholder: 'Enter text', | ||
| multiline: opts.multiline ?? false, | ||
| readonly: opts.readonly ?? false, | ||
| defaultValue: opts.defaultValue ?? undefined, | ||
| onUpdate, | ||
| }), | ||
| ); | ||
|
|
||
| if (result.current === null || result.current === undefined) | ||
| throw new Error('Editor not created'); | ||
| return { editor: result.current, unmount }; | ||
| }; | ||
|
|
||
| describe('useTextVariableEditor', () => { | ||
| let teardown: (() => void) | undefined; | ||
| afterEach(() => teardown?.()); | ||
|
|
||
| const use = (opts: Parameters<typeof setup>[0] = {}) => { | ||
| const { editor, unmount } = setup(opts); | ||
| teardown = unmount; | ||
| return editor; | ||
| }; | ||
|
|
||
| describe('initialization', () => { | ||
| it('should set content from defaultValue', () => { | ||
| expect(content(use({ defaultValue: 'hello' }))).toBe('hello'); | ||
| }); | ||
|
|
||
| it('should preserve line breaks in multiline defaultValue', () => { | ||
| const editor = use({ | ||
| multiline: true, | ||
| defaultValue: 'a\nb\nc', | ||
| }); | ||
| expect(countHardBreaks(editor)).toBe(2); | ||
| }); | ||
|
|
||
| it('should respect readonly', () => { | ||
| expect(use({ readonly: true }).isEditable).toBe(false); | ||
| teardown?.(); | ||
| expect(use({ readonly: false }).isEditable).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Enter key', () => { | ||
| const pressEnter = (editor: Editor, shift = false) => { | ||
| const handler = editor.view.props.handleKeyDown!; | ||
| return handler( | ||
| editor.view, | ||
| new KeyboardEvent('keydown', { | ||
| key: 'Enter', | ||
| shiftKey: shift, | ||
| cancelable: true, | ||
| }), | ||
| ); | ||
| }; | ||
|
|
||
| it('should insert hardBreak in multiline mode', () => { | ||
| const editor = use({ multiline: true, defaultValue: 'hi' }); | ||
| editor.commands.focus('end'); | ||
| pressEnter(editor); | ||
| expect(countHardBreaks(editor)).toBe(1); | ||
| }); | ||
|
|
||
| it('should block Enter without inserting in non-multiline mode', () => { | ||
| const editor = use({ defaultValue: 'hi' }); | ||
| editor.commands.focus('end'); | ||
| expect(pressEnter(editor)).toBe(true); | ||
| expect(content(editor)).toBe('hi'); | ||
| }); | ||
|
|
||
| it('should not intercept Shift+Enter', () => { | ||
| const editor = use({ multiline: true }); | ||
| expect(pressEnter(editor, true)).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('paste — JSON', () => { | ||
| it('should pretty-print JSON in multiline mode', () => { | ||
| const editor = use({ multiline: true }); | ||
| paste(editor, '{"a":1,"b":2}'); | ||
| expect(content(editor)).toContain('"a": 1'); | ||
| }); | ||
|
|
||
| it('should compact-print JSON in non-multiline mode', () => { | ||
| const editor = use({ multiline: false }); | ||
| paste(editor, '{"a":1,"b":2}'); | ||
| const c = content(editor); | ||
| expect(c).not.toContain('\n'); | ||
| expect(c).toContain('"a":'); | ||
| }); | ||
|
|
||
| it('should not crash when cursor exceeds reformatted doc size', () => { | ||
| const editor = use({ multiline: true }); | ||
| editor.commands.insertContent('x'.repeat(100)); | ||
| editor.commands.focus('end'); | ||
| expect(() => paste(editor, '{"a":1}')).not.toThrow(); | ||
| }); | ||
|
|
||
| it('should insert JSON at cursor without destroying existing content', () => { | ||
| const editor = use({ | ||
| multiline: true, | ||
| defaultValue: 'hello world', | ||
| }); | ||
| editor.commands.focus(); | ||
| editor.commands.setTextSelection(8); | ||
|
|
||
| paste(editor, '{"x":1}'); | ||
|
|
||
| const c = content(editor); | ||
| expect(c).toContain('hello'); | ||
| expect(c).toContain('orld'); | ||
| expect(c).toContain('"x": 1'); | ||
| }); | ||
|
|
||
| it('should not treat primitives as JSON objects', () => { | ||
| for (const val of ['"str"', '42', 'true', 'null']) { | ||
| const { editor, unmount } = setup({ multiline: true }); | ||
| expect(paste(editor, val)).toBe(false); | ||
| unmount(); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe('paste — multiline text', () => { | ||
| it('should convert newlines to hardBreak nodes', () => { | ||
| const editor = use({ multiline: true }); | ||
| paste(editor, 'a\nb\nc'); | ||
| expect(countHardBreaks(editor)).toBe(2); | ||
| expect(content(editor)).toContain('a'); | ||
| expect(content(editor)).toContain('c'); | ||
| }); | ||
|
|
||
| it('should handle consecutive newlines', () => { | ||
| const editor = use({ multiline: true }); | ||
| paste(editor, 'a\n\nc'); | ||
| expect(countHardBreaks(editor)).toBe(2); | ||
| }); | ||
|
|
||
| it('should fall through in non-multiline mode', () => { | ||
| const editor = use({ multiline: false }); | ||
| expect(paste(editor, 'a\nb')).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('paste — plain text', () => { | ||
| it('should fall through for text without newlines', () => { | ||
| expect(paste(use({ multiline: true }), 'hello')).toBe(false); | ||
| }); | ||
|
|
||
| it('should fall through for empty clipboard', () => { | ||
| expect(paste(use({ multiline: true }), '')).toBe(false); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: This test doesn't actually verify insertion at the selected cursor position; it would still pass if the JSON were pasted at the start or end as long as the original text remained.
Prompt for AI agents