-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix locks state not being propagated on updates * Add simple locking demo * feat: make titles editable * feat: background,cursor-disabled, rewind * feat: improvements * fix: add global state for slides data * fix: lock losing * chore: cleanup * fix: too slow updates * fix: merge conflicts * fix: locking demo on locations * chore: add remove lock on click outside and update `maxlength` * chore: rollback workarounds * fix: use locking * chore: improve max length implementation * chore: remove unlocking on slide click * chore: remove unlocking on slide click * chore: renaming based on review feedback * chore: renaming based on review feedback * fix: max length for text editing --------- Co-authored-by: evgeny <[email protected]>
- Loading branch information
Showing
26 changed files
with
923 additions
and
138 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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,65 @@ | ||
import React, { useCallback, useEffect, useRef } from 'react'; | ||
import ContentEditable, { ContentEditableEvent } from 'react-contenteditable'; | ||
import sanitize from 'sanitize-html'; | ||
|
||
interface EditableTextProps extends Omit<React.HTMLAttributes<HTMLElement>, 'onChange' | 'children'> { | ||
as?: string; | ||
disabled: boolean; | ||
value: string; | ||
onChange(nextValue: string): void; | ||
maxlength?: number; | ||
className?: string; | ||
} | ||
|
||
export const EditableText: React.FC<EditableTextProps> = ({ | ||
as, | ||
disabled, | ||
maxlength = 300, | ||
value, | ||
onChange, | ||
...restProps | ||
}) => { | ||
const elementRef = useRef<HTMLElement | null>(null); | ||
const handleTextChange = useCallback( | ||
(evt: ContentEditableEvent) => { | ||
const nextValue = sanitize(evt.target.value, { | ||
allowedTags: [], | ||
}); | ||
|
||
if (nextValue.length > maxlength) { | ||
onChange(value); | ||
} else { | ||
onChange(nextValue); | ||
} | ||
}, | ||
[onChange, value, maxlength], | ||
); | ||
|
||
useEffect(() => { | ||
const element = elementRef.current; | ||
if (!disabled && element) { | ||
moveCursorToEnd(element); | ||
} | ||
}, [disabled]); | ||
|
||
return ( | ||
<ContentEditable | ||
tagName={as} | ||
innerRef={elementRef} | ||
disabled={disabled} | ||
html={value} | ||
onChange={handleTextChange} | ||
{...restProps} | ||
/> | ||
); | ||
}; | ||
|
||
const moveCursorToEnd = (el: HTMLElement) => { | ||
el.focus(); | ||
const range = document.createRange(); | ||
range.selectNodeContents(el); | ||
range.collapse(false); | ||
const selection = window.getSelection(); | ||
selection?.removeAllRanges(); | ||
selection?.addRange(range); | ||
}; |
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 |
---|---|---|
@@ -1,36 +1,73 @@ | ||
import React, { useRef } from 'react'; | ||
import cn from 'classnames'; | ||
import { useElementSelect, useMembers } from '../hooks'; | ||
import { findActiveMember, getMemberFirstName, getOutlineClasses } from '../utils'; | ||
import { getMemberFirstName, getOutlineClasses } from '../utils'; | ||
import { StickyLabel } from './StickyLabel'; | ||
import { LockFilledSvg } from './svg/LockedFilled.tsx'; | ||
import { EditableText } from './EditableText.tsx'; | ||
import { useTextComponentLock } from '../hooks/useTextComponentLock.ts'; | ||
|
||
interface Props extends React.HTMLAttributes<HTMLParagraphElement> { | ||
id: string; | ||
slide: string; | ||
variant?: 'regular' | 'aside'; | ||
children: string; | ||
maxlength?: number; | ||
} | ||
|
||
export const Paragraph = ({ variant = 'regular', id, slide, className, ...props }: Props) => { | ||
const { members } = useMembers(); | ||
const { handleSelect } = useElementSelect(id); | ||
const activeMember = findActiveMember(id, slide, members); | ||
const name = getMemberFirstName(activeMember); | ||
const outlineClasses = getOutlineClasses(activeMember); | ||
export const Paragraph = ({ | ||
variant = 'regular', | ||
id, | ||
slide, | ||
className, | ||
children, | ||
maxlength = 300, | ||
...props | ||
}: Props) => { | ||
const containerRef = useRef<HTMLDivElement | null>(null); | ||
const { content, activeMember, locked, lockedByYou, editIsNotAllowed, handleSelect, handleContentUpdate } = | ||
useTextComponentLock({ | ||
id, | ||
slide, | ||
defaultText: children, | ||
containerRef, | ||
}); | ||
const memberName = getMemberFirstName(activeMember); | ||
const { outlineClasses, stickyLabelClasses } = getOutlineClasses(activeMember); | ||
|
||
return ( | ||
<p | ||
id={id} | ||
data-before={name} | ||
className={cn( | ||
'text-ably-avatar-stack-demo-slide-text cursor-pointer relative', | ||
{ | ||
'xs:w-auto text-xs xs:text-base md:text-lg xs:my-4 md:my-0': variant === 'regular', | ||
'text-[13px] p-0 leading-6': variant === 'aside', | ||
[`outline-2 outline before:content-[attr(data-before)] before:absolute before:-top-[22px] before:-left-[2px] before:px-[10px] before:text-sm before:text-white before:rounded-t-lg before:normal-case ${outlineClasses}`]: | ||
!!activeMember, | ||
}, | ||
className, | ||
)} | ||
<div | ||
ref={containerRef} | ||
{...props} | ||
onClick={handleSelect} | ||
/> | ||
className="relative" | ||
onClick={locked ? undefined : handleSelect} | ||
> | ||
<StickyLabel | ||
visible={locked} | ||
className={`${stickyLabelClasses} flex flex-row items-center`} | ||
> | ||
{lockedByYou ? 'You' : memberName} | ||
{editIsNotAllowed && <LockFilledSvg className="text-white" />} | ||
</StickyLabel> | ||
<EditableText | ||
as="p" | ||
id={id} | ||
disabled={!lockedByYou} | ||
value={content} | ||
onChange={handleContentUpdate} | ||
maxlength={maxlength} | ||
className={cn( | ||
'text-ably-avatar-stack-demo-slide-text break-all', | ||
{ | ||
'xs:w-auto text-xs xs:text-base md:text-lg xs:my-4 md:my-0': variant === 'regular', | ||
'text-[13px] p-0 leading-6': variant === 'aside', | ||
[`outline-2 outline ${outlineClasses}`]: locked, | ||
'cursor-pointer': !locked, | ||
'cursor-not-allowed': editIsNotAllowed, | ||
'bg-slate-200': editIsNotAllowed, | ||
}, | ||
className, | ||
)} | ||
/> | ||
</div> | ||
); | ||
}; |
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,13 @@ | ||
import React, { useContext } from 'react'; | ||
|
||
interface PreviewContextProviderProps { | ||
preview: boolean; | ||
children: React.ReactNode; | ||
} | ||
const PreviewContext = React.createContext<boolean>(false); | ||
|
||
export const PreviewProvider: React.FC<PreviewContextProviderProps> = ({ preview, children }) => ( | ||
<PreviewContext.Provider value={preview}>{children}</PreviewContext.Provider> | ||
); | ||
|
||
export const usePreview = () => useContext<boolean>(PreviewContext); |
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,24 @@ | ||
import React, { useMemo, useState } from 'react'; | ||
|
||
interface SlidesStateContextProps { | ||
slidesState: Record<string, string>; | ||
setContent(id: string, nextContent: string): void; | ||
} | ||
export const SlidesStateContext = React.createContext<SlidesStateContextProps>({ | ||
slidesState: {}, | ||
setContent: () => {}, | ||
}); | ||
|
||
export const SlidesStateContextProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { | ||
const [slidesState, setSlidesState] = useState<Record<string, string>>({}); | ||
const value = useMemo( | ||
() => ({ | ||
slidesState, | ||
setContent: (id: string, nextContent: string) => { | ||
setSlidesState((prevState) => ({ ...prevState, [id]: nextContent })); | ||
}, | ||
}), | ||
[slidesState, setSlidesState], | ||
); | ||
return <SlidesStateContext.Provider value={value}>{children}</SlidesStateContext.Provider>; | ||
}; |
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,18 @@ | ||
import React from 'react'; | ||
|
||
interface StickyLabelProps { | ||
visible: boolean; | ||
className?: string; | ||
children: React.ReactNode; | ||
} | ||
export const StickyLabel: React.FC<StickyLabelProps> = ({ visible, className, children }) => { | ||
if (!visible) return null; | ||
|
||
return ( | ||
<div | ||
className={`absolute -top-[22px] -left-[2px] px-[10px] text-sm text-white rounded-t-lg normal-case ${className}`} | ||
> | ||
{children} | ||
</div> | ||
); | ||
}; |
Oops, something went wrong.