Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛重写表格事件遇到扩展问题 #2791

Open
1 task
linwaiwai opened this issue Jun 25, 2024 · 3 comments
Open
1 task

🐛重写表格事件遇到扩展问题 #2791

linwaiwai opened this issue Jun 25, 2024 · 3 comments
Assignees
Labels
💤 inactive 不活跃的 Issue 或 PR, 30天没有回复

Comments

@linwaiwai
Copy link

linwaiwai commented Jun 25, 2024

🏷 Version

Package Version
@antv/s2
@antv/s2-react
@antv/s2-vue

Sheet Type

  • PivotSheet

🖋 Description

重写表格事件遇到扩展问题。

const s2Options = {
  interaction: {
    customInteractions: [
      {
        // 重写多选
        key: InteractionName.DATA_CELL_MULTI_SELECTION,
        interaction: CustomDataCellMulitSelection,
      },
      {
        key: InteractionName.DATA_CELL_BRUSH_SELECTION,
        interaction: CustomDataCellBrushSelection,
      },
    ],
 }
}

重写的CustomDataCellMulitSelection需要禁用系统内置的DataCellClick,如果不改写S2代码,则需要禁用代码:

interaction.addIntercepts([InterceptType.CLICK]);

但是S2在点击画布会调用resetState将intercepts重置,禁用的位置仅有bindEvents位置可以调用。系统DataCellClick在初始化的时候即会调用bindEvents,使用on函数进行监听的。所以在S2在root.ts中即使将其replace了但是改函数依然是生效的。

 const defaultInteractions = this.getDefaultInteractions();

    defaultInteractions.forEach(({ key, interaction: Interaction, enable }) => {
      if (enable !== false) {
        this.interactions.set(key, new Interaction(this.spreadsheet)); // 该Interaction实际已经生效了。并没有被后续CustomInteractionClass替换。
      }
    });

    if (!isEmpty(customInteractions)) {
      forEach(customInteractions, (customInteraction: CustomInteraction) => {
        const CustomInteractionClass = customInteraction.interaction;

        this.interactions.set(
          customInteraction.key,
          new CustomInteractionClass(this.spreadsheet),
        );
      });
    }

⌨️ Code Snapshots

🔗 Reproduce Link

🤔 Steps to Reproduce

😊 Expected Behavior

需要将调用bindEvents的时机调后,仅仅调用已经生效被复写的interactions即可。

// root.ts

  const defaultInteractions = this.getDefaultInteractions();

    defaultInteractions.forEach(({ key, interaction: Interaction, enable }) => {
      if (enable !== false) {
        this.interactions.set(key, new Interaction(this.spreadsheet));
      }
    });

    if (!isEmpty(customInteractions)) {
      forEach(customInteractions, (customInteraction: CustomInteraction) => {
        const CustomInteractionClass = customInteraction.interaction;

        this.interactions.set(
          customInteraction.key,
          new CustomInteractionClass(this.spreadsheet),
        );
      });
    }
    this.interactions.forEach((interaction:BaseEvent)=>{
      interaction.bindEvents();
    })
   ... 
// base-event.ts
export abstract class BaseEvent {
  public spreadsheet: SpreadSheet;

  constructor(spreadsheet: SpreadSheet) {
    this.spreadsheet = spreadsheet;
    \\ this.bindEvents();
  }
   ...
}

😅 Current Behavior

所以该代码时间无法进行内置的Click的重写。

💻 System information

Copy link
Contributor

你好 @linwaiwai,请编辑你的 issue 标题, 一个言简意赅的 issue 标题可以节省大家的时间, 请不要将标题当做正文, 或者为空。

Hello, @linwaiwai, please edit your issue title. a concise issue title will save everyone time. please do not leave the title as the body or empty.

@linwaiwai linwaiwai changed the title 🐛 🐛重写表格事件遇到扩展问题 Jun 25, 2024
@linwaiwai
Copy link
Author

@lijinke666
Copy link
Member

linwaiwai/S2@feature/event-extension

看起来你已经修复了, 就是把 bindEvents 延后? 直接提交 PR 吧, 加一个单测覆盖, 参考

test('should register custom interaction', () => {
class MyInteraction extends BaseEvent {
bindEvents() {}
}
const customInteraction = {
key: 'customInteraction',
interaction: MyInteraction,
};
mockSpreadSheetInstance.options.interaction.customInteractions = [
customInteraction,
];
rootInteraction = new RootInteraction(mockSpreadSheetInstance);
expect(rootInteraction.interactions.size).toEqual(
defaultInteractionSize + 1,
);
expect(
rootInteraction.interactions.has(customInteraction.key),
).toBeTruthy();
expect(
rootInteraction.interactions.get(customInteraction.key),
).toBeInstanceOf(MyInteraction);
});

@lijinke666 lijinke666 self-assigned this Jun 25, 2024
@github-actions github-actions bot added the 💤 inactive 不活跃的 Issue 或 PR, 30天没有回复 label Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💤 inactive 不活跃的 Issue 或 PR, 30天没有回复
Projects
None yet
Development

No branches or pull requests

2 participants