@rohal12/spindle 0.59.33 → 0.60.1

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,74 @@
1
+ export interface Snippet {
2
+ /** The command name suffix: the command is `snippet:<id>`. */
3
+ id: string;
4
+ /** The menu item's label. */
5
+ label: string;
6
+ menu: 'links' | 'logic' | 'variables' | 'inputs' | 'ui';
7
+ /**
8
+ * Inserted before the selection. Its one `|` marks the cursor slot, where
9
+ * the cursor goes when nothing is selected; it is not inserted.
10
+ */
11
+ before: string;
12
+ /** Inserted after the selection ('' for a snippet that does not wrap). */
13
+ after: string;
14
+ }
15
+
16
+ type Row = readonly [id: string, label: string, before: string, after?: string];
17
+
18
+ /** The snippets of one menu from rows of `[id, label, before, after]`. */
19
+ function group(menu: Snippet['menu'], rows: readonly Row[]): Snippet[] {
20
+ return rows.map(([id, label, before, after = '']) => ({
21
+ id,
22
+ label,
23
+ menu,
24
+ before,
25
+ after,
26
+ }));
27
+ }
28
+
29
+ /**
30
+ * The templates follow `docs/macros.md`. A block macro wraps the selection as
31
+ * its body; the slot is where the author types next.
32
+ */
33
+ export const snippets: readonly Snippet[] = [
34
+ ...group('links', [
35
+ ['bracket-link', 'Link [[…]]', '[[|', ']]'],
36
+ ['link', 'Link macro…', '{link "|" "Passage"}', '{/link}'],
37
+ ['goto', 'Go to…', '{goto "|"}'],
38
+ ['include', 'Include…', '{include "|"}'],
39
+ ['back', 'Back', '{back}|'],
40
+ ]),
41
+ ...group('logic', [
42
+ ['if', 'If…', '{if |}', '{/if}'],
43
+ ['if-else', 'If / else…', '{if |}', '{else}{/if}'],
44
+ ['for', 'For each…', '{for @item of |}', '{/for}'],
45
+ ['switch', 'Switch…', '{switch |}{case 1}', '{default}{/switch}'],
46
+ ['do', 'Do (code)', '{do}|', '{/do}'],
47
+ ['watch', 'Watch…', `{watch '|' dialog "Passage" once}`],
48
+ ]),
49
+ ...group('variables', [
50
+ ['set', 'Set…', '{set |}'],
51
+ ['unset', 'Unset…', '{unset |}'],
52
+ ['print', 'Print…', '{print |}'],
53
+ ['display', 'Display {$x}', '{$|}'],
54
+ ['computed', 'Computed…', '{computed |}'],
55
+ ]),
56
+ ...group('inputs', [
57
+ ['textbox', 'Text box…', '{textbox |}'],
58
+ ['numberbox', 'Number box…', '{numberbox |}'],
59
+ ['textarea', 'Text area…', '{textarea |}'],
60
+ ['checkbox', 'Checkbox…', '{checkbox | "Label"}'],
61
+ ['radiobutton', 'Radio button…', '{radiobutton | "value" "Label"}'],
62
+ ['listbox', 'List box…', '{listbox |}{option "Option"}', '{/listbox}'],
63
+ ['cycle', 'Cycle…', '{cycle |}{option "Option"}', '{/cycle}'],
64
+ ['button', 'Button…', '{button "|"}', '{/button}'],
65
+ ]),
66
+ ...group('ui', [
67
+ ['dialog', 'Dialog…', '{dialog "|"}', '{/dialog}'],
68
+ ['repeat', 'Repeat…', '{repeat |}', '{/repeat}'],
69
+ ['timed', 'Timed…', '{timed |}', '{/timed}'],
70
+ ['meter', 'Meter…', '{meter | 100}'],
71
+ ['nobr', 'No line breaks', '{nobr}|', '{/nobr}'],
72
+ ['widget', 'Widget…', '{widget "|"}', '{/widget}'],
73
+ ]),
74
+ ];
@@ -0,0 +1,85 @@
1
+ import type CodeMirror from 'codemirror';
2
+ import { icon, type IconName } from './icons';
3
+ import { snippets, type Snippet } from './snippets';
4
+
5
+ /** The items Twine 2.4+ accepts from `codeMirror.toolbar`. */
6
+ export type ToolbarItem =
7
+ | {
8
+ type: 'button';
9
+ command: string;
10
+ label: string;
11
+ /** Required at the top level, not allowed inside a menu. */
12
+ icon?: string;
13
+ disabled?: boolean;
14
+ iconOnly?: boolean;
15
+ }
16
+ | {
17
+ type: 'menu';
18
+ icon: string;
19
+ label: string;
20
+ items: ToolbarMenuItem[];
21
+ disabled?: boolean;
22
+ }
23
+ | { type: 'separator' };
24
+
25
+ export type ToolbarMenuItem = Extract<
26
+ ToolbarItem,
27
+ { type: 'button' | 'separator' }
28
+ >;
29
+
30
+ export interface ToolbarEnvironment {
31
+ appTheme: 'dark' | 'light';
32
+ foregroundColor: string;
33
+ locale: string;
34
+ }
35
+
36
+ const menus: readonly { id: Snippet['menu']; label: string }[] = [
37
+ { id: 'links', label: 'Links' },
38
+ { id: 'logic', label: 'Logic' },
39
+ { id: 'variables', label: 'Variables' },
40
+ { id: 'inputs', label: 'Inputs' },
41
+ { id: 'ui', label: 'UI' },
42
+ ];
43
+
44
+ /**
45
+ * The toolbar above the passage editor: one menu of snippets per group, then
46
+ * Check and Format. The items are the same for every editor, theme and
47
+ * locale; only Check and Format are disabled while the passage is empty. The
48
+ * commands they run are in `codeMirror.commands`.
49
+ */
50
+ export function toolbar(
51
+ editor: CodeMirror.Editor,
52
+ environment: ToolbarEnvironment,
53
+ ): ToolbarItem[] {
54
+ const color = environment.foregroundColor;
55
+ const empty = editor.getValue() === '';
56
+ const named = (name: IconName) => icon(name, color);
57
+ return [
58
+ ...menus.map(({ id, label }): ToolbarItem => ({
59
+ type: 'menu',
60
+ icon: named(id),
61
+ label,
62
+ items: snippets
63
+ .filter((snippet) => snippet.menu === id)
64
+ .map((snippet) => ({
65
+ type: 'button',
66
+ command: `snippet:${snippet.id}`,
67
+ label: snippet.label,
68
+ })),
69
+ })),
70
+ {
71
+ type: 'button',
72
+ command: 'check',
73
+ label: 'Check',
74
+ icon: named('check'),
75
+ disabled: empty,
76
+ },
77
+ {
78
+ type: 'button',
79
+ command: 'format',
80
+ label: 'Format',
81
+ icon: named('format'),
82
+ disabled: empty,
83
+ },
84
+ ];
85
+ }
@@ -126,6 +126,15 @@ const _toolingTypes: [
126
126
  SourceTooling.ValidateMarkupOptions,
127
127
  PublishedTooling.ValidateMarkupOptions
128
128
  >,
129
+ Same<SourceTooling.PrettierLike, PublishedTooling.PrettierLike>,
130
+ Same<
131
+ SourceTooling.PassageFormatOptions,
132
+ PublishedTooling.PassageFormatOptions
133
+ >,
134
+ Same<
135
+ typeof SourceTooling.formatPassage,
136
+ typeof PublishedTooling.formatPassage
137
+ >,
129
138
  ] = [
130
139
  true,
131
140
  true,
@@ -144,6 +153,9 @@ const _toolingTypes: [
144
153
  true,
145
154
  true,
146
155
  true,
156
+ true,
157
+ true,
158
+ true,
147
159
  ];
148
160
 
149
161
  // A typical custom macro written against the published types must type-check,
@@ -1116,3 +1116,71 @@ export interface DiscoveredMacro extends ToolingMacro {
1116
1116
  * throws.
1117
1117
  */
1118
1118
  export declare function discoverMacros(source: string): DiscoveredMacro[];
1119
+
1120
+ // ---------------------------------------------------------------------------
1121
+ // Formatting
1122
+ // ---------------------------------------------------------------------------
1123
+
1124
+ /**
1125
+ * The part of Prettier's API `formatPassage` calls: `prettier`, or
1126
+ * `prettier/standalone` with the plugins for the languages in the passage.
1127
+ */
1128
+ export interface PrettierLike {
1129
+ format(code: string, options: object): Promise<string>;
1130
+ }
1131
+
1132
+ /** What `formatPassage` formats and how. */
1133
+ export interface PassageFormatOptions {
1134
+ /**
1135
+ * What the passage holds (default: 'normal', story markup). The text of a
1136
+ * 'script' or 'stylesheet' passage is formatted whole, as JavaScript or CSS.
1137
+ */
1138
+ kind?: 'normal' | 'script' | 'stylesheet';
1139
+ /**
1140
+ * Prettier, for the `<script>` and HTML blocks of the markup and for script
1141
+ * and stylesheet passages. Without it, or where it throws on a piece of
1142
+ * code, that code stays as written.
1143
+ */
1144
+ prettier?: PrettierLike;
1145
+ /**
1146
+ * The options Prettier is given (default: `{ tabWidth: 2 }`). They replace
1147
+ * the default; the formatter sets `parser` (and HTML whitespace handling)
1148
+ * itself. Standalone Prettier needs its `plugins` in every call: wrap
1149
+ * `prettier` to add them rather than passing them here.
1150
+ */
1151
+ prettierOptions?: object;
1152
+ /**
1153
+ * Whether the named macro takes a body (default: the built-in block macros
1154
+ * and the names the passage closes with `{/name}`). Replaces that default
1155
+ * whole; use `blockMacros` to add to it.
1156
+ */
1157
+ isBlock?: (name: string) => boolean;
1158
+ /**
1159
+ * Names of block macros to add to the default `isBlock` (ignored when
1160
+ * `isBlock` is given), such as the ones the story or an editor knows.
1161
+ */
1162
+ blockMacros?: Iterable<string>;
1163
+ /**
1164
+ * Whether the named sub-macro snaps back to its parent's indentation
1165
+ * (default: else, elseif, next, case, default).
1166
+ */
1167
+ isDedentingSubMacro?: (name: string) => boolean;
1168
+ /** Wrap prose lines longer than this many characters. */
1169
+ maxLineLength?: number;
1170
+ }
1171
+
1172
+ /**
1173
+ * Format the text of a passage: indent the bodies of block macros by two
1174
+ * spaces per level, snap `{else}`, `{case}` and the like back to their
1175
+ * macro, strip trailing whitespace (keeping Markdown hard breaks, lists and
1176
+ * code fences), and format `<script>` and HTML blocks with Prettier. What the
1177
+ * runtime executes is never touched: a macro, a link or a `{do}` body keeps
1178
+ * its text, line breaks included, and a multiline JavaScript literal keeps its
1179
+ * value. What the passage displays does not change. The result ends with one
1180
+ * newline and keeps the line endings of the text (the more common of CRLF and
1181
+ * LF) and its byte order mark. It is idempotent.
1182
+ */
1183
+ export declare function formatPassage(
1184
+ body: string,
1185
+ options?: PassageFormatOptions,
1186
+ ): Promise<string>;