js-draw 1.0.1 → 1.1.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 (197) hide show
  1. package/LICENSE +21 -0
  2. package/dist/Editor.css +1 -0
  3. package/dist/bundle.js +1 -1
  4. package/dist/bundledStyles.js +1 -1
  5. package/dist/cjs/toolbar/AbstractToolbar.d.ts +9 -13
  6. package/dist/cjs/toolbar/AbstractToolbar.js +14 -19
  7. package/dist/cjs/toolbar/widgets/SaveActionWidget.d.ts +10 -0
  8. package/dist/cjs/toolbar/widgets/SaveActionWidget.js +26 -0
  9. package/dist/cjs/toolbar/widgets/keybindings.d.ts +1 -0
  10. package/dist/cjs/toolbar/widgets/keybindings.js +4 -1
  11. package/dist/cjs/version.js +1 -1
  12. package/dist/mjs/toolbar/AbstractToolbar.d.ts +9 -13
  13. package/dist/mjs/toolbar/AbstractToolbar.mjs +14 -19
  14. package/dist/mjs/toolbar/widgets/SaveActionWidget.d.ts +10 -0
  15. package/dist/mjs/toolbar/widgets/SaveActionWidget.mjs +21 -0
  16. package/dist/mjs/toolbar/widgets/keybindings.d.ts +1 -0
  17. package/dist/mjs/toolbar/widgets/keybindings.mjs +3 -0
  18. package/dist/mjs/version.mjs +1 -1
  19. package/docs/img/readme-images/js-draw.jpg +0 -0
  20. package/docs/img/readme-images/unsupported-elements--in-editor.png +0 -0
  21. package/package.json +5 -4
  22. package/src/toolbar/EdgeToolbar.scss +1 -0
  23. package/dist-test/test_imports/package-lock.json +0 -13
  24. package/dist-test/test_imports/package.json +0 -12
  25. package/dist-test/test_imports/test-imports.js +0 -11
  26. package/dist-test/test_imports/test-require.cjs +0 -14
  27. package/src/Editor.loadFrom.test.ts +0 -24
  28. package/src/Editor.test.ts +0 -107
  29. package/src/Editor.toSVG.test.ts +0 -294
  30. package/src/Editor.ts +0 -1443
  31. package/src/EditorImage.test.ts +0 -117
  32. package/src/EditorImage.ts +0 -609
  33. package/src/EventDispatcher.test.ts +0 -123
  34. package/src/EventDispatcher.ts +0 -72
  35. package/src/Pointer.ts +0 -183
  36. package/src/SVGLoader.test.ts +0 -114
  37. package/src/SVGLoader.ts +0 -672
  38. package/src/UndoRedoHistory.test.ts +0 -34
  39. package/src/UndoRedoHistory.ts +0 -102
  40. package/src/Viewport.ts +0 -322
  41. package/src/bundle/bundled.ts +0 -7
  42. package/src/commands/Command.ts +0 -45
  43. package/src/commands/Duplicate.ts +0 -75
  44. package/src/commands/Erase.ts +0 -95
  45. package/src/commands/SerializableCommand.ts +0 -49
  46. package/src/commands/UnresolvedCommand.ts +0 -37
  47. package/src/commands/invertCommand.ts +0 -58
  48. package/src/commands/lib.ts +0 -16
  49. package/src/commands/localization.ts +0 -47
  50. package/src/commands/uniteCommands.test.ts +0 -23
  51. package/src/commands/uniteCommands.ts +0 -140
  52. package/src/components/AbstractComponent.transformBy.test.ts +0 -23
  53. package/src/components/AbstractComponent.ts +0 -383
  54. package/src/components/BackgroundComponent.test.ts +0 -44
  55. package/src/components/BackgroundComponent.ts +0 -348
  56. package/src/components/ImageComponent.ts +0 -176
  57. package/src/components/RestylableComponent.ts +0 -161
  58. package/src/components/SVGGlobalAttributesObject.ts +0 -79
  59. package/src/components/Stroke.test.ts +0 -137
  60. package/src/components/Stroke.ts +0 -294
  61. package/src/components/TextComponent.test.ts +0 -202
  62. package/src/components/TextComponent.ts +0 -429
  63. package/src/components/UnknownSVGObject.test.ts +0 -10
  64. package/src/components/UnknownSVGObject.ts +0 -60
  65. package/src/components/builders/ArrowBuilder.ts +0 -106
  66. package/src/components/builders/CircleBuilder.ts +0 -100
  67. package/src/components/builders/FreehandLineBuilder.test.ts +0 -24
  68. package/src/components/builders/FreehandLineBuilder.ts +0 -210
  69. package/src/components/builders/LineBuilder.ts +0 -77
  70. package/src/components/builders/PressureSensitiveFreehandLineBuilder.ts +0 -453
  71. package/src/components/builders/RectangleBuilder.ts +0 -73
  72. package/src/components/builders/types.ts +0 -15
  73. package/src/components/lib.ts +0 -31
  74. package/src/components/localization.ts +0 -24
  75. package/src/components/util/StrokeSmoother.ts +0 -302
  76. package/src/components/util/describeComponentList.ts +0 -18
  77. package/src/dialogs/makeAboutDialog.ts +0 -82
  78. package/src/inputEvents.ts +0 -143
  79. package/src/lib.ts +0 -91
  80. package/src/localization.ts +0 -34
  81. package/src/localizations/de.ts +0 -146
  82. package/src/localizations/en.ts +0 -8
  83. package/src/localizations/es.ts +0 -74
  84. package/src/localizations/getLocalizationTable.test.ts +0 -27
  85. package/src/localizations/getLocalizationTable.ts +0 -74
  86. package/src/rendering/Display.ts +0 -247
  87. package/src/rendering/RenderablePathSpec.ts +0 -88
  88. package/src/rendering/RenderingStyle.test.ts +0 -68
  89. package/src/rendering/RenderingStyle.ts +0 -55
  90. package/src/rendering/TextRenderingStyle.ts +0 -55
  91. package/src/rendering/caching/CacheRecord.test.ts +0 -48
  92. package/src/rendering/caching/CacheRecord.ts +0 -76
  93. package/src/rendering/caching/CacheRecordManager.ts +0 -71
  94. package/src/rendering/caching/RenderingCache.test.ts +0 -43
  95. package/src/rendering/caching/RenderingCache.ts +0 -66
  96. package/src/rendering/caching/RenderingCacheNode.ts +0 -404
  97. package/src/rendering/caching/testUtils.ts +0 -35
  98. package/src/rendering/caching/types.ts +0 -34
  99. package/src/rendering/lib.ts +0 -8
  100. package/src/rendering/localization.ts +0 -20
  101. package/src/rendering/renderers/AbstractRenderer.ts +0 -232
  102. package/src/rendering/renderers/CanvasRenderer.ts +0 -312
  103. package/src/rendering/renderers/DummyRenderer.test.ts +0 -41
  104. package/src/rendering/renderers/DummyRenderer.ts +0 -142
  105. package/src/rendering/renderers/SVGRenderer.ts +0 -434
  106. package/src/rendering/renderers/TextOnlyRenderer.test.ts +0 -34
  107. package/src/rendering/renderers/TextOnlyRenderer.ts +0 -68
  108. package/src/shortcuts/KeyBinding.test.ts +0 -61
  109. package/src/shortcuts/KeyBinding.ts +0 -257
  110. package/src/shortcuts/KeyboardShortcutManager.test.ts +0 -95
  111. package/src/shortcuts/KeyboardShortcutManager.ts +0 -163
  112. package/src/shortcuts/lib.ts +0 -3
  113. package/src/testing/createEditor.ts +0 -11
  114. package/src/testing/getUniquePointerId.ts +0 -18
  115. package/src/testing/lib.ts +0 -3
  116. package/src/testing/sendPenEvent.ts +0 -36
  117. package/src/testing/sendTouchEvent.ts +0 -71
  118. package/src/toolbar/AbstractToolbar.ts +0 -542
  119. package/src/toolbar/DropdownToolbar.ts +0 -220
  120. package/src/toolbar/EdgeToolbar.test.ts +0 -54
  121. package/src/toolbar/EdgeToolbar.ts +0 -543
  122. package/src/toolbar/IconProvider.ts +0 -861
  123. package/src/toolbar/constants.ts +0 -1
  124. package/src/toolbar/lib.ts +0 -6
  125. package/src/toolbar/localization.ts +0 -136
  126. package/src/toolbar/types.ts +0 -13
  127. package/src/toolbar/widgets/ActionButtonWidget.ts +0 -39
  128. package/src/toolbar/widgets/BaseToolWidget.ts +0 -81
  129. package/src/toolbar/widgets/BaseWidget.ts +0 -495
  130. package/src/toolbar/widgets/DocumentPropertiesWidget.ts +0 -250
  131. package/src/toolbar/widgets/EraserToolWidget.ts +0 -84
  132. package/src/toolbar/widgets/HandToolWidget.ts +0 -239
  133. package/src/toolbar/widgets/InsertImageWidget.ts +0 -248
  134. package/src/toolbar/widgets/OverflowWidget.ts +0 -92
  135. package/src/toolbar/widgets/PenToolWidget.ts +0 -369
  136. package/src/toolbar/widgets/SelectionToolWidget.ts +0 -195
  137. package/src/toolbar/widgets/TextToolWidget.ts +0 -149
  138. package/src/toolbar/widgets/components/makeColorInput.ts +0 -184
  139. package/src/toolbar/widgets/components/makeFileInput.ts +0 -128
  140. package/src/toolbar/widgets/components/makeGridSelector.ts +0 -179
  141. package/src/toolbar/widgets/components/makeSeparator.ts +0 -17
  142. package/src/toolbar/widgets/components/makeThicknessSlider.ts +0 -62
  143. package/src/toolbar/widgets/keybindings.ts +0 -19
  144. package/src/toolbar/widgets/layout/DropdownLayoutManager.ts +0 -262
  145. package/src/toolbar/widgets/layout/EdgeToolbarLayoutManager.ts +0 -71
  146. package/src/toolbar/widgets/layout/types.ts +0 -74
  147. package/src/toolbar/widgets/lib.ts +0 -13
  148. package/src/tools/BaseTool.ts +0 -169
  149. package/src/tools/Eraser.test.ts +0 -103
  150. package/src/tools/Eraser.ts +0 -173
  151. package/src/tools/FindTool.test.ts +0 -67
  152. package/src/tools/FindTool.ts +0 -153
  153. package/src/tools/InputFilter/FunctionMapper.ts +0 -17
  154. package/src/tools/InputFilter/InputMapper.ts +0 -41
  155. package/src/tools/InputFilter/InputPipeline.test.ts +0 -41
  156. package/src/tools/InputFilter/InputPipeline.ts +0 -34
  157. package/src/tools/InputFilter/InputStabilizer.ts +0 -254
  158. package/src/tools/InputFilter/StrokeKeyboardControl.ts +0 -104
  159. package/src/tools/PanZoom.test.ts +0 -339
  160. package/src/tools/PanZoom.ts +0 -525
  161. package/src/tools/PasteHandler.ts +0 -94
  162. package/src/tools/Pen.test.ts +0 -260
  163. package/src/tools/Pen.ts +0 -284
  164. package/src/tools/PipetteTool.ts +0 -84
  165. package/src/tools/SelectionTool/SelectAllShortcutHandler.ts +0 -29
  166. package/src/tools/SelectionTool/Selection.ts +0 -647
  167. package/src/tools/SelectionTool/SelectionHandle.ts +0 -142
  168. package/src/tools/SelectionTool/SelectionTool.test.ts +0 -370
  169. package/src/tools/SelectionTool/SelectionTool.ts +0 -510
  170. package/src/tools/SelectionTool/TransformMode.ts +0 -112
  171. package/src/tools/SelectionTool/types.ts +0 -11
  172. package/src/tools/SoundUITool.ts +0 -221
  173. package/src/tools/TextTool.ts +0 -339
  174. package/src/tools/ToolController.ts +0 -224
  175. package/src/tools/ToolEnabledGroup.ts +0 -14
  176. package/src/tools/ToolSwitcherShortcut.ts +0 -39
  177. package/src/tools/ToolbarShortcutHandler.ts +0 -39
  178. package/src/tools/UndoRedoShortcut.test.ts +0 -62
  179. package/src/tools/UndoRedoShortcut.ts +0 -24
  180. package/src/tools/keybindings.ts +0 -85
  181. package/src/tools/lib.ts +0 -22
  182. package/src/tools/localization.ts +0 -76
  183. package/src/types.ts +0 -151
  184. package/src/util/ReactiveValue.test.ts +0 -168
  185. package/src/util/ReactiveValue.ts +0 -241
  186. package/src/util/assertions.ts +0 -55
  187. package/src/util/fileToBase64.ts +0 -18
  188. package/src/util/guessKeyCodeFromKey.ts +0 -36
  189. package/src/util/listPrefixMatch.ts +0 -19
  190. package/src/util/stopPropagationOfScrollingWheelEvents.ts +0 -20
  191. package/src/util/untilNextAnimationFrame.ts +0 -9
  192. package/src/util/waitForAll.ts +0 -18
  193. package/src/util/waitForTimeout.ts +0 -9
  194. package/src/version.test.ts +0 -12
  195. package/src/version.ts +0 -3
  196. package/tools/allLocales.js +0 -4
  197. package/tools/copyREADME.ts +0 -62
@@ -1,24 +0,0 @@
1
- import Viewport from '../../Viewport';
2
- import { Vec2, Color4 } from '@js-draw/math';
3
- import { StrokeDataPoint } from '../../types';
4
- import { makeFreehandLineBuilder } from './FreehandLineBuilder';
5
-
6
- describe('FreehandLineBuilder', () => {
7
- it('should create a dot on click', () => {
8
- const viewport = new Viewport(() => {});
9
- const startPoint: StrokeDataPoint = {
10
- pos: Vec2.zero,
11
- width: 1,
12
- time: performance.now(),
13
- color: Color4.red,
14
- };
15
- const lineBuilder = makeFreehandLineBuilder(startPoint, viewport);
16
-
17
- const component = lineBuilder.build();
18
- const bbox = component.getBBox();
19
-
20
- expect(bbox.area).toBeGreaterThan(0);
21
- expect(bbox.width).toBeGreaterThan(0.5);
22
- expect(bbox.height).toBeGreaterThan(0.5);
23
- });
24
- });
@@ -1,210 +0,0 @@
1
- import AbstractRenderer from '../../rendering/renderers/AbstractRenderer';
2
- import RenderablePathSpec from '../../rendering/RenderablePathSpec';
3
- import { Point2, Vec2, Rect2, Color4, PathCommand, PathCommandType } from '@js-draw/math';
4
- import Stroke from '../Stroke';
5
- import Viewport from '../../Viewport';
6
- import { StrokeDataPoint } from '../../types';
7
- import { ComponentBuilder, ComponentBuilderFactory } from './types';
8
- import RenderingStyle from '../../rendering/RenderingStyle';
9
- import { StrokeSmoother, Curve } from '../util/StrokeSmoother';
10
-
11
- export const makeFreehandLineBuilder: ComponentBuilderFactory = (initialPoint: StrokeDataPoint, viewport: Viewport) => {
12
- // Don't smooth if input is more than ± 3 pixels from the true curve, do smooth if
13
- // less than ±1 px from the curve.
14
- const maxSmoothingDist = viewport.getSizeOfPixelOnCanvas() * 3;
15
- const minSmoothingDist = viewport.getSizeOfPixelOnCanvas();
16
-
17
- return new FreehandLineBuilder(
18
- initialPoint, minSmoothingDist, maxSmoothingDist, viewport
19
- );
20
- };
21
-
22
- // Handles stroke smoothing and creates Strokes from user/stylus input.
23
- export default class FreehandLineBuilder implements ComponentBuilder {
24
- private isFirstSegment: boolean = true;
25
- private parts: PathCommand[] = [];
26
-
27
- private curveFitter: StrokeSmoother;
28
-
29
- private bbox: Rect2;
30
- private averageWidth: number;
31
- private widthAverageNumSamples: number = 1;
32
-
33
- public constructor(
34
- private startPoint: StrokeDataPoint,
35
-
36
- private minFitAllowed: number,
37
- maxFitAllowed: number,
38
-
39
- private viewport: Viewport,
40
- ) {
41
- this.curveFitter = new StrokeSmoother(startPoint, minFitAllowed, maxFitAllowed, (curve: Curve|null) => this.addCurve(curve));
42
-
43
- this.averageWidth = startPoint.width;
44
- this.bbox = new Rect2(this.startPoint.pos.x, this.startPoint.pos.y, 0, 0);
45
- }
46
-
47
- public getBBox(): Rect2 {
48
- return this.bbox;
49
- }
50
-
51
- protected getRenderingStyle(): RenderingStyle {
52
- return {
53
- fill: Color4.transparent,
54
- stroke: {
55
- color: this.startPoint.color,
56
- width: this.roundDistance(this.averageWidth),
57
- }
58
- };
59
- }
60
-
61
- protected previewCurrentPath(): RenderablePathSpec|null {
62
- const path = this.parts.slice();
63
- const commands = [...path, ...this.curveToPathCommands(this.curveFitter.preview())];
64
- const startPoint = this.startPoint.pos;
65
-
66
- return {
67
- startPoint,
68
-
69
- commands,
70
- style: this.getRenderingStyle(),
71
- };
72
- }
73
-
74
- protected previewFullPath(): RenderablePathSpec[]|null {
75
- const preview = this.previewCurrentPath();
76
- if (preview) {
77
- return [ preview ];
78
- }
79
- return null;
80
- }
81
-
82
- private previewStroke(): Stroke|null {
83
- const pathPreview = this.previewFullPath();
84
-
85
- if (pathPreview) {
86
- return new Stroke(pathPreview);
87
- }
88
- return null;
89
- }
90
-
91
- public preview(renderer: AbstractRenderer) {
92
- const paths = this.previewFullPath();
93
- if (paths) {
94
- const approxBBox = this.viewport.visibleRect;
95
- renderer.startObject(approxBBox);
96
- for (const path of paths) {
97
- renderer.drawPath(path);
98
- }
99
- renderer.endObject();
100
- }
101
- }
102
-
103
- public build(): Stroke {
104
- this.curveFitter.finalizeCurrentCurve();
105
- return this.previewStroke()!;
106
- }
107
-
108
- private getMinFit(): number {
109
- let minFit = Math.min(this.minFitAllowed, this.averageWidth / 3);
110
-
111
- if (minFit < 1e-10) {
112
- minFit = this.minFitAllowed;
113
- }
114
-
115
- return minFit;
116
- }
117
-
118
- private roundPoint(point: Point2): Point2 {
119
- const minFit = this.getMinFit();
120
- return Viewport.roundPoint(point, minFit);
121
- }
122
-
123
- private roundDistance(dist: number): number {
124
- const minFit = this.getMinFit();
125
- return Viewport.roundPoint(dist, minFit);
126
- }
127
-
128
- private curveToPathCommands(curve: Curve|null): PathCommand[] {
129
- // Case where no points have been added
130
- if (!curve) {
131
- // Don't create a circle around the initial point if the stroke has more than one point.
132
- if (!this.isFirstSegment) {
133
- return [];
134
- }
135
-
136
- // Make the circle small -- because of the stroke style, we'll be drawing a stroke around it.
137
- const width = Viewport.roundPoint(this.averageWidth / 10, Math.min(this.minFitAllowed, this.averageWidth / 10));
138
- const center = this.roundPoint(this.startPoint.pos);
139
-
140
- // Start on the right, cycle clockwise:
141
- // |
142
- // ----- ←
143
- // |
144
-
145
- // Draw a circle-ish shape around the start point
146
- return [
147
- {
148
- kind: PathCommandType.QuadraticBezierTo,
149
- controlPoint: center.plus(Vec2.of(width, width)),
150
-
151
- // Bottom of the circle
152
- // |
153
- // -----
154
- // |
155
- // ↑
156
- endPoint: center.plus(Vec2.of(0, width)),
157
- },
158
- {
159
- kind: PathCommandType.QuadraticBezierTo,
160
- controlPoint: center.plus(Vec2.of(-width, width)),
161
- endPoint: center.plus(Vec2.of(-width, 0)),
162
- },
163
- {
164
- kind: PathCommandType.QuadraticBezierTo,
165
- controlPoint: center.plus(Vec2.of(-width, -width)),
166
- endPoint: center.plus(Vec2.of(0, -width)),
167
- },
168
- {
169
- kind: PathCommandType.QuadraticBezierTo,
170
- controlPoint: center.plus(Vec2.of(width, -width)),
171
- endPoint: center.plus(Vec2.of(width, 0)),
172
- }
173
- ];
174
- }
175
-
176
- const result: PathCommand[] = [];
177
-
178
- if (this.isFirstSegment) {
179
- result.push({
180
- kind: PathCommandType.MoveTo,
181
- point: this.roundPoint(curve.startPoint),
182
- });
183
- }
184
-
185
- result.push({
186
- kind: PathCommandType.QuadraticBezierTo,
187
- controlPoint: this.roundPoint(curve.controlPoint),
188
- endPoint: this.roundPoint(curve.endPoint),
189
- });
190
-
191
- return result;
192
- }
193
-
194
- private addCurve(curve: Curve|null) {
195
- const parts = this.curveToPathCommands(curve);
196
- this.parts.push(...parts);
197
-
198
- if (this.isFirstSegment) {
199
- this.isFirstSegment = false;
200
- }
201
- }
202
-
203
- public addPoint(newPoint: StrokeDataPoint) {
204
- this.curveFitter.addPoint(newPoint);
205
- this.widthAverageNumSamples ++;
206
- this.averageWidth =
207
- this.averageWidth * (this.widthAverageNumSamples - 1) / this.widthAverageNumSamples
208
- + newPoint.width / this.widthAverageNumSamples;
209
- }
210
- }
@@ -1,77 +0,0 @@
1
- import { Path, PathCommandType, Rect2 } from '@js-draw/math';
2
- import AbstractRenderer from '../../rendering/renderers/AbstractRenderer';
3
- import { pathToRenderable } from '../../rendering/RenderablePathSpec';
4
- import { StrokeDataPoint } from '../../types';
5
- import Viewport from '../../Viewport';
6
- import AbstractComponent from '../AbstractComponent';
7
- import Stroke from '../Stroke';
8
- import { ComponentBuilder, ComponentBuilderFactory } from './types';
9
-
10
- export const makeLineBuilder: ComponentBuilderFactory = (initialPoint: StrokeDataPoint, viewport: Viewport) => {
11
- return new LineBuilder(initialPoint, viewport);
12
- };
13
-
14
- export default class LineBuilder implements ComponentBuilder {
15
- private endPoint: StrokeDataPoint;
16
-
17
- public constructor(private readonly startPoint: StrokeDataPoint, private readonly viewport: Viewport) {
18
- this.endPoint = startPoint;
19
- }
20
-
21
- public getBBox(): Rect2 {
22
- const preview = this.buildPreview();
23
- return preview.getBBox();
24
- }
25
-
26
- private buildPreview(): Stroke {
27
- const startPoint = this.startPoint.pos;
28
- const endPoint = this.endPoint.pos;
29
- const toEnd = endPoint.minus(startPoint).normalized();
30
-
31
- const startSize = this.startPoint.width / 2;
32
- const endSize = this.endPoint.width / 2;
33
-
34
- const lineNormal = toEnd.orthog();
35
- const scaledStartNormal = lineNormal.times(startSize);
36
- const scaledEndNormal = lineNormal.times(endSize);
37
-
38
- const strokeStartPoint = startPoint.minus(scaledStartNormal);
39
-
40
- const path = new Path(strokeStartPoint, [
41
- {
42
- kind: PathCommandType.LineTo,
43
- point: startPoint.plus(scaledStartNormal),
44
- },
45
- {
46
- kind: PathCommandType.LineTo,
47
- point: endPoint.plus(scaledEndNormal),
48
- },
49
- {
50
- kind: PathCommandType.LineTo,
51
- point: endPoint.minus(scaledEndNormal),
52
- },
53
- {
54
- kind: PathCommandType.LineTo,
55
- point: startPoint.minus(scaledStartNormal),
56
- },
57
- ]).mapPoints(point => this.viewport.roundPoint(point));
58
-
59
- const preview = new Stroke([
60
- pathToRenderable(path, { fill: this.startPoint.color })
61
- ]);
62
-
63
- return preview;
64
- }
65
-
66
- public build(): AbstractComponent {
67
- return this.buildPreview();
68
- }
69
-
70
- public preview(renderer: AbstractRenderer): void {
71
- this.buildPreview().render(renderer);
72
- }
73
-
74
- public addPoint(point: StrokeDataPoint): void {
75
- this.endPoint = point;
76
- }
77
- }