Update the sidebar tab rearranger to use Drag and Drop (again)
This commit is contained in:
parent
60034dcee2
commit
e28901dcf2
6 changed files with 213 additions and 18 deletions
|
|
@ -90,6 +90,7 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"apps": {
|
"apps": {
|
||||||
|
"discard-changes": "Discard Changes",
|
||||||
"no-settings-to-display": "No settings to display",
|
"no-settings-to-display": "No settings to display",
|
||||||
"make-global-reference": "Make Global Reference",
|
"make-global-reference": "Make Global Reference",
|
||||||
"StatusEffectIconConfig": {
|
"StatusEffectIconConfig": {
|
||||||
|
|
@ -99,7 +100,9 @@
|
||||||
"select-using-image-tagger": "Select using Image Tagger"
|
"select-using-image-tagger": "Select using Image Tagger"
|
||||||
},
|
},
|
||||||
"SidebarTabRearranger": {
|
"SidebarTabRearranger": {
|
||||||
"title": "Rearrange Sidebar Tabs"
|
"title": "Rearrange Sidebar Tabs",
|
||||||
|
"top": "Top",
|
||||||
|
"bottom": "Bottom"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"notifs": {
|
"notifs": {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
import { __ID__, filePath } from "../consts.mjs";
|
import { __ID__, filePath } from "../consts.mjs";
|
||||||
|
import { performArraySort } from "../utils/performArraySort.mjs";
|
||||||
|
|
||||||
const { HandlebarsApplicationMixin, ApplicationV2 } = foundry.applications.api;
|
const { HandlebarsApplicationMixin, ApplicationV2 } = foundry.applications.api;
|
||||||
|
const { DragDrop } = foundry.applications.ux;
|
||||||
const { getDocumentClass } = foundry.utils;
|
const { getDocumentClass } = foundry.utils;
|
||||||
|
|
||||||
export class SidebarTabRearranger extends HandlebarsApplicationMixin(ApplicationV2) {
|
export class SidebarTabRearranger extends HandlebarsApplicationMixin(ApplicationV2) {
|
||||||
|
|
@ -20,7 +22,8 @@ export class SidebarTabRearranger extends HandlebarsApplicationMixin(Application
|
||||||
closeOnSubmit: true,
|
closeOnSubmit: true,
|
||||||
submitOnChange: false,
|
submitOnChange: false,
|
||||||
},
|
},
|
||||||
actions: {},
|
actions: {
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
static PARTS = {
|
static PARTS = {
|
||||||
|
|
@ -30,9 +33,33 @@ export class SidebarTabRearranger extends HandlebarsApplicationMixin(Application
|
||||||
// #endregion Options
|
// #endregion Options
|
||||||
|
|
||||||
// #region Instance Data
|
// #region Instance Data
|
||||||
|
#order = [];
|
||||||
|
|
||||||
|
constructor(...args) {
|
||||||
|
super(...args);
|
||||||
|
|
||||||
|
// TODO: define this using the game settings
|
||||||
|
this.#order = Object.keys(ui.sidebar.constructor.TABS);
|
||||||
|
};
|
||||||
// #endregion Instance Data
|
// #endregion Instance Data
|
||||||
|
|
||||||
// #region Lifecycle
|
// #region Lifecycle
|
||||||
|
async _onRender(...args) {
|
||||||
|
await super._onRender(...args);
|
||||||
|
|
||||||
|
new DragDrop.implementation({
|
||||||
|
dragSelector: `.tab`,
|
||||||
|
dropSelector: `.drop-zone`,
|
||||||
|
callbacks: {
|
||||||
|
dragstart: this.#onDragStart.bind(this),
|
||||||
|
dragenter: this.#onDragEnter.bind(this),
|
||||||
|
dragleave: this.#onDragLeave.bind(this),
|
||||||
|
dragend: this.#onDragEnd.bind(this),
|
||||||
|
drop: this.#onDrop.bind(this),
|
||||||
|
},
|
||||||
|
}).bind(this.element);
|
||||||
|
};
|
||||||
|
|
||||||
/** @this {SidebarTabRearranger} */
|
/** @this {SidebarTabRearranger} */
|
||||||
static async #onSubmit() {};
|
static async #onSubmit() {};
|
||||||
// #endregion Lifecycle
|
// #endregion Lifecycle
|
||||||
|
|
@ -47,7 +74,11 @@ export class SidebarTabRearranger extends HandlebarsApplicationMixin(Application
|
||||||
|
|
||||||
const tabs = ui.sidebar.constructor.TABS;
|
const tabs = ui.sidebar.constructor.TABS;
|
||||||
ctx.tabs = [];
|
ctx.tabs = [];
|
||||||
for (const [id, tab] of Object.entries(tabs)) {
|
for (let i = 0; i < this.#order.length; i++) {
|
||||||
|
const id = this.#order[i];
|
||||||
|
const tab = tabs[id];
|
||||||
|
if (!tab) { continue };
|
||||||
|
|
||||||
let { documentName, gmOnly, tooltip, icon } = tab;
|
let { documentName, gmOnly, tooltip, icon } = tab;
|
||||||
if (gmOnly && !game.user.isGM) { continue };
|
if (gmOnly && !game.user.isGM) { continue };
|
||||||
|
|
||||||
|
|
@ -60,6 +91,7 @@ export class SidebarTabRearranger extends HandlebarsApplicationMixin(Application
|
||||||
id,
|
id,
|
||||||
name: game.i18n.localize(tooltip),
|
name: game.i18n.localize(tooltip),
|
||||||
icon,
|
icon,
|
||||||
|
nextIndex: i + 1,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -69,4 +101,64 @@ export class SidebarTabRearranger extends HandlebarsApplicationMixin(Application
|
||||||
|
|
||||||
// #region Actions
|
// #region Actions
|
||||||
// #endregion Actions
|
// #endregion Actions
|
||||||
|
|
||||||
|
// #region Drag & Drop
|
||||||
|
#onDragStart(event) {
|
||||||
|
/** @type {HTMLLIElement|undefined} */
|
||||||
|
const target = event.target.closest(`[data-tab-id]`);
|
||||||
|
if (!target) { return };
|
||||||
|
|
||||||
|
const tabID = target.dataset.tabId;
|
||||||
|
|
||||||
|
target.classList.add(`no-hover-styles`);
|
||||||
|
event.dataTransfer.setDragImage(target, 0, 0);
|
||||||
|
event.dataTransfer.setData(`oft/tab`, tabID);
|
||||||
|
target.closest(`.tab-list`)?.classList.add(`dragging`);
|
||||||
|
|
||||||
|
/*
|
||||||
|
This timeout is required to get the difference between the drag
|
||||||
|
image and the element in-DOM, because this puts the class removal
|
||||||
|
in a subsequent event cycle instead of being handled in the current
|
||||||
|
cycle.
|
||||||
|
*/
|
||||||
|
setTimeout(() => target.classList.remove(`no-hover-styles`), 0);
|
||||||
|
};
|
||||||
|
|
||||||
|
#onDragEnter(event) {
|
||||||
|
event.currentTarget.style.setProperty(`--colour`, `#00aa00`);
|
||||||
|
};
|
||||||
|
|
||||||
|
#onDragLeave(event) {
|
||||||
|
event.currentTarget.style.removeProperty(`--colour`);
|
||||||
|
};
|
||||||
|
|
||||||
|
#onDragEnd() {
|
||||||
|
this.element.querySelector(`.tab-list`)?.classList.remove(`dragging`);
|
||||||
|
this.element
|
||||||
|
.querySelectorAll(`[style="--colour: #00aa00"]`)
|
||||||
|
.forEach(el => el.style.removeProperty(`--colour`));
|
||||||
|
};
|
||||||
|
|
||||||
|
#onDrop(event) {
|
||||||
|
const droppedID = event.dataTransfer.getData(`oft/tab`);
|
||||||
|
this.element.querySelector(`.tab-list`)?.classList.remove(`dragging`);
|
||||||
|
event.currentTarget?.style?.removeProperty(`--colour`);
|
||||||
|
if (!droppedID) { return };
|
||||||
|
|
||||||
|
const droppedIndex = this.#order.findIndex(t => t === droppedID);
|
||||||
|
const dropTarget = event.currentTarget;
|
||||||
|
const targetIndex = parseInt(dropTarget?.dataset.moveToIndex);
|
||||||
|
if (
|
||||||
|
!dropTarget
|
||||||
|
|| droppedIndex < 0
|
||||||
|
|| targetIndex === droppedIndex
|
||||||
|
) { return };
|
||||||
|
|
||||||
|
this.#order = performArraySort(
|
||||||
|
droppedID,
|
||||||
|
{ targetIndex, list: this.#order },
|
||||||
|
);
|
||||||
|
this.render({ parts: [`list`] });
|
||||||
|
};
|
||||||
|
// #endregion Drag & Drop
|
||||||
};
|
};
|
||||||
|
|
|
||||||
31
module/utils/performArraySort.mjs
Normal file
31
module/utils/performArraySort.mjs
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
export function performArraySort(
|
||||||
|
element,
|
||||||
|
{ list, targetIndex },
|
||||||
|
) {
|
||||||
|
|
||||||
|
// Case: same position
|
||||||
|
if (list.indexOf(el => el === element) === targetIndex) {
|
||||||
|
return Array.from(list);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Case: start of array
|
||||||
|
if (targetIndex === 0) {
|
||||||
|
list = list.filter(el => el !== element);
|
||||||
|
return [element, ...list];
|
||||||
|
};
|
||||||
|
|
||||||
|
// Case: end of array
|
||||||
|
if (targetIndex === list.length - 1) {
|
||||||
|
list = list.filter(el => el !== element);
|
||||||
|
return [...list, element];
|
||||||
|
};
|
||||||
|
|
||||||
|
// Case: middle of array
|
||||||
|
const front = list
|
||||||
|
.slice(0, targetIndex)
|
||||||
|
.filter(el => el !== element);
|
||||||
|
const back = list
|
||||||
|
.slice(targetIndex)
|
||||||
|
.filter(el => el !== element);
|
||||||
|
return [...front, element, ...back];
|
||||||
|
};
|
||||||
|
|
@ -1,19 +1,55 @@
|
||||||
.oft.SidebarTabRearranger {
|
.oft.SidebarTabRearranger {
|
||||||
ol {
|
> .window-content {
|
||||||
display: flex;
|
gap: 16px;
|
||||||
flex-direction: column;
|
}
|
||||||
|
|
||||||
|
footer {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(2, 1fr);
|
||||||
gap: 8px;
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.drop-zone {
|
||||||
|
--colour: transparent;
|
||||||
|
background: color-mix(in srgb, var(--colour) 30%, transparent 70%);
|
||||||
|
border: 1px solid var(--colour);
|
||||||
|
border-radius: 4px;
|
||||||
|
transition: all 150ms ease-in-out;
|
||||||
|
width: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tab-list {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
row-gap: 8px;
|
||||||
|
column-gap: 2px;
|
||||||
list-style-type: none;
|
list-style-type: none;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
|
|
||||||
|
&.dragging > .drop-zone {
|
||||||
|
--colour: #c9593f;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
li {
|
.tab {
|
||||||
|
margin: 0;
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: row;
|
flex-direction: column;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
gap: 12px;
|
|
||||||
cursor: var(--cursor-grab);
|
cursor: var(--cursor-grab);
|
||||||
|
|
||||||
|
> .drag-handle-icon {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
&:hover:not(.no-hover-styles) {
|
||||||
|
> .sidebar-icon {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
> .drag-handle-icon {
|
||||||
|
display: initial;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.emulate-button {
|
.emulate-button {
|
||||||
|
|
@ -26,4 +62,8 @@
|
||||||
border: 1px solid var(--color-light-5);
|
border: 1px solid var(--color-light-5);
|
||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.bottom-label {
|
||||||
|
text-align: right;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,14 @@
|
||||||
<footer>
|
<footer>
|
||||||
<button>Submit</button>
|
<button type="close">
|
||||||
|
{{ localize "OFT.apps.discard-changes" }}
|
||||||
|
</button>
|
||||||
|
<button>
|
||||||
|
<oft-icon
|
||||||
|
name="icons/save"
|
||||||
|
aria-hidden="true"
|
||||||
|
var:fill="currentColor"
|
||||||
|
var:size="1rem"
|
||||||
|
></oft-icon>
|
||||||
|
{{ localize "Save Changes" }}
|
||||||
|
</button>
|
||||||
</footer>
|
</footer>
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,33 @@
|
||||||
<main>
|
<main>
|
||||||
<ol>
|
<div class="top-label">
|
||||||
|
{{ localize "OFT.apps.SidebarTabRearranger.top" }}
|
||||||
|
</div>
|
||||||
|
<div class="tab-list" data-tooltip-direction="UP">
|
||||||
|
<div
|
||||||
|
class="drop-zone"
|
||||||
|
data-move-to-index="0"
|
||||||
|
></div>
|
||||||
{{#each tabs as | tab |}}
|
{{#each tabs as | tab |}}
|
||||||
<li>
|
<div
|
||||||
<span class="emulate-button {{tab.icon}}"></span>
|
class="emulate-button tab"
|
||||||
<span>{{tab.name}}</span>
|
data-tab-id="{{tab.id}}"
|
||||||
<div style="flex-grow: 1"></div>
|
data-tooltip="{{tab.name}}"
|
||||||
|
>
|
||||||
|
<span class="sidebar-icon {{tab.icon}}"></span>
|
||||||
<oft-icon
|
<oft-icon
|
||||||
|
class="drag-handle-icon"
|
||||||
name="icons/drag-handle"
|
name="icons/drag-handle"
|
||||||
var:fill="currentColor"
|
var:fill="color-mix(in srgb, currentColor 75%, black 25%)"
|
||||||
|
var:size="1rem"
|
||||||
></oft-icon>
|
></oft-icon>
|
||||||
</li>
|
</div>
|
||||||
|
<div
|
||||||
|
class="drop-zone"
|
||||||
|
data-move-to-index="{{tab.nextIndex}}"
|
||||||
|
></div>
|
||||||
{{/each}}
|
{{/each}}
|
||||||
</ol>
|
</div>
|
||||||
|
<div class="bottom-label">
|
||||||
|
{{ localize "OFT.apps.SidebarTabRearranger.bottom" }}
|
||||||
|
</div>
|
||||||
</main>
|
</main>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue