@rpg-engine/long-bow 0.3.34 → 0.3.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,107 @@
1
+ import { Meta, Story } from '@storybook/react';
2
+ import React, { useEffect, useState } from 'react';
3
+ import { RPGUIRoot } from '../components/RPGUIRoot';
4
+ import {
5
+ defaultSpellShortcut,
6
+ SPELL_SHORTCUTS_STORAGE_KEY,
7
+ } from '../components/Spellbook/constants';
8
+ import { mockSpells } from '../components/Spellbook/mockSpells';
9
+ import { ISpellbookProps, Spellbook } from '../components/Spellbook/Spellbook';
10
+
11
+ const meta: Meta = {
12
+ title: 'Spellbook',
13
+ component: Spellbook,
14
+ };
15
+
16
+ export default meta;
17
+
18
+ const Template: Story<ISpellbookProps> = args => {
19
+ const [spellShortcuts, setSpellShortcuts] = useState([]);
20
+
21
+ useEffect(() => {
22
+ const shortcuts = localStorage.getItem(SPELL_SHORTCUTS_STORAGE_KEY);
23
+ if (shortcuts) {
24
+ setSpellShortcuts(JSON.parse(shortcuts));
25
+ }
26
+ }, []);
27
+
28
+ const setSpellShortcut = (key: string, index: number) => {
29
+ const spellShortcuts = JSON.parse(
30
+ localStorage.getItem(SPELL_SHORTCUTS_STORAGE_KEY) as string
31
+ );
32
+
33
+ const spell = mockSpells.find(s => s.key === key);
34
+ if (!spell) {
35
+ return;
36
+ }
37
+
38
+ if (!spellShortcuts) {
39
+ const newSpellShortcuts = [
40
+ defaultSpellShortcut,
41
+ defaultSpellShortcut,
42
+ defaultSpellShortcut,
43
+ defaultSpellShortcut,
44
+ ];
45
+
46
+ newSpellShortcuts[index] = spell;
47
+
48
+ localStorage.setItem(
49
+ SPELL_SHORTCUTS_STORAGE_KEY,
50
+ JSON.stringify(newSpellShortcuts)
51
+ );
52
+ } else {
53
+ spellShortcuts[index] = spell;
54
+
55
+ localStorage.setItem(
56
+ SPELL_SHORTCUTS_STORAGE_KEY,
57
+ JSON.stringify(spellShortcuts)
58
+ );
59
+ }
60
+
61
+ setSpellShortcuts(spellShortcuts);
62
+ };
63
+
64
+ const removeSpellShortcut = (index: number) => {
65
+ const spellShortcuts = JSON.parse(
66
+ localStorage.getItem(SPELL_SHORTCUTS_STORAGE_KEY) as string
67
+ );
68
+
69
+ if (!spellShortcuts) return;
70
+
71
+ spellShortcuts[index] = defaultSpellShortcut;
72
+
73
+ localStorage.setItem(
74
+ SPELL_SHORTCUTS_STORAGE_KEY,
75
+ JSON.stringify(spellShortcuts)
76
+ );
77
+
78
+ setSpellShortcuts(spellShortcuts);
79
+ };
80
+
81
+ return (
82
+ <RPGUIRoot>
83
+ <div
84
+ style={{
85
+ width: '50%',
86
+ height: '75%',
87
+ }}
88
+ >
89
+ <Spellbook
90
+ {...args}
91
+ setSpellShortcut={setSpellShortcut}
92
+ removeSpellShortcut={removeSpellShortcut}
93
+ spellShortcuts={spellShortcuts}
94
+ />
95
+ </div>
96
+ </RPGUIRoot>
97
+ );
98
+ };
99
+
100
+ export const Default = Template.bind({});
101
+ Default.args = {
102
+ onClose: () => console.log('close'),
103
+ onSpellClick: spellKey => console.log(spellKey),
104
+ spells: mockSpells,
105
+ magicLevel: 3,
106
+ mana: 5,
107
+ };