js-draw 0.1.12 → 0.2.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 (122) hide show
  1. package/.eslintrc.js +1 -0
  2. package/.firebaserc +5 -0
  3. package/.github/workflows/firebase-hosting-merge.yml +25 -0
  4. package/.github/workflows/firebase-hosting-pull-request.yml +22 -0
  5. package/.github/workflows/github-pages.yml +52 -0
  6. package/CHANGELOG.md +6 -0
  7. package/README.md +11 -6
  8. package/dist/bundle.js +1 -1
  9. package/dist/src/Color4.d.ts +19 -0
  10. package/dist/src/Color4.js +24 -3
  11. package/dist/src/Editor.d.ts +129 -2
  12. package/dist/src/Editor.js +94 -17
  13. package/dist/src/EditorImage.d.ts +7 -2
  14. package/dist/src/EditorImage.js +41 -25
  15. package/dist/src/EventDispatcher.d.ts +18 -0
  16. package/dist/src/EventDispatcher.js +19 -4
  17. package/dist/src/Pointer.js +3 -2
  18. package/dist/src/UndoRedoHistory.js +15 -2
  19. package/dist/src/Viewport.js +4 -1
  20. package/dist/src/bundle/bundled.d.ts +1 -2
  21. package/dist/src/bundle/bundled.js +1 -2
  22. package/dist/src/commands/Duplicate.d.ts +1 -1
  23. package/dist/src/commands/Duplicate.js +3 -4
  24. package/dist/src/commands/Erase.d.ts +1 -1
  25. package/dist/src/commands/Erase.js +6 -5
  26. package/dist/src/commands/SerializableCommand.d.ts +4 -5
  27. package/dist/src/commands/SerializableCommand.js +12 -4
  28. package/dist/src/commands/invertCommand.d.ts +4 -0
  29. package/dist/src/commands/invertCommand.js +44 -0
  30. package/dist/src/commands/lib.d.ts +6 -0
  31. package/dist/src/commands/lib.js +6 -0
  32. package/dist/src/commands/localization.d.ts +1 -0
  33. package/dist/src/commands/localization.js +1 -0
  34. package/dist/src/components/AbstractComponent.d.ts +13 -8
  35. package/dist/src/components/AbstractComponent.js +26 -15
  36. package/dist/src/components/SVGGlobalAttributesObject.d.ts +1 -1
  37. package/dist/src/components/SVGGlobalAttributesObject.js +7 -1
  38. package/dist/src/components/Stroke.d.ts +12 -2
  39. package/dist/src/components/Stroke.js +10 -7
  40. package/dist/src/components/Text.d.ts +2 -2
  41. package/dist/src/components/Text.js +6 -6
  42. package/dist/src/components/UnknownSVGObject.d.ts +1 -1
  43. package/dist/src/components/UnknownSVGObject.js +6 -1
  44. package/dist/src/components/lib.d.ts +4 -0
  45. package/dist/src/components/lib.js +4 -0
  46. package/dist/src/lib.d.ts +25 -0
  47. package/dist/src/lib.js +25 -0
  48. package/dist/src/math/Mat33.d.ts +47 -1
  49. package/dist/src/math/Mat33.js +48 -20
  50. package/dist/src/math/Path.js +3 -3
  51. package/dist/src/math/Rect2.d.ts +2 -2
  52. package/dist/src/math/Vec3.d.ts +62 -0
  53. package/dist/src/math/Vec3.js +62 -14
  54. package/dist/src/math/lib.d.ts +7 -0
  55. package/dist/src/math/lib.js +7 -0
  56. package/dist/src/math/rounding.js +1 -0
  57. package/dist/src/rendering/Display.d.ts +44 -0
  58. package/dist/src/rendering/Display.js +45 -6
  59. package/dist/src/rendering/caching/CacheRecord.d.ts +1 -0
  60. package/dist/src/rendering/caching/CacheRecord.js +3 -0
  61. package/dist/src/rendering/caching/CacheRecordManager.d.ts +4 -3
  62. package/dist/src/rendering/caching/CacheRecordManager.js +16 -4
  63. package/dist/src/rendering/caching/RenderingCache.d.ts +2 -3
  64. package/dist/src/rendering/caching/RenderingCache.js +9 -10
  65. package/dist/src/rendering/caching/types.d.ts +1 -3
  66. package/dist/src/rendering/renderers/CanvasRenderer.js +1 -1
  67. package/dist/src/toolbar/HTMLToolbar.js +1 -0
  68. package/dist/src/toolbar/makeColorInput.js +1 -1
  69. package/dist/src/toolbar/widgets/PenWidget.js +1 -0
  70. package/dist/src/tools/Pen.d.ts +1 -2
  71. package/dist/src/tools/Pen.js +8 -1
  72. package/dist/src/tools/PipetteTool.js +1 -0
  73. package/dist/src/tools/SelectionTool.js +45 -22
  74. package/dist/src/types.d.ts +17 -6
  75. package/dist/src/types.js +7 -5
  76. package/firebase.json +16 -0
  77. package/package.json +118 -101
  78. package/src/Color4.ts +23 -2
  79. package/src/Editor.ts +147 -25
  80. package/src/EditorImage.ts +45 -27
  81. package/src/EventDispatcher.ts +21 -6
  82. package/src/Pointer.ts +3 -2
  83. package/src/UndoRedoHistory.ts +18 -2
  84. package/src/Viewport.ts +5 -2
  85. package/src/bundle/bundled.ts +1 -2
  86. package/src/commands/Duplicate.ts +3 -4
  87. package/src/commands/Erase.ts +6 -5
  88. package/src/commands/SerializableCommand.ts +17 -9
  89. package/src/commands/invertCommand.ts +51 -0
  90. package/src/commands/lib.ts +14 -0
  91. package/src/commands/localization.ts +2 -0
  92. package/src/components/AbstractComponent.ts +31 -20
  93. package/src/components/SVGGlobalAttributesObject.ts +8 -1
  94. package/src/components/Stroke.test.ts +1 -1
  95. package/src/components/Stroke.ts +11 -7
  96. package/src/components/Text.ts +6 -7
  97. package/src/components/UnknownSVGObject.ts +7 -1
  98. package/src/components/lib.ts +9 -0
  99. package/src/lib.ts +28 -0
  100. package/src/math/Mat33.ts +48 -20
  101. package/src/math/Path.ts +3 -3
  102. package/src/math/Rect2.ts +2 -2
  103. package/src/math/Vec3.ts +62 -14
  104. package/src/math/lib.ts +15 -0
  105. package/src/math/rounding.ts +2 -0
  106. package/src/rendering/Display.ts +46 -6
  107. package/src/rendering/caching/CacheRecord.test.ts +1 -1
  108. package/src/rendering/caching/CacheRecord.ts +4 -0
  109. package/src/rendering/caching/CacheRecordManager.ts +33 -7
  110. package/src/rendering/caching/RenderingCache.ts +10 -15
  111. package/src/rendering/caching/types.ts +1 -6
  112. package/src/rendering/renderers/CanvasRenderer.ts +1 -1
  113. package/src/toolbar/HTMLToolbar.ts +1 -0
  114. package/src/toolbar/makeColorInput.ts +1 -1
  115. package/src/toolbar/widgets/PenWidget.ts +2 -0
  116. package/src/tools/PanZoom.ts +0 -1
  117. package/src/tools/Pen.ts +11 -2
  118. package/src/tools/PipetteTool.ts +2 -0
  119. package/src/tools/SelectionTool.ts +46 -18
  120. package/src/types.ts +19 -3
  121. package/tsconfig.json +4 -1
  122. package/typedoc.json +20 -0
package/package.json CHANGED
@@ -1,103 +1,120 @@
1
1
  {
2
- "name": "js-draw",
3
- "version": "0.1.12",
4
- "description": "Draw pictures using a pen, touchscreen, or mouse! JS-draw is a drawing library for JavaScript and TypeScript. ",
5
- "main": "dist/src/Editor.js",
6
- "types": "dist/src/Editor.d.ts",
7
- "exports": {
8
- ".": {
9
- "types": "./dist/src/Editor.d.ts",
10
- "default": "./dist/src/Editor.js"
11
- },
12
- "./localizations/getLocalizationTable": {
13
- "types": "./dist/src/localizations/getLocalizationTable.d.ts",
14
- "default": "./dist/src/localizations/getLocalizationTable.js"
15
- },
16
- "./getLocalizationTable": {
17
- "types": "./dist/src/localizations/getLocalizationTable.d.ts",
18
- "default": "./dist/src/localizations/getLocalizationTable.js"
19
- },
20
- "./styles": {
21
- "default": "./src/styles.js"
22
- },
23
- "./Editor": {
24
- "types": "./dist/src/Editor.d.ts",
25
- "default": "./dist/src/Editor.js"
26
- },
27
- "./localization": {
28
- "types": "./dist/src/localization.d.ts",
29
- "default": "./dist/src/localization.js"
30
- },
31
- "./toolbar/HTMLToolbar": {
32
- "types": "./dist/src/toolbar/HTMLToolbar.d.ts",
33
- "default": "./dist/src/toolbar/HTMLToolbar.js"
34
- },
35
- "./Editor.css": {
36
- "default": "./src/Editor.css"
37
- },
38
- "./toolbar/toolbar.css": {
39
- "default": "./src/toolbar/toolbar.css"
40
- },
41
- "./bundle": {
42
- "default": "./dist/bundle.js"
43
- }
44
- },
45
- "repository": {
46
- "type": "git",
47
- "url": "git+https://github.com/personalizedrefrigerator/js-draw.git"
48
- },
49
- "author": "Henry Heino",
50
- "license": "MIT",
51
- "private": false,
52
- "scripts": {
53
- "test": "jest",
54
- "build": "rm -rf ./dist; mkdir dist && yarn tsc && ts-node ./build_tools/bundle.ts",
55
- "lint": "eslint .",
56
- "linter-precommit": "eslint --fix --ext .js --ext .ts",
57
- "lint-staged": "lint-staged",
58
- "_postinstall": "husky install",
59
- "prepack": "yarn build && pinst --disable",
60
- "postpack": "pinst --enable"
61
- },
62
- "dependencies": {
63
- "@melloware/coloris": "^0.16.0",
64
- "bezier-js": "^6.1.0"
65
- },
66
- "devDependencies": {
67
- "@types/bezier-js": "^4.1.0",
68
- "@types/jest": "^28.1.7",
69
- "@types/jsdom": "^20.0.0",
70
- "@types/node": "^18.7.15",
71
- "@typescript-eslint/eslint-plugin": "^5.36.2",
72
- "@typescript-eslint/parser": "^5.36.2",
73
- "css-loader": "^6.7.1",
74
- "eslint": "^8.23.0",
75
- "husky": "^8.0.1",
76
- "jest": "^28.1.3",
77
- "jest-environment-jsdom": "^29.0.2",
78
- "jsdom": "^20.0.0",
79
- "lint-staged": "^13.0.3",
80
- "pinst": "^3.0.0",
81
- "style-loader": "^3.3.1",
82
- "terser-webpack-plugin": "^5.3.5",
83
- "ts-jest": "^28.0.8",
84
- "ts-loader": "^9.3.1",
85
- "ts-node": "^10.9.1",
86
- "typescript": "^4.8.2",
87
- "webpack": "^5.74.0"
88
- },
89
- "bugs": {
90
- "url": "https://github.com/personalizedrefrigerator/js-draw/issues"
91
- },
92
- "homepage": "https://github.com/personalizedrefrigerator/js-draw#readme",
93
- "directories": {
94
- "doc": "docs"
95
- },
96
- "keywords": [
97
- "ink",
98
- "drawing",
99
- "pen",
100
- "freehand",
101
- "svg"
102
- ]
2
+ "name": "js-draw",
3
+ "version": "0.2.0",
4
+ "description": "Draw pictures using a pen, touchscreen, or mouse! JS-draw is a drawing library for JavaScript and TypeScript. ",
5
+ "main": "./dist/src/lib.d.ts",
6
+ "types": "./dist/src/lib.js",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/src/lib.d.ts",
10
+ "default": "./dist/src/lib.js"
11
+ },
12
+ "./getLocalizationTable": {
13
+ "types": "./dist/src/localizations/getLocalizationTable.d.ts",
14
+ "default": "./dist/src/localizations/getLocalizationTable.js"
15
+ },
16
+ "./styles": {
17
+ "default": "./src/styles.js"
18
+ },
19
+ "./Editor": {
20
+ "types": "./dist/src/Editor.d.ts",
21
+ "default": "./dist/src/Editor.js"
22
+ },
23
+ "./types": {
24
+ "types": "./dist/src/types.d.ts",
25
+ "default": "./dist/src/types.js"
26
+ },
27
+ "./localization": {
28
+ "types": "./dist/src/localization.d.ts",
29
+ "default": "./dist/src/localization.js"
30
+ },
31
+ "./toolbar/HTMLToolbar": {
32
+ "types": "./dist/src/toolbar/HTMLToolbar.d.ts",
33
+ "default": "./dist/src/toolbar/HTMLToolbar.js"
34
+ },
35
+ "./Editor.css": {
36
+ "default": "./src/Editor.css"
37
+ },
38
+ "./math": {
39
+ "types": "./dist/src/math/lib.d.ts",
40
+ "default": "./dist/src/math/lib.js"
41
+ },
42
+ "./Color4": {
43
+ "types": "./dist/src/Color4.d.ts",
44
+ "default": "./dist/src/Color4.js"
45
+ },
46
+ "./components": {
47
+ "types": "./dist/src/components/lib.d.ts",
48
+ "default": "./dist/src/components/lib.js"
49
+ },
50
+ "./commands": {
51
+ "types": "./dist/src/commands/lib.d.ts",
52
+ "default": "./dist/src/commands/lib.js"
53
+ },
54
+ "./bundle": {
55
+ "types": "./dist/src/bundle/bundled.d.ts",
56
+ "default": "./dist/bundle.js"
57
+ }
58
+ },
59
+ "repository": {
60
+ "type": "git",
61
+ "url": "git+https://github.com/personalizedrefrigerator/js-draw.git"
62
+ },
63
+ "author": "Henry Heino",
64
+ "license": "MIT",
65
+ "private": false,
66
+ "scripts": {
67
+ "test": "jest",
68
+ "build": "rm -rf ./dist; mkdir dist && yarn tsc && ts-node ./build_tools/bundle.ts",
69
+ "doc": "typedoc --options typedoc.json",
70
+ "watch-docs": "typedoc --watch --options typedoc.json",
71
+ "lint": "eslint .",
72
+ "linter-precommit": "eslint --fix --ext .js --ext .ts",
73
+ "lint-staged": "lint-staged",
74
+ "prepare": "husky install && yarn build",
75
+ "prepack": "yarn build && pinst --disable",
76
+ "postpack": "pinst --enable"
77
+ },
78
+ "dependencies": {
79
+ "@melloware/coloris": "^0.16.0",
80
+ "bezier-js": "^6.1.0"
81
+ },
82
+ "devDependencies": {
83
+ "@types/bezier-js": "^4.1.0",
84
+ "@types/jest": "^28.1.7",
85
+ "@types/jsdom": "^20.0.0",
86
+ "@types/node": "^18.7.15",
87
+ "@typescript-eslint/eslint-plugin": "^5.36.2",
88
+ "@typescript-eslint/parser": "^5.36.2",
89
+ "css-loader": "^6.7.1",
90
+ "eslint": "^8.23.0",
91
+ "husky": "^8.0.1",
92
+ "jest": "^28.1.3",
93
+ "jest-environment-jsdom": "^29.0.2",
94
+ "jsdom": "^20.0.0",
95
+ "lint-staged": "^13.0.3",
96
+ "pinst": "^3.0.0",
97
+ "style-loader": "^3.3.1",
98
+ "terser-webpack-plugin": "^5.3.5",
99
+ "ts-jest": "^28.0.8",
100
+ "ts-loader": "^9.3.1",
101
+ "ts-node": "^10.9.1",
102
+ "typedoc": "^0.23.14",
103
+ "typescript": "^4.8.2",
104
+ "webpack": "^5.74.0"
105
+ },
106
+ "bugs": {
107
+ "url": "https://github.com/personalizedrefrigerator/js-draw/issues"
108
+ },
109
+ "homepage": "https://github.com/personalizedrefrigerator/js-draw#readme",
110
+ "directories": {
111
+ "doc": "docs"
112
+ },
113
+ "keywords": [
114
+ "ink",
115
+ "drawing",
116
+ "pen",
117
+ "freehand",
118
+ "svg"
119
+ ]
103
120
  }
package/src/Color4.ts CHANGED
@@ -1,14 +1,25 @@
1
1
 
2
2
  export default class Color4 {
3
3
  private constructor(
4
+ /** Red component. Should be in the range [0, 1]. */
4
5
  public readonly r: number,
6
+
7
+ /** Green component. `g` ∈ [0, 1] */
5
8
  public readonly g: number,
9
+
10
+ /** Blue component. `b` ∈ [0, 1] */
6
11
  public readonly b: number,
12
+
13
+ /** Alpha/transparent component. `a` ∈ [0, 1] */
7
14
  public readonly a: number
8
15
  ) {
9
16
  }
10
17
 
11
- // Each component should be in the range [0, 1]
18
+ /**
19
+ * Create a color from red, green, blue components. The color is fully opaque (`a = 1.0`).
20
+ *
21
+ * Each component should be in the range [0, 1].
22
+ */
12
23
  public static ofRGB(red: number, green: number, blue: number): Color4 {
13
24
  return Color4.ofRGBA(red, green, blue, 1.0);
14
25
  }
@@ -58,7 +69,7 @@ export default class Color4 {
58
69
  return Color4.ofRGBA(components[0], components[1], components[2], components[3]);
59
70
  }
60
71
 
61
- // Like fromHex, but can handle additional colors if an HTML5Canvas is available.
72
+ /** Like fromHex, but can handle additional colors if an `HTMLCanvasElement` is available. */
62
73
  public static fromString(text: string): Color4 {
63
74
  if (text.startsWith('#')) {
64
75
  return Color4.fromHex(text);
@@ -82,6 +93,7 @@ export default class Color4 {
82
93
  }
83
94
  }
84
95
 
96
+ /** @returns true if `this` and `other` are approximately equal. */
85
97
  public eq(other: Color4|null|undefined): boolean {
86
98
  if (other == null) {
87
99
  return false;
@@ -91,6 +103,15 @@ export default class Color4 {
91
103
  }
92
104
 
93
105
  private hexString: string|null = null;
106
+
107
+ /**
108
+ * @returns a hexadecimal color string representation of `this`, in the form `#rrggbbaa`.
109
+ *
110
+ * @example
111
+ * ```
112
+ * Color4.red.toHexString(); // -> #ff0000ff
113
+ * ```
114
+ */
94
115
  public toHexString(): string {
95
116
  if (this.hexString) {
96
117
  return this.hexString;
package/src/Editor.ts CHANGED
@@ -1,3 +1,21 @@
1
+ /**
2
+ * The main entrypoint for the full editor.
3
+ *
4
+ * @example
5
+ * To create an editor with a toolbar,
6
+ * ```
7
+ * const editor = new Editor(document.body);
8
+ *
9
+ * const toolbar = editor.addToolbar();
10
+ * toolbar.addActionButton('Save', () => {
11
+ * const saveData = editor.toSVG().outerHTML;
12
+ * // Do something with saveData...
13
+ * });
14
+ * ```
15
+ *
16
+ * @packageDocumentation
17
+ */
18
+
1
19
 
2
20
  import EditorImage from './EditorImage';
3
21
  import ToolController from './tools/ToolController';
@@ -21,36 +39,80 @@ import { EditorLocalization } from './localization';
21
39
  import getLocalizationTable from './localizations/getLocalizationTable';
22
40
 
23
41
  export interface EditorSettings {
24
- // Defaults to RenderingMode.CanvasRenderer
42
+ /** Defaults to `RenderingMode.CanvasRenderer` */
25
43
  renderingMode: RenderingMode,
26
44
 
27
- // Uses a default English localization if a translation is not given.
45
+ /** Uses a default English localization if a translation is not given. */
28
46
  localization: Partial<EditorLocalization>,
29
47
 
30
- // True if touchpad/mousewheel scrolling should scroll the editor instead of the document.
31
- // This does not include pinch-zoom events.
32
- // Defaults to true.
48
+ /**
49
+ * `true` if touchpad/mousewheel scrolling should scroll the editor instead of the document.
50
+ * This does not include pinch-zoom events.
51
+ * Defaults to true.
52
+ */
33
53
  wheelEventsEnabled: boolean|'only-if-focused';
34
54
 
55
+ /** Minimum zoom fraction (e.g. 0.5 → 50% zoom). */
35
56
  minZoom: number,
36
57
  maxZoom: number,
37
58
  }
38
59
 
60
+ // { @inheritDoc Editor! }
39
61
  export class Editor {
40
62
  // Wrapper around the viewport and toolbar
41
63
  private container: HTMLElement;
42
64
  private renderingRegion: HTMLElement;
43
65
 
44
- public history: UndoRedoHistory;
45
66
  public display: Display;
67
+
68
+ /**
69
+ * Handles undo/redo.
70
+ *
71
+ * @example
72
+ * ```
73
+ * const editor = new Editor(document.body);
74
+ *
75
+ * // Do something undoable.
76
+ * // ...
77
+ *
78
+ * // Undo the last action
79
+ * editor.history.undo();
80
+ * ```
81
+ */
82
+ public history: UndoRedoHistory;
83
+
84
+ /**
85
+ * Data structure for adding/removing/querying objects in the image.
86
+ *
87
+ * @example
88
+ * ```
89
+ * const editor = new Editor(document.body);
90
+ *
91
+ * // Create a path.
92
+ * const stroke = new Stroke([
93
+ * Path.fromString('M0,0 L30,30 z').toRenderable({ fill: Color4.black }),
94
+ * ]);
95
+ * const addElementCommand = editor.image.addElement(stroke);
96
+ *
97
+ * // Add the stroke to the editor
98
+ * editor.dispatch(addElementCommand);
99
+ * ```
100
+ */
46
101
  public image: EditorImage;
47
102
 
48
- // Viewport for the exported/imported image
103
+ /** Viewport for the exported/imported image. */
49
104
  private importExportViewport: Viewport;
105
+
106
+ /** @internal */
50
107
  public localization: EditorLocalization;
51
108
 
52
109
  public viewport: Viewport;
53
110
  public toolController: ToolController;
111
+
112
+ /**
113
+ * Global event dispatcher/subscriber.
114
+ * @see {@link types.EditorEventType}
115
+ */
54
116
  public notifier: EditorNotifier;
55
117
 
56
118
  private loadingWarning: HTMLElement;
@@ -59,6 +121,29 @@ export class Editor {
59
121
 
60
122
  private settings: EditorSettings;
61
123
 
124
+ /**
125
+ * @example
126
+ * ```
127
+ * const container = document.body;
128
+ *
129
+ * // Create an editor
130
+ * const editor = new Editor(container, {
131
+ * // 2e-10 and 1e12 are the default values for minimum/maximum zoom.
132
+ * minZoom: 2e-10,
133
+ * maxZoom: 1e12,
134
+ * });
135
+ *
136
+ * // Add the default toolbar
137
+ * const toolbar = editor.addToolbar();
138
+ * toolbar.addActionButton({
139
+ * label: 'Save'
140
+ * icon: createSaveIcon(),
141
+ * }, () => {
142
+ * const saveData = editor.toSVG().outerHTML;
143
+ * // Do something with saveData
144
+ * });
145
+ * ```
146
+ */
62
147
  public constructor(
63
148
  parent: HTMLElement,
64
149
  settings: Partial<EditorSettings> = {},
@@ -145,14 +230,19 @@ export class Editor {
145
230
  });
146
231
  }
147
232
 
148
- // Returns a reference to this' container.
149
- // Example usage:
150
- // editor.getRootElement().style.height = '500px';
233
+ /**
234
+ * @returns a reference to the editor's container.
235
+ *
236
+ * @example
237
+ * ```
238
+ * editor.getRootElement().style.height = '500px';
239
+ * ```
240
+ */
151
241
  public getRootElement(): HTMLElement {
152
242
  return this.container;
153
243
  }
154
244
 
155
- // [fractionLoaded] should be a number from 0 to 1, where 1 represents completely loaded.
245
+ /** @param fractionLoaded - should be a number from 0 to 1, where 1 represents completely loaded. */
156
246
  public showLoadingWarning(fractionLoaded: number) {
157
247
  const loadingPercent = Math.round(fractionLoaded * 100);
158
248
  this.loadingWarning.innerText = this.localization.loading(loadingPercent);
@@ -166,6 +256,9 @@ export class Editor {
166
256
  }
167
257
 
168
258
  private previousAccessibilityAnnouncement: string = '';
259
+
260
+ // Announce `message` for screen readers. If `message` is the same as the previous
261
+ // message, it is re-announced.
169
262
  public announceForAccessibility(message: string) {
170
263
  // Force re-announcing an announcement if announced again.
171
264
  if (message === this.previousAccessibilityAnnouncement) {
@@ -175,6 +268,10 @@ export class Editor {
175
268
  this.previousAccessibilityAnnouncement = message;
176
269
  }
177
270
 
271
+ /**
272
+ * Creates a toolbar. If `defaultLayout` is true, default buttons are used.
273
+ * @returns a reference to the toolbar.
274
+ */
178
275
  public addToolbar(defaultLayout: boolean = true): HTMLToolbar {
179
276
  const toolbar = new HTMLToolbar(this, this.container, this.localization);
180
277
 
@@ -340,8 +437,7 @@ export class Editor {
340
437
  });
341
438
  }
342
439
 
343
- // Adds event listners for keypresses to [elem] and forwards those events to the
344
- // editor.
440
+ /** Adds event listners for keypresses to `elem` and forwards those events to the editor. */
345
441
  public handleKeyEventsFrom(elem: HTMLElement) {
346
442
  elem.addEventListener('keydown', evt => {
347
443
  if (evt.key === 't' || evt.key === 'T') {
@@ -369,7 +465,7 @@ export class Editor {
369
465
  });
370
466
  }
371
467
 
372
- // Adds to history by default
468
+ /** `apply` a command. `command` will be announced for accessibility. */
373
469
  public dispatch(command: Command, addToHistory: boolean = true) {
374
470
  if (addToHistory) {
375
471
  // .push applies [command] to this
@@ -381,7 +477,21 @@ export class Editor {
381
477
  this.announceForAccessibility(command.description(this, this.localization));
382
478
  }
383
479
 
384
- // Dispatches a command without announcing it. By default, does not add to history.
480
+ /**
481
+ * Dispatches a command without announcing it. By default, does not add to history.
482
+ * Use this to show finalized commands that don't need to have `announceForAccessibility`
483
+ * called.
484
+ *
485
+ * Prefer `command.apply(editor)` for incomplete commands. `dispatchNoAnnounce` may allow
486
+ * clients to listen for the application of commands (e.g. `SerializableCommand`s so they can
487
+ * be sent across the network), while `apply` does not.
488
+ *
489
+ * @example
490
+ * ```
491
+ * const addToHistory = false;
492
+ * editor.dispatchNoAnnounce(editor.viewport.zoomTo(someRectangle), addToHistory);
493
+ * ```
494
+ */
385
495
  public dispatchNoAnnounce(command: Command, addToHistory: boolean = false) {
386
496
  if (addToHistory) {
387
497
  this.history.push(command);
@@ -390,11 +500,13 @@ export class Editor {
390
500
  }
391
501
  }
392
502
 
393
- // Apply a large transformation in chunks.
394
- // If [apply] is false, the commands are unapplied.
395
- // Triggers a re-render after each [updateChunkSize]-sized group of commands
396
- // has been applied.
397
- private async asyncApplyOrUnapplyCommands(
503
+ /**
504
+ * Apply a large transformation in chunks.
505
+ * If `apply` is `false`, the commands are unapplied.
506
+ * Triggers a re-render after each `updateChunkSize`-sized group of commands
507
+ * has been applied.
508
+ */
509
+ public async asyncApplyOrUnapplyCommands(
398
510
  commands: Command[], apply: boolean, updateChunkSize: number
399
511
  ) {
400
512
  this.display.setDraftMode(true);
@@ -423,10 +535,12 @@ export class Editor {
423
535
  this.hideLoadingWarning();
424
536
  }
425
537
 
538
+ // @see {@link #asyncApplyOrUnapplyCommands }
426
539
  public asyncApplyCommands(commands: Command[], chunkSize: number) {
427
540
  return this.asyncApplyOrUnapplyCommands(commands, true, chunkSize);
428
541
  }
429
542
 
543
+ // @see {@link #asyncApplyOrUnapplyCommands }
430
544
  public asyncUnapplyCommands(commands: Command[], chunkSize: number) {
431
545
  return this.asyncApplyOrUnapplyCommands(commands, false, chunkSize);
432
546
  }
@@ -440,6 +554,8 @@ export class Editor {
440
554
  };
441
555
 
442
556
  private rerenderQueued: boolean = false;
557
+ // Schedule a re-render for some time in the near future. Does not schedule an additional
558
+ // re-render if a re-render is already queued.
443
559
  public queueRerender() {
444
560
  if (!this.rerenderQueued) {
445
561
  this.rerenderQueued = true;
@@ -486,11 +602,13 @@ export class Editor {
486
602
  this.display.getWetInkRenderer().clear();
487
603
  }
488
604
 
489
- // Focuses the region used for text input
605
+ // Focuses the region used for text input/key commands.
490
606
  public focus() {
491
607
  this.renderingRegion.focus();
492
608
  }
493
609
 
610
+ // Creates an element that will be positioned on top of the dry/wet ink
611
+ // renderers.
494
612
  public createHTMLOverlay(overlay: HTMLElement) {
495
613
  overlay.classList.add('overlay');
496
614
  this.container.appendChild(overlay);
@@ -509,7 +627,7 @@ export class Editor {
509
627
  }
510
628
 
511
629
  // Dispatch a pen event to the currently selected tool.
512
- // Intented for unit tests.
630
+ // Intended primarially for unit tests.
513
631
  public sendPenEvent(
514
632
  eventType: InputEvtType.PointerDownEvt|InputEvtType.PointerMoveEvt|InputEvtType.PointerUpEvt,
515
633
  point: Point2,
@@ -589,7 +707,7 @@ export class Editor {
589
707
  return this.importExportViewport.visibleRect;
590
708
  }
591
709
 
592
- // Resize the output SVG
710
+ // Resize the output SVG to match `imageRect`.
593
711
  public setImportExportRect(imageRect: Rect2): Command {
594
712
  const origSize = this.importExportViewport.visibleRect.size;
595
713
  const origTransform = this.importExportViewport.canvasToScreenTransform;
@@ -615,8 +733,12 @@ export class Editor {
615
733
  };
616
734
  }
617
735
 
618
- // Alias for loadFrom(SVGLoader.fromString).
619
- // This is particularly useful when accessing a bundled version of the editor.
736
+ /**
737
+ * Alias for loadFrom(SVGLoader.fromString).
738
+ *
739
+ * This is particularly useful when accessing a bundled version of the editor,
740
+ * where `SVGLoader.fromString` is unavailable.
741
+ */
620
742
  public async loadFromSVG(svgData: string) {
621
743
  const loader = SVGLoader.fromString(svgData);
622
744
  await this.loadFrom(loader);