@rohal12/spindle 0.62.7 → 0.62.9
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/dist/pkg/headless.js +15345 -15292
- package/dist/pkg/macro-registry.js +3 -1
- package/dist/pkg/macro-registry.json +3 -1
- package/dist/pkg/story-variables.js +206 -139
- package/package.json +1 -1
- package/src/action-registry.ts +9 -6
- package/src/components/macros/Computed.tsx +5 -3
- package/src/components/macros/Cycle.tsx +1 -0
- package/src/format/format-passage.ts +37 -1
- package/src/headless.ts +4 -3
- package/src/hooks/use-modal-focus.ts +6 -19
- package/src/js-lexer.ts +26 -0
- package/src/markup/ast.ts +5 -1
- package/src/markup/code-attributes.ts +11 -3
- package/src/markup/line-column.ts +3 -8
- package/src/markup/pair.ts +20 -1
- package/src/markup/parse.ts +13 -2
- package/src/markup/render.tsx +5 -5
- package/src/observe/observable-text.ts +20 -8
- package/src/parser.ts +2 -2
- package/src/registry.ts +9 -4
- package/src/story-api.ts +1 -5
- package/src/story-variables.ts +25 -42
- package/src/tooling.ts +16 -5
- package/src/utils/element-visibility.ts +35 -0
- package/src/utils/macro-names.ts +9 -0
- package/src/utils/sorted.ts +11 -0
- package/types/headless.d.ts +2 -2
- package/types/tooling.d.ts +5 -1
|
@@ -30,6 +30,12 @@ var NameMap = class extends Map {
|
|
|
30
30
|
return super.delete(name.toLowerCase());
|
|
31
31
|
}
|
|
32
32
|
};
|
|
33
|
+
/** The latest definition of each name, using runtime replacement precedence. */
|
|
34
|
+
function effectiveNamedDefinitions(definitions) {
|
|
35
|
+
const latest = new NameMap();
|
|
36
|
+
for (const definition of definitions) latest.set(definition.name, definition);
|
|
37
|
+
return [...latest.values()];
|
|
38
|
+
}
|
|
33
39
|
/** A set of macro or widget names (see NameMap). */
|
|
34
40
|
var NameSet = class {
|
|
35
41
|
constructor(names = []) {
|
|
@@ -3630,6 +3636,98 @@ function peg$parse(input, options) {
|
|
|
3630
3636
|
if (peg$success) return peg$result;
|
|
3631
3637
|
else peg$throw();
|
|
3632
3638
|
}
|
|
3639
|
+
new NameMap();
|
|
3640
|
+
new NameMap();
|
|
3641
|
+
new NameSet();
|
|
3642
|
+
/**
|
|
3643
|
+
* How a macro argument is read (see components/macros/macro-args.ts). Every
|
|
3644
|
+
* declared parameter names one: there is no default.
|
|
3645
|
+
* Quoted strings accept `\"`, `\'` and `\\` escapes.
|
|
3646
|
+
* - `expression`: code, as written.
|
|
3647
|
+
* - `statements`: code run as statements (`{set}`), as written.
|
|
3648
|
+
* - `passage`: a passage name: a quoted string or an expression, as written.
|
|
3649
|
+
* - `variable`: a variable reference such as `$name` or `"$name"`, as written.
|
|
3650
|
+
* - `string`: one quoted string; anything else leaves the argument unset.
|
|
3651
|
+
* - `text`: one quoted string, or text with any loose quotes stripped.
|
|
3652
|
+
* - `names`: a comma-separated list of names (`@item, @i`).
|
|
3653
|
+
* - `delay`: a duration (`2s`, `500ms`, `300`) in milliseconds.
|
|
3654
|
+
* - `number`: a number.
|
|
3655
|
+
* - `flag`: a keyword, the parameter's name, at the start or end; a boolean.
|
|
3656
|
+
* - `separator`: a word (`of`) or `=` separating the parameters before it
|
|
3657
|
+
* from those after it; a boolean.
|
|
3658
|
+
* - `options`: keywords, the names of its `parameters`, each followed by a
|
|
3659
|
+
* quoted string or a number unless it is a flag.
|
|
3660
|
+
*/
|
|
3661
|
+
var PARAMETER_TYPES = [
|
|
3662
|
+
"expression",
|
|
3663
|
+
"statements",
|
|
3664
|
+
"passage",
|
|
3665
|
+
"variable",
|
|
3666
|
+
"string",
|
|
3667
|
+
"text",
|
|
3668
|
+
"names",
|
|
3669
|
+
"delay",
|
|
3670
|
+
"number",
|
|
3671
|
+
"flag",
|
|
3672
|
+
"separator",
|
|
3673
|
+
"options"
|
|
3674
|
+
];
|
|
3675
|
+
/**
|
|
3676
|
+
* What the value of a `string` or `text` argument holds, for the check at
|
|
3677
|
+
* story start and for tooling (see code-check.ts):
|
|
3678
|
+
* - `markup`: markup the macro renders (`{button}`'s label): its markup
|
|
3679
|
+
* is checked.
|
|
3680
|
+
* - `text`: plain text the macro uses as written (`{checkbox}`'s label).
|
|
3681
|
+
* - `passage`: a passage name (`{watch}`'s `goto`): the passage must exist.
|
|
3682
|
+
* - `expression`, `statements`: code (`{watch}`'s condition and `run`): it
|
|
3683
|
+
* is checked as code, and its variable references against the schema.
|
|
3684
|
+
*
|
|
3685
|
+
* Without it, the argument of a macro with `interpolate` holds `markup`,
|
|
3686
|
+
* any other `text`.
|
|
3687
|
+
*/
|
|
3688
|
+
var STRING_HOLDS = [
|
|
3689
|
+
"markup",
|
|
3690
|
+
"text",
|
|
3691
|
+
"passage",
|
|
3692
|
+
"expression",
|
|
3693
|
+
"statements"
|
|
3694
|
+
];
|
|
3695
|
+
/**
|
|
3696
|
+
* Throw if a parameter `macro` declares (or an option of one) has no type,
|
|
3697
|
+
* or one that isn't a ParameterType: arguments are read by their type, and
|
|
3698
|
+
* there is no default. Throw too if it declares what it `holds` wrongly.
|
|
3699
|
+
*/
|
|
3700
|
+
function checkParameterTypes(macro, parameters) {
|
|
3701
|
+
const holdsFix = `Give a \`string\` or \`text\` parameter one of ${STRING_HOLDS.join(", ")} to hold (see docs/custom-macros.md#what-a-string-holds).`;
|
|
3702
|
+
for (const param of parameters) {
|
|
3703
|
+
const type = param.type;
|
|
3704
|
+
if (!PARAMETER_TYPES.includes(type)) throw parameterError(macro, param, type === void 0 ? "has no type" : `has the unknown type ${JSON.stringify(type)}`, `Give it one of the types ${PARAMETER_TYPES.join(", ")} (see docs/custom-macros.md#parameter-types), or declare no parameters and read props.rawArgs.`);
|
|
3705
|
+
const holds = param.holds;
|
|
3706
|
+
if (holds !== void 0 && type !== "string" && type !== "text") throw parameterError(macro, param, `is of the type ${type}, which holds what it is`, holdsFix);
|
|
3707
|
+
if (holds !== void 0 && !STRING_HOLDS.includes(holds)) throw parameterError(macro, param, `holds the unknown ${JSON.stringify(holds)}`, holdsFix);
|
|
3708
|
+
if (param.parameters) checkParameterTypes(macro, param.parameters);
|
|
3709
|
+
}
|
|
3710
|
+
}
|
|
3711
|
+
/** The error for the parameter `param` of `macro`: what is wrong, and the fix. */
|
|
3712
|
+
function parameterError(macro, param, problem, fix) {
|
|
3713
|
+
return /* @__PURE__ */ new Error(`spindle: The parameter "${param.name}" of the macro {${macro}} ${problem}. ${fix}`);
|
|
3714
|
+
}
|
|
3715
|
+
var metadataRegistry = new NameMap();
|
|
3716
|
+
/** Indexed at registration, so parsing each tag does not scan every macro. */
|
|
3717
|
+
var companionParents = new NameMap();
|
|
3718
|
+
/** The parents declaring a branching companion (input options are values). */
|
|
3719
|
+
function branchParents(name, macros) {
|
|
3720
|
+
if (!macros) return [...companionParents.get(name) ?? []].filter((parent) => parent !== "listbox" && parent !== "cycle");
|
|
3721
|
+
return effectiveNamedDefinitions(macros).filter((macro) => macro.name.toLowerCase() !== "listbox" && macro.name.toLowerCase() !== "cycle" && macro.subMacros.some((sub) => sub.toLowerCase() === name.toLowerCase())).map((macro) => macro.name.toLowerCase());
|
|
3722
|
+
}
|
|
3723
|
+
/** The declared parameters of the macro `name`, if it declares any. */
|
|
3724
|
+
function getMacroParameters(name) {
|
|
3725
|
+
return metadataRegistry.get(name)?.parameters;
|
|
3726
|
+
}
|
|
3727
|
+
getMacroParameters.source = (name) => metadataRegistry.get(name)?.source;
|
|
3728
|
+
function getMacroRegistry() {
|
|
3729
|
+
return Array.from(metadataRegistry.values());
|
|
3730
|
+
}
|
|
3633
3731
|
//#endregion
|
|
3634
3732
|
//#region src/markup/ast.ts
|
|
3635
3733
|
/**
|
|
@@ -3726,7 +3824,7 @@ function astNode({ token, body }) {
|
|
|
3726
3824
|
};
|
|
3727
3825
|
if (!body) return withSelectors$1(node, token);
|
|
3728
3826
|
const children = buildAst(body.children);
|
|
3729
|
-
const branching = BRANCHING.has(name) && !WIDGETS.has(name);
|
|
3827
|
+
const branching = BRANCHING.has(name) && !WIDGETS.has(name) && getMacroParameters.source(name) !== "user";
|
|
3730
3828
|
if (!branching && body.branches.length === 0) {
|
|
3731
3829
|
node.children = children;
|
|
3732
3830
|
return withSelectors$1(node, token);
|
|
@@ -3741,97 +3839,18 @@ function astNode({ token, body }) {
|
|
|
3741
3839
|
}
|
|
3742
3840
|
}
|
|
3743
3841
|
}
|
|
3744
|
-
|
|
3745
|
-
|
|
3746
|
-
|
|
3747
|
-
|
|
3748
|
-
|
|
3749
|
-
|
|
3750
|
-
|
|
3751
|
-
|
|
3752
|
-
|
|
3753
|
-
|
|
3754
|
-
|
|
3755
|
-
|
|
3756
|
-
* - `text`: one quoted string, or text with any loose quotes stripped.
|
|
3757
|
-
* - `names`: a comma-separated list of names (`@item, @i`).
|
|
3758
|
-
* - `delay`: a duration (`2s`, `500ms`, `300`) in milliseconds.
|
|
3759
|
-
* - `number`: a number.
|
|
3760
|
-
* - `flag`: a keyword, the parameter's name, at the start or end; a boolean.
|
|
3761
|
-
* - `separator`: a word (`of`) or `=` separating the parameters before it
|
|
3762
|
-
* from those after it; a boolean.
|
|
3763
|
-
* - `options`: keywords, the names of its `parameters`, each followed by a
|
|
3764
|
-
* quoted string or a number unless it is a flag.
|
|
3765
|
-
*/
|
|
3766
|
-
var PARAMETER_TYPES = [
|
|
3767
|
-
"expression",
|
|
3768
|
-
"statements",
|
|
3769
|
-
"passage",
|
|
3770
|
-
"variable",
|
|
3771
|
-
"string",
|
|
3772
|
-
"text",
|
|
3773
|
-
"names",
|
|
3774
|
-
"delay",
|
|
3775
|
-
"number",
|
|
3776
|
-
"flag",
|
|
3777
|
-
"separator",
|
|
3778
|
-
"options"
|
|
3779
|
-
];
|
|
3780
|
-
/**
|
|
3781
|
-
* What the value of a `string` or `text` argument holds, for the check at
|
|
3782
|
-
* story start and for tooling (see code-check.ts):
|
|
3783
|
-
* - `markup`: markup the macro renders (`{button}`'s label): its markup
|
|
3784
|
-
* is checked.
|
|
3785
|
-
* - `text`: plain text the macro uses as written (`{checkbox}`'s label).
|
|
3786
|
-
* - `passage`: a passage name (`{watch}`'s `goto`): the passage must exist.
|
|
3787
|
-
* - `expression`, `statements`: code (`{watch}`'s condition and `run`): it
|
|
3788
|
-
* is checked as code, and its variable references against the schema.
|
|
3789
|
-
*
|
|
3790
|
-
* Without it, the argument of a macro with `interpolate` holds `markup`,
|
|
3791
|
-
* any other `text`.
|
|
3792
|
-
*/
|
|
3793
|
-
var STRING_HOLDS = [
|
|
3794
|
-
"markup",
|
|
3795
|
-
"text",
|
|
3796
|
-
"passage",
|
|
3797
|
-
"expression",
|
|
3798
|
-
"statements"
|
|
3799
|
-
];
|
|
3800
|
-
/**
|
|
3801
|
-
* Throw if a parameter `macro` declares (or an option of one) has no type,
|
|
3802
|
-
* or one that isn't a ParameterType: arguments are read by their type, and
|
|
3803
|
-
* there is no default. Throw too if it declares what it `holds` wrongly.
|
|
3804
|
-
*/
|
|
3805
|
-
function checkParameterTypes(macro, parameters) {
|
|
3806
|
-
const holdsFix = `Give a \`string\` or \`text\` parameter one of ${STRING_HOLDS.join(", ")} to hold (see docs/custom-macros.md#what-a-string-holds).`;
|
|
3807
|
-
for (const param of parameters) {
|
|
3808
|
-
const type = param.type;
|
|
3809
|
-
if (!PARAMETER_TYPES.includes(type)) throw parameterError(macro, param, type === void 0 ? "has no type" : `has the unknown type ${JSON.stringify(type)}`, `Give it one of the types ${PARAMETER_TYPES.join(", ")} (see docs/custom-macros.md#parameter-types), or declare no parameters and read props.rawArgs.`);
|
|
3810
|
-
const holds = param.holds;
|
|
3811
|
-
if (holds !== void 0 && type !== "string" && type !== "text") throw parameterError(macro, param, `is of the type ${type}, which holds what it is`, holdsFix);
|
|
3812
|
-
if (holds !== void 0 && !STRING_HOLDS.includes(holds)) throw parameterError(macro, param, `holds the unknown ${JSON.stringify(holds)}`, holdsFix);
|
|
3813
|
-
if (param.parameters) checkParameterTypes(macro, param.parameters);
|
|
3814
|
-
}
|
|
3815
|
-
}
|
|
3816
|
-
/** The error for the parameter `param` of `macro`: what is wrong, and the fix. */
|
|
3817
|
-
function parameterError(macro, param, problem, fix) {
|
|
3818
|
-
return /* @__PURE__ */ new Error(`spindle: The parameter "${param.name}" of the macro {${macro}} ${problem}. ${fix}`);
|
|
3819
|
-
}
|
|
3820
|
-
var metadataRegistry = new NameMap();
|
|
3821
|
-
/** Indexed at registration, so parsing each tag does not scan every macro. */
|
|
3822
|
-
var companionParents = new NameMap();
|
|
3823
|
-
/** The parents declaring a branching companion (listbox options are values). */
|
|
3824
|
-
function branchParents(name, macros) {
|
|
3825
|
-
if (!macros) return [...companionParents.get(name) ?? []].filter((parent) => parent !== "listbox");
|
|
3826
|
-
return macros.filter((macro) => macro.name.toLowerCase() !== "listbox" && macro.subMacros.some((sub) => sub.toLowerCase() === name.toLowerCase())).map((macro) => macro.name.toLowerCase());
|
|
3827
|
-
}
|
|
3828
|
-
/** The declared parameters of the macro `name`, if it declares any. */
|
|
3829
|
-
function getMacroParameters(name) {
|
|
3830
|
-
return metadataRegistry.get(name)?.parameters;
|
|
3831
|
-
}
|
|
3832
|
-
getMacroParameters.source = (name) => metadataRegistry.get(name)?.source;
|
|
3833
|
-
function getMacroRegistry() {
|
|
3834
|
-
return Array.from(metadataRegistry.values());
|
|
3842
|
+
//#endregion
|
|
3843
|
+
//#region src/utils/sorted.ts
|
|
3844
|
+
/** Index of the first sorted value greater than `value` (or the length). */
|
|
3845
|
+
function upperBound(sorted, value) {
|
|
3846
|
+
let low = 0;
|
|
3847
|
+
let high = sorted.length;
|
|
3848
|
+
while (low < high) {
|
|
3849
|
+
const mid = low + high >>> 1;
|
|
3850
|
+
if (sorted[mid] <= value) low = mid + 1;
|
|
3851
|
+
else high = mid;
|
|
3852
|
+
}
|
|
3853
|
+
return low;
|
|
3835
3854
|
}
|
|
3836
3855
|
//#endregion
|
|
3837
3856
|
//#region src/markup/line-column.ts
|
|
@@ -3854,14 +3873,7 @@ function sourcePositions(text) {
|
|
|
3854
3873
|
const starts = [0];
|
|
3855
3874
|
for (let i = 0; i < text.length; i++) if (text[i] === "\n") starts.push(i + 1);
|
|
3856
3875
|
return (offset) => {
|
|
3857
|
-
|
|
3858
|
-
let high = starts.length;
|
|
3859
|
-
while (low < high) {
|
|
3860
|
-
const mid = low + high >>> 1;
|
|
3861
|
-
if (starts[mid] <= offset) low = mid + 1;
|
|
3862
|
-
else high = mid;
|
|
3863
|
-
}
|
|
3864
|
-
const line = Math.max(1, low);
|
|
3876
|
+
const line = Math.max(1, upperBound(starts, offset));
|
|
3865
3877
|
return {
|
|
3866
3878
|
line,
|
|
3867
3879
|
column: offset - starts[line - 1] + 1
|
|
@@ -3882,6 +3894,8 @@ function sourcePositions(text) {
|
|
|
3882
3894
|
* runtime reads it, or, when it matches one further out, closes that one and
|
|
3883
3895
|
* what was open inside it stays unclosed.
|
|
3884
3896
|
*/
|
|
3897
|
+
/** Keep AST construction and recursive rendering within a safe call depth. */
|
|
3898
|
+
var MAX_MARKUP_DEPTH = 256;
|
|
3885
3899
|
/** The last of `items`. */
|
|
3886
3900
|
var last$1 = (items) => items[items.length - 1];
|
|
3887
3901
|
/** The macro each branch macro belongs to. */
|
|
@@ -4019,6 +4033,10 @@ function pairMarkup(tokens, options = {}) {
|
|
|
4019
4033
|
add(node);
|
|
4020
4034
|
continue;
|
|
4021
4035
|
}
|
|
4036
|
+
if (stack.length === MAX_MARKUP_DEPTH) report("nesting-limit", token, `Markup nesting exceeds the limit of ${MAX_MARKUP_DEPTH} elements`, token.start, {
|
|
4037
|
+
name: nameOf(token),
|
|
4038
|
+
limit: String(MAX_MARKUP_DEPTH)
|
|
4039
|
+
});
|
|
4022
4040
|
const body = {
|
|
4023
4041
|
children: [],
|
|
4024
4042
|
branches: []
|
|
@@ -9604,6 +9622,29 @@ function parseMemberIdentifier(src) {
|
|
|
9604
9622
|
if (object.type !== "Identifier" || object.start !== 0 || object.end !== 1 || property.type !== "Identifier" || property.start !== 2 || property.end !== src.length + 2) return null;
|
|
9605
9623
|
return property.name;
|
|
9606
9624
|
}
|
|
9625
|
+
/** Read the contiguous dot members after a variable, keeping decoded names. */
|
|
9626
|
+
function readMemberPath(src, end) {
|
|
9627
|
+
const path = [];
|
|
9628
|
+
const p = tokenizerAt(src, end, "expression", { afterOperand: true });
|
|
9629
|
+
try {
|
|
9630
|
+
for (;;) {
|
|
9631
|
+
p.nextToken();
|
|
9632
|
+
if (p.type !== types$1.dot || p.start !== end) break;
|
|
9633
|
+
const dotEnd = p.end;
|
|
9634
|
+
p.nextToken();
|
|
9635
|
+
if (p.start !== dotEnd || p.type !== types$1.name && !p.type.keyword) break;
|
|
9636
|
+
if (!/^\w+$/.test(String(p.value))) break;
|
|
9637
|
+
path.push(String(p.value));
|
|
9638
|
+
end = p.end;
|
|
9639
|
+
}
|
|
9640
|
+
} catch (error) {
|
|
9641
|
+
if (!(error instanceof SyntaxError)) throw error;
|
|
9642
|
+
}
|
|
9643
|
+
return {
|
|
9644
|
+
path,
|
|
9645
|
+
end
|
|
9646
|
+
};
|
|
9647
|
+
}
|
|
9607
9648
|
/**
|
|
9608
9649
|
* Parse `src` as `goal`, reading `%` as `percentAt` says where it says, and
|
|
9609
9650
|
* noting where it read a transient in `transientsRead`.
|
|
@@ -10314,7 +10355,7 @@ function isCodeAttribute(name) {
|
|
|
10314
10355
|
* Split a code attribute value into literal text and its `{…}` blocks that
|
|
10315
10356
|
* start with a sigil. Braces inside strings don't close a block; an
|
|
10316
10357
|
* unclosed block, any other brace and backslashes are text. A block whose
|
|
10317
|
-
* sigil is not followed by
|
|
10358
|
+
* sigil is not followed by an identifier (`{$(…)}`) is code, kept
|
|
10318
10359
|
* verbatim: not resolved, and not decoded like text.
|
|
10319
10360
|
*/
|
|
10320
10361
|
function splitSigilTemplate(template, incomplete = false) {
|
|
@@ -10339,7 +10380,11 @@ function splitSigilTemplate(template, incomplete = false) {
|
|
|
10339
10380
|
if (text) parts.push({ text });
|
|
10340
10381
|
text = "";
|
|
10341
10382
|
const block = template.slice(i, end);
|
|
10342
|
-
|
|
10383
|
+
let reference = /^.\w/.test(block);
|
|
10384
|
+
if (!reference && block[1] === "\\") lexJs(block, { variable(_sigil, _name, start) {
|
|
10385
|
+
if (start === 0) reference = true;
|
|
10386
|
+
} });
|
|
10387
|
+
parts.push(reference || incomplete && block.length === 1 && end === template.length ? { expr: block } : { verbatim: `{${block}}` });
|
|
10343
10388
|
i = end + 1;
|
|
10344
10389
|
}
|
|
10345
10390
|
if (text) parts.push({ text });
|
|
@@ -10421,7 +10466,12 @@ function analyzeMarkup(source, options = {}) {
|
|
|
10421
10466
|
nodes: paired.nodes,
|
|
10422
10467
|
errors
|
|
10423
10468
|
};
|
|
10424
|
-
const
|
|
10469
|
+
const damageEnds = [];
|
|
10470
|
+
for (const e of errors) damageEnds.push(Math.max(damageEnds[damageEnds.length - 1] ?? -1, e.offset));
|
|
10471
|
+
const inDamage = (start) => {
|
|
10472
|
+
const index = upperBound(damage, start) - 1;
|
|
10473
|
+
return index >= 0 && start <= damageEnds[index];
|
|
10474
|
+
};
|
|
10425
10475
|
const noticed = errors.map((e, i) => [damage[i] + .5, e]);
|
|
10426
10476
|
const position = paired.errors.length ? sourcePositions(source) : void 0;
|
|
10427
10477
|
for (const error of paired.errors) if (!inDamage(error.start)) noticed.push([error.noticedAt, pairingError(position, error)]);
|
|
@@ -12791,8 +12841,6 @@ function declarationRegex(sigil) {
|
|
|
12791
12841
|
const escaped = sigil.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
12792
12842
|
return new RegExp(`^${escaped}(\\w+)\\s*=\\s*(.+)$`);
|
|
12793
12843
|
}
|
|
12794
|
-
/** A `$name` or `%name` reference with its dot path, at a sigil the lexer found. */
|
|
12795
|
-
var VAR_PATH_RE = /([$%])(\w+(?:\.\w+)*)/y;
|
|
12796
12844
|
var VALID_VAR_TYPES = /* @__PURE__ */ new Set([
|
|
12797
12845
|
"number",
|
|
12798
12846
|
"string",
|
|
@@ -13018,36 +13066,19 @@ function checkReference(ref, declared) {
|
|
|
13018
13066
|
return null;
|
|
13019
13067
|
}
|
|
13020
13068
|
var NO_STORE_VAR_MACROS = /* @__PURE__ */ new Set();
|
|
13021
|
-
/** A `.field` path written right after a reference, which may be empty. */
|
|
13022
|
-
var FIELD_PATH_RE = /(?:\.\w+)*/y;
|
|
13023
13069
|
/**
|
|
13024
13070
|
* The reference to variable `name`, which JavaScript reads at `[start, end)`
|
|
13025
13071
|
* of `code` (the sigil and the name as written, which may be spelled with
|
|
13026
13072
|
* escapes), with the dot path written after it.
|
|
13027
13073
|
*/
|
|
13028
|
-
function referenceAt(code, sigil, name, start, end, at) {
|
|
13029
|
-
|
|
13030
|
-
const fields = FIELD_PATH_RE.exec(code)[0];
|
|
13074
|
+
function referenceAt(code, { sigil, name, start, end }, at) {
|
|
13075
|
+
const fields = readMemberPath(code, end);
|
|
13031
13076
|
return {
|
|
13032
13077
|
sigil,
|
|
13033
13078
|
name,
|
|
13034
|
-
path: fields
|
|
13079
|
+
path: fields.path,
|
|
13035
13080
|
start: at(start),
|
|
13036
|
-
end: at(
|
|
13037
|
-
};
|
|
13038
|
-
}
|
|
13039
|
-
/** A reference written at `index` of `term`, a `variable` parameter as written. */
|
|
13040
|
-
function termReferenceAt(term, index, at) {
|
|
13041
|
-
VAR_PATH_RE.lastIndex = index;
|
|
13042
|
-
const match = VAR_PATH_RE.exec(term);
|
|
13043
|
-
if (!match) return void 0;
|
|
13044
|
-
const [name, ...path] = match[2].split(".");
|
|
13045
|
-
return {
|
|
13046
|
-
sigil: match[1],
|
|
13047
|
-
name,
|
|
13048
|
-
path,
|
|
13049
|
-
start: at(index),
|
|
13050
|
-
end: at(index + match[0].length)
|
|
13081
|
+
end: at(fields.end)
|
|
13051
13082
|
};
|
|
13052
13083
|
}
|
|
13053
13084
|
/**
|
|
@@ -13084,7 +13115,7 @@ function scanCode(code, at, emit, goal = "expression") {
|
|
|
13084
13115
|
}
|
|
13085
13116
|
for (const ref of parsed.refs) {
|
|
13086
13117
|
if (ref.sigil !== "$" && ref.sigil !== "%") continue;
|
|
13087
|
-
emit(referenceAt(code, ref
|
|
13118
|
+
emit(referenceAt(code, ref, at));
|
|
13088
13119
|
}
|
|
13089
13120
|
}
|
|
13090
13121
|
/** `scanCode` for code acorn can't parse: lexed leniently. */
|
|
@@ -13092,7 +13123,12 @@ function scanCodeLeniently(code, at, emit, goal) {
|
|
|
13092
13123
|
lexJs(code, {
|
|
13093
13124
|
variable(sigil, name, index, _nesting, end) {
|
|
13094
13125
|
if (sigil !== "$" && sigil !== "%") return;
|
|
13095
|
-
emit(referenceAt(code,
|
|
13126
|
+
emit(referenceAt(code, {
|
|
13127
|
+
sigil,
|
|
13128
|
+
name,
|
|
13129
|
+
start: index,
|
|
13130
|
+
end
|
|
13131
|
+
}, at));
|
|
13096
13132
|
},
|
|
13097
13133
|
literal() {}
|
|
13098
13134
|
}, goal);
|
|
@@ -13121,7 +13157,7 @@ function scanArgs(token, parametersOf, at, emit, options) {
|
|
|
13121
13157
|
const params = parametersOf(token.name);
|
|
13122
13158
|
let byParameters = false;
|
|
13123
13159
|
if (params) {
|
|
13124
|
-
if (
|
|
13160
|
+
if (!options.storeVarMacros.has(token.name.toLowerCase())) scanVariableParameters(token, params, at, emit, options.all);
|
|
13125
13161
|
const pieces = [...argPieces(token.rawArgs, token.argsStart, params, token.name, "")];
|
|
13126
13162
|
byParameters = !pieces.some((piece) => piece.kind === "argument-error");
|
|
13127
13163
|
if (byParameters) scanPieces(pieces, at, emit, options);
|
|
@@ -13132,10 +13168,10 @@ function scanArgs(token, parametersOf, at, emit, options) {
|
|
|
13132
13168
|
}
|
|
13133
13169
|
/**
|
|
13134
13170
|
* Report the variables the `variable` parameters of a macro name
|
|
13135
|
-
* (`{unset $x}`, `{computed $x = …}`)
|
|
13136
|
-
*
|
|
13171
|
+
* (`{unset $x}`, `{computed $x = …}`). Computed members read variables even
|
|
13172
|
+
* when the receiver itself is only included in the complete editor list.
|
|
13137
13173
|
*/
|
|
13138
|
-
function scanVariableParameters(token, params, at, emit) {
|
|
13174
|
+
function scanVariableParameters(token, params, at, emit, all) {
|
|
13139
13175
|
const spans = /* @__PURE__ */ new Map();
|
|
13140
13176
|
try {
|
|
13141
13177
|
parseMacroArgs(token.rawArgs, params, spans);
|
|
@@ -13146,8 +13182,10 @@ function scanVariableParameters(token, params, at, emit) {
|
|
|
13146
13182
|
for (const param of params) {
|
|
13147
13183
|
const span = spans.get(param);
|
|
13148
13184
|
if (param.type !== "variable" || !span) continue;
|
|
13149
|
-
const
|
|
13150
|
-
|
|
13185
|
+
const start = token.argsStart + span[0];
|
|
13186
|
+
scanCode(token.rawArgs.slice(span[0], span[1]), (i) => at(start + i), (ref) => {
|
|
13187
|
+
if (all || ref.start !== at(start)) emit(ref);
|
|
13188
|
+
});
|
|
13151
13189
|
}
|
|
13152
13190
|
}
|
|
13153
13191
|
/** Report the references in the selectors of a token, which are interpolated. */
|
|
@@ -25372,7 +25410,7 @@ async function formatLf(text, options) {
|
|
|
25372
25410
|
}
|
|
25373
25411
|
if (region.type === "html") {
|
|
25374
25412
|
const htmlText = region.lines.join("\n");
|
|
25375
|
-
if (WHITESPACE_AS_WRITTEN.test(htmlText) || region.lines.some((line) => literalLines.has(line.trim()))) {
|
|
25413
|
+
if (WHITESPACE_AS_WRITTEN.test(htmlText) || region.lines.some((line) => literalLines.has(line.trim())) || hasMarkdownBoundaries(htmlText, isBlock)) {
|
|
25376
25414
|
for (let i = 0; i < region.lines.length; i++) preservedLines.add(lines.length + i);
|
|
25377
25415
|
lines.push(...region.lines);
|
|
25378
25416
|
continue;
|
|
@@ -25405,6 +25443,30 @@ async function formatLf(text, options) {
|
|
|
25405
25443
|
}
|
|
25406
25444
|
/** Literal HTML and opaque SVG, whose text can inherit significant whitespace. */
|
|
25407
25445
|
var WHITESPACE_AS_WRITTEN = /<(?:pre|textarea|svg)[\s>]|white-space\s*:/i;
|
|
25446
|
+
/** HTML whitespace formatting must not collapse the Markdown in its bodies. */
|
|
25447
|
+
var HTML_MARKDOWN_BOUNDARIES = /* @__PURE__ */ new Set([
|
|
25448
|
+
"hardBreakTrailing",
|
|
25449
|
+
"hardBreakEscape",
|
|
25450
|
+
"atxHeading",
|
|
25451
|
+
"setextHeading",
|
|
25452
|
+
"listOrdered",
|
|
25453
|
+
"listUnordered",
|
|
25454
|
+
"blockQuote",
|
|
25455
|
+
"thematicBreak",
|
|
25456
|
+
"definition",
|
|
25457
|
+
"table",
|
|
25458
|
+
"codeFenced"
|
|
25459
|
+
]);
|
|
25460
|
+
function hasMarkdownBoundaries(source, isBlock) {
|
|
25461
|
+
const { tokens } = analyzeMarkup(source, { hooks: { isBlock } });
|
|
25462
|
+
let possibleBoundary = false;
|
|
25463
|
+
for (const token of tokens) {
|
|
25464
|
+
if (token.type !== "text") continue;
|
|
25465
|
+
if (/\n[ \t]*\n/.test(token.value)) return true;
|
|
25466
|
+
if (/[\\#*+\d=>[`~|-]|[ \t]\n/.test(token.value)) possibleBoundary = true;
|
|
25467
|
+
}
|
|
25468
|
+
return possibleBoundary && markdownLines(source, isBlock, HTML_MARKDOWN_BOUNDARIES).size > 0;
|
|
25469
|
+
}
|
|
25408
25470
|
/** Preserve whole literal elements, including openers beside nested tags. */
|
|
25409
25471
|
function whitespaceSensitiveLines(source, isBlock) {
|
|
25410
25472
|
const lines = /* @__PURE__ */ new Set();
|
|
@@ -25773,9 +25835,14 @@ var transformCache = /* @__PURE__ */ new Map();
|
|
|
25773
25835
|
* validator, with no Preact or DOM dependencies. pkg/tooling.js re-exports
|
|
25774
25836
|
* them, binding the validator to its macro metadata.
|
|
25775
25837
|
*/
|
|
25838
|
+
/** Widgets take precedence over macros regardless of metadata input order. */
|
|
25839
|
+
function effectiveToolingMacros(macros) {
|
|
25840
|
+
const list = [...macros];
|
|
25841
|
+
return effectiveNamedDefinitions([...list.filter((m) => !m.widget), ...list.filter((m) => m.widget)]);
|
|
25842
|
+
}
|
|
25776
25843
|
function tokenizationOptions(options) {
|
|
25777
25844
|
if (!options.macros) return options;
|
|
25778
|
-
const macros =
|
|
25845
|
+
const macros = effectiveToolingMacros(options.macros);
|
|
25779
25846
|
const parametersOf = parameterLookup(macros);
|
|
25780
25847
|
return {
|
|
25781
25848
|
...options,
|
|
@@ -25803,7 +25870,7 @@ function tokenizeMarkupTolerant(source, options = {}) {
|
|
|
25803
25870
|
*/
|
|
25804
25871
|
function validateStoryMarkup(passages, macros, options = {}) {
|
|
25805
25872
|
const list = [...passages];
|
|
25806
|
-
const all =
|
|
25873
|
+
const all = effectiveToolingMacros(macros);
|
|
25807
25874
|
const known = /* @__PURE__ */ new Set();
|
|
25808
25875
|
const widgets = effectiveWidgetDefinitions(widgetDefinitions$1(list, parameterLookup(all)));
|
|
25809
25876
|
const blocks = new Set(widgets.filter((def) => def.block).map((def) => def.name.toLowerCase()));
|
|
@@ -25848,7 +25915,7 @@ function blockLookup(macros) {
|
|
|
25848
25915
|
* The macros are those the markup may use, built-in and user-defined.
|
|
25849
25916
|
*/
|
|
25850
25917
|
function passagePieces(source, macros = []) {
|
|
25851
|
-
const all =
|
|
25918
|
+
const all = effectiveToolingMacros(macros);
|
|
25852
25919
|
const parametersOf = parameterLookup(all);
|
|
25853
25920
|
return passagePiecesOfTokens(source, all, tokenizeMarkupTolerant(source, { hooks: { isRaw: (name) => isRawMacro(name, parametersOf.widget?.(name) !== void 0) } }).tokens);
|
|
25854
25921
|
}
|
|
@@ -25858,7 +25925,7 @@ function passagePieces(source, macros = []) {
|
|
|
25858
25925
|
* part of the published tooling API.
|
|
25859
25926
|
*/
|
|
25860
25927
|
function passagePiecesOfTokens(source, macros, tokens) {
|
|
25861
|
-
const all =
|
|
25928
|
+
const all = effectiveToolingMacros(macros);
|
|
25862
25929
|
return [...passagePiecesOf(source, tokens, parameterLookup(all), { isBlock: blockLookup(all) })];
|
|
25863
25930
|
}
|
|
25864
25931
|
/**
|
package/package.json
CHANGED
package/src/action-registry.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isReaderVisible } from './utils/element-visibility';
|
|
2
|
+
import { readOnlyValue } from './execute-mutation';
|
|
2
3
|
import { emit } from './event-emitter';
|
|
3
4
|
import {
|
|
4
5
|
checkFields,
|
|
@@ -250,7 +251,7 @@ export async function settleRunningActions(): Promise<void> {
|
|
|
250
251
|
}
|
|
251
252
|
|
|
252
253
|
export function getActions(): StoryAction[] {
|
|
253
|
-
return Array.from(actions.values(), listed);
|
|
254
|
+
return readOnlyValue(Array.from(actions.values(), listed));
|
|
254
255
|
}
|
|
255
256
|
|
|
256
257
|
/**
|
|
@@ -258,15 +259,17 @@ export function getActions(): StoryAction[] {
|
|
|
258
259
|
* function, see registerAction), its nested dialogs' left out.
|
|
259
260
|
*/
|
|
260
261
|
export function getDialogActions(dialog: () => void): StoryAction[] {
|
|
261
|
-
return
|
|
262
|
-
.
|
|
263
|
-
|
|
264
|
-
|
|
262
|
+
return readOnlyValue(
|
|
263
|
+
Array.from(actions.values())
|
|
264
|
+
.map((entries) => entries[entries.length - 1]!)
|
|
265
|
+
.filter((entry) => entry.dialog === dialog)
|
|
266
|
+
.map((entry) => effectiveAction(entry.action)),
|
|
267
|
+
);
|
|
265
268
|
}
|
|
266
269
|
|
|
267
270
|
export function getAction(id: string): StoryAction | undefined {
|
|
268
271
|
const entries = actions.get(id);
|
|
269
|
-
return entries && listed(entries);
|
|
272
|
+
return entries && readOnlyValue(listed(entries));
|
|
270
273
|
}
|
|
271
274
|
|
|
272
275
|
function listed(entries: Entry[]): StoryAction {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { useStoryStore } from '../../store';
|
|
2
2
|
import { evaluate } from '../../expression';
|
|
3
|
+
import { parseReferencePath } from '../../js-lexer';
|
|
3
4
|
import { readState } from '../../execute-mutation';
|
|
4
5
|
import { deepEqualStrict } from '../../structural';
|
|
5
6
|
import { defineMacro } from '../../define-macro';
|
|
@@ -27,14 +28,15 @@ function assignmentArgs(
|
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
const { target = '', expression: expr = '' } = args;
|
|
30
|
-
|
|
31
|
+
const path = parseReferencePath(target);
|
|
32
|
+
if (!path || path.nested || path.sigil === '%') {
|
|
31
33
|
throw new Error(
|
|
32
34
|
`{computed}: target must be $name, _name, or @name, got "${target}"`,
|
|
33
35
|
);
|
|
34
36
|
}
|
|
35
37
|
|
|
36
|
-
checkVariableName(
|
|
37
|
-
return { target, expr };
|
|
38
|
+
checkVariableName(path.name, target);
|
|
39
|
+
return { target: path.sigil + path.name, expr };
|
|
38
40
|
}
|
|
39
41
|
|
|
40
42
|
/**
|
|
@@ -288,7 +288,8 @@ async function formatLf(
|
|
|
288
288
|
// Prettier cannot see a `white-space` style. Leave such HTML as written.
|
|
289
289
|
if (
|
|
290
290
|
WHITESPACE_AS_WRITTEN.test(htmlText) ||
|
|
291
|
-
region.lines.some((line) => literalLines.has(line.trim()))
|
|
291
|
+
region.lines.some((line) => literalLines.has(line.trim())) ||
|
|
292
|
+
hasMarkdownBoundaries(htmlText, isBlock)
|
|
292
293
|
) {
|
|
293
294
|
for (let i = 0; i < region.lines.length; i++)
|
|
294
295
|
preservedLines.add(lines.length + i);
|
|
@@ -378,6 +379,41 @@ async function formatLf(
|
|
|
378
379
|
/** Literal HTML and opaque SVG, whose text can inherit significant whitespace. */
|
|
379
380
|
const WHITESPACE_AS_WRITTEN = /<(?:pre|textarea|svg)[\s>]|white-space\s*:/i;
|
|
380
381
|
|
|
382
|
+
/** HTML whitespace formatting must not collapse the Markdown in its bodies. */
|
|
383
|
+
const HTML_MARKDOWN_BOUNDARIES = new Set([
|
|
384
|
+
'hardBreakTrailing',
|
|
385
|
+
'hardBreakEscape',
|
|
386
|
+
'atxHeading',
|
|
387
|
+
'setextHeading',
|
|
388
|
+
'listOrdered',
|
|
389
|
+
'listUnordered',
|
|
390
|
+
'blockQuote',
|
|
391
|
+
'thematicBreak',
|
|
392
|
+
'definition',
|
|
393
|
+
'table',
|
|
394
|
+
'codeFenced',
|
|
395
|
+
]);
|
|
396
|
+
|
|
397
|
+
function hasMarkdownBoundaries(
|
|
398
|
+
source: string,
|
|
399
|
+
isBlock: (name: string) => boolean,
|
|
400
|
+
): boolean {
|
|
401
|
+
const { tokens } = analyzeMarkup(source, { hooks: { isBlock } });
|
|
402
|
+
let possibleBoundary = false;
|
|
403
|
+
for (const token of tokens) {
|
|
404
|
+
if (token.type !== 'text') continue;
|
|
405
|
+
if (/\n[ \t]*\n/.test(token.value)) return true;
|
|
406
|
+
// Non-text nodes become inline spans in Markdown. Without punctuation
|
|
407
|
+
// for a block construct or a hard break, their surrounding text cannot
|
|
408
|
+
// create a boundary. Avoid parsing thousands of placeholder-only bodies.
|
|
409
|
+
if (/[\\#*+\d=>[`~|-]|[ \t]\n/.test(token.value)) possibleBoundary = true;
|
|
410
|
+
}
|
|
411
|
+
return (
|
|
412
|
+
possibleBoundary &&
|
|
413
|
+
markdownLines(source, isBlock, HTML_MARKDOWN_BOUNDARIES).size > 0
|
|
414
|
+
);
|
|
415
|
+
}
|
|
416
|
+
|
|
381
417
|
/** Preserve whole literal elements, including openers beside nested tags. */
|
|
382
418
|
function whitespaceSensitiveLines(
|
|
383
419
|
source: string,
|