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

fix: sync state on undo when editor is unfocused #5737

Merged
merged 11 commits into from
Oct 16, 2024
5 changes: 5 additions & 0 deletions .changeset/curvy-seals-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-react': patch
---

fix: sync built-in state on undo when editor is unfocused
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ permissions:
jobs:
ci:
name: ${{ matrix.command }}
runs-on: ubuntu-latest
# Pin the version to avoid dependency installation issues
runs-on: ubuntu-22.04
strategy:
matrix:
command:
Expand Down
5 changes: 5 additions & 0 deletions docs/libraries/slate-history/history-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ Undo to the previous saved state.

### Merging and Saving

#### `HistoryEditor.withMerging(editor: HistoryEditor, fn: () => void): void`

Apply a series of changes inside a synchronous `fn`, These operations will
be merged into the previous history.

#### `HistoryEditor.withoutMerging(editor: HistoryEditor, fn: () => void): void`

Apply a series of changes inside a synchronous `fn`, without merging any of
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"test": "yarn run test:mocha && yarn run test:jest",
"test:custom": "mocha --require ./config/babel/register.cjs ./packages/slate/test/index.js",
"test:inspect": "yarn test --inspect-brk",
"test:integration": "playwright install && run-p -r serve playwright",
"test:integration-local": "run-p -r serve playwright",
"test:integration": "playwright install --with-deps && run-p -r serve playwright",
"test:integration-local": "playwright install && run-p -r serve playwright",
"test:mocha": "mocha --require ./config/babel/register.cjs ./packages/{slate,slate-history,slate-hyperscript}/test/**/*.{js,ts}",
"test:jest": "jest --config jest.config.js",
"tsc:examples": "tsc --project ./site/tsconfig.example.json",
Expand Down
25 changes: 24 additions & 1 deletion packages/slate-react/src/components/editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1042,8 +1042,31 @@ export const Editable = forwardRef(
op()
}
deferredOperations.current = []

// COMPAT: Since `beforeinput` doesn't fully `preventDefault`,
// there's a chance that content might be placed in the browser's undo stack.
// This means undo can be triggered even when the div is not focused,
// and it only triggers the input event for the node. (2024/10/09)
if (!ReactEditor.isFocused(editor)) {
const native = event.nativeEvent as InputEvent
const maybeHistoryEditor: any = editor
if (
native.inputType === 'historyUndo' &&
typeof maybeHistoryEditor.undo === 'function'
) {
maybeHistoryEditor.undo()
return
}
if (
native.inputType === 'historyRedo' &&
typeof maybeHistoryEditor.redo === 'function'
) {
maybeHistoryEditor.redo()
return
}
}
},
[attributes.onInput]
[attributes.onInput, editor]
)}
onBlur={useCallback(
(event: React.FocusEvent<HTMLDivElement>) => {
Expand Down
Loading