Skip to content

Commit

Permalink
feat: canvas支持挂载s2实例
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhaiyang committed Apr 9, 2024
1 parent d70a224 commit 7d5a38b
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 4 deletions.
6 changes: 6 additions & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare global {
interface HTMLCanvasElement {
$sheetInstance?: any;
}
}
export {}
Original file line number Diff line number Diff line change
Expand Up @@ -1218,4 +1218,11 @@ describe('PivotSheet Tests', () => {

expect(onDestroy).toHaveBeenCalledTimes(1);
});

test('get sheetInstance from canvas', () => {
const canvas = s2.getCanvasElement();
expect(canvas.$sheetInstance).toEqual(s2);
s2.destroy();
expect(canvas.$sheetInstance).toBe(undefined);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,11 @@ describe('TableSheet Tests', () => {
test('should get content height', () => {
expect(s2.getContentHeight()).toEqual(120);
});

test('get sheetInstance from canvas', () => {
const canvas = s2.getCanvasElement();
expect((canvas as any).$sheetInstance).toEqual(s2);
s2.destroy();
expect((canvas as any).$sheetInstance).toBe(undefined);
});
});
14 changes: 13 additions & 1 deletion packages/s2-core/src/sheet-type/spread-sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,18 @@ export abstract class SpreadSheet extends EE {
this.initHdAdapter();
this.registerIcons();
this.setOverscrollBehavior();
this.mountSheetInstance();
}

private mountSheetInstance(destroy?: boolean) {
const canvas = this.getCanvasElement();
if (canvas) {
if (!destroy) {
canvas.$sheetInstance = this;
} else {
delete canvas.$sheetInstance;
}
}
}

private setOverscrollBehavior() {
Expand Down Expand Up @@ -440,8 +452,8 @@ export abstract class SpreadSheet extends EE {
this.store?.clear();
this.destroyTooltip();
this.clearCanvasEvent();
this.mountSheetInstance(true);
this.container?.destroy();

removeOffscreenCanvas();
}

Expand Down
3 changes: 2 additions & 1 deletion packages/s2-core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"tests/*":["s2-core/__tests__/*"]
},
},
"exclude": ["node_modules", "coverage", "esm", "lib", "dist", "temp"]
"exclude": ["node_modules", "coverage", "esm", "lib", "dist", "temp"],
"include": ["src", "../../global.d.ts"]
}
2 changes: 1 addition & 1 deletion packages/s2-react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
}
},
"exclude": ["node_modules", "coverage", "esm", "lib", "dist", "temp"],
"include": ["src", "./typings.d.ts", "playground"]
"include": ["src", "./typings.d.ts", "playground", "../../global.d.ts"]
}
2 changes: 1 addition & 1 deletion packages/s2-vue/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"jsx": "preserve"
},
"exclude": ["node_modules", "coverage", "esm", "lib", "dist", "temp"],
"include": ["src", "./typings.d.ts"]
"include": ["src", "./typings.d.ts", "../../global.d.ts"]
}

0 comments on commit 7d5a38b

Please sign in to comment.