js-draw 0.24.0 → 0.25.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 (75) hide show
  1. package/README.md +217 -0
  2. package/dist/bundle.js +2 -2
  3. package/dist/bundledStyles.js +1 -1
  4. package/dist/cjs/localizations/de.js +1 -1
  5. package/dist/cjs/localizations/es.js +1 -1
  6. package/dist/cjs/rendering/renderers/CanvasRenderer.js +2 -0
  7. package/dist/cjs/rendering/renderers/SVGRenderer.d.ts +1 -1
  8. package/dist/cjs/rendering/renderers/SVGRenderer.js +4 -2
  9. package/dist/cjs/shortcuts/KeyBinding.js +1 -1
  10. package/dist/cjs/testing/getUniquePointerId.d.ts +4 -0
  11. package/dist/cjs/testing/getUniquePointerId.js +16 -0
  12. package/dist/cjs/testing/sendPenEvent.d.ts +1 -1
  13. package/dist/cjs/testing/sendPenEvent.js +4 -1
  14. package/dist/cjs/testing/sendTouchEvent.js +2 -9
  15. package/dist/cjs/toolbar/IconProvider.d.ts +1 -1
  16. package/dist/cjs/toolbar/IconProvider.js +76 -10
  17. package/dist/cjs/toolbar/localization.d.ts +2 -2
  18. package/dist/cjs/toolbar/localization.js +2 -2
  19. package/dist/cjs/toolbar/widgets/BaseToolWidget.d.ts +2 -0
  20. package/dist/cjs/toolbar/widgets/BaseToolWidget.js +7 -0
  21. package/dist/cjs/toolbar/widgets/PenToolWidget.d.ts +3 -1
  22. package/dist/cjs/toolbar/widgets/PenToolWidget.js +125 -41
  23. package/dist/cjs/toolbar/widgets/SelectionToolWidget.js +4 -0
  24. package/dist/cjs/tools/BaseTool.d.ts +17 -1
  25. package/dist/cjs/tools/BaseTool.js +18 -0
  26. package/dist/cjs/tools/Pen.d.ts +5 -2
  27. package/dist/cjs/tools/Pen.js +37 -4
  28. package/dist/cjs/tools/ToolController.js +14 -2
  29. package/dist/mjs/localizations/de.mjs +1 -1
  30. package/dist/mjs/localizations/es.mjs +1 -1
  31. package/dist/mjs/rendering/renderers/CanvasRenderer.mjs +2 -0
  32. package/dist/mjs/rendering/renderers/SVGRenderer.d.ts +1 -1
  33. package/dist/mjs/rendering/renderers/SVGRenderer.mjs +4 -2
  34. package/dist/mjs/shortcuts/KeyBinding.mjs +1 -1
  35. package/dist/mjs/testing/getUniquePointerId.d.ts +4 -0
  36. package/dist/mjs/testing/getUniquePointerId.mjs +14 -0
  37. package/dist/mjs/testing/sendPenEvent.d.ts +1 -1
  38. package/dist/mjs/testing/sendPenEvent.mjs +4 -1
  39. package/dist/mjs/testing/sendTouchEvent.mjs +2 -9
  40. package/dist/mjs/toolbar/IconProvider.d.ts +1 -1
  41. package/dist/mjs/toolbar/IconProvider.mjs +76 -10
  42. package/dist/mjs/toolbar/localization.d.ts +2 -2
  43. package/dist/mjs/toolbar/localization.mjs +2 -2
  44. package/dist/mjs/toolbar/widgets/BaseToolWidget.d.ts +2 -0
  45. package/dist/mjs/toolbar/widgets/BaseToolWidget.mjs +7 -0
  46. package/dist/mjs/toolbar/widgets/PenToolWidget.d.ts +3 -1
  47. package/dist/mjs/toolbar/widgets/PenToolWidget.mjs +125 -41
  48. package/dist/mjs/toolbar/widgets/SelectionToolWidget.mjs +4 -0
  49. package/dist/mjs/tools/BaseTool.d.ts +17 -1
  50. package/dist/mjs/tools/BaseTool.mjs +18 -0
  51. package/dist/mjs/tools/Pen.d.ts +5 -2
  52. package/dist/mjs/tools/Pen.mjs +37 -4
  53. package/dist/mjs/tools/ToolController.mjs +14 -2
  54. package/package.json +4 -3
  55. package/src/localizations/de.ts +2 -2
  56. package/src/localizations/es.ts +1 -1
  57. package/src/rendering/renderers/CanvasRenderer.ts +2 -0
  58. package/src/rendering/renderers/SVGRenderer.ts +6 -3
  59. package/src/shortcuts/KeyBinding.ts +1 -1
  60. package/src/testing/getUniquePointerId.ts +18 -0
  61. package/src/testing/sendPenEvent.ts +6 -1
  62. package/src/testing/sendTouchEvent.ts +2 -9
  63. package/src/toolbar/IconProvider.ts +92 -23
  64. package/src/toolbar/localization.ts +4 -4
  65. package/src/toolbar/toolbar.css +1 -0
  66. package/src/toolbar/widgets/BaseToolWidget.ts +10 -1
  67. package/src/toolbar/widgets/PenToolWidget.css +53 -0
  68. package/src/toolbar/widgets/PenToolWidget.ts +156 -44
  69. package/src/toolbar/widgets/SelectionToolWidget.ts +4 -0
  70. package/src/tools/BaseTool.ts +22 -1
  71. package/src/tools/PanZoom.test.ts +39 -0
  72. package/src/tools/Pen.test.ts +68 -0
  73. package/src/tools/Pen.ts +42 -4
  74. package/src/tools/ToolController.ts +17 -2
  75. package/tools/copyREADME.ts +62 -0
package/README.md ADDED
@@ -0,0 +1,217 @@
1
+ # js-draw
2
+
3
+ [NPM package](https://www.npmjs.com/package/js-draw) | [GitHub](https://github.com/personalizedrefrigerator/js-draw) | [Documentation](https://personalizedrefrigerator.github.io/js-draw/typedoc/modules/lib.html) | [Try it!](https://personalizedrefrigerator.github.io/js-draw/example/example.html)
4
+
5
+ ![](docs/img/readme-images/js-draw.jpg)
6
+
7
+ For example usage, see [one of the examples](https://github.com/personalizedrefrigerator/js-draw/blob/main/docs/examples.md) or read [the documentation](https://personalizedrefrigerator.github.io/js-draw/typedoc/modules/lib.html).
8
+
9
+ # API
10
+
11
+ To use `js-draw`,
12
+
13
+ ## Creating an `Editor`
14
+
15
+ ### With a bundler that supports importing `.css` files
16
+
17
+ To create a new `Editor` and add it as a child of `document.body`,
18
+ ```ts
19
+ import Editor from 'js-draw';
20
+ import 'js-draw/styles';
21
+
22
+ const editor = new Editor(document.body);
23
+ ```
24
+
25
+ The `import js-draw/styles` step requires a bundler that can import `.css` files. For example, [`webpack` with `css-loader`.](https://webpack.js.org/loaders/css-loader/)
26
+
27
+ ### With a bundler that doesn't support importing `.css` files
28
+
29
+ Import the pre-bundled version of the editor to apply CSS after loading the page.
30
+ ```ts
31
+ import Editor from 'js-draw';
32
+ import 'js-draw/bundledStyles';
33
+
34
+ const editor = new Editor(document.body);
35
+ ```
36
+ `js-draw/bundledStyles` is a version of the editor's stylesheets pre-processed by `Webpack`. As such, `import`ing or including it with a `<script src="..."></script>` tag applies editor-specific CSS to the document.
37
+
38
+ ### Without a bundler
39
+
40
+ If you're not using a bundler, consider using the pre-bundled editor:
41
+ ```html
42
+ <!-- Replace 0.20.0 with the latest version of js-draw -->
43
+ <script src="https://cdn.jsdelivr.net/npm/js-draw@0.20.0/dist/bundle.js"></script>
44
+ <script>
45
+ const editor = new jsdraw.Editor(document.body);
46
+ editor.addToolbar();
47
+ editor.getRootElement().style.height = '600px';
48
+ </script>
49
+ ```
50
+
51
+ **Note**: To ensure the CDN-hosted version of `js-draw` hasn't been tampered with, consider [including an `integrity="..."` attribute](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity). [Read more about using SRI with JSDelivr](https://www.jsdelivr.com/using-sri-with-dynamic-files).
52
+
53
+
54
+ ## Adding a toolbar
55
+
56
+ To create a toolbar with the default tools:
57
+ ```ts
58
+ const toolbar = editor.addToolbar();
59
+ ```
60
+
61
+ Custom actions can be added to the toolbar. For example, to add a `save` button:
62
+ ```ts
63
+ toolbar.addActionButton('Save', () => {
64
+ const svgElem = editor.toSVG();
65
+ console.log('The saved SVG:', svgElem.outerHTML);
66
+ });
67
+ ```
68
+ or alternatively, with an icon,
69
+ ```ts
70
+ toolbar.addActionButton({
71
+ label: 'Save'
72
+ icon: editor.icons.makeSaveIcon(),
73
+ }, () => {
74
+ // Save content here.
75
+ });
76
+ ```
77
+
78
+ ## Loading from an SVG
79
+
80
+ ```ts
81
+ editor.loadFromSVG(`
82
+ <svg
83
+ viewBox="156 74 200 150"
84
+ width="200" height="150"
85
+ >
86
+ <path d="M156,150Q190,190 209,217L213,215Q193,187 160,148M209,217Q212,218 236,178L232,176Q210,215 213,215M236,178Q240,171 307,95L305,93Q237,168 232,176M307,95Q312,90 329,78L327,74Q309,87 305,93" fill="#07a837"></path>
87
+ </svg>
88
+ `);
89
+ ```
90
+
91
+ **Note**: While `js-draw` supports a small subset of the SVG markup language, it tries to preserve unrecognised SVG elements.
92
+
93
+ For example, although `js-draw` doesn't support `<circle/>` elements,
94
+ ```xml
95
+ <svg
96
+ viewBox="156 74 200 150"
97
+ width="200" height="150"
98
+ >
99
+ <path d="M156,150Q190,190 209,217L213,215Q193,187 160,148M209,217Q212,218 236,178L232,176Q210,215 213,215M236,178Q240,171 307,95L305,93Q237,168 232,176M307,95Q312,90 329,78L327,74Q309,87 305,93" fill="#07a837"></path>
100
+ <circle cx=200 cy=100 r=40 fill='red'/>
101
+ </svg>
102
+ ```
103
+ renders as
104
+
105
+ ![screenshot of the image editor, displaying a green checkmark. The circle is invisible](docs/img/readme-images/unsupported-elements--in-editor.png)
106
+
107
+ but exports to
108
+ ```xml
109
+ <svg viewBox="156 74 200 150" width="200" height="150" version="1.1" baseProfile="full" xmlns="http://www.w3.org/2000/svg"><g><path d="M156,150M156,150Q190,190 209,217L213,215Q193,187 160,148M209,217M209,217Q212,218 236,178L232,176Q210,215 213,215M236,178M236,178Q240,171 307,95L305,93Q237,168 232,176M307,95M307,95Q312,90 329,78L327,74Q309,87 305,93" fill="#07a837"></path></g><circle cx="200" cy="100" r="40" fill="red"></circle></svg>
110
+ ```
111
+
112
+ which **does** contain the `<circle/>` element.
113
+
114
+ ## Settings/configuration
115
+ ### Disabling touchpad panning
116
+
117
+ Touchpad/mousewheel pan gestures can conflict with gestures used to scroll the document. To turn off touchpad pan gestures (and scrolling the editor with the mousewheel),
118
+ ```ts
119
+ const editor = new Editor(document.body, {
120
+ wheelEventsEnabled: false,
121
+ });
122
+ ```
123
+
124
+ Alternatively, to only enable touchpad panning when the editor has focus,
125
+ ```ts
126
+ const editor = new Editor(document.body, {
127
+ wheelEventsEnabled: 'only-if-focused',
128
+ });
129
+ ```
130
+
131
+ ### Localization
132
+
133
+ If a user's language is available in [src/localizations/](src/localizations/) (as determined by `navigator.languages`), that localization will be used.
134
+
135
+ To override the default language, use `getLocalizationTable([ 'custom locale here' ])`. For example,
136
+ ```ts
137
+ const editor = new Editor(document.body, {
138
+ // Force the Spanish (Español) localizaiton
139
+ localization: getLocalizationTable([ 'es' ]),
140
+ });
141
+ ```
142
+
143
+ <details><summary>Creating a custom localization</summary>
144
+
145
+ See [src/localization.ts](src/localization.ts) for a list of strings that can be translated.
146
+
147
+ Many of the default strings in the editor might be overridden like this:
148
+ ```ts
149
+ const editor = new Editor(document.body, {
150
+ // Example partial Spanish localization
151
+ localization: {
152
+ // Not all translated strings need to be specified. If a string isn't given,
153
+ // the English (default) localization will be used
154
+
155
+ // Strings for the main editor interface
156
+ // (see src/localization.ts)
157
+ loading: (percentage: number) => `Cargando: ${percentage}%...`,
158
+ imageEditor: 'Editor de dibujos',
159
+
160
+ undoAnnouncement: (commandDescription: string) => `${commandDescription} fue deshecho`,
161
+ redoAnnouncement: (commandDescription: string) => `${commandDescription} fue rehecho`,
162
+
163
+ // Strings for the toolbar
164
+ // (see src/toolbar/localization.ts)
165
+ pen: 'Lapiz',
166
+ eraser: 'Borrador',
167
+ select: 'Selecciona',
168
+ thicknessLabel: 'Tamaño: ',
169
+ colorLabel: 'Color: ',
170
+
171
+ ...
172
+ },
173
+ });
174
+ ```
175
+
176
+ </details>
177
+
178
+ ## Changing the editor's color theme
179
+
180
+ The editor's color theme is specified using CSS. Its default theme looks like this:
181
+ ```css
182
+ .imageEditorContainer {
183
+ /* Deafult colors for the editor -- light mode */
184
+
185
+ --primary-background-color: white;
186
+ --primary-background-color-transparent: rgba(255, 255, 255, 0.5);
187
+ --secondary-background-color: #faf;
188
+ --primary-foreground-color: black;
189
+ --secondary-foreground-color: black;
190
+ --primary-shadow-color: rgba(0, 0, 0, 0.5);
191
+ }
192
+
193
+ @media (prefers-color-scheme: dark) {
194
+ .imageEditorContainer {
195
+ /* Deafult colors for the editor -- dark mode */
196
+
197
+ --primary-background-color: #151515;
198
+ --primary-background-color-transparent: rgba(50, 50, 50, 0.5);
199
+ --secondary-background-color: #607;
200
+ --primary-foreground-color: white;
201
+ --secondary-foreground-color: white;
202
+ --primary-shadow-color: rgba(250, 250, 250, 0.5);
203
+ }
204
+ }
205
+ ```
206
+
207
+ To override it, use a more specific CSS selector to set the theme variables. For example,
208
+ ```css
209
+ body .imageEditorContainer {
210
+ --primary-background-color: green;
211
+ --primary-background-color-transparent: rgba(255, 240, 200, 0.5);
212
+ --secondary-background-color: yellow;
213
+ --primary-foreground-color: black;
214
+ --secondary-foreground-color: black;
215
+ }
216
+ ```
217
+ disables the dark theme and creates a theme that primarily uses yellow/green colors.