-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…e-edit-to-avoid-complexity-on-HTML #91 inline-edit HTML is componentized
- Loading branch information
Showing
7 changed files
with
197 additions
and
99 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/common/components/inline-edit/components/html-edit.widget.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,45 @@ | ||
import { Html } from 'react-konva-utils'; | ||
import { forwardRef } from 'react'; | ||
import { EditType, StyleDivProps } from '../inline-edit.model'; | ||
|
||
interface Props { | ||
divProps: StyleDivProps; | ||
value: string; | ||
editType: EditType; | ||
onSetEditText: (e: string) => void; | ||
} | ||
|
||
export const HtmlEditWidget = forwardRef<any, Props>( | ||
({ divProps, onSetEditText, value, editType }, ref) => { | ||
return ( | ||
<Html | ||
divProps={{ | ||
style: { | ||
position: divProps.position, | ||
top: divProps.top, | ||
left: divProps.left, | ||
width: divProps.width, | ||
height: divProps.height, | ||
}, | ||
}} | ||
> | ||
{editType === 'input' && ( | ||
<input | ||
ref={ref} | ||
style={{ width: '100%', height: '100%' }} | ||
value={value} | ||
onChange={e => onSetEditText(e.target.value)} | ||
/> | ||
)} | ||
{editType === 'textarea' && ( | ||
<textarea | ||
ref={ref} | ||
style={{ width: '100%', height: '100%' }} | ||
value={value} | ||
onChange={e => onSetEditText(e.target.value)} | ||
/> | ||
)} | ||
</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 @@ | ||
export * from './html-edit.widget'; |
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,2 @@ | ||
export * from './use-submit-cancel-hook'; | ||
export * from './use-position-hook'; |
31 changes: 31 additions & 0 deletions
31
src/common/components/inline-edit/hooks/use-position-hook.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 { useCallback } from 'react'; | ||
import { addPxSuffix, calculateCoordinateValue } from '../inline-edit.utils'; | ||
import { Coord, Size } from '@/core/model'; | ||
|
||
export const usePositionHook = (coords: Coord, size: Size, scale: number) => { | ||
// TODO: this can be optimized using React.useCallback, issue #90 | ||
// https://github.com/Lemoncode/quickmock/issues/90 | ||
const calculateTextAreaXPosition = useCallback( | ||
() => calculateCoordinateValue(coords.x, scale), | ||
[coords.x, scale] | ||
); | ||
const calculateTextAreaYPosition = useCallback( | ||
() => calculateCoordinateValue(coords.y, scale), | ||
[coords.y, scale] | ||
); | ||
const calculateWidth = useCallback( | ||
() => addPxSuffix(size.width), | ||
[size.width] | ||
); | ||
const calculateHeight = useCallback( | ||
() => addPxSuffix(size.height), | ||
[size.height] | ||
); | ||
|
||
return { | ||
calculateTextAreaXPosition, | ||
calculateTextAreaYPosition, | ||
calculateWidth, | ||
calculateHeight, | ||
}; | ||
}; |
74 changes: 74 additions & 0 deletions
74
src/common/components/inline-edit/hooks/use-submit-cancel-hook.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,74 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
import { EditType } from '../inline-edit.model'; | ||
|
||
interface Configuration { | ||
editType: EditType | undefined; | ||
isEditable: boolean; | ||
text: string; | ||
onTextSubmit: (text: string) => void; | ||
} | ||
|
||
export const useSubmitCancelHook = ( | ||
configuration: Configuration, | ||
setEditText: React.Dispatch<React.SetStateAction<string>> | ||
) => { | ||
const { editType, isEditable, text, onTextSubmit } = configuration; | ||
const inputRef = useRef<HTMLInputElement>(null); | ||
const textAreaRef = useRef<HTMLTextAreaElement>(null); | ||
const [isEditing, setIsEditing] = useState(false); | ||
|
||
const getActiveInputRef = (): | ||
| HTMLInputElement | ||
| HTMLTextAreaElement | ||
| null => (editType === 'input' ? inputRef.current : textAreaRef.current); | ||
|
||
// handle click outside of the input when editing | ||
useEffect(() => { | ||
if (!isEditable) return; | ||
|
||
const handleClickOutside = (event: MouseEvent) => { | ||
if ( | ||
getActiveInputRef() && | ||
!getActiveInputRef()?.contains(event.target as Node) | ||
) { | ||
setIsEditing(false); | ||
onTextSubmit(getActiveInputRef()?.value || ''); | ||
} | ||
}; | ||
|
||
const handleKeyDown = (event: KeyboardEvent) => { | ||
if (isEditing && event.key === 'Escape') { | ||
setIsEditing(false); | ||
setEditText(text); | ||
} | ||
|
||
if (editType === 'input' && isEditable && event.key === 'Enter') { | ||
setIsEditing(false); | ||
onTextSubmit(getActiveInputRef()?.value || ''); | ||
} | ||
}; | ||
|
||
if (isEditing) { | ||
getActiveInputRef()?.focus(); | ||
getActiveInputRef()?.select(); | ||
document.addEventListener('mousedown', handleClickOutside); | ||
document.addEventListener('keydown', handleKeyDown); | ||
} else { | ||
document.removeEventListener('mousedown', handleClickOutside); | ||
document.removeEventListener('keydown', handleKeyDown); | ||
} | ||
|
||
return () => { | ||
document.removeEventListener('mousedown', handleClickOutside); | ||
document.removeEventListener('keydown', handleKeyDown); | ||
}; | ||
}, [isEditing]); | ||
|
||
return { | ||
isEditing, | ||
setIsEditing, | ||
inputRef, | ||
textAreaRef, | ||
getActiveInputRef, | ||
}; | ||
}; |
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,11 @@ | ||
export type EditType = 'input' | 'textarea'; | ||
|
||
type PositionType = 'absolute' | 'relative' | 'fixed' | 'static'; | ||
|
||
export interface StyleDivProps { | ||
position: PositionType; | ||
top: string | number; | ||
left: string | number; | ||
width: string | number; | ||
height: string | number; | ||
} |
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