-
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
13 changed files
with
233 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' |
63 changes: 63 additions & 0 deletions
63
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,63 @@ | ||
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('useHighlightAutofill', () => { | ||
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 resultsContainer = getByTestId('enforce-highlight-autofill-status') | ||
|
||
fireEvent.click(button) | ||
|
||
// After first update | ||
expect(resultsContainer).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 resultsContainer = getByTestId('enforce-highlight-autofill-status') | ||
|
||
fireEvent.click(button) | ||
|
||
// After first update | ||
expect(resultsContainer).toHaveTextContent('true') | ||
|
||
fireEvent.click(button) | ||
|
||
// After second update | ||
expect(resultsContainer).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/hooks/use-isomorphic-layout-effect' | ||
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' |
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
41 changes: 41 additions & 0 deletions
41
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,41 @@ | ||
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) | ||
} | ||
}) | ||
}, | ||
{ | ||
discrete: true, | ||
} | ||
) | ||
} |
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