js-draw 1.0.2 → 1.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 (41) hide show
  1. package/dist/Editor.css +7 -4
  2. package/dist/bundle.js +1 -1
  3. package/dist/bundledStyles.js +1 -1
  4. package/dist/cjs/Editor.d.ts +4 -3
  5. package/dist/cjs/lib.d.ts +15 -8
  6. package/dist/cjs/lib.js +13 -6
  7. package/dist/cjs/toolbar/AbstractToolbar.d.ts +9 -13
  8. package/dist/cjs/toolbar/AbstractToolbar.js +14 -19
  9. package/dist/cjs/toolbar/DropdownToolbar.d.ts +24 -0
  10. package/dist/cjs/toolbar/DropdownToolbar.js +24 -0
  11. package/dist/cjs/toolbar/EdgeToolbar.d.ts +28 -0
  12. package/dist/cjs/toolbar/EdgeToolbar.js +30 -2
  13. package/dist/cjs/toolbar/widgets/SaveActionWidget.d.ts +10 -0
  14. package/dist/cjs/toolbar/widgets/SaveActionWidget.js +26 -0
  15. package/dist/cjs/toolbar/widgets/keybindings.d.ts +1 -0
  16. package/dist/cjs/toolbar/widgets/keybindings.js +4 -1
  17. package/dist/cjs/util/adjustEditorThemeForContrast.d.ts +53 -0
  18. package/dist/cjs/util/adjustEditorThemeForContrast.js +100 -0
  19. package/dist/cjs/util/lib.d.ts +1 -0
  20. package/dist/cjs/util/lib.js +8 -0
  21. package/dist/cjs/version.js +1 -1
  22. package/dist/mjs/Editor.d.ts +4 -3
  23. package/dist/mjs/lib.d.ts +15 -8
  24. package/dist/mjs/lib.mjs +15 -8
  25. package/dist/mjs/toolbar/AbstractToolbar.d.ts +9 -13
  26. package/dist/mjs/toolbar/AbstractToolbar.mjs +14 -19
  27. package/dist/mjs/toolbar/DropdownToolbar.d.ts +24 -0
  28. package/dist/mjs/toolbar/DropdownToolbar.mjs +24 -0
  29. package/dist/mjs/toolbar/EdgeToolbar.d.ts +28 -0
  30. package/dist/mjs/toolbar/EdgeToolbar.mjs +30 -2
  31. package/dist/mjs/toolbar/widgets/SaveActionWidget.d.ts +10 -0
  32. package/dist/mjs/toolbar/widgets/SaveActionWidget.mjs +21 -0
  33. package/dist/mjs/toolbar/widgets/keybindings.d.ts +1 -0
  34. package/dist/mjs/toolbar/widgets/keybindings.mjs +3 -0
  35. package/dist/mjs/util/adjustEditorThemeForContrast.d.ts +53 -0
  36. package/dist/mjs/util/adjustEditorThemeForContrast.mjs +98 -0
  37. package/dist/mjs/util/lib.d.ts +1 -0
  38. package/dist/mjs/util/lib.mjs +1 -0
  39. package/dist/mjs/version.mjs +1 -1
  40. package/package.json +3 -3
  41. package/src/toolbar/EdgeToolbar.scss +7 -4
@@ -0,0 +1,98 @@
1
+ import { Color4, Vec3 } from '@js-draw/math';
2
+ /**
3
+ * Adjusts the current editor theme such that colors have appropriate contrast.
4
+ *
5
+ * As this method overrides CSS variables using the `.style` property,
6
+ * **assumes** all original theme variables are set using CSS and not the `.style` property.
7
+ *
8
+ * If the editor changes theme in response to the system theme, this method should be
9
+ * called whenever the system theme changes (e.g. by using [the `matchMedia`](https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia)
10
+ * method).
11
+ *
12
+ * @example
13
+ * ```ts,runnable
14
+ * import { Editor, adjustEditorThemeForContrast } from 'js-draw';
15
+ *
16
+ * const editor = new Editor(document.body);
17
+ * editor.addToolbar();
18
+ *
19
+ * const css = `
20
+ * :root .imageEditorContainer {
21
+ * --background-color-1: #ffff77;
22
+ * --foreground-color-1: #fff;
23
+ * --background-color-2: #ffff99;
24
+ * --foreground-color-2: #ffff88;
25
+ * --background-color-3: #ddffff;
26
+ * --foreground-color-3: #eeffff;
27
+ * --selection-background-color: #9f7;
28
+ * --selection-foreground-color: #98f;
29
+ * }
30
+ *
31
+ * @media screen and (prefers-color-scheme: dark) {
32
+ * :root .imageEditorContainer {
33
+ * --background-color-1: black;
34
+ * }
35
+ * }
36
+ * `;
37
+ * editor.addStyleSheet(css);
38
+ *
39
+ * adjustEditorThemeForContrast(editor);
40
+ *
41
+ * // Because adjustEditorThemeForContrast overrides the current theme, it should be called again
42
+ * // to allow the editor to switch between light/dark themes.
43
+ * window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
44
+ * adjustEditorThemeForContrast(editor);
45
+ * });
46
+ *
47
+ * window.matchMedia('print').addEventListener('change', () => {
48
+ * adjustEditorThemeForContrast(editor);
49
+ * });
50
+ * ```
51
+ */
52
+ const adjustEditorThemeForContrast = (editor) => {
53
+ const editorElem = editor.getRootElement();
54
+ // Each set of entries in colorPairs should resolve to colors with sufficient
55
+ // contrast.
56
+ const colorPairs = [
57
+ ['--background-color-1', '--foreground-color-1'],
58
+ ['--background-color-2', '--foreground-color-2'],
59
+ ['--background-color-3', '--foreground-color-3'],
60
+ ['--selection-background-color', '--selection-foreground-color'],
61
+ ];
62
+ // Clear any overrides
63
+ for (const [backgroundVar, foregroundVar] of colorPairs) {
64
+ editorElem.style.setProperty(backgroundVar, null);
65
+ editorElem.style.setProperty(foregroundVar, null);
66
+ }
67
+ const styles = getComputedStyle(editorElem);
68
+ const minContrast = 3;
69
+ for (const [backgroundVar, foregroundVar] of colorPairs) {
70
+ let color1 = Color4.fromString(styles.getPropertyValue(backgroundVar));
71
+ let color2 = Color4.fromString(styles.getPropertyValue(foregroundVar));
72
+ let swappedColors = false;
73
+ // Ensure that color1 has the lesser luminance
74
+ if (color1.relativeLuminance() < color2.relativeLuminance()) {
75
+ const tmp = color1;
76
+ color1 = color2;
77
+ color2 = tmp;
78
+ swappedColors = true;
79
+ }
80
+ let colorsUpdated = false;
81
+ let currentContrast = Color4.contrastRatio(color1, color2);
82
+ const iterations = 0;
83
+ while (currentContrast < minContrast && iterations < 5) {
84
+ const step = Vec3.of(0.1, 0.1, 0.1);
85
+ color1 = Color4.fromRGBVector(color1.rgb.plus(step));
86
+ color2 = Color4.fromRGBVector(color2.rgb.minus(step));
87
+ currentContrast = Color4.contrastRatio(color1, color2);
88
+ colorsUpdated = true;
89
+ }
90
+ if (colorsUpdated) {
91
+ const newBackground = swappedColors ? color2 : color1;
92
+ const newForeground = swappedColors ? color1 : color2;
93
+ editorElem.style.setProperty(foregroundVar, newForeground.toHexString());
94
+ editorElem.style.setProperty(backgroundVar, newBackground.toHexString());
95
+ }
96
+ }
97
+ };
98
+ export default adjustEditorThemeForContrast;
@@ -0,0 +1 @@
1
+ export { default as adjustEditorThemeForContrast } from './adjustEditorThemeForContrast';
@@ -0,0 +1 @@
1
+ export { default as adjustEditorThemeForContrast } from './adjustEditorThemeForContrast.mjs';
@@ -1,3 +1,3 @@
1
1
  export default {
2
- number: '1.0.2',
2
+ number: '1.2.0',
3
3
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-draw",
3
- "version": "1.0.2",
3
+ "version": "1.2.0",
4
4
  "description": "Draw pictures using a pen, touchscreen, or mouse! JS-draw is a drawing library for JavaScript and TypeScript. ",
5
5
  "types": "./dist/mjs/lib.d.ts",
6
6
  "main": "./dist/cjs/lib.js",
@@ -64,7 +64,7 @@
64
64
  "postpack": "ts-node tools/copyREADME.ts revert"
65
65
  },
66
66
  "dependencies": {
67
- "@js-draw/math": "^1.0.2",
67
+ "@js-draw/math": "^1.2.0",
68
68
  "@melloware/coloris": "0.21.0"
69
69
  },
70
70
  "devDependencies": {
@@ -87,5 +87,5 @@
87
87
  "freehand",
88
88
  "svg"
89
89
  ],
90
- "gitHead": "f5a92284625b49b2ef6541bdafce1bd926c10441"
90
+ "gitHead": "3e77c7d833ecdc13bcb57e905280ba547629680a"
91
91
  }
@@ -1,12 +1,12 @@
1
1
  @keyframes toolbar--edgemenu-transition-in {
2
- from { translate: 0 100%; }
3
- to { translate: 0; }
2
+ from { transform: translate(0, 100%); }
3
+ to { transform: translate(0, 0); }
4
4
  }
5
5
 
6
6
  @keyframes toolbar--edgemenu-transition-out {
7
7
  // Don't include `from { translate: 0 }` because initially,
8
8
  // the translation might not be zero (e.g. during a drag).
9
- to { translate: 0 100%; }
9
+ to { transform: translate(0, 100%); }
10
10
  }
11
11
 
12
12
  @keyframes toolbar--edgemenu-container-transition-in {
@@ -249,6 +249,7 @@
249
249
 
250
250
  > .toolbar-button > .toolbar-icon {
251
251
  margin-left: 10px;
252
+ margin-right: 0;
252
253
  }
253
254
 
254
255
  // Show last
@@ -267,6 +268,7 @@
267
268
  > .toolbar-icon {
268
269
  height: 100%;
269
270
  margin-right: 10px;
271
+ margin-left: 0;
270
272
  }
271
273
  }
272
274
  }
@@ -377,6 +379,7 @@
377
379
  user-select: none;
378
380
 
379
381
  background-color: var(--background-color-2);
382
+ color: var(--foreground-color-2);
380
383
  --icon-color: var(--foreground-color-2);
381
384
 
382
385
  box-shadow: 0px 0px 1px var(--shadow-color);
@@ -391,7 +394,7 @@
391
394
  border-top-left-radius: $border-radius;
392
395
  border-top-right-radius: $border-radius;
393
396
 
394
- transition: translate 0.1s ease, padding-bottom 0.1s ease;
397
+ transition: transform 0.1s ease, padding-bottom 0.1s ease;
395
398
 
396
399
  input, textarea {
397
400
  user-select: auto;