Skip to content

Commit

Permalink
feat(slate): improve speed of slate/craft integration (#241)
Browse files Browse the repository at this point in the history
* avoid applying the same actions twice

* fix bug in acitons
  • Loading branch information
mresposito authored May 14, 2021
1 parent 6c12e69 commit 763274c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
20 changes: 8 additions & 12 deletions packages/core/src/editor/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,11 @@ const Methods = (
eventType: NodeEventTypes,
nodeIdSelector: NodeSelector<NodeSelectorType.Id>
) {
const current = state.events[eventType];
if (current.size > 0) {
current.forEach((id) => {
state.events[eventType].forEach((id) => {
if (state.nodes[id]) {
state.nodes[id].events[eventType] = false;
});
}
}
});

state.events[eventType] = new Set();

Expand All @@ -329,13 +328,10 @@ const Methods = (
});

const nodeIds: Set<NodeId> = new Set(targets.map(({ node }) => node.id));

if (nodeIds) {
nodeIds.forEach((id) => {
state.nodes[id].events[eventType] = true;
});
state.events[eventType] = nodeIds;
}
nodeIds.forEach((id) => {
state.nodes[id].events[eventType] = true;
});
state.events[eventType] = nodeIds;
},

/**
Expand Down
9 changes: 1 addition & 8 deletions packages/slate/src/extend/editable/useCraftStateSync.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,9 @@ export const useCraftStateSync = () => {
}, [slateSelection]);

useEffect(() => {
const { apply, onChange } = slateEditor;
const { apply } = slateEditor;

slateEditor.onChange = () => {
onChange();

// Don't update Craft state if only selection operations had been performed
if (slateEditor.operations.every((op) => op.type === 'set_selection')) {
return;
}

let actionCreator = actions.history.throttle(500);

if (isCraftOverriding.current) {
Expand Down

0 comments on commit 763274c

Please sign in to comment.