Allows commenting outside edit mode when seamless editing is disabled. (#5422)

This commit is contained in:
Tom Moor
2023-06-10 15:56:00 +03:00
committed by GitHub
parent 3f7e66980b
commit d319bb7d9a
12 changed files with 112 additions and 42 deletions

View File

@@ -14,6 +14,12 @@ import {
Node as ProsemirrorNode,
} from "prosemirror-model";
import { EditorState, Selection, Plugin, Transaction } from "prosemirror-state";
import {
AddMarkStep,
RemoveMarkStep,
ReplaceAroundStep,
ReplaceStep,
} from "prosemirror-transform";
import { Decoration, EditorView, NodeViewConstructor } from "prosemirror-view";
import * as React from "react";
import styled, { css, DefaultTheme, ThemeProps } from "styled-components";
@@ -70,7 +76,9 @@ export type Props = {
/** If the editor should not allow editing */
readOnly?: boolean;
/** If the editor should still allow editing checkboxes when it is readOnly */
readOnlyWriteCheckboxes?: boolean;
canUpdate?: boolean;
/** If the editor should still allow commenting when it is readOnly */
canComment?: boolean;
/** A dictionary of translated strings used in the editor */
dictionary: Dictionary;
/** The reading direction of the text content, if known */
@@ -441,9 +449,17 @@ export class Editor extends React.PureComponent<
const isEditingCheckbox = (tr: Transaction) =>
tr.steps.some(
(step: any) =>
step.slice?.content?.firstChild?.type.name ===
this.schema.nodes.checkbox_item.name
(step) =>
(step instanceof ReplaceAroundStep || step instanceof ReplaceStep) &&
step.slice.content?.firstChild?.type.name ===
this.schema.nodes.checkbox_item.name
);
const isEditingComment = (tr: Transaction) =>
tr.steps.some(
(step) =>
(step instanceof AddMarkStep || step instanceof RemoveMarkStep) &&
step.mark.type.name === this.schema.marks.comment.name
);
const self = this; // eslint-disable-line
@@ -469,8 +485,8 @@ export class Editor extends React.PureComponent<
if (
transactions.some((tr) => tr.docChanged) &&
(!self.props.readOnly ||
(self.props.readOnlyWriteCheckboxes &&
transactions.some(isEditingCheckbox)))
(self.props.canUpdate && transactions.some(isEditingCheckbox)) ||
(self.props.canComment && transactions.some(isEditingComment)))
) {
self.handleChange();
}
@@ -723,15 +739,8 @@ export class Editor extends React.PureComponent<
};
public render() {
const {
dir,
readOnly,
readOnlyWriteCheckboxes,
grow,
style,
className,
onKeyDown,
} = this.props;
const { dir, readOnly, canUpdate, grow, style, className, onKeyDown } =
this.props;
const { isRTL } = this.state;
return (
@@ -751,11 +760,24 @@ export class Editor extends React.PureComponent<
rtl={isRTL}
grow={grow}
readOnly={readOnly}
readOnlyWriteCheckboxes={readOnlyWriteCheckboxes}
readOnlyWriteCheckboxes={canUpdate}
focusedCommentId={this.props.focusedCommentId}
editorStyle={this.props.editorStyle}
ref={this.elementRef}
/>
{this.view && (
<SelectionToolbar
rtl={isRTL}
readOnly={readOnly}
canComment={this.props.canComment}
isTemplate={this.props.template === true}
onOpen={this.handleOpenSelectionToolbar}
onClose={this.handleCloseSelectionToolbar}
onSearchLink={this.props.onSearchLink}
onClickLink={this.props.onClickLink}
onCreateLink={this.props.onCreateLink}
/>
)}
{!readOnly && this.view && (
<>
{this.marks.link && (
@@ -799,15 +821,6 @@ export class Editor extends React.PureComponent<
}
/>
)}
<SelectionToolbar
rtl={isRTL}
isTemplate={this.props.template === true}
onOpen={this.handleOpenSelectionToolbar}
onClose={this.handleCloseSelectionToolbar}
onSearchLink={this.props.onSearchLink}
onClickLink={this.props.onClickLink}
onCreateLink={this.props.onCreateLink}
/>
<BlockMenu
rtl={isRTL}
isActive={