-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
249 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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 @@ | ||
export { useEnforceHighlightAutofill } from './use-enforce-highlight-autofill' |
67 changes: 67 additions & 0 deletions
67
packages/picasso-forms/src/RichTextEditor/hooks/use-enforce-highlight-autofill.test.tsx
This file contains 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,67 @@ | ||
import React from 'react' | ||
import { render, fireEvent } from '@toptal/picasso/test-utils' | ||
|
||
import { useEnforceHighlightAutofill } from './use-enforce-highlight-autofill' | ||
import { useFormConfig } from '../../FormConfig' | ||
|
||
jest.mock('../../FormConfig', () => ({ | ||
useFormConfig: jest.fn(), | ||
})) | ||
|
||
const mockedUseFormConfig = useFormConfig as jest.MockedFunction< | ||
typeof useFormConfig | ||
> | ||
|
||
const TestComponent = () => { | ||
const { enforceHighlightAutofill, registerChangeOrFocus } = | ||
useEnforceHighlightAutofill() | ||
|
||
return ( | ||
<> | ||
<button onClick={registerChangeOrFocus}>trigger</button> | ||
<div data-testid='enforce-highlight-autofill-status'> | ||
{enforceHighlightAutofill ? 'true' : 'false'} | ||
</div> | ||
</> | ||
) | ||
} | ||
|
||
describe('useEnforceHighlightAutofill', () => { | ||
describe('when form does not highlight autofilled fields', () => { | ||
it('does not enforce highlighting', () => { | ||
mockedUseFormConfig.mockReturnValue({ highlightAutofill: false }) | ||
const { getByText, getByTestId } = render(<TestComponent />) | ||
const button = getByText('trigger') | ||
const autofillStatusContainer = getByTestId( | ||
'enforce-highlight-autofill-status' | ||
) | ||
|
||
fireEvent.click(button) | ||
|
||
// After first update | ||
expect(autofillStatusContainer).toHaveTextContent('false') | ||
}) | ||
}) | ||
|
||
describe('when form highlights autofilled fields', () => { | ||
it('highlighting is enforced after first update and not enforced after following updates', async () => { | ||
mockedUseFormConfig.mockReturnValue({ highlightAutofill: true }) | ||
const { getByText, getByTestId } = render(<TestComponent />) | ||
|
||
const button = getByText('trigger') | ||
const autofillStatusContainer = getByTestId( | ||
'enforce-highlight-autofill-status' | ||
) | ||
|
||
fireEvent.click(button) | ||
|
||
// After first update | ||
expect(autofillStatusContainer).toHaveTextContent('true') | ||
|
||
fireEvent.click(button) | ||
|
||
// After second update | ||
expect(autofillStatusContainer).toHaveTextContent('false') | ||
}) | ||
}) | ||
}) |
34 changes: 34 additions & 0 deletions
34
packages/picasso-forms/src/RichTextEditor/hooks/use-enforce-highlight-autofill.ts
This file contains 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,34 @@ | ||
import { useCallback, useMemo, useState } from 'react' | ||
|
||
import { useFormConfig } from '../../FormConfig' | ||
|
||
const TRACKED_EVENTS_LIMIT = 3 | ||
|
||
export const useEnforceHighlightAutofill = () => { | ||
const { highlightAutofill } = useFormConfig() | ||
const [timesChangeOrFocusTriggered, setTimesChangeOrFocusTriggered] = | ||
useState(0) | ||
|
||
const enforceHighlightAutofill = useMemo(() => { | ||
if (!highlightAutofill) { | ||
return false | ||
} | ||
|
||
return timesChangeOrFocusTriggered < 2 | ||
}, [highlightAutofill, timesChangeOrFocusTriggered]) | ||
|
||
const registerChangeOrFocus = useCallback(() => { | ||
if (!highlightAutofill) { | ||
return | ||
} | ||
|
||
if (timesChangeOrFocusTriggered < TRACKED_EVENTS_LIMIT) { | ||
setTimesChangeOrFocusTriggered(timesChangeOrFocusTriggered + 1) | ||
} | ||
}, [timesChangeOrFocusTriggered]) | ||
|
||
return { | ||
enforceHighlightAutofill, | ||
registerChangeOrFocus, | ||
} | ||
} |
This file contains 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 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
31 changes: 31 additions & 0 deletions
31
packages/picasso/src/LexicalEditor/plugins/TriggerInitialOnChangePlugin.ts
This file contains 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,31 @@ | ||
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext' | ||
import { useIsomorphicLayoutEffect } from '@toptal/picasso-shared' | ||
import type { EditorState, LexicalEditor } from 'lexical' | ||
|
||
const TriggerInitialOnChangePlugin = ({ | ||
onChange, | ||
}: { | ||
onChange: ( | ||
editorState: EditorState, | ||
editor: LexicalEditor, | ||
tags: Set<string> | ||
) => void | ||
}) => { | ||
const [editor] = useLexicalComposerContext() | ||
|
||
useIsomorphicLayoutEffect(() => { | ||
if (onChange) { | ||
return editor.registerUpdateListener( | ||
({ editorState, prevEditorState, tags }) => { | ||
if (prevEditorState.isEmpty()) { | ||
onChange(editorState, editor, tags) | ||
} | ||
} | ||
) | ||
} | ||
}, [editor, onChange]) | ||
|
||
return null | ||
} | ||
|
||
export default TriggerInitialOnChangePlugin |
This file contains 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 @@ | ||
export { default as TriggerInitialOnChangePlugin } from './TriggerInitialOnChangePlugin' |
18 changes: 18 additions & 0 deletions
18
packages/picasso/src/LexicalEditor/utils/getDomValue.test.ts
This file contains 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,18 @@ | ||
import type { ASTType } from '../../RichText' | ||
import { getDomValue } from './getDomValue' | ||
|
||
describe('getDomValue', () => { | ||
it('returns DOM structure from AST', () => { | ||
const inputValue: ASTType = { | ||
type: 'root', | ||
children: [{ type: 'text', value: 'Example of default text' }], | ||
} | ||
|
||
const result = getDomValue(inputValue) | ||
const serializer = new XMLSerializer() | ||
|
||
expect(serializer.serializeToString(result)).toBe( | ||
'<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body>Example of default text</body></html>' | ||
) | ||
}) | ||
}) |
This file contains 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,10 @@ | ||
import type { HastNode } from 'hast-util-to-dom/lib' | ||
import toHtml from 'hast-util-to-html' | ||
|
||
import type { ASTType } from '../../RichText' | ||
|
||
export const getDomValue = (value: ASTType) => { | ||
const parser = new DOMParser() | ||
|
||
return parser.parseFromString(toHtml(value as HastNode), 'text/html') | ||
} |
This file contains 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
36 changes: 36 additions & 0 deletions
36
packages/picasso/src/LexicalEditor/utils/setEditorValue.ts
This file contains 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,36 @@ | ||
import type { LexicalEditor as LexicalEditorType } from 'lexical' | ||
import { | ||
$createParagraphNode, | ||
$isLineBreakNode, | ||
$getRoot, | ||
$isDecoratorNode, | ||
$isElementNode, | ||
} from 'lexical' | ||
import { $generateNodesFromDOM } from '@lexical/html' | ||
|
||
import type { ASTType } from '../../RichText' | ||
import { getDomValue } from './getDomValue' | ||
|
||
export const setEditorValue = (editor: LexicalEditorType, value: ASTType) => { | ||
const domValue = getDomValue(value) | ||
|
||
editor.update(() => { | ||
const root = $getRoot() | ||
const lexicalValueNodes = $generateNodesFromDOM(editor, domValue) | ||
|
||
lexicalValueNodes.forEach(node => { | ||
if ($isElementNode(node) || $isDecoratorNode(node)) { | ||
root.append(node) | ||
} else if ($isLineBreakNode(node)) { | ||
const paragraphNode = $createParagraphNode() | ||
|
||
root.append(paragraphNode) | ||
} else { | ||
const paragraphNode = $createParagraphNode() | ||
|
||
paragraphNode.append(node) | ||
root.append(paragraphNode) | ||
} | ||
}) | ||
}) | ||
} |
This file contains 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 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