|
| 1 | +import { HookComp } from "comps/hooks/hookComp"; |
| 2 | +import { EditorState } from "comps/editorState"; |
| 3 | +import { wrapActionExtraInfo, type Comp } from "lowcoder-core"; |
| 4 | +import { messageInstance } from "lowcoder-design"; |
| 5 | +import { trans } from "i18n"; |
| 6 | + |
| 7 | +type CopyableHookType = "modal" | "drawer"; |
| 8 | + |
| 9 | +type CopyableHookComp = HookComp & { |
| 10 | + children: HookComp["children"] & { |
| 11 | + compType: { getView: () => CopyableHookType }; |
| 12 | + }; |
| 13 | +}; |
| 14 | + |
| 15 | +const copyableHookTypes = new Set<CopyableHookType>(["modal", "drawer"]); |
| 16 | + |
| 17 | +export class HookCompOperator { |
| 18 | + private static copyHooks: CopyableHookComp[] = []; |
| 19 | + |
| 20 | + /** |
| 21 | + * Copy modals/drawers by name from selectedCompNames. |
| 22 | + */ |
| 23 | + static copyComp(editorState: EditorState, compRecords: Record<string, Comp>) { |
| 24 | + const selectedNames = Array.from(editorState.selectedCompNames); |
| 25 | + if (!selectedNames.length) { |
| 26 | + return false; |
| 27 | + } |
| 28 | + |
| 29 | + const hookMap = editorState.getHooksComp().getAllCompItems(); |
| 30 | + const selectedHookComps = Object.values(hookMap) |
| 31 | + .filter((comp: any) => { |
| 32 | + const name = comp.children.name.getView(); |
| 33 | + const compType = comp.children.compType.getView(); |
| 34 | + return selectedNames.includes(name) && copyableHookTypes.has(compType); |
| 35 | + }) as CopyableHookComp[]; |
| 36 | + |
| 37 | + if (!selectedHookComps.length) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + this.copyHooks = selectedHookComps; |
| 42 | + messageInstance.success(trans("notification.copySuccess")); |
| 43 | + return true; |
| 44 | + } |
| 45 | + |
| 46 | + static clearCopy() { |
| 47 | + this.copyHooks = []; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Paste previously copied modals/drawers and re-generate nested component names. |
| 52 | + */ |
| 53 | + static pasteComp(editorState: EditorState) { |
| 54 | + if (!this.copyHooks.length) { |
| 55 | + messageInstance.info(trans("gridCompOperator.selectCompFirst")); |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + const hooksComp = editorState.getHooksComp(); |
| 60 | + const nameGenerator = editorState.getNameGenerator(); |
| 61 | + const newNames = new Set<string>(); |
| 62 | + |
| 63 | + this.copyHooks.forEach((hookComp) => { |
| 64 | + const compType = hookComp.children.compType.getView(); |
| 65 | + const newName = nameGenerator.genItemName(compType); |
| 66 | + const childComp: any = hookComp.children.comp; |
| 67 | + const baseValue = childComp?.toJsonValue ? childComp.toJsonValue() : {}; |
| 68 | + const pasteValue = |
| 69 | + childComp?.getPasteValue?.(nameGenerator) ?? {}; |
| 70 | + |
| 71 | + const payload = { |
| 72 | + ...(hookComp.toJsonValue() as any), |
| 73 | + name: newName, |
| 74 | + comp: { |
| 75 | + ...baseValue, |
| 76 | + ...pasteValue, |
| 77 | + }, |
| 78 | + }; |
| 79 | + |
| 80 | + hooksComp.dispatch( |
| 81 | + wrapActionExtraInfo( |
| 82 | + hooksComp.pushAction(payload), |
| 83 | + { |
| 84 | + compInfos: [ |
| 85 | + { |
| 86 | + type: "add", |
| 87 | + compName: newName, |
| 88 | + compType, |
| 89 | + }, |
| 90 | + ], |
| 91 | + } |
| 92 | + ) |
| 93 | + ); |
| 94 | + newNames.add(newName); |
| 95 | + }); |
| 96 | + |
| 97 | + editorState.setSelectedCompNames(newNames, "leftPanel"); |
| 98 | + messageInstance.success(trans("notification.copySuccess")); |
| 99 | + return true; |
| 100 | + } |
| 101 | +} |
| 102 | + |
0 commit comments