Editor line wrapping support (not used yet)

This commit is contained in:
Gregory Schier
2023-04-04 16:40:37 -07:00
parent b6b549ca18
commit 9147252e5b

View File

@@ -35,6 +35,7 @@ export interface EditorProps {
onFocus?: () => void; onFocus?: () => void;
onBlur?: () => void; onBlur?: () => void;
singleLine?: boolean; singleLine?: boolean;
wrapLines?: boolean;
format?: (v: string) => string; format?: (v: string) => string;
autocomplete?: GenericCompletionConfig; autocomplete?: GenericCompletionConfig;
actions?: ReactNode; actions?: ReactNode;
@@ -59,6 +60,7 @@ const _Editor = forwardRef<EditorView | undefined, EditorProps>(function Editor(
format, format,
autocomplete, autocomplete,
actions, actions,
wrapLines,
}: EditorProps, }: EditorProps,
ref, ref,
) { ) {
@@ -93,6 +95,15 @@ const _Editor = forwardRef<EditorView | undefined, EditorProps>(function Editor(
cm.current?.view.dispatch({ effects: effect }); cm.current?.view.dispatch({ effects: effect });
}, [placeholder]); }, [placeholder]);
// Update wrap lines
const wrapLinesCompartment = useRef(new Compartment());
useEffect(() => {
if (cm.current === null) return;
const ext = wrapLines ? [EditorView.lineWrapping] : [];
const effect = wrapLinesCompartment.current.reconfigure(ext);
cm.current?.view.dispatch({ effects: effect });
}, [wrapLines]);
// Update language extension when contentType changes // Update language extension when contentType changes
useEffect(() => { useEffect(() => {
if (cm.current === null) return; if (cm.current === null) return;
@@ -126,16 +137,15 @@ const _Editor = forwardRef<EditorView | undefined, EditorProps>(function Editor(
doc: `${defaultValue ?? ''}`, doc: `${defaultValue ?? ''}`,
extensions: [ extensions: [
languageCompartment.of(langExt), languageCompartment.of(langExt),
placeholderCompartment.current.of( placeholderCompartment.current.of([]),
placeholderExt(placeholderElFromText(placeholder ?? '')), wrapLinesCompartment.current.of([]),
),
...getExtensions({ ...getExtensions({
container, container,
readOnly,
singleLine,
onChange: handleChange, onChange: handleChange,
onFocus: handleFocus, onFocus: handleFocus,
onBlur: handleBlur, onBlur: handleBlur,
readOnly,
singleLine,
}), }),
], ],
}); });