* Select a component * @param {Component} el Component to select * @param {Object} [opts={}] Options, optional * @public
(el?: Component | Component[], opts: SelectComponentOptions = {})
| 547 | * @public |
| 548 | */ |
| 549 | setSelected(el?: Component | Component[], opts: SelectComponentOptions = {}) { |
| 550 | const { event } = opts; |
| 551 | const ctrlKey = event && (event.ctrlKey || event.metaKey); |
| 552 | const { shiftKey } = event || {}; |
| 553 | const models = (isArray(el) ? el : [el]) |
| 554 | .map((cmp) => cmp?.delegate?.select?.(cmp) || cmp) |
| 555 | .filter(Boolean) as Component[]; |
| 556 | const selected = this.getSelectedAll(); |
| 557 | const mltSel = this.getConfig().multipleSelection; |
| 558 | const multiple = isArray(el); |
| 559 | |
| 560 | if (multiple || !el) { |
| 561 | this.removeSelected(selected.filter((s) => !contains(models, s))); |
| 562 | } |
| 563 | |
| 564 | models.forEach((model) => { |
| 565 | if (model) { |
| 566 | this.trigger(ComponentsEvents.selectBefore, model, opts); |
| 567 | |
| 568 | // Check for valid selectable |
| 569 | if (!model.get('selectable') || opts.abort) { |
| 570 | if (opts.useValid) { |
| 571 | let parent = model.parent(); |
| 572 | while (parent && !parent.get('selectable')) parent = parent.parent(); |
| 573 | model = parent!; |
| 574 | } else { |
| 575 | return; |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | // Handle multiple selection |
| 581 | if (ctrlKey && mltSel) { |
| 582 | return this.toggleSelected(model); |
| 583 | } else if (shiftKey && mltSel) { |
| 584 | if (this.isEditing()) { |
| 585 | // Fixes #6345 where a shift click while editing text should not assume a selection of a component |
| 586 | return; |
| 587 | } |
| 588 | this.clearSelection(this.Canvas.getWindow()); |
| 589 | const coll = model.collection; |
| 590 | const index = model.index(); |
| 591 | let min: number | undefined, max: number | undefined; |
| 592 | |
| 593 | // Fin min and max siblings |
| 594 | this.getSelectedAll().forEach((sel) => { |
| 595 | const selColl = sel.collection; |
| 596 | const selIndex = sel.index(); |
| 597 | if (selColl === coll) { |
| 598 | if (selIndex < index) { |
| 599 | // First model BEFORE the selected one |
| 600 | min = isUndefined(min) ? selIndex : Math.max(min, selIndex); |
| 601 | } else if (selIndex > index) { |
| 602 | // First model AFTER the selected one |
| 603 | max = isUndefined(max) ? selIndex : Math.min(max, selIndex); |
| 604 | } |
| 605 | } |
| 606 | }); |
no test coverage detected