lexical icon indicating copy to clipboard operation
lexical copied to clipboard

Feature: Readonly editor with state updates & listeners

Open yudori opened this issue 4 years ago • 8 comments

I have a usecase where:

  1. I don't want the editor to be editable by the user
  2. I want the capability to receive editor updates and subscribe to commands like click, drag, etc.

So far, I haven't been able to find a clean solution that achieves both. Setting the editor to readonly in the initialConfig 'freezes' the editor thereby achieving (1) but not (2). It would be nice to have an editor that is non-editable but can still hooked up to the in-built listeners. For react, having a prop that can override isReadOnly in the LexicalContentEditable seems to be one way to achieve this.

yudori avatar May 13 '22 04:05 yudori

We should possibly let event commands go through for click and drag. Right now they're guarded on by the readonly state. Editor state updates should work in read only mode though.

trueadm avatar May 16 '22 08:05 trueadm

@trueadm That sounds ideal to me! I would also argue selection change events would fit into that category of events that should be emitted even in read only mode

yudori avatar May 16 '22 22:05 yudori

@trueadm That sounds ideal to me! I would also argue selection change events would fit into that category of events that should be emitted even in read only mode

I’d need to think about this more as selecting text in a content editable has different DOM heuristics to when you do it on non contenteditables. I’m not even sure it’s possible.

trueadm avatar May 16 '22 23:05 trueadm

Hey, is there any update on this? Is it possible to have a readOnly editor that listens to selection state changes?

pguduguntla avatar Jun 06 '22 00:06 pguduguntla

@pguduguntla The solution I went with is to leave the global lexical editor readonly state as false and instead use a custom 'readonly' contenteditable in the text plugin.

Custom ReadOnlyContentEditable.ts

import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
import React, { useCallback } from 'react';

export type Props = {
    id?: string,
};

export default function ReadOnlyContentEditable(props: Props): JSX.Element {
    const [editor] = useLexicalComposerContext();
    const ref = useCallback(
        (rootElement: null | HTMLElement) => {
            editor.setRootElement(rootElement);
        },
        [editor]
    );

    return (
        <div
            contentEditable={false}
            id={props.id}
            ref={ref}
        />
    );
}

Usage in LexicalPlainTextPlugin

<LexicalPlainTextPlugin
  contentEditable={<ReadOnlyContentEditable />}
  ...
/>

yudori avatar Jun 06 '22 00:06 yudori

@yudori Oh sweet! That works for my use case, thanks so much man!

pguduguntla avatar Jun 06 '22 00:06 pguduguntla

@yudori @zurfyx it is causing a problem in my condition. When I am trying to add the new value by setting contenteditable={stateTrueBool} then my new data is lost after setting conditEditable=false. Can you please guide me on why is it deleting the new data.

mahmedmushtaq avatar Aug 05 '22 06:08 mahmedmushtaq

@mahmedmushtaq maybe you need to call editor.setEditorState with the new editor state after you change contentEditable flag?

6peterlu avatar Oct 05 '22 14:10 6peterlu