-
Notifications
You must be signed in to change notification settings - Fork 237
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
[#5156] Add dialog for splitting items in inventory into two stacks #5157
Open
arbron
wants to merge
1
commit into
4.3.x
Choose a base branch
from
split-stack
base: 4.3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+210
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* Version of the default range picker that has number inputs on both sides. | ||
*/ | ||
export default class DoubleRangePickerElement extends foundry.applications.elements.HTMLRangePickerElement { | ||
constructor() { | ||
super(); | ||
this.#min = Number(this.getAttribute("min")) ?? 0; | ||
this.#max = Number(this.getAttribute("max")) ?? 1; | ||
} | ||
|
||
/** @override */ | ||
static tagName = "double-range-picker"; | ||
|
||
/** | ||
* The range input. | ||
* @type {HTMLInputElement} | ||
*/ | ||
#rangeInput; | ||
|
||
/** | ||
* The left number input. | ||
* @type {HTMLInputElement} | ||
*/ | ||
#leftInput; | ||
|
||
/** | ||
* The right number input. | ||
* @type {HTMLInputElement} | ||
*/ | ||
#rightInput; | ||
|
||
/** | ||
* The minimum allowed value for the range. | ||
* @type {number} | ||
*/ | ||
#min; | ||
|
||
/** | ||
* The maximum allowed value for the range. | ||
* @type {number} | ||
*/ | ||
#max; | ||
|
||
/* -------------------------------------------- */ | ||
|
||
/** @inheritDoc */ | ||
_buildElements() { | ||
const [range, right] = super._buildElements(); | ||
this.#rangeInput = range; | ||
this.#rightInput = right; | ||
this.#leftInput = this.#rightInput.cloneNode(); | ||
return [this.#leftInput, this.#rangeInput, this.#rightInput]; | ||
} | ||
|
||
/* -------------------------------------------- */ | ||
|
||
/** @inheritDoc */ | ||
_refresh() { | ||
super._refresh(); | ||
if ( !this.#rangeInput ) return; | ||
this.#leftInput.valueAsNumber = this.#max - this.#rightInput.valueAsNumber + this.#min; | ||
} | ||
|
||
/* -------------------------------------------- */ | ||
|
||
/** @inheritDoc */ | ||
_activateListeners() { | ||
super._activateListeners(); | ||
this.#rangeInput.addEventListener("input", this.#onDragSlider.bind(this)); | ||
this.#leftInput.addEventListener("change", this.#onChangeInput.bind(this)); | ||
} | ||
|
||
/* -------------------------------------------- */ | ||
|
||
/** | ||
* Update display of the number input as the range slider is actively changed. | ||
* @param {InputEvent} event The originating input event | ||
*/ | ||
#onDragSlider(event) { | ||
event.preventDefault(); | ||
this.#leftInput.valueAsNumber = this.#max - this.#rangeInput.valueAsNumber + this.#min; | ||
} | ||
|
||
/* -------------------------------------------- */ | ||
|
||
/** | ||
* Handle changes to the left input. | ||
* @param {InputEvent} event The originating input change event | ||
*/ | ||
#onChangeInput(event) { | ||
event.stopPropagation(); | ||
this.value = this.#max - event.currentTarget.valueAsNumber + this.#min; | ||
} | ||
|
||
/* -------------------------------------------- */ | ||
|
||
/** @override */ | ||
_toggleDisabled(disabled) { | ||
super._toggleDisabled(disabled); | ||
this.#leftInput.toggleAttribute("disabled", disabled); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import Dialog5e from "../api/dialog.mjs"; | ||
|
||
/** | ||
* Small dialog for splitting a stack of items into two. | ||
*/ | ||
export default class SplitStackDialog extends Dialog5e { | ||
/** @override */ | ||
static DEFAULT_OPTIONS = { | ||
buttons: [{ | ||
id: "split", | ||
label: "DND5E.SplitStack.Action", | ||
icon: "fa-solid fa-arrows-split-up-and-left" | ||
}], | ||
classes: ["split-stack"], | ||
document: null, | ||
form: { | ||
handler: SplitStackDialog.#handleFormSubmission | ||
}, | ||
position: { | ||
width: 400 | ||
}, | ||
window: { | ||
title: "DND5E.SplitStack.Title" | ||
} | ||
}; | ||
|
||
/* -------------------------------------------- */ | ||
|
||
/** @inheritDoc */ | ||
static PARTS = { | ||
...super.PARTS, | ||
content: { | ||
template: "systems/dnd5e/templates/apps/split-stack-dialog.hbs" | ||
} | ||
}; | ||
|
||
/* -------------------------------------------- */ | ||
/* Rendering */ | ||
/* -------------------------------------------- */ | ||
|
||
/** @override */ | ||
async _prepareContentContext(context, options) { | ||
const total = this.options.document.system.quantity ?? 1; | ||
context.max = Math.max(1, total - 1); | ||
context.left = Math.ceil(total / 2); | ||
context.right = total - context.left; | ||
return context; | ||
} | ||
|
||
/* -------------------------------------------- */ | ||
/* Form Handling */ | ||
/* -------------------------------------------- */ | ||
|
||
/** | ||
* Handle submission of the dialog. | ||
* @this {SplitStackDialog} | ||
* @param {Event|SubmitEvent} event The form submission event. | ||
* @param {HTMLFormElement} form The submitted form. | ||
* @param {FormDataExtended} formData Data from the dialog. | ||
*/ | ||
static async #handleFormSubmission(event, form, formData) { | ||
const right = formData.object.right ?? 0; | ||
const left = (this.options.document.system.quantity ?? 1) - right; | ||
if ( left === this.options.document.system.quantity ) return; | ||
await this.options.document.update({ "system.quantity": left }, { render: false }); | ||
await this.options.document.clone({ "system.quantity": right }, { addSource: true, save: true }); | ||
this.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<section class="flexrow"> | ||
<double-range-picker name="right" value="{{ right }}" min="1" max="{{ max }}" step="1"></double-range-picker> | ||
</section> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
render: false
to prevent the actor sheet rendering twice? This would prevent the item sheet rendering too, though. So maybe justPromise.all
like is done with advancements? (Or swap the order of operations here.)