Skip to content

Commit

Permalink
apply new destructor to ObiEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
yeze322 committed Nov 14, 2019
1 parent 4558897 commit cf43cb3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,37 +68,7 @@ export const ObiEditor: FC<ObiEditorProps> = ({
break;
case NodeEventTypes.Delete:
handler = e => {
// TODO: move the shared logic into shared lib as a generic destruction process
const findLgTemplates = (value: any): string[] => {
const targetNames = ['prompt', 'unrecognizedPrompt', 'defaultValueResponse', 'invalidPrompt', 'activity'];
const targets: string[] = [];

targetNames.forEach(name => {
if (has(value, name)) {
targets.push(get(value, name));
}
});

const templates: string[] = [];
targets.forEach(target => {
// only match auto generated lg temapte name
const reg = /\[(bfd((?:activity)|(?:prompt)|(?:unrecognizedPrompt)|(?:defaultValueResponse)|(?:invalidPrompt))-\d{6})\]/g;
let matchResult;
while ((matchResult = reg.exec(target)) !== null) {
const templateName = matchResult[1];
templates.push(templateName);
}
});

return templates;
};

const cleanLgTemplate = async (removedData: any): Promise<void> => {
const templateNames: string[] = findLgTemplates(removedData);
const lgFileId = 'common';
await removeLgTemplates(lgFileId, templateNames);
};
onChange(deleteNode(data, e.id, cleanLgTemplate));
onChange(deleteNode(data, e.id, (lgTemplates: string[]) => removeLgTemplates('common', lgTemplates)));
onFocusSteps([]);
};
break;
Expand Down Expand Up @@ -140,7 +110,9 @@ export const ObiEditor: FC<ObiEditorProps> = ({
break;
case NodeEventTypes.DeleteSelection:
handler = e => {
const dialog = deleteNodes(data, e.actionIds);
const dialog = deleteNodes(data, e.actionIds, (lgTemplates: string[]) =>
removeLgTemplates('common', lgTemplates)
);
onChange(dialog);
onFocusSteps([]);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

import { cloneDeep, get, set } from 'lodash';
import { seedNewDialog, deepCopyAction } from '@bfc/shared';
import { seedNewDialog, deepCopyAction, deleteAction, deleteActions } from '@bfc/shared';

import { getFriendlyName } from '../components/nodes/utils';

Expand Down Expand Up @@ -96,7 +96,7 @@ export function queryNode(inputDialog, path) {
return target.currentData;
}

export function deleteNode(inputDialog, path, callbackOnRemovedData?: (removedData: any) => any) {
export function deleteNode(inputDialog, path, deleteLgTemplates: (lgTempaltes: string[]) => any) {
const dialog = cloneDeep(inputDialog);
const target = locateNode(dialog, path);
if (!target) return dialog;
Expand All @@ -112,15 +112,12 @@ export function deleteNode(inputDialog, path, callbackOnRemovedData?: (removedDa
delete parentData[currentKey];
}

// invoke callback handler
if (callbackOnRemovedData && typeof callbackOnRemovedData === 'function') {
callbackOnRemovedData(deletedData);
}
deleteAction(deletedData, deleteLgTemplates);

return dialog;
}

export function deleteNodes(inputDialog, nodeIds: string[], callbackOnRemovedData?: (removedData: any) => any) {
export function deleteNodes(inputDialog, nodeIds: string[], deleteLgTemplates: (lgTempaltes: string[]) => any) {
const dialog = cloneDeep(inputDialog);

const nodeLocations = nodeIds.map(id => locateNode(dialog, id));
Expand All @@ -146,10 +143,7 @@ export function deleteNodes(inputDialog, nodeIds: string[], callbackOnRemovedDat
}
});

// invoke callback handler
if (callbackOnRemovedData && typeof callbackOnRemovedData === 'function') {
deletedNodes.forEach(x => callbackOnRemovedData(x));
}
deleteActions(deletedNodes, deleteLgTemplates);

return dialog;
}
Expand Down Expand Up @@ -178,7 +172,8 @@ export function copyNodes(inputDialog, nodeIds: string[]): any[] {

export function cutNodes(inputDialog, nodeIds: string[]) {
const nodesData = copyNodes(inputDialog, nodeIds);
const newDialog = deleteNodes(inputDialog, nodeIds);
// TODO: revisit how does cut/paste work with undo/redo #1212
const newDialog = deleteNodes(inputDialog, nodeIds, () => {});

return { dialog: newDialog, cutData: nodesData };
}
Expand Down

0 comments on commit cf43cb3

Please sign in to comment.