@rohal12/spindle 0.16.1 → 0.17.0
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.
- package/dist/pkg/format.js +1 -1
- package/package.json +1 -1
- package/src/action-registry.ts +2 -1
- package/src/components/PassageDialog.tsx +69 -0
- package/src/components/macros/Button.tsx +33 -46
- package/src/components/macros/Checkbox.tsx +32 -35
- package/src/components/macros/Computed.tsx +49 -54
- package/src/components/macros/Cycle.tsx +43 -53
- package/src/components/macros/Dialog.tsx +40 -0
- package/src/components/macros/Do.tsx +15 -21
- package/src/components/macros/For.tsx +57 -75
- package/src/components/macros/Goto.tsx +18 -22
- package/src/components/macros/If.tsx +39 -53
- package/src/components/macros/Include.tsx +30 -46
- package/src/components/macros/Listbox.tsx +38 -48
- package/src/components/macros/MacroError.tsx +18 -0
- package/src/components/macros/MacroLink.tsx +36 -51
- package/src/components/macros/MenubarButtons.tsx +158 -0
- package/src/components/macros/Meter.tsx +35 -52
- package/src/components/macros/Numberbox.tsx +30 -47
- package/src/components/macros/PassageDisplay.tsx +26 -29
- package/src/components/macros/Print.tsx +19 -36
- package/src/components/macros/Radiobutton.tsx +33 -38
- package/src/components/macros/Repeat.tsx +49 -54
- package/src/components/macros/SaveManager.tsx +400 -0
- package/src/components/macros/Set.tsx +19 -24
- package/src/components/{SettingsDialog.tsx → macros/SettingsControls.tsx} +18 -36
- package/src/components/macros/Stop.tsx +11 -12
- package/src/components/macros/StoryTitle.tsx +16 -16
- package/src/components/macros/Switch.tsx +41 -59
- package/src/components/macros/Textarea.tsx +26 -45
- package/src/components/macros/Textbox.tsx +27 -44
- package/src/components/macros/Timed.tsx +52 -55
- package/src/components/macros/Type.tsx +61 -62
- package/src/components/macros/Unset.tsx +21 -22
- package/src/components/macros/Widget.tsx +11 -15
- package/src/components/macros/option-utils.ts +13 -0
- package/src/define-macro.ts +154 -0
- package/src/macros/register-builtins.ts +4 -7
- package/src/markup/ast.ts +10 -16
- package/src/markup/render.tsx +0 -13
- package/src/story-api.ts +7 -0
- package/src/styles.css +37 -68
- package/src/components/PassageLink.tsx +0 -49
- package/src/components/SaveLoadDialog.tsx +0 -408
- package/src/components/macros/Back.tsx +0 -32
- package/src/components/macros/Forward.tsx +0 -34
- package/src/components/macros/QuickLoad.tsx +0 -42
- package/src/components/macros/QuickSave.tsx +0 -30
- package/src/components/macros/Restart.tsx +0 -35
- package/src/components/macros/Saves.tsx +0 -24
- package/src/components/macros/SettingsButton.tsx +0 -28
|
@@ -1,48 +1,29 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { useAction } from '../../hooks/use-action';
|
|
3
|
-
import { registerMacro } from '../../registry';
|
|
4
|
-
import type { MacroProps } from '../../registry';
|
|
1
|
+
import { defineMacro } from '../../define-macro';
|
|
5
2
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const varName = match[1]!.replace(/["']/g, '');
|
|
12
|
-
const placeholder = match[2] || '';
|
|
13
|
-
return { varName, placeholder };
|
|
14
|
-
}
|
|
3
|
+
defineMacro({
|
|
4
|
+
name: 'textarea',
|
|
5
|
+
storeVar: true,
|
|
6
|
+
render({ rawArgs }, ctx) {
|
|
7
|
+
const { placeholder } = ctx.parseVarArgs(rawArgs);
|
|
15
8
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
9
|
+
ctx.useAction({
|
|
10
|
+
type: 'textarea',
|
|
11
|
+
key: `$${ctx.varName}`,
|
|
12
|
+
authorId: ctx.id,
|
|
13
|
+
label: placeholder || ctx.varName!,
|
|
14
|
+
variable: ctx.varName,
|
|
15
|
+
value: ctx.value,
|
|
16
|
+
perform: (v) => ctx.setValue!(v !== undefined ? String(v) : ''),
|
|
17
|
+
});
|
|
19
18
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
const cls = className ? `macro-textarea ${className}` : 'macro-textarea';
|
|
34
|
-
|
|
35
|
-
return (
|
|
36
|
-
<textarea
|
|
37
|
-
id={id}
|
|
38
|
-
class={cls}
|
|
39
|
-
value={value == null ? '' : String(value)}
|
|
40
|
-
placeholder={placeholder}
|
|
41
|
-
onInput={(e) =>
|
|
42
|
-
setVariable(name, (e.target as HTMLTextAreaElement).value)
|
|
43
|
-
}
|
|
44
|
-
/>
|
|
45
|
-
);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
registerMacro('textarea', Textarea);
|
|
19
|
+
return (
|
|
20
|
+
<textarea
|
|
21
|
+
id={ctx.id}
|
|
22
|
+
class={ctx.cls}
|
|
23
|
+
value={ctx.value == null ? '' : String(ctx.value)}
|
|
24
|
+
placeholder={placeholder}
|
|
25
|
+
onInput={(e) => ctx.setValue!((e.target as HTMLTextAreaElement).value)}
|
|
26
|
+
/>
|
|
27
|
+
);
|
|
28
|
+
},
|
|
29
|
+
});
|
|
@@ -1,47 +1,30 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { useAction } from '../../hooks/use-action';
|
|
3
|
-
import { registerMacro } from '../../registry';
|
|
4
|
-
import type { MacroProps } from '../../registry';
|
|
1
|
+
import { defineMacro } from '../../define-macro';
|
|
5
2
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const varName = match[1]!.replace(/["']/g, '');
|
|
12
|
-
const placeholder = match[2] || '';
|
|
13
|
-
return { varName, placeholder };
|
|
14
|
-
}
|
|
3
|
+
defineMacro({
|
|
4
|
+
name: 'textbox',
|
|
5
|
+
storeVar: true,
|
|
6
|
+
render({ rawArgs }, ctx) {
|
|
7
|
+
const { placeholder } = ctx.parseVarArgs(rawArgs);
|
|
15
8
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
9
|
+
ctx.useAction({
|
|
10
|
+
type: 'textbox',
|
|
11
|
+
key: `$${ctx.varName}`,
|
|
12
|
+
authorId: ctx.id,
|
|
13
|
+
label: placeholder || ctx.varName!,
|
|
14
|
+
variable: ctx.varName,
|
|
15
|
+
value: ctx.value,
|
|
16
|
+
perform: (v) => ctx.setValue!(v !== undefined ? String(v) : ''),
|
|
17
|
+
});
|
|
19
18
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const cls = className ? `macro-textbox ${className}` : 'macro-textbox';
|
|
34
|
-
|
|
35
|
-
return (
|
|
36
|
-
<input
|
|
37
|
-
type="text"
|
|
38
|
-
id={id}
|
|
39
|
-
class={cls}
|
|
40
|
-
value={value == null ? '' : String(value)}
|
|
41
|
-
placeholder={placeholder}
|
|
42
|
-
onInput={(e) => setVariable(name, (e.target as HTMLInputElement).value)}
|
|
43
|
-
/>
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
registerMacro('textbox', Textbox);
|
|
19
|
+
return (
|
|
20
|
+
<input
|
|
21
|
+
type="text"
|
|
22
|
+
id={ctx.id}
|
|
23
|
+
class={ctx.cls}
|
|
24
|
+
value={ctx.value == null ? '' : String(ctx.value)}
|
|
25
|
+
placeholder={placeholder}
|
|
26
|
+
onInput={(e) => ctx.setValue!((e.target as HTMLInputElement).value)}
|
|
27
|
+
/>
|
|
28
|
+
);
|
|
29
|
+
},
|
|
30
|
+
});
|
|
@@ -1,56 +1,53 @@
|
|
|
1
|
-
import { useState, useEffect, useMemo } from 'preact/hooks';
|
|
2
|
-
import { renderNodes } from '../../markup/render';
|
|
3
1
|
import { parseDelay } from '../../utils/parse-delay';
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
registerSubMacro('next');
|
|
2
|
+
import { defineMacro } from '../../define-macro';
|
|
3
|
+
|
|
4
|
+
defineMacro({
|
|
5
|
+
name: 'timed',
|
|
6
|
+
subMacros: ['next'],
|
|
7
|
+
interpolate: true,
|
|
8
|
+
render({ branches = [] }, ctx) {
|
|
9
|
+
const { useState, useEffect, useMemo } = ctx.hooks;
|
|
10
|
+
|
|
11
|
+
const sections = useMemo(() => {
|
|
12
|
+
return branches.map((branch) => ({
|
|
13
|
+
delay: branch.rawArgs ? parseDelay(branch.rawArgs) : 0,
|
|
14
|
+
nodes: branch.children,
|
|
15
|
+
className: branch.className,
|
|
16
|
+
id: branch.id,
|
|
17
|
+
}));
|
|
18
|
+
}, [branches]);
|
|
19
|
+
|
|
20
|
+
const [visibleIndex, setVisibleIndex] = useState(-1);
|
|
21
|
+
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
if (visibleIndex >= sections.length - 1) return;
|
|
24
|
+
|
|
25
|
+
const nextIndex = visibleIndex + 1;
|
|
26
|
+
const delay = sections[nextIndex]!.delay;
|
|
27
|
+
|
|
28
|
+
const timer = setTimeout(() => {
|
|
29
|
+
setVisibleIndex(nextIndex);
|
|
30
|
+
}, delay);
|
|
31
|
+
|
|
32
|
+
return () => clearTimeout(timer);
|
|
33
|
+
}, [visibleIndex, sections]);
|
|
34
|
+
|
|
35
|
+
if (visibleIndex < 0) return null;
|
|
36
|
+
|
|
37
|
+
const section = sections[visibleIndex]!;
|
|
38
|
+
const content = ctx.renderNodes(section.nodes);
|
|
39
|
+
const sectionClass = ctx.resolve!(section.className);
|
|
40
|
+
const sectionId = ctx.resolve!(section.id);
|
|
41
|
+
|
|
42
|
+
if (sectionClass || sectionId)
|
|
43
|
+
return (
|
|
44
|
+
<span
|
|
45
|
+
id={sectionId}
|
|
46
|
+
class={sectionClass}
|
|
47
|
+
>
|
|
48
|
+
{content}
|
|
49
|
+
</span>
|
|
50
|
+
);
|
|
51
|
+
return <>{content}</>;
|
|
52
|
+
},
|
|
53
|
+
});
|
|
@@ -1,76 +1,75 @@
|
|
|
1
|
-
import { useState, useEffect, useRef } from 'preact/hooks';
|
|
2
|
-
import { renderInlineNodes } from '../../markup/render';
|
|
3
1
|
import { parseDelay } from '../../utils/parse-delay';
|
|
4
|
-
import {
|
|
5
|
-
import { registerMacro } from '../../registry';
|
|
6
|
-
import type { MacroProps } from '../../registry';
|
|
2
|
+
import { defineMacro } from '../../define-macro';
|
|
7
3
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
4
|
+
defineMacro({
|
|
5
|
+
name: 'type',
|
|
6
|
+
interpolate: true,
|
|
7
|
+
render({ rawArgs, children = [] }, ctx) {
|
|
8
|
+
const { useState, useEffect, useRef } = ctx.hooks;
|
|
9
|
+
const speed = parseDelay(rawArgs);
|
|
10
|
+
const containerRef = useRef<HTMLSpanElement>(null);
|
|
11
|
+
const [totalChars, setTotalChars] = useState(0);
|
|
12
|
+
const [visibleChars, setVisibleChars] = useState(0);
|
|
16
13
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
}, []);
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
if (containerRef.current) {
|
|
16
|
+
const text = containerRef.current.textContent || '';
|
|
17
|
+
setTotalChars(text.length);
|
|
18
|
+
}
|
|
19
|
+
}, []);
|
|
24
20
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
21
|
+
// Typewriter interval
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
if (totalChars === 0) return;
|
|
24
|
+
if (visibleChars >= totalChars) return;
|
|
29
25
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
26
|
+
const timer = setInterval(() => {
|
|
27
|
+
setVisibleChars((c) => {
|
|
28
|
+
if (c >= totalChars) {
|
|
29
|
+
clearInterval(timer);
|
|
30
|
+
return c;
|
|
31
|
+
}
|
|
32
|
+
return c + 1;
|
|
33
|
+
});
|
|
34
|
+
}, speed);
|
|
39
35
|
|
|
40
|
-
|
|
41
|
-
|
|
36
|
+
return () => clearInterval(timer);
|
|
37
|
+
}, [totalChars, speed]);
|
|
42
38
|
|
|
43
|
-
|
|
39
|
+
const done = visibleChars >= totalChars && totalChars > 0;
|
|
44
40
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
41
|
+
const cls = [
|
|
42
|
+
'macro-type',
|
|
43
|
+
done ? 'macro-type-done' : '',
|
|
44
|
+
ctx.className || '',
|
|
45
|
+
]
|
|
46
|
+
.filter(Boolean)
|
|
47
|
+
.join(' ');
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
<span
|
|
51
|
-
id={id}
|
|
52
|
-
class={cls}
|
|
53
|
-
ref={containerRef}
|
|
54
|
-
style={{
|
|
55
|
-
clipPath: totalChars > 0 && !done ? undefined : undefined,
|
|
56
|
-
}}
|
|
57
|
-
>
|
|
49
|
+
return (
|
|
58
50
|
<span
|
|
59
|
-
|
|
51
|
+
id={ctx.id}
|
|
52
|
+
class={cls}
|
|
53
|
+
ref={containerRef}
|
|
60
54
|
style={{
|
|
61
|
-
|
|
62
|
-
visibility: totalChars === 0 ? 'hidden' : 'visible',
|
|
63
|
-
clipPath:
|
|
64
|
-
totalChars > 0 && !done
|
|
65
|
-
? `inset(0 ${((totalChars - visibleChars) / totalChars) * 100}% 0 0)`
|
|
66
|
-
: undefined,
|
|
55
|
+
clipPath: totalChars > 0 && !done ? undefined : undefined,
|
|
67
56
|
}}
|
|
68
57
|
>
|
|
69
|
-
|
|
58
|
+
<span
|
|
59
|
+
class="macro-type-inner"
|
|
60
|
+
style={{
|
|
61
|
+
display: 'inline',
|
|
62
|
+
visibility: totalChars === 0 ? 'hidden' : 'visible',
|
|
63
|
+
clipPath:
|
|
64
|
+
totalChars > 0 && !done
|
|
65
|
+
? `inset(0 ${((totalChars - visibleChars) / totalChars) * 100}% 0 0)`
|
|
66
|
+
: undefined,
|
|
67
|
+
}}
|
|
68
|
+
>
|
|
69
|
+
{ctx.renderInlineNodes(children)}
|
|
70
|
+
</span>
|
|
71
|
+
{!done && totalChars > 0 && <span class="macro-type-cursor" />}
|
|
70
72
|
</span>
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
registerMacro('type', Type);
|
|
73
|
+
);
|
|
74
|
+
},
|
|
75
|
+
});
|
|
@@ -1,28 +1,27 @@
|
|
|
1
|
-
import { useRef } from 'preact/hooks';
|
|
2
1
|
import { useStoryStore } from '../../store';
|
|
3
|
-
import {
|
|
4
|
-
import type { MacroProps } from '../../registry';
|
|
2
|
+
import { defineMacro } from '../../define-macro';
|
|
5
3
|
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
defineMacro({
|
|
5
|
+
name: 'unset',
|
|
6
|
+
render({ rawArgs }, ctx) {
|
|
7
|
+
const ran = ctx.hooks.useRef(false);
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
if (!ran.current) {
|
|
10
|
+
ran.current = true;
|
|
11
|
+
const state = useStoryStore.getState();
|
|
12
|
+
const name = rawArgs.trim();
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
14
|
+
if (name.startsWith('$')) {
|
|
15
|
+
state.deleteVariable(name.slice(1));
|
|
16
|
+
} else if (name.startsWith('_')) {
|
|
17
|
+
state.deleteTemporary(name.slice(1));
|
|
18
|
+
} else {
|
|
19
|
+
console.error(
|
|
20
|
+
`spindle: {unset} expects a variable ($name or _name), got "${name}"`,
|
|
21
|
+
);
|
|
22
|
+
}
|
|
22
23
|
}
|
|
23
|
-
}
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
registerMacro('unset', Unset);
|
|
25
|
+
return null;
|
|
26
|
+
},
|
|
27
|
+
});
|
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
import { useLayoutEffect } from 'preact/hooks';
|
|
2
1
|
import { registerWidget } from '../../widgets/widget-registry';
|
|
3
|
-
import {
|
|
4
|
-
import type { MacroProps } from '../../registry';
|
|
2
|
+
import { defineMacro } from '../../define-macro';
|
|
5
3
|
|
|
6
|
-
/**
|
|
7
|
-
* Parse widget definition args: "WidgetName" or "WidgetName" $param1 $param2
|
|
8
|
-
*/
|
|
9
4
|
function parseWidgetDef(rawArgs: string): { name: string; params: string[] } {
|
|
10
5
|
const tokens = rawArgs.trim().split(/\s+/);
|
|
11
6
|
const name = tokens[0]!.replace(/["']/g, '');
|
|
@@ -13,14 +8,15 @@ function parseWidgetDef(rawArgs: string): { name: string; params: string[] } {
|
|
|
13
8
|
return { name, params };
|
|
14
9
|
}
|
|
15
10
|
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
defineMacro({
|
|
12
|
+
name: 'widget',
|
|
13
|
+
render({ rawArgs, children = [] }, ctx) {
|
|
14
|
+
const { name, params } = parseWidgetDef(rawArgs);
|
|
18
15
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
ctx.hooks.useLayoutEffect(() => {
|
|
17
|
+
registerWidget(name, children, params);
|
|
18
|
+
}, []);
|
|
22
19
|
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
registerMacro('widget', Widget);
|
|
20
|
+
return null;
|
|
21
|
+
},
|
|
22
|
+
});
|
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
import type { ASTNode } from '../../markup/ast';
|
|
2
2
|
|
|
3
|
+
export function parseVarArgs(rawArgs: string): {
|
|
4
|
+
varName: string;
|
|
5
|
+
placeholder: string;
|
|
6
|
+
} {
|
|
7
|
+
const match = rawArgs.match(/^\s*(["']?\$\w+["']?)\s*(?:["'](.*)["'])?\s*$/);
|
|
8
|
+
if (!match) {
|
|
9
|
+
return { varName: rawArgs.trim(), placeholder: '' };
|
|
10
|
+
}
|
|
11
|
+
const varName = match[1]!.replace(/["']/g, '');
|
|
12
|
+
const placeholder = match[2] || '';
|
|
13
|
+
return { varName, placeholder };
|
|
14
|
+
}
|
|
15
|
+
|
|
3
16
|
/**
|
|
4
17
|
* Walk AST children to find {option} macro nodes, returning their rawArgs as values.
|
|
5
18
|
*/
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { h, Fragment } from 'preact';
|
|
2
|
+
import type { VNode, ComponentChildren } from 'preact';
|
|
3
|
+
import {
|
|
4
|
+
useContext,
|
|
5
|
+
useState,
|
|
6
|
+
useRef,
|
|
7
|
+
useEffect,
|
|
8
|
+
useLayoutEffect,
|
|
9
|
+
useCallback,
|
|
10
|
+
useMemo,
|
|
11
|
+
} from 'preact/hooks';
|
|
12
|
+
import { useInterpolate } from './hooks/use-interpolate';
|
|
13
|
+
import { useMergedLocals } from './hooks/use-merged-locals';
|
|
14
|
+
import {
|
|
15
|
+
LocalsUpdateContext,
|
|
16
|
+
renderNodes,
|
|
17
|
+
renderInlineNodes,
|
|
18
|
+
} from './markup/render';
|
|
19
|
+
import { executeMutation } from './execute-mutation';
|
|
20
|
+
import { evaluate } from './expression';
|
|
21
|
+
import { useStoryStore } from './store';
|
|
22
|
+
import { useAction } from './hooks/use-action';
|
|
23
|
+
import type { UseActionOptions } from './hooks/use-action';
|
|
24
|
+
import { collectText } from './utils/extract-text';
|
|
25
|
+
import { currentSourceLocation } from './utils/source-location';
|
|
26
|
+
import { parseVarArgs, extractOptions } from './components/macros/option-utils';
|
|
27
|
+
import { registerMacro, registerSubMacro } from './registry';
|
|
28
|
+
import type { MacroProps } from './registry';
|
|
29
|
+
|
|
30
|
+
export function macroClass(type: string, className?: string): string {
|
|
31
|
+
const base = `macro-${type}`;
|
|
32
|
+
return className ? `${base} ${className}` : base;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface MacroContext {
|
|
36
|
+
className?: string;
|
|
37
|
+
id?: string;
|
|
38
|
+
resolve?: (s: string | undefined) => string | undefined;
|
|
39
|
+
cls: string;
|
|
40
|
+
mutate: (code: string) => void;
|
|
41
|
+
update: (key: string, value: unknown) => void;
|
|
42
|
+
getValues: () => Record<string, unknown>;
|
|
43
|
+
merged?: readonly [
|
|
44
|
+
Record<string, unknown>,
|
|
45
|
+
Record<string, unknown>,
|
|
46
|
+
Record<string, unknown>,
|
|
47
|
+
];
|
|
48
|
+
varName?: string;
|
|
49
|
+
value?: unknown;
|
|
50
|
+
setValue?: (value: unknown) => void;
|
|
51
|
+
evaluate?: (expr: string) => unknown;
|
|
52
|
+
collectText: typeof collectText;
|
|
53
|
+
sourceLocation: typeof currentSourceLocation;
|
|
54
|
+
parseVarArgs: typeof parseVarArgs;
|
|
55
|
+
extractOptions: typeof extractOptions;
|
|
56
|
+
wrap: (content: ComponentChildren) => VNode<any>;
|
|
57
|
+
useAction: (opts: UseActionOptions) => string;
|
|
58
|
+
h: typeof h;
|
|
59
|
+
renderNodes: typeof renderNodes;
|
|
60
|
+
renderInlineNodes: typeof renderInlineNodes;
|
|
61
|
+
hooks: {
|
|
62
|
+
useState: typeof useState;
|
|
63
|
+
useRef: typeof useRef;
|
|
64
|
+
useEffect: typeof useEffect;
|
|
65
|
+
useLayoutEffect: typeof useLayoutEffect;
|
|
66
|
+
useCallback: typeof useCallback;
|
|
67
|
+
useMemo: typeof useMemo;
|
|
68
|
+
useContext: typeof useContext;
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface MacroDefinition {
|
|
73
|
+
name: string;
|
|
74
|
+
subMacros?: string[];
|
|
75
|
+
interpolate?: boolean;
|
|
76
|
+
merged?: boolean;
|
|
77
|
+
storeVar?: boolean;
|
|
78
|
+
render: (props: MacroProps, ctx: MacroContext) => VNode | null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const sharedHooks = {
|
|
82
|
+
useState,
|
|
83
|
+
useRef,
|
|
84
|
+
useEffect,
|
|
85
|
+
useLayoutEffect,
|
|
86
|
+
useCallback,
|
|
87
|
+
useMemo,
|
|
88
|
+
useContext,
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export function defineMacro(config: MacroDefinition): void {
|
|
92
|
+
function Wrapper(props: MacroProps) {
|
|
93
|
+
// className/id resolved first (interpolate may transform them)
|
|
94
|
+
let className = props.className;
|
|
95
|
+
let id = props.id;
|
|
96
|
+
let resolve: ((s: string | undefined) => string | undefined) | undefined;
|
|
97
|
+
if (config.interpolate) {
|
|
98
|
+
resolve = useInterpolate();
|
|
99
|
+
className = resolve(className);
|
|
100
|
+
id = resolve(id);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Always-on: cssClass + mutation
|
|
104
|
+
const { update, getValues } = useContext(LocalsUpdateContext);
|
|
105
|
+
const ctx: MacroContext = {
|
|
106
|
+
collectText,
|
|
107
|
+
sourceLocation: currentSourceLocation,
|
|
108
|
+
parseVarArgs,
|
|
109
|
+
extractOptions,
|
|
110
|
+
h,
|
|
111
|
+
renderNodes,
|
|
112
|
+
renderInlineNodes,
|
|
113
|
+
hooks: sharedHooks,
|
|
114
|
+
useAction,
|
|
115
|
+
className,
|
|
116
|
+
id,
|
|
117
|
+
resolve,
|
|
118
|
+
cls: macroClass(config.name, className),
|
|
119
|
+
mutate: (code: string) => executeMutation(code, getValues(), update),
|
|
120
|
+
update,
|
|
121
|
+
getValues,
|
|
122
|
+
wrap: undefined as any,
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
if (config.merged) {
|
|
126
|
+
ctx.merged = useMergedLocals();
|
|
127
|
+
const merged = ctx.merged;
|
|
128
|
+
ctx.evaluate = (expr: string) =>
|
|
129
|
+
evaluate(expr, merged[0], merged[1], merged[2]);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (config.storeVar) {
|
|
133
|
+
const firstToken = props.rawArgs.trim().split(/[\s"']+/)[0] ?? '';
|
|
134
|
+
const varName = firstToken.replace(/["']/g, '').replace(/^\$/, '');
|
|
135
|
+
ctx.varName = varName;
|
|
136
|
+
ctx.value = useStoryStore((s) => s.variables[varName]);
|
|
137
|
+
const setVariable = useStoryStore((s) => s.setVariable);
|
|
138
|
+
ctx.setValue = (value: unknown) => setVariable(varName, value);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
ctx.wrap = (content: ComponentChildren): VNode<any> => {
|
|
142
|
+
if (ctx.className || ctx.id)
|
|
143
|
+
return h('span', { id: ctx.id, class: ctx.className }, content);
|
|
144
|
+
return h(Fragment, null, content);
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
return config.render(props, ctx);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
registerMacro(config.name, Wrapper);
|
|
151
|
+
if (config.subMacros) {
|
|
152
|
+
for (const sub of config.subMacros) registerSubMacro(sub);
|
|
153
|
+
}
|
|
154
|
+
}
|