MCPcopy
hub / github.com/GrapesJS/grapesjs / setSelected

Method setSelected

packages/core/src/editor/model/Editor.ts:549–628  ·  view source on GitHub ↗

* Select a component * @param {Component} el Component to select * @param {Object} [opts={}] Options, optional * @public

(el?: Component | Component[], opts: SelectComponentOptions = {})

Source from the content-addressed store, hash-verified

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 });

Callers 15

index.tsFile · 0.80
index.tsFile · 0.80
Sectors.tsFile · 0.80
Properties.tsFile · 0.80
ComponentView.tsFile · 0.80
ClassTagsView.tsFile · 0.80
ClassManager.tsFile · 0.80
_pageUpdatedMethod · 0.80
getOnComponentDragEndFunction · 0.80
selectMethod · 0.80
stopMethod · 0.80
__endDragMethod · 0.80

Calls 15

getSelectedAllMethod · 0.95
getConfigMethod · 0.95
removeSelectedMethod · 0.95
triggerMethod · 0.95
toggleSelectedMethod · 0.95
isEditingMethod · 0.95
clearSelectionMethod · 0.95
addSelectedMethod · 0.95
isUndefinedFunction · 0.85
containsFunction · 0.50
selectMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected