Skip to content

πŸŽ₯ Make videos programmatically with React

Add documentation article for Editor Starter undo + redo

Jonny Burger375680e4ae67
diff --git a/packages/docs/docs/editor-starter/features.mdx b/packages/docs/docs/editor-starter/features.mdx
index 5ce7af796d..d2d4637593 100644
--- a/packages/docs/docs/editor-starter/features.mdx
+++ b/packages/docs/docs/editor-starter/features.mdx
@@ -1,6 +1,6 @@
---
image: /generated/articles-docs-editor-starter-features.png
-title: Editor Starter – Features
+title: Features
sidebar_label: Features
id: features
---
diff --git a/packages/docs/docs/editor-starter/undo-and-redo.mdx b/packages/docs/docs/editor-starter/undo-and-redo.mdx
new file mode 100644
index 0000000000..5817e95d21
--- /dev/null
+++ b/packages/docs/docs/editor-starter/undo-and-redo.mdx
@@ -0,0 +1,82 @@
+---
+image: /generated/articles-docs-editor-starter-undo-and-redo.png
+title: Undo and Redo
+sidebar_label: Undo and Redo
+id: undo-redo
+---
+
+The Editor Starter implemnts a basic Undo and Redo feature.
+It works by keeping an array of snapshots of the state of the editor in memory.
+
+## Undoable state
+
+State is separated into undoable and non-undoable parts.
+Undoable state is located within the `undoableState` object of the root state.
+
+**Undoable state** may be:
+
+- Position, size and other properties of an item
+- Track position and arrangement
+- Video properties like dimensions and frame rate
+
+**Non-undoable state** may be:
+
+- Asset upload progress
+- Zoom level
+- Captioning progress
+- Clipboard state
+- Rendering state
+- Selection state
+
+## Undo stack
+
+An undo stack is an array of previous states that is being kept in memory.
+By default, the undo stack holds 50 states.
+
+Whenever updating a state, you should set the `commitToUndoStack` property appropriately to indicate whether the state update should be committed to the undo stack.
+
+Actions that you may not want to add to the undo stack are high-frequency state update like:
+
+- Dragging an item to a new position in the canvas
+- Dragging an item to a new position in the timeline
+- Trimming an item in the timeline
+- Changing a value in the inspector with the slider
+
+In these cases, it is appropriate to only commit to the undo stack once the mouse cursor is released.
+
+## Preventing state changes
+
+You should prevent an unnecessary write to the undo stack when nothing has changed.
+Nothing is written to the undo stack when the identity of the `undoableState` object has not changed.
+
+This may require you to add additional checks to your state update functions to return the same object if nothing has changed.
+
+```ts title="Preventing an identity change"
+export const markAsDragging = (state: EditorState, itemId: string): EditorState => {
+ return changeItem(state, itemId, (item) => {
+ if (item.isDragging) {
+ // The item would not change, so we return the original object
+ return item;
+ }
+
+ return {
+ ...item,
+ isDragging: true,
+ };
+ });
+};
+```
+
+## Undo and Redo interactions
+
+You can undo and redo actions:
+
+- by using the buttons
+- the built-in keyboard shortcuts: <kbd>Ctrl</kbd> + <kbd>Z</kbd> for undo and <kbd>Ctrl</kbd> + <kbd>Y</kbd> for redo.
+
+This behavior is controlled by the following [flags](/docs/editor-starter/features):
+
+- [`FEATURE_UNDO_BUTTON`](https://github.com/search?q=repo%3Aremotion-dev%2Feditor-starter%20FEATURE_UNDO_BUTTON&type=code)
+- [`FEATURE_REDO_BUTTON`](https://github.com/search?q=repo%3Aremotion-dev%2Feditor-starter%20FEATURE_REDO_BUTTON&type=code)
+- [`FEATURE_UNDO_SHORTCUT`](https://github.com/search?q=repo%3Aremotion-dev%2Feditor-starter%20FEATURE_UNDO_SHORTCUT&type=code)
+- [`FEATURE_REDO_SHORTCUT`](https://github.com/search?q=repo%3Aremotion-dev%2Feditor-starter%20FEATURE_REDO_SHORTCUT&type=code)
diff --git a/packages/docs/docs/player/premounting.mdx b/packages/docs/docs/player/premounting.mdx
index a4ef62778a..5247b8d61b 100644
--- a/packages/docs/docs/player/premounting.mdx
+++ b/packages/docs/docs/player/premounting.mdx
@@ -74,13 +74,7 @@ Use the following component like you would `<Sequence>`:
```tsx twoslash title="PremountedSequence.tsx"
import React, {forwardRef, useMemo} from 'react';
-import {
- Freeze,
- getRemotionEnvironment,
- Sequence,
- SequenceProps,
- useCurrentFrame,
-} from 'remotion';
+import {Freeze, getRemotionEnvironment, Sequence, SequenceProps, useCurrentFrame} from 'remotion';
export type PremountedSequenceProps = SequenceProps & {
premountFor: number;
@@ -99,36 +93,24 @@ const PremountedSequenceRefForwardingFunction: React.ForwardRefRenderFunction<
}
const {style: passedStyle, from = 0, ...otherProps} = props;
- const active =
- frame < from &&
- frame >= from - premountFor &&
- !getRemotionEnvironment().isRendering;
+ const active = frame < from && frame >= from - premountFor && !getRemotionEnvironment().isRendering;
const style: React.CSSProperties = useMemo(() => {
return {
...passedStyle,
opacity: active ? 0 : 1,
- // @ts-expect-error Only in the docs - it will not give a type error in a Remotion project
pointerEvents: active ? 'none' : (passedStyle?.pointerEvents ?? 'auto'),
};
}, [active, passedStyle]);
return (
<Freeze frame={from} active={active}>
- <Sequence
- ref={ref}
- name={`<PremountedSequence premountFor={${premountFor}}>`}
- from={from}
- style={style}
- {...otherProps}
- />
+ <Sequence ref={ref} name={`<PremountedSequence premountFor={${premountFor}}>`} from={from} style={style} {...otherProps} />
</Freeze>
);
};
-export const PremountedSequence = forwardRef(
- PremountedSequenceRefForwardingFunction,
-);
+export const PremountedSequence = forwardRef(PremountedSequenceRefForwardingFunction);
```
:::note
diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.ts
index 01926fbd5b..a6a5d553cc 100644
--- a/packages/docs/sidebars.ts
+++ b/packages/docs/sidebars.ts
@@ -1335,7 +1335,7 @@ const sidebars: SidebarsConfig = {
type: 'doc',
id: 'editor-starter/features',
},
- items: ['editor-starter/features'],
+ items: ['editor-starter/features', 'editor-starter/undo-redo'],
},
{
type: 'link',
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index 958e651dd9..2dc349992a 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -1279,13 +1279,22 @@ export const articles = [
},
{
id: 'features',
- title: 'Editor Starter – Features',
+ title: 'Features',
relativePath: 'docs/editor-starter/features.mdx',
compId: 'articles-docs-editor-starter-features',
crumb: null,
noAi: false,
slug: 'editor-starter/features',
},
+ {
+ id: 'undo-redo',
+ title: 'Undo and Redo',
+ relativePath: 'docs/editor-starter/undo-and-redo.mdx',
+ compId: 'articles-docs-editor-starter-undo-and-redo',
+ crumb: null,
+ noAi: false,
+ slug: 'editor-starter/undo-redo',
+ },
{
id: 'enable-scss/enable-scss',
title: 'enableScss()',