create-fixed-drag-plugin.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import { FlowDragLayer } from '@flowgram.ai/renderer';
  6. import { FlowNodeEntity } from '@flowgram.ai/document';
  7. import { definePluginCreator, PluginContext } from '@flowgram.ai/core';
  8. // import { SelectorBounds } from './selector-bounds';
  9. export interface FixDragPluginOptions<CTX extends PluginContext = PluginContext> {
  10. enable?: boolean;
  11. /**
  12. * Callback when drag drop
  13. */
  14. onDrop?: (ctx: CTX, dropData: { dragNodes: FlowNodeEntity[]; dropNode: FlowNodeEntity }) => void;
  15. /**
  16. * Check can drop
  17. * @param ctx
  18. * @param dropData
  19. */
  20. canDrop?: (
  21. ctx: CTX,
  22. dropData: { dragNodes: FlowNodeEntity[]; dropNode: FlowNodeEntity; isBranch?: boolean }
  23. ) => boolean;
  24. }
  25. export const createFixedDragPlugin = definePluginCreator<FixDragPluginOptions<any>>({
  26. onInit(ctx, opts): void {
  27. // 默认可用,所以强制判断 false
  28. if (opts.enable !== false) {
  29. ctx.playground.registerLayer(FlowDragLayer, {
  30. onDrop: opts.onDrop ? opts.onDrop.bind(null, ctx) : undefined,
  31. canDrop: opts.canDrop ? opts.canDrop.bind(null, ctx) : undefined,
  32. });
  33. }
  34. },
  35. });