js-draw 0.15.0 → 0.15.1
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.
- package/.github/ISSUE_TEMPLATE/translation.yml +16 -0
- package/CHANGELOG.md +6 -0
- package/dist/bundle.js +1 -1
- package/dist/src/components/AbstractComponent.js +12 -6
- package/dist/src/tools/SelectionTool/SelectionTool.js +3 -1
- package/package.json +1 -1
- package/src/components/AbstractComponent.transformBy.test.ts +22 -0
- package/src/components/AbstractComponent.ts +10 -3
- package/src/toolbar/toolbar.css +7 -0
- package/src/tools/SelectionTool/SelectionTool.ts +2 -0
@@ -80,7 +80,7 @@ export default class AbstractComponent {
|
|
80
80
|
}
|
81
81
|
// Returns a command that updates this component's z-index.
|
82
82
|
setZIndex(newZIndex) {
|
83
|
-
return new AbstractComponent.TransformElementCommand(Mat33.identity, this.getId(), this, newZIndex);
|
83
|
+
return new AbstractComponent.TransformElementCommand(Mat33.identity, this.getId(), this, newZIndex, this.getZIndex());
|
84
84
|
}
|
85
85
|
// @returns true iff this component can be selected (e.g. by the selection tool.)
|
86
86
|
isSelectable() {
|
@@ -163,22 +163,26 @@ AbstractComponent.TransformElementCommand = (_a = class extends UnresolvedSerial
|
|
163
163
|
// Construct a new TransformElementCommand. `component`, while optional, should
|
164
164
|
// be provided if available. If not provided, it will be fetched from the editor's
|
165
165
|
// document when the command is applied.
|
166
|
-
constructor(affineTransfm, componentID, component, targetZIndex) {
|
166
|
+
constructor(affineTransfm, componentID, component, targetZIndex, origZIndex) {
|
167
167
|
super(AbstractComponent.transformElementCommandId, componentID, component);
|
168
168
|
this.affineTransfm = affineTransfm;
|
169
|
-
this.origZIndex =
|
169
|
+
this.origZIndex = origZIndex;
|
170
170
|
this.targetZIndex = targetZIndex !== null && targetZIndex !== void 0 ? targetZIndex : AbstractComponent.zIndexCounter++;
|
171
171
|
// Ensure that we keep drawing on top even after changing the z-index.
|
172
172
|
if (this.targetZIndex >= AbstractComponent.zIndexCounter) {
|
173
173
|
AbstractComponent.zIndexCounter = this.targetZIndex + 1;
|
174
174
|
}
|
175
|
+
if (component && origZIndex === undefined) {
|
176
|
+
this.origZIndex = component.getZIndex();
|
177
|
+
}
|
175
178
|
}
|
176
179
|
resolveComponent(image) {
|
180
|
+
var _a;
|
177
181
|
if (this.component) {
|
178
182
|
return;
|
179
183
|
}
|
180
184
|
super.resolveComponent(image);
|
181
|
-
this.origZIndex = this.component.getZIndex();
|
185
|
+
(_a = this.origZIndex) !== null && _a !== void 0 ? _a : (this.origZIndex = this.component.getZIndex());
|
182
186
|
}
|
183
187
|
updateTransform(editor, newTransfm) {
|
184
188
|
if (!this.component) {
|
@@ -218,16 +222,18 @@ AbstractComponent.TransformElementCommand = (_a = class extends UnresolvedSerial
|
|
218
222
|
id: this.componentID,
|
219
223
|
transfm: this.affineTransfm.toArray(),
|
220
224
|
targetZIndex: this.targetZIndex,
|
225
|
+
origZIndex: this.origZIndex,
|
221
226
|
};
|
222
227
|
}
|
223
228
|
},
|
224
229
|
(() => {
|
225
230
|
SerializableCommand.register(AbstractComponent.transformElementCommandId, (json, editor) => {
|
226
|
-
var _a;
|
231
|
+
var _a, _b;
|
227
232
|
const elem = (_a = editor.image.lookupElement(json.id)) !== null && _a !== void 0 ? _a : undefined;
|
228
233
|
const transform = new Mat33(...json.transfm);
|
229
234
|
const targetZIndex = json.targetZIndex;
|
230
|
-
|
235
|
+
const origZIndex = (_b = json.origZIndex) !== null && _b !== void 0 ? _b : undefined;
|
236
|
+
return new AbstractComponent.TransformElementCommand(transform, json.id, elem, targetZIndex, origZIndex);
|
231
237
|
});
|
232
238
|
})(),
|
233
239
|
_a);
|
@@ -158,7 +158,7 @@ export default class SelectionTool extends BaseTool {
|
|
158
158
|
}
|
159
159
|
}
|
160
160
|
onGestureCancel() {
|
161
|
-
var _a, _b, _c;
|
161
|
+
var _a, _b, _c, _d;
|
162
162
|
if (this.selectionBoxHandlingEvt) {
|
163
163
|
(_a = this.selectionBox) === null || _a === void 0 ? void 0 : _a.onDragCancel();
|
164
164
|
}
|
@@ -167,6 +167,8 @@ export default class SelectionTool extends BaseTool {
|
|
167
167
|
(_b = this.selectionBox) === null || _b === void 0 ? void 0 : _b.cancelSelection();
|
168
168
|
this.selectionBox = this.prevSelectionBox;
|
169
169
|
(_c = this.selectionBox) === null || _c === void 0 ? void 0 : _c.addTo(this.handleOverlay);
|
170
|
+
(_d = this.selectionBox) === null || _d === void 0 ? void 0 : _d.recomputeRegion();
|
171
|
+
this.prevSelectionBox = null;
|
170
172
|
}
|
171
173
|
this.expandingSelectionBox = false;
|
172
174
|
}
|
package/package.json
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
import { Color4, EditorImage, Mat33, Path, Rect2, Vec2 } from '../lib';
|
2
|
+
import createEditor from '../testing/createEditor';
|
3
|
+
import Stroke from './Stroke';
|
4
|
+
|
5
|
+
describe('AbstractComponent.transformBy', () => {
|
6
|
+
it('should restore the component\'s z-index on undo', () => {
|
7
|
+
const editor = createEditor();
|
8
|
+
const component = new Stroke([ Path.fromRect(Rect2.unitSquare).toRenderable({ fill: Color4.red }) ]);
|
9
|
+
EditorImage.addElement(component).apply(editor);
|
10
|
+
|
11
|
+
const origZIndex = component.getZIndex();
|
12
|
+
|
13
|
+
const transformCommand = component.transformBy(Mat33.translation(Vec2.unitX));
|
14
|
+
transformCommand.apply(editor);
|
15
|
+
|
16
|
+
// Should increase the z-index on applying a transform
|
17
|
+
expect(component.getZIndex()).toBeGreaterThan(origZIndex);
|
18
|
+
|
19
|
+
transformCommand.unapply(editor);
|
20
|
+
expect(component.getZIndex()).toBe(origZIndex);
|
21
|
+
});
|
22
|
+
});
|
@@ -138,7 +138,7 @@ export default abstract class AbstractComponent {
|
|
138
138
|
|
139
139
|
// Returns a command that updates this component's z-index.
|
140
140
|
public setZIndex(newZIndex: number): SerializableCommand {
|
141
|
-
return new AbstractComponent.TransformElementCommand(Mat33.identity, this.getId(), this, newZIndex);
|
141
|
+
return new AbstractComponent.TransformElementCommand(Mat33.identity, this.getId(), this, newZIndex, this.getZIndex());
|
142
142
|
}
|
143
143
|
|
144
144
|
// @returns true iff this component can be selected (e.g. by the selection tool.)
|
@@ -156,7 +156,6 @@ export default abstract class AbstractComponent {
|
|
156
156
|
private static transformElementCommandId = 'transform-element';
|
157
157
|
|
158
158
|
private static TransformElementCommand = class extends UnresolvedSerializableCommand {
|
159
|
-
private origZIndex: number|null = null;
|
160
159
|
private targetZIndex: number;
|
161
160
|
|
162
161
|
// Construct a new TransformElementCommand. `component`, while optional, should
|
@@ -167,6 +166,7 @@ export default abstract class AbstractComponent {
|
|
167
166
|
componentID: string,
|
168
167
|
component?: AbstractComponent,
|
169
168
|
targetZIndex?: number,
|
169
|
+
private origZIndex?: number,
|
170
170
|
) {
|
171
171
|
super(AbstractComponent.transformElementCommandId, componentID, component);
|
172
172
|
this.targetZIndex = targetZIndex ?? AbstractComponent.zIndexCounter++;
|
@@ -175,6 +175,10 @@ export default abstract class AbstractComponent {
|
|
175
175
|
if (this.targetZIndex >= AbstractComponent.zIndexCounter) {
|
176
176
|
AbstractComponent.zIndexCounter = this.targetZIndex + 1;
|
177
177
|
}
|
178
|
+
|
179
|
+
if (component && origZIndex === undefined) {
|
180
|
+
this.origZIndex = component.getZIndex();
|
181
|
+
}
|
178
182
|
}
|
179
183
|
|
180
184
|
protected resolveComponent(image: EditorImage): void {
|
@@ -183,7 +187,7 @@ export default abstract class AbstractComponent {
|
|
183
187
|
}
|
184
188
|
|
185
189
|
super.resolveComponent(image);
|
186
|
-
this.origZIndex
|
190
|
+
this.origZIndex ??= this.component!.getZIndex();
|
187
191
|
}
|
188
192
|
|
189
193
|
private updateTransform(editor: Editor, newTransfm: Mat33) {
|
@@ -233,12 +237,14 @@ export default abstract class AbstractComponent {
|
|
233
237
|
const elem = editor.image.lookupElement(json.id) ?? undefined;
|
234
238
|
const transform = new Mat33(...(json.transfm as Mat33Array));
|
235
239
|
const targetZIndex = json.targetZIndex;
|
240
|
+
const origZIndex = json.origZIndex ?? undefined;
|
236
241
|
|
237
242
|
return new AbstractComponent.TransformElementCommand(
|
238
243
|
transform,
|
239
244
|
json.id,
|
240
245
|
elem,
|
241
246
|
targetZIndex,
|
247
|
+
origZIndex,
|
242
248
|
);
|
243
249
|
});
|
244
250
|
}
|
@@ -248,6 +254,7 @@ export default abstract class AbstractComponent {
|
|
248
254
|
id: this.componentID,
|
249
255
|
transfm: this.affineTransfm.toArray(),
|
250
256
|
targetZIndex: this.targetZIndex,
|
257
|
+
origZIndex: this.origZIndex,
|
251
258
|
};
|
252
259
|
}
|
253
260
|
};
|
package/src/toolbar/toolbar.css
CHANGED
@@ -125,6 +125,13 @@
|
|
125
125
|
padding: 15px;
|
126
126
|
padding-top: 5px;
|
127
127
|
|
128
|
+
display: flex;
|
129
|
+
flex-wrap: wrap;
|
130
|
+
flex-direction: column;
|
131
|
+
max-height: 80vh;
|
132
|
+
|
133
|
+
max-width: fit-content;
|
134
|
+
|
128
135
|
/* Prevent overlap/being displayed under the undo/redo buttons */
|
129
136
|
z-index: 2;
|
130
137
|
background-color: var(--primary-background-color);
|
@@ -200,6 +200,8 @@ export default class SelectionTool extends BaseTool {
|
|
200
200
|
this.selectionBox?.cancelSelection();
|
201
201
|
this.selectionBox = this.prevSelectionBox;
|
202
202
|
this.selectionBox?.addTo(this.handleOverlay);
|
203
|
+
this.selectionBox?.recomputeRegion();
|
204
|
+
this.prevSelectionBox = null;
|
203
205
|
}
|
204
206
|
|
205
207
|
this.expandingSelectionBox = false;
|