Skip to content

Commit

Permalink
Fix save prompt on new song
Browse files Browse the repository at this point in the history
  • Loading branch information
tillvit committed Dec 15, 2024
1 parent 165011f commit 1497e38
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
17 changes: 9 additions & 8 deletions app/src/data/SMPropertiesData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ type SMPropertyGroupData = {
title: string
items: SMPropertyData[]
}

// we have an extra history since we don't want new song prompt to interfere with the current one
type SMPropertyCustomInput = {
type: "custom"
create: (app: App, sm: Simfile) => HTMLElement
create: (app: App, sm: Simfile, history: ActionHistory) => HTMLElement
}
type SMPropertyStringInput = {
type: "string"
Expand Down Expand Up @@ -148,7 +148,7 @@ export const SM_PROPERTIES_DATA: SMPropertyGroupData[] = [
propName: "SAMPLESTART",
input: {
type: "custom",
create: (_, sm) => {
create: (_, sm, history) => {
const updateValues = () => {
if (toSpinner.value < fromSpinner.value) {
toSpinner.setValue(fromSpinner.value)
Expand All @@ -157,7 +157,7 @@ export const SM_PROPERTIES_DATA: SMPropertyGroupData[] = [
const lastLength = sm.properties.SAMPLELENGTH ?? "10"
const newStart = fromSpinner.value.toString()
const newLength = (toSpinner.value - fromSpinner.value).toString()
ActionHistory.instance.run({
history.run({
action: () => {
sm.properties.SAMPLESTART = newStart
sm.properties.SAMPLELENGTH = newLength
Expand Down Expand Up @@ -225,11 +225,12 @@ export const SM_PROPERTIES_DATA: SMPropertyGroupData[] = [
export function createInputElement(
app: App,
sm: Simfile,
history: ActionHistory,
data: SMPropertyData
) {
switch (data.input.type) {
case "custom":
return data.input.create(app, sm)
return data.input.create(app, sm, history)
case "string": {
const input = document.createElement("input")
input.type = "text"
Expand All @@ -241,7 +242,7 @@ export function createInputElement(
input.onblur = () => {
const lastValue = sm.properties[data.propName]
const newValue = input.value
ActionHistory.instance.run({
history.run({
action: () => {
sm.properties[data.propName] = newValue
input.value = newValue
Expand Down Expand Up @@ -271,7 +272,7 @@ export function createInputElement(
}
const lastValue = sm.properties[data.propName]
const newValue = value.toString()
ActionHistory.instance.run({
history.run({
action: () => {
sm.properties[data.propName] = newValue
spinner.setValue(parseFloat(newValue))
Expand Down Expand Up @@ -349,7 +350,7 @@ export function createInputElement(

const setValue = (value: string | undefined) => {
const lastValue = sm.properties[data.propName] ?? ""
ActionHistory.instance.run({
history.run({
action: () => {
sm.properties[data.propName] = value
input.value = value ?? ""
Expand Down
8 changes: 7 additions & 1 deletion app/src/gui/window/NewSongWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
SM_PROPERTIES_DATA,
createInputElement,
} from "../../data/SMPropertiesData"
import { ActionHistory } from "../../util/ActionHistory"
import { FileHandler } from "../../util/file-handler/FileHandler"
import { Icons } from "../Icons"
import { ConfirmationWindow } from "./ConfirmationWindow"
Expand All @@ -15,6 +16,7 @@ export class NewSongWindow extends Window {
app: App

private readonly sm: Simfile
private readonly history
private fileTable: { [key: string]: File } = {}

constructor(app: App) {
Expand All @@ -31,6 +33,7 @@ export class NewSongWindow extends Window {
const blob = new Blob([DEFAULT_SM], { type: "text/plain" })
const file = new File([blob], "song.sm", { type: "text/plain" })
this.sm = new Simfile(file)
this.history = new ActionHistory(app)
this.app = app
this.initView()
}
Expand Down Expand Up @@ -66,7 +69,10 @@ export class NewSongWindow extends Window {
grid.appendChild(
this.createFileElement(item.propName, item.input.typeName)
)
else grid.appendChild(createInputElement(this.app, this.sm, item))
else
grid.appendChild(
createInputElement(this.app, this.sm, this.history, item)
)
})
groupContainer.appendChild(title)
groupContainer.appendChild(grid)
Expand Down
8 changes: 7 additions & 1 deletion app/src/gui/window/SMPropertiesWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
SM_PROPERTIES_DATA,
createInputElement,
} from "../../data/SMPropertiesData"
import { ActionHistory } from "../../util/ActionHistory"
import { EventHandler } from "../../util/EventHandler"
import { Window } from "./Window"

Expand Down Expand Up @@ -61,7 +62,12 @@ export class SMPropertiesWindow extends Window {

grid.appendChild(label)
grid.appendChild(
createInputElement(this.app, this.app.chartManager.loadedSM!, item)
createInputElement(
this.app,
this.app.chartManager.loadedSM!,
ActionHistory.instance,
item
)
)
})
groupContainer.appendChild(title)
Expand Down

0 comments on commit 1497e38

Please sign in to comment.