Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return (state: S | undefined, action: ReturnType) => {
if (action.type === ac.type && state) {
if (isDraftable(state)) {
const draft = createDraft(state);
const reResult = re(draft, action);
const finishedDraft = finishDraft(draft);
if (finishedDraft === state && reResult !== undefined) {
return reResult;
}
return finishedDraft;
}
// Support primitive-returning reducers
return re(state as Draft<s>, action);
}
return (state || s) as any;
};
}</s>
transform(editor: Editor, op: Operation) {
editor.children = createDraft(editor.children)
let selection = editor.selection && createDraft(editor.selection)
switch (op.type) {
case 'insert_node': {
const { path, node } = op
const parent = Node.parent(editor, path)
const index = path[path.length - 1]
parent.children.splice(index, 0, node)
if (selection) {
for (const [point, key] of Range.points(selection)) {
selection[key] = Point.transform(point, op)!
}
}
break
transform(editor: Editor, op: Operation) {
editor.children = createDraft(editor.children)
let selection = editor.selection && createDraft(editor.selection)
switch (op.type) {
case 'insert_node': {
const { path, node } = op
const parent = Node.parent(editor, path)
const index = path[path.length - 1]
parent.children.splice(index, 0, node)
if (selection) {
for (const [point, key] of Range.points(selection)) {
selection[key] = Point.transform(point, op)!
}
}
break
}
transform(editor: Editor, op: Operation) {
editor.children = createDraft(editor.children)
let selection = editor.selection && createDraft(editor.selection)
switch (op.type) {
case 'insert_node': {
const { path, node } = op
const parent = Node.parent(editor, path)
const index = path[path.length - 1]
parent.children.splice(index, 0, node)
if (selection) {
for (const [point, key] of Range.points(selection)) {
selection[key] = Point.transform(point, op)!
}
}
break
protected changeState(effect: Function, state: any, action: ActionResponseHandle | ActionNormalHandle): any {
if (isDraftable(state)) {
const draft = createDraft(state);
const responseDraft = effect(draft, action);
if (responseDraft === undefined) {
state = finishDraft(draft);
} else if (isDraft(responseDraft)) {
state = finishDraft(responseDraft);
} else {
state = responseDraft;
}
} else {
state = effect(state, action);
if (state === undefined) {
throw new StateReturnRequiredError(action.type);
}
}
function safeCreateDraft(state) {
let draft
try {
draft = createDraft(state)
}
catch (error) {
if (error.message !== 'First argument to `createDraft` must be a plain object, an array, or an immerable object') {
throw error
}
draft = state
}
return draft
}
const reducer = produce((state: State, action: Actions) => {
if (action.type === OPEN_TUTORIAL_DRAWER) {
state.drawerOpen = true;
return;
}
if (action.type === CLOSE_TUTORIAL_DRAWER) {
state.drawerOpen = false;
return;
}
if (action.type === RESET_TUTORIAL_ACTION) {
const drawerOpen = state.drawerOpen;
state = createDraft(initialState);
state.drawerOpen = drawerOpen;
return state;
}
if (action.type === SET_TUTORIAL_ACTION) {
const drawerOpen = state.drawerOpen;
state = createDraft(initialState);
state.drawerOpen = drawerOpen;
state.tutorial = action.payload.tutorial;
state.currentStepIndex = 0;
return state;
}
if (action.type === SET_TUTORIAL_FORM_VALUES) {
if (state.formValues) {
state.formValues[action.payload.form] = action.payload.values;
if (action.type === CLOSE_TUTORIAL_DRAWER) {
state.drawerOpen = false;
return;
}
if (action.type === RESET_TUTORIAL_ACTION) {
const drawerOpen = state.drawerOpen;
state = createDraft(initialState);
state.drawerOpen = drawerOpen;
return state;
}
if (action.type === SET_TUTORIAL_ACTION) {
const drawerOpen = state.drawerOpen;
state = createDraft(initialState);
state.drawerOpen = drawerOpen;
state.tutorial = action.payload.tutorial;
state.currentStepIndex = 0;
return state;
}
if (action.type === SET_TUTORIAL_FORM_VALUES) {
if (state.formValues) {
state.formValues[action.payload.form] = action.payload.values;
} else {
state.formValues = {
[action.payload.form]: action.payload.values,
};
}
return;
}