diff --git a/src/app/components/editor/Editor.css.ts b/src/app/components/editor/Editor.css.ts index 658bc1af..990c6a74 100644 --- a/src/app/components/editor/Editor.css.ts +++ b/src/app/components/editor/Editor.css.ts @@ -13,6 +13,21 @@ export const Editor = style([ }, ]); +export const EditorRow = style({ + display: 'grid', + gridTemplateColumns: 'auto 1fr auto', + alignItems: 'center', +}); + +export const EditorRowMultiline = style({ + gridTemplateColumns: 'auto 1fr', + gridTemplateAreas: ` + "before textarea" + "before after" + `, + alignItems: 'start', +}); + export const EditorOptions = style([ DefaultReset, { @@ -20,7 +35,23 @@ export const EditorOptions = style([ }, ]); -export const EditorTextareaScroll = style({}); +export const EditorOptionsMultiline = style({ + gridArea: 'before', + alignSelf: 'end', +}); + +export const EditorOptionsAfterMultiline = style({ + gridArea: 'after', + justifySelf: 'end', +}); + +export const EditorTextareaScroll = style({ + minWidth: 0, +}); + +export const EditorTextareaScrollMultiline = style({ + gridArea: 'textarea', +}); export const EditorReplacementContent = style([ DefaultReset, @@ -39,11 +70,16 @@ export const EditorReplacementContent = style([ }, ]); +export const EditorReplacementContentMultiline = style({ + paddingLeft: config.space.S200, + paddingRight: config.space.S200, +}); + export const EditorTextarea = style([ DefaultReset, { flexGrow: 1, - height: '100%', + height: 'auto', padding: `${toRem(13)} ${toRem(1)}`, selectors: { [`${EditorTextareaScroll}:first-child &`]: { @@ -59,6 +95,11 @@ export const EditorTextarea = style([ }, ]); +export const EditorTextareaMultiline = style({ + paddingLeft: config.space.S200, + paddingRight: config.space.S200, +}); + export const EditorPlaceholderContainer = style([ DefaultReset, { @@ -74,6 +115,11 @@ export const EditorPlaceholderTextVisual = style([ display: 'block', paddingTop: toRem(13), paddingLeft: toRem(1), + selectors: { + [`${EditorTextareaScroll}:first-child &`]: { + paddingLeft: toRem(13), + }, + }, }, ]); diff --git a/src/app/components/editor/Editor.tsx b/src/app/components/editor/Editor.tsx index c706d5e2..4fa1c0ee 100644 --- a/src/app/components/editor/Editor.tsx +++ b/src/app/components/editor/Editor.tsx @@ -5,6 +5,7 @@ import { ReactNode, forwardRef, useCallback, + useRef, useState, } from 'react'; import { Box, Scroll, Text } from 'folds'; @@ -99,6 +100,64 @@ export const CustomEditor = forwardRef( const [slateInitialValue] = useState(() => [ { type: BlockType.Paragraph, children: [{ text: '' }] }, ]); + const editableRef = useRef(null); + const beforeRef = useRef(null); + const afterRef = useRef(null); + const isMultilineRef = useRef(false); + const [isMultiline, setIsMultiline] = useState(false); + const layoutIsMultiline = isMultiline && !replacementContent; + + const handleChange = useCallback( + (value: Descendant[]) => { + const hasMultipleBlocks = editor.children.length > 1; + const text = Editor.string(editor, []); + const hasExplicitNewlines = text.includes('\n'); + + const editable = editableRef.current; + if (editable) { + const computedStyle = getComputedStyle(editable); + const lineHeight = parseFloat(computedStyle.lineHeight) || 20; + const paddingTop = parseFloat(computedStyle.paddingTop) || 0; + const paddingBottom = parseFloat(computedStyle.paddingBottom) || 0; + const contentHeight = editable.scrollHeight - paddingTop - paddingBottom; + const isWrappingNow = contentHeight > lineHeight * 1.5; + + let nextMultiline: boolean; + + if (!isMultilineRef.current) { + nextMultiline = hasMultipleBlocks || hasExplicitNewlines || isWrappingNow; + } else { + const beforeWidth = beforeRef.current?.offsetWidth ?? 0; + const afterWidth = afterRef.current?.offsetWidth ?? 0; + const paddingLeft = parseFloat(computedStyle.paddingLeft) || 0; + const paddingRight = parseFloat(computedStyle.paddingRight) || 0; + const availableSingleLineWidth = + editable.offsetWidth - beforeWidth - afterWidth - paddingLeft - paddingRight; + + const canvas = document.createElement('canvas'); + const ctx = canvas.getContext('2d'); + let wouldWrapInSingleLine = false; + if (ctx) { + ctx.font = computedStyle.font; + const textWidth = ctx.measureText(text).width; + wouldWrapInSingleLine = textWidth > availableSingleLineWidth; + } + + nextMultiline = hasMultipleBlocks || hasExplicitNewlines || wouldWrapInSingleLine; + } + + isMultilineRef.current = nextMultiline; + setIsMultiline(nextMultiline); + } else { + const nextMultiline = hasMultipleBlocks || hasExplicitNewlines; + isMultilineRef.current = nextMultiline; + setIsMultiline(nextMultiline); + } + + onChange?.(value); + }, + [editor, onChange] + ); const renderElement = useCallback( (props: RenderElementProps) => , @@ -136,16 +195,25 @@ export const CustomEditor = forwardRef( return (
- + {top} - + {before && ( - + {before} )} ( hideTrack > {replacementContent ? ( -
{replacementContent}
+
+ {replacementContent} +
) : ( ( )}
{after && ( - + {after} )}