js-draw 0.18.2 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (269) hide show
  1. package/.eslintrc.js +1 -0
  2. package/CHANGELOG.md +10 -0
  3. package/dist/bundle.js +2 -2
  4. package/dist/bundledStyles.js +1 -0
  5. package/dist/cjs/src/Color4.d.ts +8 -0
  6. package/dist/cjs/src/Color4.js +67 -0
  7. package/dist/cjs/src/Editor.d.ts +2 -2
  8. package/dist/cjs/src/Editor.js +7 -7
  9. package/dist/cjs/src/SVGLoader.js +77 -12
  10. package/dist/cjs/src/Viewport.d.ts +2 -0
  11. package/dist/cjs/src/Viewport.js +6 -2
  12. package/dist/cjs/src/components/AbstractComponent.d.ts +2 -2
  13. package/dist/cjs/src/components/AbstractComponent.js +3 -3
  14. package/dist/cjs/src/components/{ImageBackground.d.ts → BackgroundComponent.d.ts} +23 -3
  15. package/dist/cjs/src/components/BackgroundComponent.js +309 -0
  16. package/dist/cjs/src/components/RestylableComponent.d.ts +21 -2
  17. package/dist/cjs/src/components/Stroke.d.ts +35 -0
  18. package/dist/cjs/src/components/Stroke.js +37 -3
  19. package/dist/cjs/src/components/TextComponent.d.ts +27 -17
  20. package/dist/cjs/src/components/TextComponent.js +23 -1
  21. package/dist/cjs/src/components/lib.d.ts +4 -3
  22. package/dist/cjs/src/components/lib.js +2 -2
  23. package/dist/cjs/src/components/util/StrokeSmoother.js +25 -15
  24. package/dist/cjs/src/lib.d.ts +30 -0
  25. package/dist/cjs/src/lib.js +30 -0
  26. package/dist/cjs/src/localizations/de.js +1 -1
  27. package/dist/cjs/src/localizations/es.js +1 -1
  28. package/dist/cjs/src/math/Path.js +1 -1
  29. package/dist/cjs/src/math/polynomial/QuadraticBezier.d.ts +28 -0
  30. package/dist/cjs/src/math/polynomial/QuadraticBezier.js +115 -0
  31. package/dist/cjs/src/math/polynomial/solveQuadratic.d.ts +6 -0
  32. package/dist/cjs/src/math/polynomial/solveQuadratic.js +36 -0
  33. package/dist/cjs/src/rendering/RenderingStyle.d.ts +4 -4
  34. package/dist/cjs/src/rendering/TextRenderingStyle.d.ts +10 -10
  35. package/dist/cjs/src/rendering/lib.d.ts +2 -0
  36. package/dist/cjs/src/rendering/renderers/AbstractRenderer.d.ts +2 -2
  37. package/dist/cjs/src/rendering/renderers/CanvasRenderer.d.ts +2 -2
  38. package/dist/cjs/src/rendering/renderers/CanvasRenderer.js +5 -3
  39. package/dist/cjs/src/rendering/renderers/DummyRenderer.d.ts +2 -2
  40. package/dist/cjs/src/rendering/renderers/SVGRenderer.d.ts +2 -2
  41. package/dist/cjs/src/rendering/renderers/SVGRenderer.js +15 -6
  42. package/dist/cjs/src/rendering/renderers/TextOnlyRenderer.d.ts +2 -2
  43. package/dist/cjs/src/toolbar/IconProvider.d.ts +2 -2
  44. package/dist/cjs/src/toolbar/localization.d.ts +2 -1
  45. package/dist/cjs/src/toolbar/localization.js +3 -2
  46. package/dist/cjs/src/toolbar/widgets/BaseWidget.js +1 -1
  47. package/dist/cjs/src/toolbar/widgets/DocumentPropertiesWidget.d.ts +5 -0
  48. package/dist/cjs/src/toolbar/widgets/DocumentPropertiesWidget.js +77 -2
  49. package/dist/cjs/src/toolbar/widgets/PenToolWidget.js +1 -1
  50. package/dist/cjs/src/tools/FindTool.js +1 -1
  51. package/dist/cjs/src/tools/SoundUITool.d.ts +24 -0
  52. package/dist/cjs/src/tools/SoundUITool.js +164 -0
  53. package/dist/cjs/src/tools/TextTool.d.ts +2 -2
  54. package/dist/cjs/src/tools/ToolController.js +6 -1
  55. package/dist/cjs/src/tools/lib.d.ts +1 -0
  56. package/dist/cjs/src/tools/lib.js +3 -1
  57. package/dist/cjs/src/tools/localization.d.ts +3 -0
  58. package/dist/cjs/src/tools/localization.js +3 -0
  59. package/dist/mjs/src/Color4.d.ts +8 -0
  60. package/dist/mjs/src/Color4.mjs +64 -0
  61. package/dist/mjs/src/Editor.d.ts +2 -2
  62. package/dist/mjs/src/Editor.mjs +6 -6
  63. package/dist/mjs/src/SVGLoader.mjs +76 -11
  64. package/dist/mjs/src/Viewport.d.ts +2 -0
  65. package/dist/mjs/src/Viewport.mjs +6 -2
  66. package/dist/mjs/src/components/AbstractComponent.d.ts +2 -2
  67. package/dist/mjs/src/components/AbstractComponent.mjs +3 -3
  68. package/dist/mjs/src/components/{ImageBackground.d.ts → BackgroundComponent.d.ts} +23 -3
  69. package/dist/mjs/src/components/BackgroundComponent.mjs +279 -0
  70. package/dist/mjs/src/components/RestylableComponent.d.ts +21 -2
  71. package/dist/mjs/src/components/Stroke.d.ts +35 -0
  72. package/dist/mjs/src/components/Stroke.mjs +37 -3
  73. package/dist/mjs/src/components/TextComponent.d.ts +27 -17
  74. package/dist/mjs/src/components/TextComponent.mjs +23 -1
  75. package/dist/mjs/src/components/lib.d.ts +4 -3
  76. package/dist/mjs/src/components/lib.mjs +2 -2
  77. package/dist/mjs/src/components/util/StrokeSmoother.mjs +25 -15
  78. package/dist/mjs/src/lib.d.ts +30 -0
  79. package/dist/mjs/src/lib.mjs +30 -0
  80. package/dist/mjs/src/localizations/de.mjs +1 -1
  81. package/dist/mjs/src/localizations/es.mjs +1 -1
  82. package/dist/mjs/src/math/Path.mjs +1 -1
  83. package/dist/mjs/src/math/polynomial/QuadraticBezier.d.ts +28 -0
  84. package/dist/mjs/src/math/polynomial/QuadraticBezier.mjs +109 -0
  85. package/dist/mjs/src/math/polynomial/solveQuadratic.d.ts +6 -0
  86. package/dist/mjs/src/math/polynomial/solveQuadratic.mjs +34 -0
  87. package/dist/mjs/src/rendering/RenderingStyle.d.ts +4 -4
  88. package/dist/mjs/src/rendering/TextRenderingStyle.d.ts +10 -10
  89. package/dist/mjs/src/rendering/lib.d.ts +2 -0
  90. package/dist/mjs/src/rendering/renderers/AbstractRenderer.d.ts +2 -2
  91. package/dist/mjs/src/rendering/renderers/CanvasRenderer.d.ts +2 -2
  92. package/dist/mjs/src/rendering/renderers/CanvasRenderer.mjs +5 -3
  93. package/dist/mjs/src/rendering/renderers/DummyRenderer.d.ts +2 -2
  94. package/dist/mjs/src/rendering/renderers/SVGRenderer.d.ts +2 -2
  95. package/dist/mjs/src/rendering/renderers/SVGRenderer.mjs +15 -6
  96. package/dist/mjs/src/rendering/renderers/TextOnlyRenderer.d.ts +2 -2
  97. package/dist/mjs/src/toolbar/IconProvider.d.ts +2 -2
  98. package/dist/mjs/src/toolbar/localization.d.ts +2 -1
  99. package/dist/mjs/src/toolbar/localization.mjs +3 -2
  100. package/dist/mjs/src/toolbar/widgets/BaseWidget.mjs +1 -1
  101. package/dist/mjs/src/toolbar/widgets/DocumentPropertiesWidget.d.ts +5 -0
  102. package/dist/mjs/src/toolbar/widgets/DocumentPropertiesWidget.mjs +54 -2
  103. package/dist/mjs/src/toolbar/widgets/PenToolWidget.mjs +1 -1
  104. package/dist/mjs/src/tools/FindTool.mjs +1 -1
  105. package/dist/mjs/src/tools/SoundUITool.d.ts +24 -0
  106. package/dist/mjs/src/tools/SoundUITool.mjs +158 -0
  107. package/dist/mjs/src/tools/TextTool.d.ts +2 -2
  108. package/dist/mjs/src/tools/ToolController.mjs +6 -1
  109. package/dist/mjs/src/tools/lib.d.ts +1 -0
  110. package/dist/mjs/src/tools/lib.mjs +1 -0
  111. package/dist/mjs/src/tools/localization.d.ts +3 -0
  112. package/dist/mjs/src/tools/localization.mjs +3 -0
  113. package/jest.config.js +1 -1
  114. package/package.json +19 -17
  115. package/src/Editor.css +2 -2
  116. package/src/tools/SoundUITool.css +15 -0
  117. package/src/tools/tools.css +4 -0
  118. package/dist/cjs/src/components/ImageBackground.js +0 -146
  119. package/dist/mjs/src/components/ImageBackground.mjs +0 -139
  120. package/src/Color4.test.ts +0 -40
  121. package/src/Color4.ts +0 -236
  122. package/src/Editor.loadFrom.test.ts +0 -24
  123. package/src/Editor.toSVG.test.ts +0 -111
  124. package/src/Editor.ts +0 -1122
  125. package/src/EditorImage.test.ts +0 -120
  126. package/src/EditorImage.ts +0 -603
  127. package/src/EventDispatcher.test.ts +0 -123
  128. package/src/EventDispatcher.ts +0 -71
  129. package/src/Pointer.ts +0 -127
  130. package/src/SVGLoader.test.ts +0 -114
  131. package/src/SVGLoader.ts +0 -511
  132. package/src/UndoRedoHistory.test.ts +0 -33
  133. package/src/UndoRedoHistory.ts +0 -102
  134. package/src/Viewport.ts +0 -319
  135. package/src/bundle/bundled.ts +0 -7
  136. package/src/commands/Command.ts +0 -45
  137. package/src/commands/Duplicate.ts +0 -48
  138. package/src/commands/Erase.ts +0 -74
  139. package/src/commands/SerializableCommand.ts +0 -49
  140. package/src/commands/UnresolvedCommand.ts +0 -37
  141. package/src/commands/invertCommand.ts +0 -51
  142. package/src/commands/lib.ts +0 -16
  143. package/src/commands/localization.ts +0 -47
  144. package/src/commands/uniteCommands.test.ts +0 -23
  145. package/src/commands/uniteCommands.ts +0 -135
  146. package/src/components/AbstractComponent.transformBy.test.ts +0 -22
  147. package/src/components/AbstractComponent.ts +0 -364
  148. package/src/components/ImageBackground.test.ts +0 -35
  149. package/src/components/ImageBackground.ts +0 -176
  150. package/src/components/ImageComponent.ts +0 -171
  151. package/src/components/RestylableComponent.ts +0 -142
  152. package/src/components/SVGGlobalAttributesObject.ts +0 -81
  153. package/src/components/Stroke.test.ts +0 -139
  154. package/src/components/Stroke.ts +0 -245
  155. package/src/components/TextComponent.test.ts +0 -99
  156. package/src/components/TextComponent.ts +0 -315
  157. package/src/components/UnknownSVGObject.test.ts +0 -10
  158. package/src/components/UnknownSVGObject.ts +0 -60
  159. package/src/components/builders/ArrowBuilder.ts +0 -107
  160. package/src/components/builders/FreehandLineBuilder.ts +0 -212
  161. package/src/components/builders/LineBuilder.ts +0 -77
  162. package/src/components/builders/PressureSensitiveFreehandLineBuilder.ts +0 -454
  163. package/src/components/builders/RectangleBuilder.ts +0 -74
  164. package/src/components/builders/types.ts +0 -15
  165. package/src/components/lib.ts +0 -25
  166. package/src/components/localization.ts +0 -22
  167. package/src/components/util/StrokeSmoother.ts +0 -293
  168. package/src/components/util/describeComponentList.ts +0 -18
  169. package/src/lib.ts +0 -37
  170. package/src/localization.ts +0 -34
  171. package/src/localizations/de.ts +0 -98
  172. package/src/localizations/en.ts +0 -8
  173. package/src/localizations/es.ts +0 -74
  174. package/src/localizations/getLocalizationTable.test.ts +0 -27
  175. package/src/localizations/getLocalizationTable.ts +0 -55
  176. package/src/math/LineSegment2.test.ts +0 -99
  177. package/src/math/LineSegment2.ts +0 -160
  178. package/src/math/Mat33.test.ts +0 -244
  179. package/src/math/Mat33.ts +0 -437
  180. package/src/math/Path.fromString.test.ts +0 -223
  181. package/src/math/Path.test.ts +0 -198
  182. package/src/math/Path.toString.test.ts +0 -77
  183. package/src/math/Path.ts +0 -790
  184. package/src/math/Rect2.test.ts +0 -204
  185. package/src/math/Rect2.ts +0 -315
  186. package/src/math/Triangle.ts +0 -29
  187. package/src/math/Vec2.test.ts +0 -30
  188. package/src/math/Vec2.ts +0 -18
  189. package/src/math/Vec3.test.ts +0 -44
  190. package/src/math/Vec3.ts +0 -218
  191. package/src/math/lib.ts +0 -15
  192. package/src/math/rounding.test.ts +0 -65
  193. package/src/math/rounding.ts +0 -156
  194. package/src/rendering/Display.ts +0 -249
  195. package/src/rendering/RenderingStyle.test.ts +0 -68
  196. package/src/rendering/RenderingStyle.ts +0 -55
  197. package/src/rendering/TextRenderingStyle.ts +0 -45
  198. package/src/rendering/caching/CacheRecord.test.ts +0 -49
  199. package/src/rendering/caching/CacheRecord.ts +0 -77
  200. package/src/rendering/caching/CacheRecordManager.ts +0 -71
  201. package/src/rendering/caching/RenderingCache.test.ts +0 -44
  202. package/src/rendering/caching/RenderingCache.ts +0 -66
  203. package/src/rendering/caching/RenderingCacheNode.ts +0 -405
  204. package/src/rendering/caching/testUtils.ts +0 -35
  205. package/src/rendering/caching/types.ts +0 -34
  206. package/src/rendering/lib.ts +0 -6
  207. package/src/rendering/localization.ts +0 -20
  208. package/src/rendering/renderers/AbstractRenderer.ts +0 -222
  209. package/src/rendering/renderers/CanvasRenderer.ts +0 -296
  210. package/src/rendering/renderers/DummyRenderer.test.ts +0 -42
  211. package/src/rendering/renderers/DummyRenderer.ts +0 -136
  212. package/src/rendering/renderers/SVGRenderer.ts +0 -354
  213. package/src/rendering/renderers/TextOnlyRenderer.ts +0 -70
  214. package/src/testing/beforeEachFile.ts +0 -8
  215. package/src/testing/createEditor.ts +0 -11
  216. package/src/testing/global.d.ts +0 -17
  217. package/src/testing/lib.ts +0 -3
  218. package/src/testing/loadExpectExtensions.ts +0 -25
  219. package/src/testing/sendPenEvent.ts +0 -31
  220. package/src/testing/sendTouchEvent.ts +0 -78
  221. package/src/toolbar/HTMLToolbar.ts +0 -492
  222. package/src/toolbar/IconProvider.ts +0 -736
  223. package/src/toolbar/lib.ts +0 -4
  224. package/src/toolbar/localization.ts +0 -106
  225. package/src/toolbar/makeColorInput.ts +0 -145
  226. package/src/toolbar/types.ts +0 -5
  227. package/src/toolbar/widgets/ActionButtonWidget.ts +0 -39
  228. package/src/toolbar/widgets/BaseToolWidget.ts +0 -56
  229. package/src/toolbar/widgets/BaseWidget.ts +0 -377
  230. package/src/toolbar/widgets/DocumentPropertiesWidget.ts +0 -167
  231. package/src/toolbar/widgets/EraserToolWidget.ts +0 -85
  232. package/src/toolbar/widgets/HandToolWidget.ts +0 -250
  233. package/src/toolbar/widgets/InsertImageWidget.ts +0 -223
  234. package/src/toolbar/widgets/OverflowWidget.ts +0 -92
  235. package/src/toolbar/widgets/PenToolWidget.ts +0 -288
  236. package/src/toolbar/widgets/SelectionToolWidget.ts +0 -190
  237. package/src/toolbar/widgets/TextToolWidget.ts +0 -145
  238. package/src/toolbar/widgets/lib.ts +0 -13
  239. package/src/tools/BaseTool.ts +0 -76
  240. package/src/tools/Eraser.test.ts +0 -103
  241. package/src/tools/Eraser.ts +0 -139
  242. package/src/tools/FindTool.ts +0 -152
  243. package/src/tools/PanZoom.test.ts +0 -310
  244. package/src/tools/PanZoom.ts +0 -520
  245. package/src/tools/PasteHandler.ts +0 -95
  246. package/src/tools/Pen.test.ts +0 -194
  247. package/src/tools/Pen.ts +0 -226
  248. package/src/tools/PipetteTool.ts +0 -55
  249. package/src/tools/SelectionTool/SelectAllShortcutHandler.ts +0 -28
  250. package/src/tools/SelectionTool/Selection.ts +0 -607
  251. package/src/tools/SelectionTool/SelectionHandle.ts +0 -108
  252. package/src/tools/SelectionTool/SelectionTool.test.ts +0 -261
  253. package/src/tools/SelectionTool/SelectionTool.ts +0 -480
  254. package/src/tools/SelectionTool/TransformMode.ts +0 -114
  255. package/src/tools/SelectionTool/types.ts +0 -11
  256. package/src/tools/TextTool.ts +0 -326
  257. package/src/tools/ToolController.ts +0 -178
  258. package/src/tools/ToolEnabledGroup.ts +0 -14
  259. package/src/tools/ToolSwitcherShortcut.ts +0 -39
  260. package/src/tools/ToolbarShortcutHandler.ts +0 -34
  261. package/src/tools/UndoRedoShortcut.test.ts +0 -56
  262. package/src/tools/UndoRedoShortcut.ts +0 -25
  263. package/src/tools/lib.ts +0 -21
  264. package/src/tools/localization.ts +0 -66
  265. package/src/types.ts +0 -234
  266. package/src/util/assertions.ts +0 -55
  267. package/src/util/fileToBase64.ts +0 -18
  268. package/src/util/untilNextAnimationFrame.ts +0 -9
  269. package/src/util/waitForTimeout.ts +0 -9
@@ -0,0 +1,309 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.backgroundTypeToClassNameMap = exports.imageBackgroundNonAutomaticSecondaryColorCSSClassName = exports.imageBackgroundGridSizeCSSPrefix = exports.imageBackgroundCSSClassName = exports.BackgroundType = void 0;
30
+ const Color4_1 = __importDefault(require("../Color4"));
31
+ const EditorImage_1 = require("../EditorImage");
32
+ const Rect2_1 = __importDefault(require("../math/Rect2"));
33
+ const AbstractComponent_1 = __importDefault(require("./AbstractComponent"));
34
+ const RestylableComponent_1 = require("./RestylableComponent");
35
+ const Path_1 = __importStar(require("../math/Path"));
36
+ const Vec2_1 = require("../math/Vec2");
37
+ const Viewport_1 = __importDefault(require("../Viewport"));
38
+ const rounding_1 = require("../math/rounding");
39
+ var BackgroundType;
40
+ (function (BackgroundType) {
41
+ BackgroundType[BackgroundType["SolidColor"] = 0] = "SolidColor";
42
+ BackgroundType[BackgroundType["Grid"] = 1] = "Grid";
43
+ BackgroundType[BackgroundType["None"] = 2] = "None";
44
+ })(BackgroundType = exports.BackgroundType || (exports.BackgroundType = {}));
45
+ exports.imageBackgroundCSSClassName = 'js-draw-image-background';
46
+ // Class name prefix indicating the size of the background's grid cells (if present).
47
+ exports.imageBackgroundGridSizeCSSPrefix = 'js-draw-image-background-grid-';
48
+ // Flag included in rendered SVGs (etc) that indicates that the secondary color of the
49
+ // background has been manually set.
50
+ exports.imageBackgroundNonAutomaticSecondaryColorCSSClassName = 'js-draw-image-background-non-automatic-secondary-color';
51
+ exports.backgroundTypeToClassNameMap = {
52
+ [BackgroundType.Grid]: 'js-draw-image-background-grid',
53
+ [BackgroundType.SolidColor]: exports.imageBackgroundCSSClassName,
54
+ [BackgroundType.None]: '',
55
+ };
56
+ // Represents the background of the editor's canvas.
57
+ class BackgroundComponent extends AbstractComponent_1.default {
58
+ constructor(backgroundType, mainColor) {
59
+ super('image-background', 0);
60
+ this.backgroundType = backgroundType;
61
+ this.mainColor = mainColor;
62
+ this.viewportSizeChangeListener = null;
63
+ this.gridSize = Viewport_1.default.getGridSize(2);
64
+ this.gridStrokeWidth = 0.7;
65
+ this.secondaryColor = null;
66
+ // eslint-disable-next-line @typescript-eslint/prefer-as-const
67
+ this.isRestylableComponent = true;
68
+ this.contentBBox = Rect2_1.default.empty;
69
+ }
70
+ static ofGrid(backgroundColor, gridSize, gridColor, gridStrokeWidth) {
71
+ const background = new BackgroundComponent(BackgroundType.Grid, backgroundColor);
72
+ if (gridSize !== undefined) {
73
+ background.gridSize = gridSize;
74
+ }
75
+ if (gridColor !== undefined) {
76
+ background.secondaryColor = gridColor;
77
+ }
78
+ if (gridStrokeWidth !== undefined) {
79
+ background.gridStrokeWidth = gridStrokeWidth;
80
+ }
81
+ return background;
82
+ }
83
+ getBackgroundType() {
84
+ return this.backgroundType;
85
+ }
86
+ // @internal
87
+ getMainColor() {
88
+ return this.mainColor;
89
+ }
90
+ // @internal
91
+ getSecondaryColor() {
92
+ return this.secondaryColor;
93
+ }
94
+ // @internal
95
+ getGridSize() {
96
+ return this.gridSize;
97
+ }
98
+ getStyle() {
99
+ let color = this.mainColor;
100
+ if (this.backgroundType === BackgroundType.None) {
101
+ color = undefined;
102
+ }
103
+ return {
104
+ color,
105
+ };
106
+ }
107
+ updateStyle(style) {
108
+ return (0, RestylableComponent_1.createRestyleComponentCommand)(this.getStyle(), style, this);
109
+ }
110
+ // @internal
111
+ forceStyle(style, editor) {
112
+ const fill = style.color;
113
+ if (!fill) {
114
+ return;
115
+ }
116
+ this.mainColor = fill;
117
+ // A solid background and transparent fill is equivalent to no background.
118
+ if (fill.eq(Color4_1.default.transparent) && this.backgroundType === BackgroundType.SolidColor) {
119
+ this.backgroundType = BackgroundType.None;
120
+ }
121
+ else if (this.backgroundType === BackgroundType.None) {
122
+ this.backgroundType = BackgroundType.SolidColor;
123
+ }
124
+ if (editor) {
125
+ editor.image.queueRerenderOf(this);
126
+ editor.queueRerender();
127
+ }
128
+ }
129
+ onAddToImage(image) {
130
+ if (this.viewportSizeChangeListener) {
131
+ console.warn('onAddToImage called when background is already in an image');
132
+ this.onRemoveFromImage();
133
+ }
134
+ this.viewportSizeChangeListener = image.notifier.on(EditorImage_1.EditorImageEventType.ExportViewportChanged, () => {
135
+ this.recomputeBBox(image);
136
+ });
137
+ this.recomputeBBox(image);
138
+ }
139
+ onRemoveFromImage() {
140
+ var _a;
141
+ (_a = this.viewportSizeChangeListener) === null || _a === void 0 ? void 0 : _a.remove();
142
+ this.viewportSizeChangeListener = null;
143
+ }
144
+ recomputeBBox(image) {
145
+ const importExportRect = image.getImportExportViewport().visibleRect;
146
+ if (!this.contentBBox.eq(importExportRect)) {
147
+ this.contentBBox = importExportRect;
148
+ // Re-render this if already added to the EditorImage.
149
+ image.queueRerenderOf(this);
150
+ }
151
+ }
152
+ generateGridPath(visibleRect) {
153
+ var _a, _b;
154
+ const targetRect = (_b = (_a = visibleRect === null || visibleRect === void 0 ? void 0 : visibleRect.grownBy(this.gridStrokeWidth)) === null || _a === void 0 ? void 0 : _a.intersection(this.contentBBox)) !== null && _b !== void 0 ? _b : this.contentBBox;
155
+ const roundDownToGrid = (coord) => Math.floor(coord / this.gridSize) * this.gridSize;
156
+ const roundUpToGrid = (coord) => Math.ceil(coord / this.gridSize) * this.gridSize;
157
+ const startY = roundUpToGrid(targetRect.y);
158
+ const endY = roundDownToGrid(targetRect.y + targetRect.h);
159
+ const startX = roundUpToGrid(targetRect.x);
160
+ const endX = roundDownToGrid(targetRect.x + targetRect.w);
161
+ const result = [];
162
+ // Don't generate grids with a huge number of rows/columns -- such grids
163
+ // take a long time to render and are likely invisible due to the number of
164
+ // cells.
165
+ const rowCount = (endY - startY) / this.gridSize;
166
+ const colCount = (endX - startX) / this.gridSize;
167
+ const maxGridCols = 1000;
168
+ const maxGridRows = 1000;
169
+ if (rowCount > maxGridRows || colCount > maxGridCols) {
170
+ return Path_1.default.empty;
171
+ }
172
+ const startPoint = Vec2_1.Vec2.of(targetRect.x, startY);
173
+ for (let y = startY; y <= endY; y += this.gridSize) {
174
+ result.push({
175
+ kind: Path_1.PathCommandType.MoveTo,
176
+ point: Vec2_1.Vec2.of(targetRect.x, y),
177
+ });
178
+ result.push({
179
+ kind: Path_1.PathCommandType.LineTo,
180
+ point: Vec2_1.Vec2.of(targetRect.x + targetRect.w, y),
181
+ });
182
+ }
183
+ for (let x = startX; x <= endX; x += this.gridSize) {
184
+ result.push({
185
+ kind: Path_1.PathCommandType.MoveTo,
186
+ point: Vec2_1.Vec2.of(x, targetRect.y),
187
+ });
188
+ result.push({
189
+ kind: Path_1.PathCommandType.LineTo,
190
+ point: Vec2_1.Vec2.of(x, targetRect.y + targetRect.h)
191
+ });
192
+ }
193
+ return new Path_1.default(startPoint, result);
194
+ }
195
+ render(canvas, visibleRect) {
196
+ if (this.backgroundType === BackgroundType.None) {
197
+ return;
198
+ }
199
+ const clip = true;
200
+ canvas.startObject(this.contentBBox, clip);
201
+ if (this.backgroundType === BackgroundType.SolidColor || this.backgroundType === BackgroundType.Grid) {
202
+ // If the rectangle for this region contains the visible rect,
203
+ // we can fill the entire visible rectangle (which may be more efficient than
204
+ // filling the entire region for this.)
205
+ if (visibleRect) {
206
+ const intersection = visibleRect.intersection(this.contentBBox);
207
+ if (intersection) {
208
+ canvas.fillRect(intersection, this.mainColor);
209
+ }
210
+ }
211
+ else {
212
+ canvas.fillRect(this.contentBBox, this.mainColor);
213
+ }
214
+ }
215
+ if (this.backgroundType === BackgroundType.Grid) {
216
+ let gridColor = this.secondaryColor;
217
+ gridColor !== null && gridColor !== void 0 ? gridColor : (gridColor = Color4_1.default.ofRGBA(1 - this.mainColor.r, 1 - this.mainColor.g, 1 - this.mainColor.b, 0.2));
218
+ // If the background fill is completely transparent, ensure visibility on otherwise light
219
+ // and dark backgrounds.
220
+ if (this.mainColor.a === 0) {
221
+ gridColor = Color4_1.default.ofRGBA(0.5, 0.5, 0.5, 0.2);
222
+ }
223
+ const style = {
224
+ fill: Color4_1.default.transparent,
225
+ stroke: { width: this.gridStrokeWidth, color: gridColor }
226
+ };
227
+ canvas.drawPath(this.generateGridPath(visibleRect).toRenderable(style));
228
+ }
229
+ const backgroundTypeCSSClass = exports.backgroundTypeToClassNameMap[this.backgroundType];
230
+ const classNames = [exports.imageBackgroundCSSClassName];
231
+ if (backgroundTypeCSSClass !== exports.imageBackgroundCSSClassName) {
232
+ classNames.push(backgroundTypeCSSClass);
233
+ const gridSizeStr = (0, rounding_1.toRoundedString)(this.gridSize).replace(/[.]/g, 'p');
234
+ classNames.push(exports.imageBackgroundGridSizeCSSPrefix + gridSizeStr);
235
+ }
236
+ if (this.secondaryColor !== null) {
237
+ classNames.push(exports.imageBackgroundNonAutomaticSecondaryColorCSSClassName);
238
+ }
239
+ canvas.endObject(this.getLoadSaveData(), classNames);
240
+ }
241
+ intersects(lineSegment) {
242
+ return this.contentBBox.getEdges().some(edge => edge.intersects(lineSegment));
243
+ }
244
+ isSelectable() {
245
+ return false;
246
+ }
247
+ isBackground() {
248
+ return true;
249
+ }
250
+ serializeToJSON() {
251
+ var _a;
252
+ return {
253
+ mainColor: this.mainColor.toHexString(),
254
+ secondaryColor: (_a = this.secondaryColor) === null || _a === void 0 ? void 0 : _a.toHexString(),
255
+ backgroundType: this.backgroundType,
256
+ gridSize: this.gridSize,
257
+ gridStrokeWidth: this.gridStrokeWidth,
258
+ };
259
+ }
260
+ applyTransformation(_affineTransfm) {
261
+ // Do nothing — it doesn't make sense to transform the background.
262
+ }
263
+ description(localizationTable) {
264
+ if (this.backgroundType === BackgroundType.SolidColor) {
265
+ return localizationTable.filledBackgroundWithColor(this.mainColor.toString());
266
+ }
267
+ else {
268
+ return localizationTable.emptyBackground;
269
+ }
270
+ }
271
+ createClone() {
272
+ return new BackgroundComponent(this.backgroundType, this.mainColor);
273
+ }
274
+ // @internal
275
+ static deserializeFromJSON(json) {
276
+ var _a, _b;
277
+ if (typeof json === 'string') {
278
+ json = JSON.parse(json);
279
+ }
280
+ if (typeof json.mainColor !== 'string') {
281
+ throw new Error('Error deserializing — mainColor must be of type string.');
282
+ }
283
+ let backgroundType;
284
+ const jsonBackgroundType = json.backgroundType;
285
+ if (jsonBackgroundType === BackgroundType.None || jsonBackgroundType === BackgroundType.Grid
286
+ || jsonBackgroundType === BackgroundType.SolidColor) {
287
+ backgroundType = jsonBackgroundType;
288
+ }
289
+ else {
290
+ const exhaustivenessCheck = jsonBackgroundType;
291
+ return exhaustivenessCheck;
292
+ }
293
+ const mainColor = Color4_1.default.fromHex(json.mainColor);
294
+ const secondaryColor = json.secondaryColor ? Color4_1.default.fromHex(json.secondaryColor) : null;
295
+ const gridSize = (_a = json.gridSize) !== null && _a !== void 0 ? _a : undefined;
296
+ const gridStrokeWidth = (_b = json.gridStrokeWidth) !== null && _b !== void 0 ? _b : undefined;
297
+ const result = new BackgroundComponent(backgroundType, mainColor);
298
+ result.secondaryColor = secondaryColor;
299
+ if (gridSize) {
300
+ result.gridSize = gridSize;
301
+ }
302
+ if (gridStrokeWidth) {
303
+ result.gridStrokeWidth = gridStrokeWidth;
304
+ }
305
+ return result;
306
+ }
307
+ }
308
+ exports.default = BackgroundComponent;
309
+ AbstractComponent_1.default.registerComponent('image-background', BackgroundComponent.deserializeFromJSON);
@@ -1,16 +1,35 @@
1
1
  import Color4 from '../Color4';
2
2
  import SerializableCommand from '../commands/SerializableCommand';
3
3
  import Editor from '../Editor';
4
- import TextStyle from '../rendering/TextRenderingStyle';
4
+ import TextRenderingStyle from '../rendering/TextRenderingStyle';
5
5
  import AbstractComponent from './AbstractComponent';
6
6
  export interface ComponentStyle {
7
7
  color?: Color4;
8
- textStyle?: TextStyle;
8
+ textStyle?: TextRenderingStyle;
9
9
  }
10
10
  export declare const createRestyleComponentCommand: (initialStyle: ComponentStyle, newStyle: ComponentStyle, component: RestyleableComponent) => SerializableCommand;
11
11
  export declare const isRestylableComponent: (component: AbstractComponent) => component is RestyleableComponent;
12
+ /**
13
+ * An interface to be implemented by components with a changable color or {@link TextRenderingStyle}.
14
+ *
15
+ * All such classes must have a member variable, `isRestylableComponent` that is set to `true`
16
+ * to allow testing whether the class is a `RestylableComponent` (see {@link isRestylableComponent}).
17
+ */
12
18
  export interface RestyleableComponent extends AbstractComponent {
19
+ /**
20
+ * @returns a partial representation of this component's style.
21
+ */
13
22
  getStyle(): ComponentStyle;
23
+ /**
24
+ * Returns a {@link Command} that updates portions of this component's style
25
+ * to the given `style`.
26
+ *
27
+ * @example
28
+ * For some component and editor,
29
+ * ```ts
30
+ * editor.dispatch(component.updateStyle({ color: Color4.red }));
31
+ * ```
32
+ */
14
33
  updateStyle(style: ComponentStyle): SerializableCommand;
15
34
  /**
16
35
  * Set the style of this component in a way that can't be undone/redone
@@ -8,11 +8,43 @@ import AbstractRenderer, { RenderablePathSpec } from '../rendering/renderers/Abs
8
8
  import AbstractComponent from './AbstractComponent';
9
9
  import { ImageComponentLocalization } from './localization';
10
10
  import RestyleableComponent, { ComponentStyle } from './RestylableComponent';
11
+ /**
12
+ * Represents an {@link AbstractComponent} made up of one or more {@link Path}s.
13
+ *
14
+ * @example
15
+ * For some {@link Editor} editor and `Stroke` stroke,
16
+ *
17
+ * **Restyling**:
18
+ * ```ts
19
+ * editor.dispatch(stroke.updateStyle({ color: Color4.red }));
20
+ * ```
21
+ *
22
+ * **Transforming**:
23
+ * ```ts
24
+ * editor.dispatch(stroke.transformBy(Mat33.translation(Vec2.of(10, 0))));
25
+ * ```
26
+ */
11
27
  export default class Stroke extends AbstractComponent implements RestyleableComponent {
12
28
  private parts;
13
29
  protected contentBBox: Rect2;
14
30
  readonly isRestylableComponent: true;
15
31
  private approximateRenderingTime;
32
+ /**
33
+ * Creates a `Stroke` from the given `parts`. All parts should have the
34
+ * same color.
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * // A path that starts at (1,1), moves to the right by (2, 0),
39
+ * // then moves down and right by (3, 3)
40
+ * const path = Path.fromString('m1,1 2,0 3,3');
41
+ *
42
+ * const stroke = new Stroke([
43
+ * // Fill with red
44
+ * path.toRenderable({ fill: Color4.red })
45
+ * ]);
46
+ * ```
47
+ */
16
48
  constructor(parts: RenderablePathSpec[]);
17
49
  getStyle(): ComponentStyle;
18
50
  updateStyle(style: ComponentStyle): SerializableCommand;
@@ -22,6 +54,9 @@ export default class Stroke extends AbstractComponent implements RestyleableComp
22
54
  getProportionalRenderingTime(): number;
23
55
  private bboxForPart;
24
56
  protected applyTransformation(affineTransfm: Mat33): void;
57
+ /**
58
+ * @returns the {@link Path.union} of all paths that make up this stroke.
59
+ */
25
60
  getPath(): Path;
26
61
  description(localization: ImageComponentLocalization): string;
27
62
  protected createClone(): AbstractComponent;
@@ -8,12 +8,43 @@ const Rect2_1 = __importDefault(require("../math/Rect2"));
8
8
  const RenderingStyle_1 = require("../rendering/RenderingStyle");
9
9
  const AbstractComponent_1 = __importDefault(require("./AbstractComponent"));
10
10
  const RestylableComponent_1 = require("./RestylableComponent");
11
+ /**
12
+ * Represents an {@link AbstractComponent} made up of one or more {@link Path}s.
13
+ *
14
+ * @example
15
+ * For some {@link Editor} editor and `Stroke` stroke,
16
+ *
17
+ * **Restyling**:
18
+ * ```ts
19
+ * editor.dispatch(stroke.updateStyle({ color: Color4.red }));
20
+ * ```
21
+ *
22
+ * **Transforming**:
23
+ * ```ts
24
+ * editor.dispatch(stroke.transformBy(Mat33.translation(Vec2.of(10, 0))));
25
+ * ```
26
+ */
11
27
  class Stroke extends AbstractComponent_1.default {
12
- // Creates a `Stroke` from the given `parts`. All parts should have the
13
- // same color.
28
+ /**
29
+ * Creates a `Stroke` from the given `parts`. All parts should have the
30
+ * same color.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * // A path that starts at (1,1), moves to the right by (2, 0),
35
+ * // then moves down and right by (3, 3)
36
+ * const path = Path.fromString('m1,1 2,0 3,3');
37
+ *
38
+ * const stroke = new Stroke([
39
+ * // Fill with red
40
+ * path.toRenderable({ fill: Color4.red })
41
+ * ]);
42
+ * ```
43
+ */
14
44
  constructor(parts) {
15
45
  var _a;
16
46
  super('stroke');
47
+ // @internal
17
48
  // eslint-disable-next-line @typescript-eslint/prefer-as-const
18
49
  this.isRestylableComponent = true;
19
50
  this.approximateRenderingTime = 0;
@@ -99,7 +130,7 @@ class Stroke extends AbstractComponent_1.default {
99
130
  if (!bbox.intersects(visibleRect)) {
100
131
  continue;
101
132
  }
102
- const muchBiggerThanVisible = bbox.size.x > visibleRect.size.x * 2 || bbox.size.y > visibleRect.size.y * 2;
133
+ const muchBiggerThanVisible = bbox.size.x > visibleRect.size.x * 3 || bbox.size.y > visibleRect.size.y * 3;
103
134
  if (muchBiggerThanVisible && !part.path.roughlyIntersects(visibleRect, (_b = (_a = part.style.stroke) === null || _a === void 0 ? void 0 : _a.width) !== null && _b !== void 0 ? _b : 0)) {
104
135
  continue;
105
136
  }
@@ -146,6 +177,9 @@ class Stroke extends AbstractComponent_1.default {
146
177
  };
147
178
  });
148
179
  }
180
+ /**
181
+ * @returns the {@link Path.union} of all paths that make up this stroke.
182
+ */
149
183
  getPath() {
150
184
  let result = null;
151
185
  for (const part of this.parts) {
@@ -4,18 +4,26 @@ import Mat33 from '../math/Mat33';
4
4
  import Rect2 from '../math/Rect2';
5
5
  import Editor from '../Editor';
6
6
  import AbstractRenderer from '../rendering/renderers/AbstractRenderer';
7
- import { TextStyle } from '../rendering/TextRenderingStyle';
7
+ import { TextRenderingStyle } from '../rendering/TextRenderingStyle';
8
8
  import AbstractComponent from './AbstractComponent';
9
9
  import { ImageComponentLocalization } from './localization';
10
10
  import RestyleableComponent, { ComponentStyle } from './RestylableComponent';
11
+ /**
12
+ * Displays text.
13
+ */
11
14
  export default class TextComponent extends AbstractComponent implements RestyleableComponent {
12
15
  protected readonly textObjects: Array<string | TextComponent>;
13
16
  private transform;
14
17
  private style;
15
18
  protected contentBBox: Rect2;
16
19
  readonly isRestylableComponent: true;
17
- constructor(textObjects: Array<string | TextComponent>, transform: Mat33, style: TextStyle);
18
- static applyTextStyles(ctx: CanvasRenderingContext2D, style: TextStyle): void;
20
+ /**
21
+ * Creates a new text object from a list of component text or child TextComponents.
22
+ *
23
+ * @see {@link fromLines}
24
+ */
25
+ constructor(textObjects: Array<string | TextComponent>, transform: Mat33, style: TextRenderingStyle);
26
+ static applyTextStyles(ctx: CanvasRenderingContext2D, style: TextRenderingStyle): void;
19
27
  private static textMeasuringCtx;
20
28
  private static estimateTextDimens;
21
29
  private static getTextDimens;
@@ -28,19 +36,7 @@ export default class TextComponent extends AbstractComponent implements Restylea
28
36
  getStyle(): ComponentStyle;
29
37
  updateStyle(style: ComponentStyle): SerializableCommand;
30
38
  forceStyle(style: ComponentStyle, editor: Editor | null): void;
31
- getTextStyle(): {
32
- renderingStyle: {
33
- fill: import("../Color4").default;
34
- stroke: {
35
- color: import("../Color4").default;
36
- width: number;
37
- } | undefined;
38
- };
39
- size: number;
40
- fontFamily: string;
41
- fontWeight?: string | undefined;
42
- fontVariant?: string | undefined;
43
- };
39
+ getTextStyle(): TextRenderingStyle;
44
40
  getBaselinePos(): import("../lib").Vec3;
45
41
  getTransform(): Mat33;
46
42
  protected applyTransformation(affineTransfm: Mat33): void;
@@ -49,5 +45,19 @@ export default class TextComponent extends AbstractComponent implements Restylea
49
45
  description(localizationTable: ImageComponentLocalization): string;
50
46
  protected serializeToJSON(): Record<string, any>;
51
47
  static deserializeFromString(json: any): TextComponent;
52
- static fromLines(lines: string[], transform: Mat33, style: TextStyle): AbstractComponent;
48
+ /**
49
+ * Creates a `TextComponent` from `lines`.
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * const textStyle = {
54
+ * size: 12,
55
+ * fontFamily: 'serif',
56
+ * renderingStyle: { fill: Color4.black },
57
+ * };
58
+ *
59
+ * const text = TextComponent.fromLines('foo\nbar'.split('\n'), Mat33.identity, textStyle);
60
+ * ```
61
+ */
62
+ static fromLines(lines: string[], transform: Mat33, style: TextRenderingStyle): AbstractComponent;
53
63
  }
@@ -11,7 +11,15 @@ const TextRenderingStyle_1 = require("../rendering/TextRenderingStyle");
11
11
  const AbstractComponent_1 = __importDefault(require("./AbstractComponent"));
12
12
  const RestylableComponent_1 = require("./RestylableComponent");
13
13
  const componentTypeId = 'text';
14
+ /**
15
+ * Displays text.
16
+ */
14
17
  class TextComponent extends AbstractComponent_1.default {
18
+ /**
19
+ * Creates a new text object from a list of component text or child TextComponents.
20
+ *
21
+ * @see {@link fromLines}
22
+ */
15
23
  constructor(textObjects, transform, style) {
16
24
  super(componentTypeId);
17
25
  this.textObjects = textObjects;
@@ -155,7 +163,7 @@ class TextComponent extends AbstractComponent_1.default {
155
163
  editor.queueRerender();
156
164
  }
157
165
  }
158
- // See this.getStyle
166
+ // See {@link getStyle}
159
167
  getTextStyle() {
160
168
  return (0, TextRenderingStyle_1.cloneTextStyle)(this.style);
161
169
  }
@@ -237,6 +245,20 @@ class TextComponent extends AbstractComponent_1.default {
237
245
  const transform = new Mat33_1.default(...transformData);
238
246
  return new TextComponent(textObjects, transform, style);
239
247
  }
248
+ /**
249
+ * Creates a `TextComponent` from `lines`.
250
+ *
251
+ * @example
252
+ * ```ts
253
+ * const textStyle = {
254
+ * size: 12,
255
+ * fontFamily: 'serif',
256
+ * renderingStyle: { fill: Color4.black },
257
+ * };
258
+ *
259
+ * const text = TextComponent.fromLines('foo\nbar'.split('\n'), Mat33.identity, textStyle);
260
+ * ```
261
+ */
240
262
  static fromLines(lines, transform, style) {
241
263
  let lastComponent = null;
242
264
  const components = [];
@@ -7,6 +7,7 @@ export { default as AbstractComponent } from './AbstractComponent';
7
7
  import Stroke from './Stroke';
8
8
  import TextComponent from './TextComponent';
9
9
  import ImageComponent from './ImageComponent';
10
- import RestyleableComponent, { createRestyleComponentCommand, isRestylableComponent } from './RestylableComponent';
11
- import ImageBackground from './ImageBackground';
12
- export { Stroke, TextComponent as Text, RestyleableComponent, createRestyleComponentCommand, isRestylableComponent, TextComponent, Stroke as StrokeComponent, ImageBackground as BackgroundComponent, ImageComponent, };
10
+ import RestyleableComponent from './RestylableComponent';
11
+ import { createRestyleComponentCommand, isRestylableComponent, ComponentStyle as RestyleableComponentStyle } from './RestylableComponent';
12
+ import BackgroundComponent from './BackgroundComponent';
13
+ export { Stroke, TextComponent as Text, RestyleableComponent, createRestyleComponentCommand, isRestylableComponent, RestyleableComponentStyle, TextComponent, Stroke as StrokeComponent, BackgroundComponent, ImageComponent, };
@@ -39,5 +39,5 @@ exports.ImageComponent = ImageComponent_1.default;
39
39
  const RestylableComponent_1 = require("./RestylableComponent");
40
40
  Object.defineProperty(exports, "createRestyleComponentCommand", { enumerable: true, get: function () { return RestylableComponent_1.createRestyleComponentCommand; } });
41
41
  Object.defineProperty(exports, "isRestylableComponent", { enumerable: true, get: function () { return RestylableComponent_1.isRestylableComponent; } });
42
- const ImageBackground_1 = __importDefault(require("./ImageBackground"));
43
- exports.BackgroundComponent = ImageBackground_1.default;
42
+ const BackgroundComponent_1 = __importDefault(require("./BackgroundComponent"));
43
+ exports.BackgroundComponent = BackgroundComponent_1.default;