Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi line image caption #4324

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions scripts/core/editor3/components/media/MediaBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ export class MediaBlockComponent extends React.Component<any, any> {
<PlainTextEditor
classes="image-block__description"
spellcheck={true}
multiLine={true}
placeholder={gettext('Caption')}
onFocus={setLocked}
value={data.description_text}
Expand Down
18 changes: 13 additions & 5 deletions scripts/core/ui/components/PlainTextEditor/PlainTextEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export interface IProps {
placeholder?: string;
onFocus?: () => void;
disabled?: boolean;

// Defaults to false.
multiLine?: boolean;
}

interface IState {
Expand All @@ -51,15 +54,17 @@ function updateStateWithValue(value: string, editorState: EditorState) {
}

export class PlainTextEditor extends React.Component<IProps, IState> {
spellcheckerTimeout?: number;
selection: SelectionState;
private spellcheckerTimeout?: number;
private selection: SelectionState;
private lastComputedValue: string;

constructor(props) {
super(props);
this.lastComputedValue = props.value?.toString() || '';

this.state = {
editorState: EditorState.createWithContent(
ContentState.createFromText(props.value?.toString() || ''),
ContentState.createFromText(this.lastComputedValue),
),
hasFocus: false,
};
Expand Down Expand Up @@ -120,7 +125,9 @@ export class PlainTextEditor extends React.Component<IProps, IState> {
* This version works fine and we can still handle our own selection state
* */
UNSAFE_componentWillReceiveProps(props: IProps) {
this.setState({editorState: updateStateWithValue(props.value || '', this.state.editorState)});
if (this.lastComputedValue !== props.value) {
this.setState({editorState: updateStateWithValue(props.value || '', this.state.editorState)});
}
}

handleEditorChange(editorState: EditorState) {
Expand All @@ -130,6 +137,7 @@ export class PlainTextEditor extends React.Component<IProps, IState> {
) {
const value = editorState.getCurrentContent().getPlainText();

this.lastComputedValue = value;
this.props.onChange(value, this.props.onChangeData);
}

Expand All @@ -153,7 +161,7 @@ export class PlainTextEditor extends React.Component<IProps, IState> {
}

handleKeyCommand(command: DraftEditorCommand): DraftHandleValue {
if (command === 'split-block') {
if (command === 'split-block' && this.props.multiLine !== true) {
return 'handled'; // disable Enter
}

Expand Down