Skip to content

Commit

Permalink
refactor(portable-text-editor): refactor mutation and patch handling
Browse files Browse the repository at this point in the history
This will make the change events 'patch', 'throttle' and 'mutation' work more correctly together.
There could be rare situations where mutations would not be sent in how this was set up before.
  • Loading branch information
skogsmaskin committed Mar 29, 2022
1 parent d891474 commit b318626
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,19 +248,17 @@ export class PortableTextEditor extends React.Component<PortableTextEditorProps,
const finalPatches = compactPatches(this.pendingPatches)
if (finalPatches.length > 0) {
onChange({type: 'mutation', patches: finalPatches})
this.pendingPatches = []
debug('Flushing', finalPatches)
}
this.pendingPatches = []
}

private onEditorChange = (next: EditorChange): void => {
const {onChange} = this.props
switch (next.type) {
case 'mutation':
if (this.isThrottling) {
this.pendingPatches = [...this.pendingPatches, ...next.patches]
} else {
this.flush()
}
case 'patch':
this.pendingPatches.push(next.patch)
onChange(next)
break
case 'throttle':
if (next.throttle !== this.isThrottling) {
Expand All @@ -270,9 +268,7 @@ export class PortableTextEditor extends React.Component<PortableTextEditorProps,
this.isThrottling = true
} else {
this.isThrottling = false
if (this.pendingPatches.length > 0) {
this.flush()
}
this.flush()
}
break
case 'selection':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ export function createWithPatches(
PATCHING.set(editor, true)
previousChildren = editor.children

// This will cancel the throttle when the user is not producing anything for a short time
const cancelThrottle = debounce(() => {
// This will cancel the throttle when the user is not producing any patches for a short time
const cancelThrottleDebounced = debounce(() => {
change$.next({type: 'throttle', throttle: false})
}, THROTTLE_EDITOR_MS)

Expand Down Expand Up @@ -198,20 +198,14 @@ export function createWithPatches(
if (patches.length > 0) {
// Signal throttling
change$.next({type: 'throttle', throttle: true})
// Emit all patches immediately
// Emit all patches
patches.forEach((patch) => {
change$.next({
type: 'patch',
patch,
})
})

// Emit mutation after user is done typing (we show only local state as that happens)
change$.next({
type: 'mutation',
patches: patches,
})
cancelThrottle()
cancelThrottleDebounced()
}
return editor
}
Expand Down

3 comments on commit b318626

@vercel
Copy link

@vercel vercel bot commented on b318626 Mar 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

studio-workshop – ./dev/workshop

studio-workshop.sanity.build
studio-workshop-git-next.sanity.build

@vercel
Copy link

@vercel vercel bot commented on b318626 Mar 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

test-studio – ./

test-studio-git-next.sanity.build
test-studio.sanity.build

@vercel
Copy link

@vercel vercel bot commented on b318626 Mar 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

perf-studio – ./

perf-studio.sanity.build
perf-studio-git-next.sanity.build

Please sign in to comment.