js-draw 0.9.2 → 0.9.3

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 (39) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/dist/bundle.js +1 -1
  3. package/dist/src/Editor.d.ts +2 -2
  4. package/dist/src/EditorImage.d.ts +1 -1
  5. package/dist/src/EventDispatcher.d.ts +1 -1
  6. package/dist/src/SVGLoader.d.ts +2 -2
  7. package/dist/src/UndoRedoHistory.d.ts +2 -2
  8. package/dist/src/Viewport.d.ts +1 -1
  9. package/dist/src/commands/SerializableCommand.d.ts +1 -1
  10. package/dist/src/components/AbstractComponent.d.ts +3 -3
  11. package/dist/src/components/SVGGlobalAttributesObject.d.ts +1 -1
  12. package/dist/src/components/builders/FreehandLineBuilder.d.ts +2 -0
  13. package/dist/src/components/builders/FreehandLineBuilder.js +10 -2
  14. package/dist/src/components/builders/types.d.ts +1 -1
  15. package/dist/src/components/util/StrokeSmoother.d.ts +1 -1
  16. package/dist/src/math/Mat33.d.ts +1 -1
  17. package/dist/src/math/Path.d.ts +1 -1
  18. package/dist/src/math/Vec2.d.ts +2 -2
  19. package/dist/src/rendering/caching/testUtils.d.ts +1 -1
  20. package/dist/src/rendering/caching/types.d.ts +2 -2
  21. package/dist/src/rendering/renderers/SVGRenderer.js +1 -1
  22. package/dist/src/toolbar/IconProvider.d.ts +1 -1
  23. package/dist/src/toolbar/localization.d.ts +1 -0
  24. package/dist/src/toolbar/localization.js +1 -0
  25. package/dist/src/toolbar/makeColorInput.d.ts +2 -2
  26. package/dist/src/toolbar/widgets/BaseWidget.d.ts +1 -1
  27. package/dist/src/toolbar/widgets/TextToolWidget.js +23 -2
  28. package/dist/src/tools/BaseTool.js +4 -4
  29. package/dist/src/tools/PipetteTool.d.ts +1 -1
  30. package/dist/src/tools/SelectionTool/SelectionHandle.d.ts +3 -3
  31. package/dist/src/tools/TextTool.js +8 -2
  32. package/dist/src/tools/ToolbarShortcutHandler.d.ts +1 -1
  33. package/dist/src/types.d.ts +8 -8
  34. package/package.json +16 -16
  35. package/src/components/builders/FreehandLineBuilder.ts +12 -2
  36. package/src/rendering/renderers/SVGRenderer.ts +1 -1
  37. package/src/toolbar/localization.ts +2 -0
  38. package/src/toolbar/widgets/TextToolWidget.ts +29 -1
  39. package/src/tools/TextTool.ts +10 -2
@@ -34,10 +34,14 @@ export default class TextToolWidget extends BaseToolWidget {
34
34
  protected fillDropdown(dropdown: HTMLElement): boolean {
35
35
  const fontRow = document.createElement('div');
36
36
  const colorRow = document.createElement('div');
37
+ const sizeRow = document.createElement('div');
37
38
 
38
39
  const fontInput = document.createElement('select');
39
40
  const fontLabel = document.createElement('label');
40
41
 
42
+ const sizeInput = document.createElement('input');
43
+ const sizeLabel = document.createElement('label');
44
+
41
45
  const [ colorInput, colorInputContainer, setColorInputValue ] = makeColorInput(this.editor, color => {
42
46
  this.tool.setColor(color);
43
47
  });
@@ -52,12 +56,20 @@ export default class TextToolWidget extends BaseToolWidget {
52
56
  fontsInInput.add(fontName);
53
57
  };
54
58
 
59
+ sizeInput.setAttribute('type', 'number');
60
+ sizeInput.min = '1';
61
+ sizeInput.max = '128';
62
+
55
63
  fontLabel.innerText = this.localizationTable.fontLabel;
56
64
  colorLabel.innerText = this.localizationTable.colorLabel;
65
+ sizeLabel.innerText = this.localizationTable.textSize;
57
66
 
58
67
  colorInput.id = `${toolbarCSSPrefix}-text-color-input-${TextToolWidget.idCounter++}`;
59
68
  colorLabel.setAttribute('for', colorInput.id);
60
69
 
70
+ sizeInput.id = `${toolbarCSSPrefix}-text-size-input-${TextToolWidget.idCounter++}`;
71
+ sizeLabel.setAttribute('for', sizeInput.id);
72
+
61
73
  addFontToInput('monospace');
62
74
  addFontToInput('serif');
63
75
  addFontToInput('sans-serif');
@@ -68,12 +80,22 @@ export default class TextToolWidget extends BaseToolWidget {
68
80
  this.tool.setFontFamily(fontInput.value);
69
81
  };
70
82
 
83
+ sizeInput.onchange = () => {
84
+ const size = parseInt(sizeInput.value);
85
+ if (!isNaN(size) && size > 0) {
86
+ this.tool.setFontSize(size);
87
+ }
88
+ };
89
+
71
90
  colorRow.appendChild(colorLabel);
72
91
  colorRow.appendChild(colorInputContainer);
73
92
 
74
93
  fontRow.appendChild(fontLabel);
75
94
  fontRow.appendChild(fontInput);
76
95
 
96
+ sizeRow.appendChild(sizeLabel);
97
+ sizeRow.appendChild(sizeInput);
98
+
77
99
  this.updateDropdownInputs = () => {
78
100
  const style = this.tool.getTextStyle();
79
101
  setColorInputValue(style.renderingStyle.fill);
@@ -82,10 +104,11 @@ export default class TextToolWidget extends BaseToolWidget {
82
104
  addFontToInput(style.fontFamily);
83
105
  }
84
106
  fontInput.value = style.fontFamily;
107
+ sizeInput.value = `${style.size}`;
85
108
  };
86
109
  this.updateDropdownInputs();
87
110
 
88
- dropdown.replaceChildren(colorRow, fontRow);
111
+ dropdown.replaceChildren(colorRow, sizeRow, fontRow);
89
112
  return true;
90
113
  }
91
114
 
@@ -96,6 +119,7 @@ export default class TextToolWidget extends BaseToolWidget {
96
119
  ...super.serializeState(),
97
120
 
98
121
  fontFamily: textStyle.fontFamily,
122
+ textSize: textStyle.size,
99
123
  color: textStyle.renderingStyle.fill.toHexString(),
100
124
  };
101
125
  }
@@ -109,6 +133,10 @@ export default class TextToolWidget extends BaseToolWidget {
109
133
  this.tool.setColor(Color4.fromHex(state.color));
110
134
  }
111
135
 
136
+ if (state.textSize && typeof(state.textSize) === 'number') {
137
+ this.tool.setFontSize(state.textSize);
138
+ }
139
+
112
140
  super.deserializeFrom(state);
113
141
  }
114
142
  }
@@ -135,8 +135,11 @@ export default class TextTool extends BaseTool {
135
135
  this.textInputElem.style.width = `${this.textInputElem.scrollWidth}px`;
136
136
  this.textInputElem.style.height = `${this.textInputElem.scrollHeight}px`;
137
137
 
138
+ // Get the ascent based on the font, using a character that is tall in most fonts.
139
+ const tallCharacter = '⎢';
140
+ const ascent = this.getTextAscent(tallCharacter, this.textStyle);
141
+
138
142
  const rotation = this.textRotation + viewport.getRotationAngle();
139
- const ascent = this.getTextAscent(this.textInputElem.value || 'W', this.textStyle);
140
143
  const scale: Mat33 = this.getTextScaleMatrix();
141
144
  this.textInputElem.style.transform = `${scale.toCSSMatrix()} rotate(${rotation * 180 / Math.PI}deg) translate(0, ${-ascent}px)`;
142
145
  this.textInputElem.style.transformOrigin = 'top left';
@@ -208,7 +211,12 @@ export default class TextTool extends BaseTool {
208
211
  const halfTestRegionSize = Vec2.of(2.5, 2.5).times(this.editor.viewport.getSizeOfPixelOnCanvas());
209
212
  const testRegion = Rect2.fromCorners(canvasPos.minus(halfTestRegionSize), canvasPos.plus(halfTestRegionSize));
210
213
  const targetNodes = this.editor.image.getElementsIntersectingRegion(testRegion);
211
- const targetTextNodes = targetNodes.filter(node => node instanceof TextComponent) as TextComponent[];
214
+ let targetTextNodes = targetNodes.filter(node => node instanceof TextComponent) as TextComponent[];
215
+
216
+ // Don't try to edit text nodes that contain the viewport (this allows us
217
+ // to zoom in on text nodes and add text on top of them.)
218
+ const visibleRect = this.editor.viewport.visibleRect;
219
+ targetTextNodes = targetTextNodes.filter(node => !node.getBBox().containsRect(visibleRect));
212
220
 
213
221
  // End any TextNodes we're currently editing.
214
222
  this.flushInput();