create-fixed-drag-plugin.ts 1.3 KB

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