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

feat: callback function add this point to graph #5926

Merged
merged 5 commits into from
Jun 25, 2024
Merged

Conversation

Aarebecca
Copy link
Contributor

  • Options 回调函数添加 this 指向为 Graph

目前添加 this 指向的函数:元素样式映射、交互、插件、数据转换

const graph = new Graph({
  // ...
  node: {
    style: {
      fill: function () {
        // this => graph
        return 'blue';
      },
    },
    state: {
      selected: function () {
        // this => graph
        return {
          fill: 'red',
        };
      },
      activated: {
        fill: function () {
          // this => graph
          return 'green';
        },
      },
    },
  },
  behaviors: [
    'click-select',
    function () {
      // this => graph
      return {
        type: 'drag-element',
        enable: () => {
          // this.xxx
        },
      };
    },
  ],
});
  • Options callback function adds this pointer to Graph

Functions that currently add this pointer: element style mapping, interaction, plugins, data transformation

@@ -64,7 +63,7 @@ export const behaviorExpandCollapseCombo: TestCase = async (context) => {
panel.add(config, 'dropEffect', ['link', 'move', 'none']).onChange((value: string) => {
graph.setBehaviors((behaviors) => {
return behaviors.map((behavior) => {
if (isObject(behavior) && behavior.type === 'drag-element') {
if (typeof behavior === 'object' && behavior.type === 'drag-element') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isObject 无法区分 function 和 object,所以使用 typeof


export type BehaviorOptions = ExtensionOptions<BuiltInBehaviorOptions>;
export type BehaviorOptions = (string | CustomBehaviorOption | ((this: Graph) => CustomBehaviorOption))[];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

复杂的泛型传递不利于文档生成和用户查看, 所以改成简单类型

@Aarebecca Aarebecca merged commit 338707e into v5 Jun 25, 2024
3 of 5 checks passed
@Aarebecca Aarebecca deleted the feat/function-this branch June 25, 2024 04:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants