Skip to content

Commit d5053ed

Browse files
feat(core): Update stored states
1 parent 806bbd9 commit d5053ed

File tree

6 files changed

+56
-2
lines changed

6 files changed

+56
-2
lines changed

packages/documentation/docs/development.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ Internals related:
121121

122122
- `core`
123123

124-
Relates to central behavior of KS
124+
Relates to central behavior of KS. Also includes state management.
125125

126126
- `filters`
127127

packages/documentation/docs/sections/state-management.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Resets every setting to the state that KS would have if you used it for the firs
3434

3535
When you have stored any states, they will appear in this list. Clicking on their name will load them.
3636

37-
You can also copy the state to the clipboard, or delete it. When copying a state, the **Compress data** setting is respected.
37+
You can also sync the stored state with your current settings state, copy the state to the clipboard, or delete it. When copying a state, the **Compress data** setting is respected.
3838

3939
<!-- prettier-ignore-start -->
4040
*[KG]: Kittens Game

packages/userscript/source/i18n/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
"state.confirmDestruction": "Are you sure?",
107107
"state.deleted": "State loaded.",
108108
"state.loaded": "State loaded.",
109+
"state.updated": "State updated.",
109110
"status.auto.disable": "Disable Auto {0}",
110111
"status.auto.enable": "Enable Auto {0}",
111112
"status.ks.disable": "Disabling the Kitten Scientists!",

packages/userscript/source/images/Icons.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ export class Icons {
4444
static readonly Summary =
4545
"M15.45 16.95q.6 0 1.05-.45.45-.45.45-1.05 0-.6-.45-1.05-.45-.45-1.05-.45-.6 0-1.05.45-.45.45-.45 1.05 0 .6.45 1.05.45.45 1.05.45Zm0 8.55q.6 0 1.05-.45.45-.45.45-1.05 0-.6-.45-1.05-.45-.45-1.05-.45-.6 0-1.05.45-.45.45-.45 1.05 0 .6.45 1.05.45.45 1.05.45Zm0 8.55q.6 0 1.05-.45.45-.45.45-1.05 0-.6-.45-1.05-.45-.45-1.05-.45-.6 0-1.05.45-.45.45-.45 1.05 0 .6.45 1.05.45.45 1.05.45ZM9 42q-1.2 0-2.1-.9Q6 40.2 6 39V9q0-1.2.9-2.1Q7.8 6 9 6h23.1l9.9 9.9V39q0 1.2-.9 2.1-.9.9-2.1.9Zm0-3h30V17.55h-8.55V9H9v30ZM9 9v8.55V9v30V9Z";
4646

47+
static readonly Sync =
48+
"M8.35 40v-3h6.5l-.75-.6q-3.2-2.55-4.65-5.55-1.45-3-1.45-6.7 0-5.3 3.125-9.525Q14.25 10.4 19.35 8.8v3.1q-3.75 1.45-6.05 4.825T11 24.15q0 3.15 1.175 5.475 1.175 2.325 3.175 4.025l1.5 1.05v-6.2h3V40Zm20.35-.75V36.1q3.8-1.45 6.05-4.825T37 23.85q0-2.4-1.175-4.875T32.75 14.6l-1.45-1.3v6.2h-3V8h11.5v3h-6.55l.75.7q3 2.8 4.5 6t1.5 6.15q0 5.3-3.1 9.55-3.1 4.25-8.2 5.85Z";
49+
4750
static readonly Time =
4851
"m31.35 33.65 2.25-2.25-7.95-8V13.35h-3V24.6ZM24 44q-4.1 0-7.75-1.575-3.65-1.575-6.375-4.3-2.725-2.725-4.3-6.375Q4 28.1 4 24t1.575-7.75q1.575-3.65 4.3-6.375 2.725-2.725 6.375-4.3Q19.9 4 24 4t7.75 1.575q3.65 1.575 6.375 4.3 2.725 2.725 4.3 6.375Q44 19.9 44 24t-1.575 7.75q-1.575 3.65-4.3 6.375-2.725 2.725-6.375 4.3Q28.1 44 24 44Zm0-20Zm0 17q7 0 12-5t5-12q0-7-5-12T24 7q-7 0-12 5T7 24q0 7 5 12t12 5Z";
4952

packages/userscript/source/ui/StateManagementUi.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { UserScript } from "../UserScript";
99
import { ButtonListItem } from "./components/ButtonListItem";
1010
import { CopyButton } from "./components/buttons-icon/CopyButton";
1111
import { DeleteButton } from "./components/buttons-icon/DeleteButton";
12+
import { UpdateButton } from "./components/buttons-icon/UpdateButton";
1213
import { ExplainerListItem } from "./components/ExplainerListItem";
1314
import { HeaderListItem } from "./components/HeaderListItem";
1415
import { LabelListItem } from "./components/LabelListItem";
@@ -177,6 +178,13 @@ export class StateManagementUi extends SettingsPanel<StateSettings> {
177178
);
178179
listItem.addChild(copyButton);
179180

181+
const updateButton = new UpdateButton(this._host);
182+
updateButton.element.on(
183+
"click",
184+
() => void this.updateState(state, this._host.engine.stateSerialize())
185+
);
186+
listItem.addChild(updateButton);
187+
180188
this.stateList.addChild(listItem);
181189
}
182190
}
@@ -305,6 +313,30 @@ export class StateManagementUi extends SettingsPanel<StateSettings> {
305313
this._host.engine.imessage("state.loaded");
306314
}
307315

316+
updateState(state: StoredState, newState: EngineState) {
317+
if (
318+
!this.setting.noConfirm.enabled &&
319+
!window.confirm(this._host.engine.i18n("state.confirmDestruction"))
320+
) {
321+
return;
322+
}
323+
324+
const index = this.states.indexOf(state);
325+
if (index < 0) {
326+
return;
327+
}
328+
329+
this.states[index] = {
330+
label: state.label,
331+
state: newState,
332+
timestamp: new Date().toISOString(),
333+
};
334+
this._storeStates();
335+
this.refreshUi();
336+
337+
this._host.engine.imessage("state.updated");
338+
}
339+
308340
deleteState(state: StoredState) {
309341
if (
310342
!this.setting.noConfirm.enabled &&
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Icons } from "../../../images/Icons";
2+
import { UserScript } from "../../../UserScript";
3+
import { IconButton } from "../IconButton";
4+
5+
/**
6+
* A button that is intended to initiate an update action.
7+
*/
8+
export class UpdateButton extends IconButton {
9+
/**
10+
* Constructs a `UpdateButton`.
11+
*
12+
* @param host A reference to the host.
13+
* @param title The `title` of the button.
14+
*/
15+
constructor(host: UserScript, title = "Update") {
16+
super(host, Icons.Sync, title);
17+
}
18+
}

0 commit comments

Comments
 (0)