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

Chore(TS) Object interactivity #8401

Merged
merged 14 commits into from
Oct 30, 2022
Merged

Chore(TS) Object interactivity #8401

merged 14 commits into from
Oct 30, 2022

Conversation

asturur
Copy link
Member

@asturur asturur commented Oct 29, 2022

Motivation

Continuing conversiont to TS.

Description

Continue the TS migration, change to imports.
For now the mixin is not optional.

Ideally we can split in this way:

importing static canvas adds the stacking mixin
importing canvas adds staticCanvas and the interactivity mixin + controls.
I m not yet sure how the optional import is supposed to work

Changes

  • converted mixin to Class
  • divided setCoord in the interactive vs non interactive part
  • moved oCoords to ObjectInteractivity
  • moved calcOCoords to ObjectInteractivity
  • moved _corner to ObjectInteractivity

BREAKING:

  • removed more chainability
  • changed default value of _corner from 0 to '' ( to avoid the weird typing )
  • removed the skipCornerCoord from setCoords, that was a silly performance optimization

Gist

In Action

Comment on lines +1 to +12
export function applyMixins(derivedCtor: any, constructors: any[]) {
constructors.forEach((baseCtor) => {
Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
Object.defineProperty(
derivedCtor.prototype,
name,
Object.getOwnPropertyDescriptor(baseCtor.prototype, name) ||
Object.create(null)
);
});
});
}
Copy link
Member Author

Choose a reason for hiding this comment

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

unused. i commited because i wanted to use it, we can keep it and then trash it if is not useful

Comment on lines -2137 to -2138
(function (global) {
const fabric = global.fabric;
fabric.Object = FabricObject;
})(typeof exports !== 'undefined' ? exports : window);
Copy link
Member Author

Choose a reason for hiding this comment

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

moved this to InteractiveFabricObject

@github-actions
Copy link
Contributor

github-actions bot commented Oct 29, 2022

Build Stats

file / KB (diff) bundled reduced* minified gzipped
fabric.min.js 1116.023 (-0.527) 333.947 (+0.235) 93.366 (+0.069)
src/canvas.class.ts 49.579 (-0.450) 52.141 (-0.460)
src/controls/default_controls.ts 2.863 (+0.005) 2.805 (+0.021)
src/controls/scale.ts 9.040 (+0.006) 8.230 (0.000)
src/controls/util.ts 3.998 (+0.006) 3.076 (0.000)
src/gradient/gradient.class.ts 10.794 (+0.006) 9.396 (+0.011)
src/mixins/animation.mixin.ts 8.071 (+0.006) 8.538 (+0.011)
src/mixins/itext.svg_export.ts 10.178 (+0.006) 10.050 (+0.011)
src/mixins/object.svg_export.ts 10.486 (+0.006) 10.818 (+0.021)
src/mixins/object_ancestry.mixin.ts 4.584 (+0.006) 5.074 (+0.011)
src/mixins/object_geometry.mixin.ts 25.982 (-2.693) 23.961 (-2.548)
src/mixins/object_interactivity.mixin.ts 20.363 (+5.560) 18.733 (+3.581)
src/mixins/object_stacking.mixin.ts 3.838 (+0.006) 4.023 (+0.011)
src/mixins/object_straightening.mixin.ts 2.690 (+0.006) 2.814 (+0.011)
src/parser/parseSVGDocument.ts 3.511 (+0.006) 3.069 (+0.011)
src/parser/setStrokeFillOpacity.ts 0.962 (+0.006) 0.850 (+0.021)
src/pattern.class.ts 5.275 (+0.006) 4.471 (+0.011)
src/shadow.class.ts 6.646 (+0.006) 6.948 (+0.011)
src/shapes/circle.class.ts 5.791 (+0.006) 5.701 (+0.043)
src/shapes/ellipse.class.ts 4.786 (+0.006) 5.051 (+0.021)
src/shapes/fabricObject.class.js 0.334 (+0.334) 0.150 (+0.150)
src/shapes/group.class.ts 36.218 (+0.006) 38.121 (+0.043)
src/shapes/image.class.ts 27.703 (+0.006) 28.705 (+0.064)
src/shapes/itext.class.ts 20.231 (+0.006) 21.185 (+0.011)
src/shapes/line.class.ts 8.841 (+0.006) 9.288 (+0.021)
src/shapes/object.class.ts 61.098 (-1.845) 50.195 (-1.617)
src/static_canvas.class.ts 61.044 (-0.006) 64.157 (-0.001)
src/util/misc/matrix.ts 6.032 (+0.047) 5.026 (0.000)

*inaccurate, see link

Comment on lines +621 to +622
callSuper?: TCallSuper;

Copy link
Member Author

Choose a reason for hiding this comment

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

this is a compatibility layer for classes that now import extended classes that are not created with fabric.createClass


/**
* Color of controlling corners of an object (when it's active and transparentCorners false)
* @since 1.6.2
* @type String
* @default null
*/
cornerStrokeColor: string | null;
cornerStrokeColor: string;
Copy link
Member Author

Choose a reason for hiding this comment

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

empty string has the same effect of null, so we can reduce to string and keep it compatible with the embedded types of ctx.fillStyle and ctx.strokeStyle

Copy link
Contributor

Choose a reason for hiding this comment

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

good
I came across that in #8400 as well

Comment on lines 1 to 12
import {
InteractiveFabricObject
} from "../mixins/object_interactivity.mixin";

// TODO somehow we have to make a tree-shakeable import

export { InteractiveFabricObject as FabricObject }

(function (global) {
const fabric = global.fabric;
fabric.Object = InteractiveFabricObject;
})(typeof exports !== 'undefined' ? exports : window);
Copy link
Member Author

Choose a reason for hiding this comment

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

if we don't find a better way, this together with the dead code remover plugin for rollup can do the optional imports.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am currently for the several entry points approach

import { Canvas, Text } from 'fabric/node';
import { Canvas, Text } from 'fabric/static'; // or other name
import { Canvas, Text } from 'fabric'; // interactive by default

Copy link
Member Author

Choose a reason for hiding this comment

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

well the point is that Rect and Circle have to extend either from Object or Interactive Object, so that is why i get confused on how i change that throug imports.
Yes we can have different entry points that create 3 bundles and people that use the bundle entirely can do that, or they can just import the single TS files as they see fit. But at that point, you have to provide 2 version of Rect/Circle/Text...?

Till i m not done converting i ll ignore it

Copy link
Contributor

@ShaMan123 ShaMan123 Oct 29, 2022

Choose a reason for hiding this comment

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

A headache indeed
So turn into Mixins, all of them

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes but it stays complicated mixin or class is still a bit complicated.

When we have a full ts folder we can worry about thay

@asturur
Copy link
Member Author

asturur commented Oct 29, 2022

@ShaMan123 @melchiar this is ready to go

@github-actions
Copy link
Contributor

github-actions bot commented Oct 29, 2022

Coverage after merging object-interactivity into master will be

82.42%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
HEADER.js54.90%48.15%0%65.22%105, 105, 105, 12, 14, 14, 14, 14, 14, 16–17, 21, 21–22, 22, 22, 24, 24, 26, 28, 30–31
src
   cache.ts97.06%90%100%100%57
   canvas.class.ts93.36%90.22%94.12%95.54%1045, 1045–1046, 1049, 1069, 1069, 1104, 1137–1138, 1166–1167, 1200, 1208, 1318–1319, 1321–1322, 1342–1343, 1501, 1506, 1516, 1520, 472–473, 478, 487, 636–638, 683–684, 733–734, 737, 739, 782–784, 826, 831–832, 860–861
   config.ts77.27%66.67%66.67%84.62%130, 138–140, 151–153
   constants.ts100%100%100%100%
   intersection.class.ts100%100%100%100%
   pattern.class.ts92.19%85.71%100%96.30%110, 116, 127, 136, 88
   point.class.ts100%100%100%100%
   shadow.class.ts95.95%90%100%100%173, 240, 9
   static_canvas.class.ts90.21%83.99%96.77%92.82%1153–1154, 1154, 1154–1155, 1289, 1355–1356, 1359, 1408–1409, 1502, 1517, 1521, 1547–1548, 1577–1578, 1611–1612, 1653–1654, 1657, 1659, 1659, 1659, 1659, 1663, 1663, 1663–1665, 1687–1688, 1729–1730, 1733, 1735, 1735, 1735, 1735, 1739, 1739, 1739–1741, 1814, 1814–1815, 1888, 1890, 1890, 1890, 1890, 1890–1891, 1894–1895, 1895, 1895–1896, 1899, 1899, 1899, 1901, 1904, 1910, 1912–1913, 1913, 1913, 1916–1917, 1917, 1917, 1920, 279–280, 282–283, 285–286, 299–300, 302–303, 617, 643, 699–702, 902
src/brushes
   base_brush.class.ts100%100%100%100%
   circle_brush.class.ts2.99%0%0%3.92%100–101, 103, 105–107, 116, 116, 116, 118, 120, 122–124, 126–129, 137, 144, 146, 26, 31–32, 40–44, 48–52, 59–62, 70–74, 76, 84, 84, 84, 84, 84–85, 87, 87, 87–90, 92
   pattern_brush.class.ts5.26%0%0%8.33%16, 20–23, 25–26, 26, 26–29, 37–38, 40, 44, 55, 55, 55, 63–65, 65, 65, 72–73, 75–76, 76, 80
   pencil_brush.class.ts91.95%85.42%100%93.69%125–126, 155, 155–157, 279, 283, 288–289, 71–72, 87–88
   spray_brush.class.ts2.30%0%0%3.08%102–104, 106–107, 115, 115, 115, 115, 115–116, 118–119, 126–127, 129, 131–135, 144, 148–149, 149, 157, 157, 157–160, 162–165, 169–170, 172, 174–177, 180, 187–188, 190, 192–193, 195, 202–203, 205–206, 209, 209, 216, 216, 220, 25–26, 28–30, 30, 30–32, 36, 45, 52, 59, 66, 73, 80, 92–94
src/color
   color.class.ts91.67%84.51%100%94.44%325–326, 330–331, 334–335, 41, 45, 72–73, 73, 75, 75, 75–76, 78–79
   color_map.ts100%100%100%100%
   constants.ts100%100%100%100%
   index.ts100%100%100%100%
   util.ts100%100%100%100%
src/controls
   actions.ts100%100%100%100%
   changeWidth.ts100%100%100%100%
   control.class.ts93.98%88.89%90.91%97.78%236, 320, 320, 355
   controls.render.ts81.63%78%100%84.78%106, 111, 121, 121, 45, 50, 61, 61, 65–72, 81–82
   default_controls.ts72.73%50%100%100%109, 113, 120
   drag.ts100%100%100%100%
   rotate.ts20%12.50%50%22.22%44, 50, 50, 50–51, 54–56, 58, 58, 58, 58, 58–60, 60, 60–62, 64, 64, 64–66, 66, 66–67, 72, 72, 72–73, 75, 77, 79–80
   scale.ts94.41%94.74%100%93.59%129–130, 132–134, 181–183, 42
   scaleSkew.ts80.56%66.67%100%92.31%14, 28, 30, 30, 30, 32, 34
   skew.ts91.03%79.31%100%97.67%131–132, 163–164, 171, 177, 179
   util.ts100%100%100%100%
   wrapWithFireEvent.ts100%100%100%100%
   wrapWithFixedAnchor.ts100%100%100%100%
src/filters
   2d_backend.class.ts96.43%83.33%100%100%78
   WebGLProbe.ts40%40%57.14%34.78%38–39, 49–53, 61, 64, 66, 66, 66–67, 67, 67–70, 72, 74, 78
   base_filter.class.ts28.90%31.82%36.36%26.17%100–102, 102, 102–103, 112–116, 116, 116–117, 124–125, 125, 125–128, 143, 159, 169–174, 178, 181, 181, 181–184, 184, 184–185, 185, 188–189, 195, 204–205, 210–214, 257–260, 273, 273, 273–274, 276, 292–294, 294, 294, 294, 294–295, 297, 299–300, 306–307, 309–311, 315–316, 318, 322–324, 328, 332, 352, 352, 352–356, 393, 78, 78, 78–79, 79, 79–80, 80, 80–81, 86–89, 89, 89–90, 99
   blendcolor_filter.class.ts10%4.76%28.57%9.72%104, 126, 128, 128, 128–130, 135, 145–147, 155, 157–160, 162–165, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 169–172, 174–177, 179–182, 185–188, 190–193, 195–198, 200–203, 205–206, 206, 209–210,

Copy link
Contributor

@ShaMan123 ShaMan123 left a comment

Choose a reason for hiding this comment

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

Did a superficial review sine the diff is not readable
Looks good
A few minor comments/questions

* @param {Point} size
* @param {Object} styleOverride object to override the object style
*/
_drawBorders(ctx: CanvasRenderingContext2D, size: Point, styleOverride: Record<string, any> = {}): void {
Copy link
Contributor

Choose a reason for hiding this comment

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

private?

Copy link
Member Author

Choose a reason for hiding this comment

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

maybe? not sure, at some point we have to decide what becomes private for the sake of reducing exposure Not sure if someone would want to call _drawBorders directly

Comment on lines 1 to 12
import {
InteractiveFabricObject
} from "../mixins/object_interactivity.mixin";

// TODO somehow we have to make a tree-shakeable import

export { InteractiveFabricObject as FabricObject }

(function (global) {
const fabric = global.fabric;
fabric.Object = InteractiveFabricObject;
})(typeof exports !== 'undefined' ? exports : window);
Copy link
Contributor

Choose a reason for hiding this comment

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

I am currently for the several entry points approach

import { Canvas, Text } from 'fabric/node';
import { Canvas, Text } from 'fabric/static'; // or other name
import { Canvas, Text } from 'fabric'; // interactive by default


/**
* Color of controlling corners of an object (when it's active and transparentCorners false)
* @since 1.6.2
* @type String
* @default null
*/
cornerStrokeColor: string | null;
cornerStrokeColor: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

good
I came across that in #8400 as well

* @type number|string|any
* @default 0
*/
__corner: number | string;
Copy link
Contributor

Choose a reason for hiding this comment

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

why not make all 0 assignments null or undefined?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes i thought the same, we could move it to 0 as default rather than null. I don't think anyone is gonna cry for that.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am confused
I thought removing 0 in favor of null/undefined

Copy link
Member Author

Choose a reason for hiding this comment

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

sorry nor zero nor null. just empty string or optional with undefined.

i din't like null it often makes no sense to me.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am asking since I saw the code still has __corner = 0

Copy link
Member Author

Choose a reason for hiding this comment

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

either empty string or undefined i suppose. With optional string ( and so undefined ) looking more clean

@ShaMan123
Copy link
Contributor

I see you missed prettier and changelog, they are failing

@ShaMan123 ShaMan123 mentioned this pull request Oct 29, 2022
2 tasks
asturur and others added 2 commits October 30, 2022 01:11
@asturur asturur merged commit d19b7fd into master Oct 30, 2022
@asturur asturur deleted the object-interactivity branch October 30, 2022 22:19
frankrousseau pushed a commit to cgwire/fabric.js that referenced this pull request Jan 6, 2023
Co-authored-by: Shachar <34343793+ShaMan123@users.noreply.github.com>
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