Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UI] Improve drag-and-drop support in APL editor #1273

Merged
merged 3 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ui/core/components/individual_sim_ui/apl_values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ export function valueListFieldConfig(field: string): AplHelpers.APLPickerBuilder
index: number,
config: ListItemPickerConfig<Player<any>, APLValue | undefined>,
) => new APLValuePicker(parent, player, config),
allowedActions: ['create', 'delete'],
allowedActions: ['copy', 'create', 'delete', 'move'],
actions: {
create: {
useIcon: true,
Expand Down
244 changes: 185 additions & 59 deletions ui/core/components/pickers/list_picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,24 @@ export class ListPicker<ModObject, ItemType> extends Input<ModObject, Array<Item
private actionEnabled(action: ListItemAction): boolean {
return !this.config.allowedActions || this.config.allowedActions.includes(action);
}

private addHoverListeners(button: HTMLButtonElement) {
button.addEventListener(
'mouseenter',
() => {
button.classList.add('hover');
},
{ signal: this.signal },
);

button.addEventListener(
'mouseleave',
() => {
button.classList.remove('hover');
},
{ signal: this.signal },
);
}

private addNewPicker() {
const index = this.itemPickerPairs.length;
Expand All @@ -178,7 +196,13 @@ export class ListPicker<ModObject, ItemType> extends Input<ModObject, Array<Item

const itemHeader = document.createElement('div');
itemHeader.classList.add('list-picker-item-header');


const popover = document.createElement('div');
popover.classList.add('list-picker-item-popover');
popover.setAttribute('popover', 'auto');
itemHeader.appendChild(popover);
let hasActions = false;

if (this.config.inlineMenuBar) {
itemContainer.appendChild(itemElem);
itemContainer.appendChild(itemHeader);
Expand All @@ -205,9 +229,65 @@ export class ListPicker<ModObject, ItemType> extends Input<ModObject, Array<Item

const item: ItemPickerPair<ItemType> = { elem: itemContainer, picker: itemPicker, idx: index };

if (this.actionEnabled('delete')) {
if (!this.config.minimumItems || index + 1 > this.config.minimumItems) {
hasActions = true;
const deleteButton = ListPicker.makeActionElem('list-picker-item-delete', 'fa-times');
deleteButton.classList.add('link-danger');
popover.appendChild(deleteButton);

const deleteButtonTooltip = tippy(deleteButton, {
allowHTML: false,
content: `Delete ${this.config.itemLabel}`,
});

deleteButton.addEventListener(
'click',
() => {
const newList = this.config.getValue(this.modObject);
newList.splice(index, 1);
this.config.setValue(TypedEvent.nextEventID(), this.modObject, newList);
deleteButtonTooltip.hide();
},
{ signal: this.signal },
);
this.addOnDisposeCallback(() => deleteButtonTooltip?.destroy());
this.addHoverListeners(deleteButton);
}
}

if (this.actionEnabled('copy')) {
hasActions = true;
const copyButton = ListPicker.makeActionElem('list-picker-item-copy', 'fa-copy');
popover.appendChild(copyButton);
const copyButtonTooltip = tippy(copyButton, {
allowHTML: false,
content: `Copy to New ${this.config.itemLabel}`,
});

copyButton.addEventListener(
'click',
() => {
const newList = this.config.getValue(this.modObject).slice();
newList.splice(index, 0, this.config.copyItem(newList[index]));
this.config.setValue(TypedEvent.nextEventID(), this.modObject, newList);
copyButtonTooltip.hide();
},
{ signal: this.signal },
);
this.addOnDisposeCallback(() => copyButtonTooltip?.destroy());
this.addHoverListeners(copyButton);
}

if (this.actionEnabled('move')) {
hasActions = true;
itemContainer.classList.add('draggable');
if (this.config.itemLabel) {
itemContainer.classList.add(this.config.itemLabel.toLowerCase().replace(' ', '-'));
}

const moveButton = ListPicker.makeActionElem('list-picker-item-move', 'fa-arrows-up-down');
itemHeader.appendChild(moveButton);
popover.appendChild(moveButton);

const moveButtonTooltip = tippy(moveButton, {
allowHTML: false,
Expand All @@ -224,12 +304,33 @@ export class ListPicker<ModObject, ItemType> extends Input<ModObject, Array<Item
this.addOnDisposeCallback(() => {
moveButtonTooltip?.destroy();
});

this.addHoverListeners(moveButton);

moveButton.addEventListener(
'mousedown',
() => {
moveButton.setAttribute('draggable', 'true');
itemContainer.setAttribute('draggable', 'true');
},
{ signal: this.signal },
);

moveButton.addEventListener(
'mouseup',
() => {
moveButton.removeAttribute('draggable');
itemContainer.removeAttribute('draggable');
},
{ signal: this.signal },
);

moveButton.draggable = true;
moveButton.addEventListener(
'dragstart',
event => {
if (event.target == moveButton) {
const popoverRect = popover.getBoundingClientRect();
event.dataTransfer!.setDragImage(itemContainer, popoverRect.width, popoverRect.height / 2);
event.dataTransfer!.dropEffect = 'move';
event.dataTransfer!.effectAllowed = 'move';
itemContainer.classList.add('dragfrom');
Expand All @@ -241,15 +342,21 @@ export class ListPicker<ModObject, ItemType> extends Input<ModObject, Array<Item
},
{ signal: this.signal },
);

const invalidDropTarget = () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is what I meant :D

return !curDragData ||
curDragData.listPicker.config.itemLabel !== this.config.itemLabel ||
(this.config.itemLabel === 'Action' && curDragData.listPicker !== this);
};

let dragEnterCounter = 0;
itemContainer.addEventListener(
'dragenter',
event => {
if (!curDragData || curDragData.listPicker != this) {
if (invalidDropTarget()) {
return;
}
event.preventDefault();
event.stopPropagation();
dragEnterCounter++;
itemContainer.classList.add('dragto');
},
Expand All @@ -259,7 +366,7 @@ export class ListPicker<ModObject, ItemType> extends Input<ModObject, Array<Item
itemContainer.addEventListener(
'dragleave',
event => {
if (!curDragData || curDragData.listPicker != this) {
if (invalidDropTarget()) {
return;
}
event.preventDefault();
Expand All @@ -274,30 +381,69 @@ export class ListPicker<ModObject, ItemType> extends Input<ModObject, Array<Item
itemContainer.addEventListener(
'dragover',
event => {
if (!curDragData || curDragData.listPicker != this) {
if (invalidDropTarget()) {
if (curDragData && this.config.itemLabel === 'Action' && curDragData.listPicker !== this) {
event.dataTransfer!.dropEffect = 'none';
}
return;
}
event.dataTransfer!.dropEffect = 'move';
event.stopPropagation();
event.preventDefault();
},
{ signal: this.signal },
);

const cleanupAfterDrag = () => {
if (!curDragData) {
return;
}
moveButton.removeAttribute('draggable');
itemContainer.removeAttribute('draggable');
curDragData.item.elem.removeAttribute('draggable');
[...document.querySelectorAll('.dragfrom,.dragto')].forEach(elem => {
elem.classList.remove('dragfrom');
elem.classList.remove('dragto');
});
}

itemContainer.addEventListener(
'dragend',
event => {
if (invalidDropTarget()) {
return;
}
event.stopPropagation();
cleanupAfterDrag();
curDragData = null;
},
{ signal: this.signal },
);

itemContainer.addEventListener(
'drop',
event => {
if (!curDragData || curDragData.listPicker != this) {
if (!curDragData || curDragData.listPicker.config.itemLabel !== this.config.itemLabel) {
return;
}
event.preventDefault();
dragEnterCounter = 0;
itemContainer.classList.remove('dragto');
curDragData.item.elem.classList.remove('dragfrom');
event.stopPropagation();
cleanupAfterDrag();

const srcIdx = curDragData.item.idx;
const dstIdx = index;
const newList = this.config.getValue(this.modObject);
const arrElem = newList[srcIdx];
newList.splice(srcIdx, 1);
let arrElem;

if (curDragData.listPicker !== this) {
const oldList = curDragData.listPicker.config.getValue(curDragData.listPicker.modObject);
arrElem = oldList[srcIdx];
oldList.splice(srcIdx, 1);
curDragData.listPicker.config.setValue(TypedEvent.nextEventID(), curDragData.listPicker.modObject, oldList);
} else {
arrElem = newList[srcIdx];
newList.splice(srcIdx, 1);
}

newList.splice(dstIdx, 0, arrElem);
this.config.setValue(TypedEvent.nextEventID(), this.modObject, newList);

Expand All @@ -306,51 +452,31 @@ export class ListPicker<ModObject, ItemType> extends Input<ModObject, Array<Item
{ signal: this.signal },
);
}

if (this.actionEnabled('copy')) {
const copyButton = ListPicker.makeActionElem('list-picker-item-copy', 'fa-copy');
itemHeader.appendChild(copyButton);
const copyButtonTooltip = tippy(copyButton, {
allowHTML: false,
content: `Copy to New ${this.config.itemLabel}`,
});

copyButton.addEventListener(
'click',

if (hasActions) {
const actionsButton = ListPicker.makeActionElem('list-picker-item-actions', 'fa-ellipsis')
itemHeader.appendChild(actionsButton);
actionsButton.addEventListener(
'mouseover',
() => {
const newList = this.config.getValue(this.modObject).slice();
newList.splice(index, 0, this.config.copyItem(newList[index]));
this.config.setValue(TypedEvent.nextEventID(), this.modObject, newList);
copyButtonTooltip.hide();
popover.showPopover();
const actionsButtonRect = actionsButton.getBoundingClientRect();
const popoverRect = popover.getBoundingClientRect();
const diff = (popoverRect.height - actionsButtonRect.height) / 2;
popover.style.top = (actionsButtonRect.top - diff) + 'px';
popover.style.left = (actionsButtonRect.right - popoverRect.width + 10) + 'px';
popover.classList.add('hover')
},
{ signal: this.signal },
);
this.addOnDisposeCallback(() => copyButtonTooltip?.destroy());
}

if (this.actionEnabled('delete')) {
if (!this.config.minimumItems || index + 1 > this.config.minimumItems) {
const deleteButton = ListPicker.makeActionElem('list-picker-item-delete', 'fa-times');
deleteButton.classList.add('link-danger');
itemHeader.appendChild(deleteButton);

const deleteButtonTooltip = tippy(deleteButton, {
allowHTML: false,
content: `Delete ${this.config.itemLabel}`,
});

deleteButton.addEventListener(
'click',
() => {
const newList = this.config.getValue(this.modObject);
newList.splice(index, 1);
this.config.setValue(TypedEvent.nextEventID(), this.modObject, newList);
deleteButtonTooltip.hide();
},
{ signal: this.signal },
);
this.addOnDisposeCallback(() => deleteButtonTooltip?.destroy());
}
{ signal: this.signal }
)
popover.addEventListener(
'mouseleave',
() => {
popover.classList.remove('hover');
popover.hidePopover();
},
{ signal: this.signal }
)
}

this.itemPickerPairs.push(item);
Expand Down Expand Up @@ -410,9 +536,9 @@ export class ListPicker<ModObject, ItemType> extends Input<ModObject, Array<Item
validationTooltip.setContent('');
const validations = getValidations(player);
if (!validations.length) {
validationElem.style.visibility = 'hidden';
validationElem.style.display = 'none';
} else {
validationElem.style.visibility = 'visible';
validationElem.style.removeProperty('display');
const formattedValidations = await Promise.all(
validations.map(async w => {
return { ...w, validation: await ActionId.replaceAllInString(w.validation) };
Expand Down
24 changes: 23 additions & 1 deletion ui/scss/core/components/_list_picker.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@

.list-picker-item-container {
border: var(--border-default);

&.dragfrom {
background-color: color-mix(in srgb, var(--bs-body-bg) 80%, transparent);
filter: opacity(0.5);
cursor: move;
}

&.draggable:has(> .list-picker-item-header .list-picker-item-popover.hover) {
background-color: color-mix(in srgb, var(--bs-primary) 5%, transparent);
}

&:not(:last-child) {
margin-bottom: var(--spacer-3);
Expand All @@ -32,7 +42,6 @@
padding: 0;
border: 0;
margin: 0;
flex: 0;
}
}

Expand Down Expand Up @@ -60,6 +69,19 @@

.list-picker-item-action {
margin-left: var(--spacer-2);

&.list-picker-item-move {
cursor: move;
}
}

.list-picker-item-popover:popover-open {
inset: unset;
position: relative;
background-color: color-mix(in srgb, var(--bs-body-bg) 80%, transparent);
border: 1px solid black;
border-radius: 5px;
padding: 5px 10px;
}
}
}
Expand Down
Loading
Loading