Skip to content
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
25 changes: 17 additions & 8 deletions packages/block-editor/src/components/rich-text/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import {
} from '../block-edit/context';
import FormatToolbarContainer from './format-toolbar-container';
import { store as blockEditorStore } from '../../store';
import { useMarkPersistent } from './use-mark-persistent';
import { useEventListeners } from './event-listeners';
import FormatEdit from './format-edit';
import {
Expand Down Expand Up @@ -436,6 +435,9 @@ function RichTextWrapper(
]
);

// When the field last changed, or 0 after a format change.
const lastChangeRef = useRef( 0 );

const {
value,
getValue,
Expand All @@ -445,7 +447,20 @@ function RichTextWrapper(
[ getDomPositionKey ]: getDomPosition,
} = useRichText( {
value: adjustedValue,
onChange: adjustedOnChange,
onChange( newValue: unknown, meta: { __unstableText: string } ) {
// Typing goes into one undo level until a second without typing or
// a format change, after which the next change starts a new one.
if ( Date.now() - lastChangeRef.current >= 1000 ) {
__unstableMarkLastChangeAsPersistent();
}
// The hook is untyped, and calls it with the field's kind of value.
( adjustedOnChange as ( ...args: unknown[] ) => void )(
newValue,
meta
);
lastChangeRef.current =
meta.__unstableText === value.text ? 0 : Date.now();
},
selectionStart,
selectionEnd,
onSelectionChange,
Expand Down Expand Up @@ -614,12 +629,6 @@ function RichTextWrapper(
ariaActiveDescendant,
] );

useMarkPersistent( {
html: adjustedValue,
value,
onMarkPersistent: __unstableMarkLastChangeAsPersistent,
} );

const [ keyboardShortcuts ] = useState(
() => new Set< ( event: KeyboardEvent ) => void >()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,12 @@ function Fields( {
contentRef,
}: FieldsProps ) {
const registry = useRegistry();
const { updateBlockAttributes, replaceBlocks, selectionChange } =
useDispatch( blockEditorStore );
const {
updateBlockAttributes,
replaceBlocks,
selectionChange,
__unstableMarkLastChangeAsPersistent,
} = useDispatch( blockEditorStore );
const keys: string[] = useSelect(
( select ) => {
const { getBlockName } = select( blockEditorStore );
Expand Down Expand Up @@ -334,6 +338,8 @@ function Fields( {
),
{ uniqueByBlock: true }
);
// The next change to these blocks starts a new undo level.
__unstableMarkLastChangeAsPersistent();
return;
}
// The selected text was replaced, by an inline image or a
Expand Down

This file was deleted.

15 changes: 0 additions & 15 deletions packages/block-editor/src/components/rich-text/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,6 @@ export type FieldFormatSettings = {
disabled?: boolean;
};

/**
* A snapshot of the value, to classify the change since the last one.
*/
export type MarkPersistentSnapshot = {
html: unknown;
text: string;
hasActiveFormats: boolean;
};

export type UseMarkPersistentOptions = {
html: unknown;
value: Pick< RichTextValue, 'text' | 'activeFormats' >;
onMarkPersistent: () => void;
};

/**
* Props of `RichText.Content`. Other props go to the element.
*/
Expand Down

This file was deleted.

Loading
Loading