@vttforge/cli 0.5.2 → 0.5.4

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.
Files changed (45) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +21 -23
  3. package/dist/bin.mjs +1 -1
  4. package/dist/index.d.mts +1 -1
  5. package/dist/index.mjs +1 -1
  6. package/dist/{src-CWhUNM__.mjs → src-BrNh991M.mjs} +90 -4
  7. package/dist/src-BrNh991M.mjs.map +1 -0
  8. package/package.json +3 -3
  9. package/templates/module-js/README.md +46 -22
  10. package/templates/module-js/lang/en.json +19 -1
  11. package/templates/module-js/module.json +7 -2
  12. package/templates/module-js/scripts/constants.mjs +12 -0
  13. package/templates/module-js/scripts/data/note-data.mjs +18 -0
  14. package/templates/module-js/scripts/enricher.mjs +56 -0
  15. package/templates/module-js/scripts/main.mjs +75 -53
  16. package/templates/module-js/scripts/sheets/note-sheet.mjs +69 -0
  17. package/templates/module-js/styles/main.css +70 -9
  18. package/templates/module-js/templates/item/note-sheet.hbs +34 -0
  19. package/templates/module-ts/README.md +51 -22
  20. package/templates/module-ts/lang/en.json +19 -1
  21. package/templates/module-ts/module.json +7 -2
  22. package/templates/module-ts/scripts/constants.ts +12 -0
  23. package/templates/module-ts/scripts/data/note-data.ts +19 -0
  24. package/templates/module-ts/scripts/enricher.ts +52 -0
  25. package/templates/module-ts/scripts/main.ts +73 -55
  26. package/templates/module-ts/scripts/sheets/note-sheet.ts +76 -0
  27. package/templates/module-ts/styles/main.css +70 -9
  28. package/templates/module-ts/templates/item/note-sheet.hbs +34 -0
  29. package/templates/module-ts/tsconfig.json +8 -2
  30. package/templates/system-js/README.md +45 -27
  31. package/templates/system-js/scripts/data/character-data.mjs +65 -59
  32. package/templates/system-js/scripts/data/gear-data.mjs +31 -31
  33. package/templates/system-js/scripts/main.mjs +38 -21
  34. package/templates/system-js/scripts/sheets/character-sheet.mjs +49 -50
  35. package/templates/system-js/scripts/sheets/gear-sheet.mjs +14 -12
  36. package/templates/system-js/templates/actor/character-sheet.hbs +1 -1
  37. package/templates/system-ts/README.md +45 -27
  38. package/templates/system-ts/scripts/data/character-data.ts +80 -64
  39. package/templates/system-ts/scripts/data/gear-data.ts +35 -31
  40. package/templates/system-ts/scripts/main.ts +38 -21
  41. package/templates/system-ts/scripts/sheets/character-sheet.ts +99 -98
  42. package/templates/system-ts/scripts/sheets/gear-sheet.ts +26 -20
  43. package/templates/system-ts/templates/actor/character-sheet.hbs +1 -1
  44. package/templates/system-ts/tsconfig.json +1 -0
  45. package/dist/src-CWhUNM__.mjs.map +0 -1
@@ -0,0 +1,52 @@
1
+ /**
2
+ * `@Note[id]` — a link to a note from any enriched text field.
3
+ *
4
+ * Declared as data and handed to `registerModule({ enrichers })`, which
5
+ * registers it as `{{ID}}.note`. The pattern carries the `g` flag because
6
+ * enrichment runs it through `matchAll`, and `onRender` binds the click
7
+ * where the markup is declared rather than in a separate hook.
8
+ */
9
+ import type { EnricherRegistration } from '@vttforge/core';
10
+ import { NOTE_TYPE } from './constants.js';
11
+
12
+ interface NoteLike {
13
+ readonly name: string;
14
+ readonly type: string;
15
+ readonly sheet: { render(force?: boolean): unknown };
16
+ }
17
+
18
+ function findNote(id: string): NoteLike | undefined {
19
+ const item = game.items.get(id) as NoteLike | undefined;
20
+ return item?.type === NOTE_TYPE ? item : undefined;
21
+ }
22
+
23
+ export const noteEnricher: EnricherRegistration = {
24
+ id: 'note',
25
+ pattern: /@Note\[([^\]]+)\]/g,
26
+
27
+ enricher(match) {
28
+ const id = match[1];
29
+ const note = id === undefined ? undefined : findNote(id);
30
+ // Leave the text alone when the note is gone: a dead link is worse than
31
+ // the raw reference, which at least says what was meant.
32
+ if (note === undefined) return null;
33
+
34
+ const link = document.createElement('a');
35
+ link.className = '{{ID}}-note-link';
36
+ link.dataset.noteId = id;
37
+ link.draggable = false;
38
+ link.append(Object.assign(document.createElement('i'), { className: 'fa-solid fa-note-sticky' }));
39
+ link.append(note.name);
40
+ return link;
41
+ },
42
+
43
+ onRender(element) {
44
+ for (const link of element.querySelectorAll<HTMLElement>('a.{{ID}}-note-link')) {
45
+ link.addEventListener('click', (event) => {
46
+ event.preventDefault();
47
+ const { noteId } = link.dataset;
48
+ if (noteId) findNote(noteId)?.sheet.render(true);
49
+ });
50
+ }
51
+ },
52
+ };
@@ -1,71 +1,89 @@
1
1
  /**
2
2
  * {{TITLE}} — entry point.
3
3
  *
4
- * Demonstrates the typical Foundry module lifecycle:
5
- *
6
- * - `Hooks.once("init")` — register settings (here via `SystemConfig` from
7
- * `@vttforge/core` for typed access) and expose the public API on
8
- * `game.modules.get(MODULE_ID).api`.
9
- * - `Hooks.once("ready")` — surface a one-shot notification if the user
10
- * opted in via settings.
11
- * - `Hooks.on("renderActorSheetV2", ...)` — example hook listener that
12
- * tags any actor sheet with a discreet `.{{ID}}-badge` so you can verify
13
- * the module is actually attached without hunting through devtools.
4
+ * One `registerModule` call replaces the `Hooks.once("init", ...)` block most
5
+ * modules copy from each other: the sub-type, its sheet, the enricher, the
6
+ * settings, and the public API.
14
7
  */
15
8
  import './foundry-globals.js';
16
- import { SystemConfig } from '@vttforge/core';
9
+ import { registerModule, SystemConfig, VttfError } from '@vttforge/core';
10
+ import { MODULE_ID, NOTE_TYPE } from './constants.js';
11
+ import { NoteData } from './data/note-data.js';
12
+ import { noteEnricher } from './enricher.js';
13
+ import { NoteSheet } from './sheets/note-sheet.js';
17
14
 
18
- const MODULE_ID = '{{ID}}';
19
15
  const settings = new SystemConfig(MODULE_ID);
20
16
 
17
+ /** What `game.modules.get("{{ID}}").api` offers other modules and macros. */
21
18
  interface ModuleApi {
22
- greet(name: string): string;
23
- getSetting<T>(key: string): T;
19
+ /** The prefixed type key, for `item.type === api.noteType` checks. */
20
+ readonly noteType: string;
21
+ createNote(name: string, body?: string): Promise<unknown>;
24
22
  }
25
23
 
26
- Hooks.once('init', () => {
27
- settings.register('showWelcome', {
28
- name: '{{LOCALE_PREFIX}}.Settings.showWelcome.name',
29
- hint: '{{LOCALE_PREFIX}}.Settings.showWelcome.hint',
30
- scope: 'client',
31
- config: true,
32
- type: Boolean,
33
- default: true,
34
- });
24
+ const api: ModuleApi = {
25
+ noteType: NOTE_TYPE,
26
+ createNote(name, body = '') {
27
+ return CONFIG.Item.documentClass.create(
28
+ { name, type: NOTE_TYPE, system: { body } },
29
+ { renderSheet: true },
30
+ );
31
+ },
32
+ };
35
33
 
36
- const api: ModuleApi = {
37
- greet(name: string): string {
38
- return `Hello, ${name}!`;
39
- },
40
- getSetting<T>(key: string): T {
41
- return settings.get(key) as T;
42
- },
43
- };
34
+ try {
35
+ registerModule({
36
+ id: MODULE_ID,
44
37
 
45
- const moduleHandle = game.modules.get(MODULE_ID);
46
- if (moduleHandle) {
47
- moduleHandle.api = api;
48
- }
49
- });
38
+ // Registered as `{{ID}}.note` — the prefix is added for you, and the
39
+ // manifest declares the same key under `documentTypes.Item`.
40
+ itemDataModels: { note: NoteData },
50
41
 
51
- Hooks.once('ready', () => {
52
- const shouldWelcome = settings.get<boolean>('showWelcome');
53
- if (shouldWelcome) {
54
- ui.notifications?.info(game.i18n.localize('{{LOCALE_PREFIX}}.Welcome'));
55
- }
56
- });
42
+ // Declared here rather than with `Items.registerSheet`. Foundry keys a
43
+ // sheet by `${scope}.${class name}` and saves that key on every document
44
+ // using it; a bundler renames classes between builds, and the saved key
45
+ // then names a sheet that no longer exists. The `id` is written down, so
46
+ // the key does not move. Pick it once and keep it.
47
+ sheets: [
48
+ {
49
+ id: 'note',
50
+ document: 'Item',
51
+ sheet: NoteSheet,
52
+ types: [NOTE_TYPE],
53
+ makeDefault: true,
54
+ label: '{{LOCALE_PREFIX}}.Sheet.Note.title',
55
+ },
56
+ ],
57
+
58
+ enrichers: [noteEnricher],
57
59
 
58
- Hooks.on('renderActorSheetV2', (_app: unknown, element: HTMLElement) => {
59
- // Decorative: tag the rendered sheet so it's obvious the module is
60
- // active. Remove or replace with real behaviour once you start shipping
61
- // module features.
62
- const header = element.querySelector('.window-header .window-title');
63
- if (header && !header.querySelector(`.{{ID}}-badge`)) {
64
- const badge = document.createElement('span');
65
- badge.className = '{{ID}}-badge';
66
- // Double-quoted because `vttforge init` rejects `"` and `\` in the
67
- // title — apostrophes in titles like "Daisy's Module" stay intact.
68
- badge.textContent = "{{TITLE}}";
69
- header.appendChild(badge);
60
+ // Runs first inside `init` — the usual home for the module API, so it is
61
+ // there before anything that might hook `init` after us asks for it.
62
+ onBeforeInit: () => {
63
+ const handle = game.modules.get(MODULE_ID);
64
+ if (handle) handle.api = api;
65
+ },
66
+
67
+ onAfterInit: () => {
68
+ settings.register('showWelcome', {
69
+ name: '{{LOCALE_PREFIX}}.Settings.showWelcome.name',
70
+ hint: '{{LOCALE_PREFIX}}.Settings.showWelcome.hint',
71
+ scope: 'client',
72
+ config: true,
73
+ type: Boolean,
74
+ default: true,
75
+ });
76
+ },
77
+
78
+ onReady: () => {
79
+ if (settings.get<boolean>('showWelcome')) {
80
+ ui.notifications?.info(game.i18n.localize('{{LOCALE_PREFIX}}.Welcome'));
81
+ }
82
+ },
83
+ });
84
+ } catch (err) {
85
+ if (err instanceof VttfError) {
86
+ console.error(`[${err.code}] ${err.message} — see ${err.docsUrl}`);
70
87
  }
71
- });
88
+ throw err;
89
+ }
@@ -0,0 +1,76 @@
1
+ /**
2
+ * NoteSheet — the `note` Item sheet on `BaseItemSheet()`.
3
+ *
4
+ * One part, no tabs, one action. The smallest sheet a module can ship.
5
+ */
6
+ import { BaseItemSheet } from '@vttforge/core';
7
+ import { MODULE_ID } from '../constants.js';
8
+ import type { NoteData } from '../data/note-data.js';
9
+
10
+ /**
11
+ * What this sheet reads off its item.
12
+ *
13
+ * Foundry's own `Item` type is not wired in — see `foundry-globals.ts` — so
14
+ * the sheet says what it needs. Grow this as the sheet grows.
15
+ */
16
+ interface NoteItem {
17
+ readonly id: string;
18
+ readonly name: string;
19
+ readonly img: string;
20
+ readonly isOwner: boolean;
21
+ readonly system: NoteData;
22
+ update(changes: Record<string, unknown>): Promise<unknown>;
23
+ }
24
+
25
+ export class NoteSheet extends BaseItemSheet() {
26
+ static override DEFAULT_OPTIONS = foundry.utils.mergeObject(
27
+ super.DEFAULT_OPTIONS,
28
+ {
29
+ id: '{{ID}}-note',
30
+ classes: ['{{ID}}', 'sheet', 'item', 'note'],
31
+ window: {
32
+ title: '{{LOCALE_PREFIX}}.Sheet.Note.title',
33
+ icon: 'fa-solid fa-note-sticky',
34
+ },
35
+ position: { width: 480, height: 480 },
36
+ actions: {
37
+ togglePinned: NoteSheet._onTogglePinned,
38
+ },
39
+ },
40
+ { inplace: false },
41
+ );
42
+
43
+ static PARTS = {
44
+ sheet: { template: `modules/${MODULE_ID}/templates/item/note-sheet.hbs` },
45
+ };
46
+
47
+ /**
48
+ * The item this sheet is for.
49
+ *
50
+ * `this.document` is `unknown` on the base — which document a sheet is for
51
+ * is the module's to know. One cast, here, and everything below is typed.
52
+ */
53
+ get item(): NoteItem {
54
+ return this.document as NoteItem;
55
+ }
56
+
57
+ override async _prepareContext(options: unknown): Promise<Record<string, unknown>> {
58
+ const context = await super._prepareContext(options);
59
+ const { item } = this;
60
+ context.item = item;
61
+ context.system = item.system;
62
+ context.isEditable = this.isEditable;
63
+ context.reference = `@Note[${item.id}]`;
64
+ context.enrichedBody = await foundry.applications.ux.TextEditor.implementation.enrichHTML(
65
+ item.system.body,
66
+ { relativeTo: item, secrets: item.isOwner },
67
+ );
68
+ return context;
69
+ }
70
+
71
+ // ApplicationV2 declares action handlers static and calls them with `this`
72
+ // bound to the sheet instance. `this: NoteSheet` says so to TypeScript.
73
+ static async _onTogglePinned(this: NoteSheet): Promise<void> {
74
+ await this.item.update({ 'system.pinned': !this.item.system.pinned });
75
+ }
76
+ }
@@ -1,16 +1,77 @@
1
1
  /**
2
2
  * {{TITLE}} stylesheet.
3
3
  *
4
- * Scope all rules under the `.{{ID}}` namespace so they don't leak into
5
- * other modules or the host system's sheets. Foundry's CSS cascade is
6
- * already layered, but namespacing makes intent explicit.
4
+ * Every rule is scoped under `.{{ID}}` so nothing leaks into the host system's
5
+ * sheets. Colours come from Foundry's own variables so the sheet follows the
6
+ * light and dark themes.
7
7
  */
8
- .{{ID}}-badge {
9
- display: inline-flex;
8
+ .{{ID}}.sheet .sheet-header {
9
+ display: flex;
10
10
  align-items: center;
11
- gap: 0.4em;
12
- padding: 0.15em 0.5em;
11
+ gap: 0.5rem;
12
+ margin-bottom: 0.5rem;
13
+ }
14
+
15
+ .{{ID}}.sheet .sheet-header .portrait {
16
+ width: 48px;
17
+ height: 48px;
18
+ object-fit: cover;
19
+ border: 1px solid var(--color-border-dark, #7a7971);
13
20
  border-radius: 4px;
14
- background: rgba(0, 0, 0, 0.08);
15
- font-size: 0.8em;
21
+ cursor: pointer;
22
+ }
23
+
24
+ .{{ID}}.sheet .sheet-header input[name="name"] {
25
+ flex: 1;
26
+ font-size: var(--font-size-20, 1.25rem);
27
+ }
28
+
29
+ .{{ID}}.sheet .sheet-header .pin {
30
+ flex: 0 0 auto;
31
+ width: 2rem;
32
+ height: 2rem;
33
+ opacity: 0.5;
34
+ }
35
+
36
+ .{{ID}}.sheet .sheet-header .pin.active {
37
+ opacity: 1;
38
+ color: var(--color-warm-2, #c9593f);
39
+ }
40
+
41
+ .{{ID}}.sheet .sheet-body {
42
+ display: flex;
43
+ flex-direction: column;
44
+ gap: 0.5rem;
45
+ height: 100%;
46
+ }
47
+
48
+ .{{ID}}.sheet prose-mirror,
49
+ .{{ID}}.sheet .body-readonly {
50
+ flex: 1;
51
+ min-height: 12rem;
52
+ }
53
+
54
+ .{{ID}}.sheet .reference {
55
+ display: flex;
56
+ align-items: baseline;
57
+ gap: 0.5rem;
58
+ color: var(--color-text-secondary, #4b4a44);
59
+ font-size: var(--font-size-12, 0.75rem);
60
+ }
61
+
62
+ /* Rendered by the `note` enricher wherever `@Note[id]` appears. */
63
+ a.{{ID}}-note-link {
64
+ display: inline-flex;
65
+ align-items: center;
66
+ gap: 0.25em;
67
+ padding: 0 0.35em;
68
+ border: 1px dotted var(--color-border-dark, #7a7971);
69
+ border-radius: 3px;
70
+ background: var(--color-bg-tertiary, rgba(0, 0, 0, 0.06));
71
+ text-decoration: none;
72
+ }
73
+
74
+ a.{{ID}}-note-link:hover {
75
+ border-style: solid;
76
+ text-shadow: none;
16
77
  }
@@ -0,0 +1,34 @@
1
+ {{!-- Note sheet — one part, no tabs. --}}
2
+ <form autocomplete="off">
3
+
4
+ <header class="sheet-header">
5
+ <img class="portrait" src="{{item.img}}" alt="{{item.name}}" data-edit="img" />
6
+ <input type="text" name="name" value="{{item.name}}"
7
+ placeholder="{{localize '{{LOCALE_PREFIX}}.Sheet.NamePlaceholder'}}" />
8
+ <button type="button" class="pin {{#if system.pinned}}active{{/if}}"
9
+ data-action="togglePinned"
10
+ title="{{localize '{{LOCALE_PREFIX}}.Note.Pinned'}}">
11
+ <i class="fa-solid fa-thumbtack"></i>
12
+ </button>
13
+ </header>
14
+
15
+ <section class="sheet-body">
16
+ {{#if isEditable}}
17
+ <prose-mirror name="system.body"
18
+ button="true"
19
+ editable="{{isEditable}}"
20
+ toggled="false"
21
+ value="{{system.body}}">
22
+ {{{enrichedBody}}}
23
+ </prose-mirror>
24
+ {{else}}
25
+ <div class="body-readonly">{{{enrichedBody}}}</div>
26
+ {{/if}}
27
+
28
+ <footer class="reference">
29
+ <code>{{reference}}</code>
30
+ <small>{{localize '{{LOCALE_PREFIX}}.Note.ReferenceHint'}}</small>
31
+ </footer>
32
+ </section>
33
+
34
+ </form>
@@ -4,6 +4,7 @@
4
4
  "module": "esnext",
5
5
  "moduleResolution": "bundler",
6
6
  "strict": true,
7
+ "noImplicitOverride": true,
7
8
  "esModuleInterop": true,
8
9
  "skipLibCheck": true,
9
10
  "isolatedModules": true,
@@ -13,6 +14,11 @@
13
14
  "resolveJsonModule": true,
14
15
  "allowImportingTsExtensions": false
15
16
  },
16
- "include": ["scripts/**/*"],
17
- "exclude": ["node_modules", "dist"]
17
+ "include": [
18
+ "scripts/**/*"
19
+ ],
20
+ "exclude": [
21
+ "node_modules",
22
+ "dist"
23
+ ]
18
24
  }
@@ -2,55 +2,73 @@
2
2
 
3
3
  {{DESCRIPTION}}
4
4
 
5
- Built on [VTTForge](https://github.com/vttforge/vttforge) — typed data models, declarative sheet boilerplate, migration runner, error catalogue, and a Vite-based build pipeline.
6
-
7
- > **Note** — VTTForge is currently in pre-release and not yet published to npm. The dependencies in `package.json` (`@vttforge/core`, `@vttforge/styles`, `@vttforge/vite-plugin`) will resolve once VTTForge ships its first public release. Until then, work from a local checkout: `pnpm link --global` from each VTTForge package, then `pnpm link --global @vttforge/core @vttforge/styles @vttforge/vite-plugin` here.
5
+ A Foundry VTT v13+ system built on [VTTForge](https://vttforge.dev): typed
6
+ data models, sheet bases that already know their tabs and drops, a migration
7
+ runner, and a build that produces what foundryvtt.com expects.
8
8
 
9
9
  ## Quick start
10
10
 
11
11
  ```bash
12
12
  pnpm install
13
- pnpm dev # vite build --watch + auto-symlinks dist/ into Foundry's Data
14
- pnpm build # one-shot build + emits {{ID}}-<version>.zip for foundryvtt.com
13
+ pnpm dev # build, link dist/ into Foundry's data dir, watch
14
+ pnpm build # dist/ plus {{ID}}-<version>.zip
15
15
  ```
16
16
 
17
- `pnpm dev` (`vttforge dev` under the hood) auto-detects your Foundry user-data
18
- directory on the first run, prompts you to confirm it, and saves the choice to
19
- `.vttforge/config.json` for next time. Override with `--data-dir <path>` or set
20
- `FOUNDRY_DATA_DIR` in your environment.
17
+ `pnpm dev` asks where Foundry keeps its data on the first run and remembers
18
+ the answer in `.vttforge/config.json`. Override with `--foundry-data <path>`
19
+ or `FOUNDRY_DATA_DIR`. If Foundry runs in a container it cannot follow the
20
+ symlink, and the command prints the compose mount to use instead.
21
21
 
22
- Run Foundry with `--hotReload` so saved files reload the world without a page
23
- refresh — `dev` watches `dist/` for changes, and Foundry's built-in dispatcher
24
- swaps CSS / Handlebars / JSON on the fly.
22
+ Save a template and the open sheet redraws in place; save a stylesheet and the
23
+ CSS swaps. Enable **VTTForge Dev** in the world once; `pnpm dev` links it in.
25
24
 
26
- Then launch Foundry, create a world that uses **{{TITLE}}**, and open a character — the sheet is the `CharacterSheet` shipped in `scripts/sheets/`.
25
+ Then create a world on **{{TITLE}}** and open a character.
27
26
 
28
27
  ## What's inside
29
28
 
30
29
  | Path | Purpose |
31
30
  |---|---|
32
- | `system.json` | Manifest (id, compatibility, document types, manifest-side sanitization paths) |
33
- | `template.json` | Foundry document type declarations (Actor + Item subtypes) |
34
- | `scripts/main.mjs` | Entry point — one call to `registerSystem` from `@vttforge/core` |
35
- | `scripts/data/*.mjs` | Typed data models (`BaseTypeDataModel()` from `@vttforge/core`) |
36
- | `scripts/sheets/*.mjs` | Sheet boilerplate eliminators (`BaseActorSheet()` / `BaseItemSheet()`) |
37
- | `scripts/migrations.mjs` | `createMigrationRunner()` — versioned data migrations |
38
- | `templates/` | Handlebars templates for sheets |
39
- | `styles/main.css` | Stylesheet — imports `@vttforge/styles` as the design system base |
40
- | `lang/en.json` | Localization strings |
31
+ | `system.json` | Manifest: types, `htmlFields`, hot-reload paths, migration flags |
32
+ | `template.json` | The type names Foundry expects to see declared |
33
+ | `scripts/main.mjs` | One `registerSystem` call: models, sheets, initiative, settings, migrations |
34
+ | `scripts/data/*.mjs` | Data models. The schema is a function handed to `BaseTypeDataModel`, which is what keeps the schema in one place |
35
+ | `scripts/sheets/*.mjs` | Sheets on `BaseActorSheet` / `BaseItemSheet`: `static TABS`, `static DRAG_DROP`, `onDropItem` |
36
+ | `scripts/migrations.mjs` | `createMigrationRunner`: versioned, idempotent, GM-gated |
37
+ | `templates/` | Handlebars, using v13's own elements (`<prose-mirror>`, `data-action`) |
38
+ | `styles/main.css` | Imports `@vttforge/styles` and scopes your rules under `.{{ID}}` |
39
+ | `lang/en.json` | Strings, under the `{{LOCALE_PREFIX}}` prefix |
40
+
41
+ ## Two things worth knowing before you edit
42
+
43
+ **Sheets are registered by id, not by class name.** `registerSystem({ sheets })`
44
+ pins each sheet under `{{ID}}.<id>`. Foundry saves that key on every actor
45
+ whose owner picked the sheet, and derives it from the class name unless told
46
+ otherwise, and a bundler renames classes between builds. Keep the ids; renaming one
47
+ loses the sheet choice on every document already using it.
48
+
49
+ **Derived values live in the schema.** `mod` on each ability is computed in
50
+ `prepareDerivedData`, but it is declared as a field. JavaScript has no
51
+ `declare`: a plain class field would emit and reset the property to
52
+ `undefined` after every data preparation.
53
+
54
+ ## Checks
41
55
 
42
- ## Releasing to Foundry
56
+ ```bash
57
+ npx vttforge audit # manifest + source against the v13 list of quiet breakages
58
+ ```
43
59
 
44
- Tag-push triggers `.github/workflows/release.yml`, which builds the system, zips `dist/`, and attaches both `{{ID}}-<version>.zip` and `dist/system.json` to a GitHub Release.
60
+ ## Releasing
61
+
62
+ Push a tag and `.github/workflows/release.yml` builds, zips, and attaches
63
+ `{{ID}}-<version>.zip` plus `system.json` to a GitHub Release:
45
64
 
46
65
  ```bash
47
66
  git tag v0.1.0
48
67
  git push --tags
49
68
  ```
50
69
 
51
- Foundry installs from the `system.json` URL on the Release — point users at the `latest/download/system.json` URL in the release notes so they auto-update on every tag.
52
-
53
- For foundryvtt.com submissions, submit the same manifest URL pattern.
70
+ Point Foundry, and foundryvtt.com, at the release's
71
+ `latest/download/system.json`, so installs auto-update on every tag.
54
72
 
55
73
  ## License
56
74
 
@@ -1,78 +1,84 @@
1
1
  /**
2
- * CharacterData — typed schema for the `character` Actor type.
2
+ * CharacterData — the `character` Actor type.
3
3
  *
4
- * Quick stats are HP / AC / SPD / INIT, abilities are the six classic
5
- * scores (str/dex/con/int/wis/cha). Derived state (modifiers, max HP,
6
- * armor class, initiative) is computed in-memory in `prepareDerivedData`.
7
- *
8
- * `prepareBaseData` initialises fields that Active Effects need to mutate
9
- * (base max HP before any AE bonus); `prepareDerivedData` then computes
10
- * the AE-aware values (modifiers, percentages, totals). Never write to the
11
- * database in either hook — they're purely in-memory derivations.
4
+ * Quick stats are HP / AC / SPD / INIT; abilities are the six classic scores.
5
+ * Derived state (modifiers, max HP, armour class, initiative) is computed in
6
+ * memory in `prepareDerivedData`. Never write to the database from there.
12
7
  */
13
8
  import { BaseTypeDataModel, fields } from '@vttforge/core';
14
9
 
15
- export class CharacterData extends BaseTypeDataModel() {
16
- static defineSchema() {
17
- const f = fields();
18
- // The six ability scores are written once, as a factory rather than a
19
- // shared options object: a field keeps the options it was handed, and
20
- // some field classes write back into them.
21
- const score = () =>
22
- new f.NumberField({
10
+ /**
11
+ * The schema, as a function handed to the factory.
12
+ *
13
+ * It has to be a function rather than an object because `fields()` reads a
14
+ * Foundry global that does not exist when this module is first evaluated.
15
+ * Handing it to `BaseTypeDataModel(...)` rather than declaring a `static
16
+ * defineSchema()` is also what types the fields if you ever add `checkJs`.
17
+ */
18
+ const defineCharacterSchema = () => {
19
+ const f = fields();
20
+
21
+ // One ability. `value` is stored and is what the sheet's input writes to
22
+ // (`name="system.abilities.str.value"`). `mod` is derived on every data
23
+ // preparation; it lives in the schema because JavaScript has no `declare`
24
+ // — a class field would emit and reset the property to undefined.
25
+ const score = () =>
26
+ new f.SchemaField({
27
+ value: new f.NumberField({
23
28
  required: true,
24
29
  nullable: false,
25
30
  integer: true,
26
31
  min: 1,
27
32
  max: 30,
28
33
  initial: 10,
29
- });
30
- return {
31
- level: new f.NumberField({
32
- required: true,
33
- nullable: false,
34
- integer: true,
35
- min: 1,
36
- max: 20,
37
- initial: 1,
38
34
  }),
39
- speed: new f.NumberField({
40
- required: true,
41
- nullable: false,
42
- integer: true,
43
- min: 0,
44
- initial: 30,
45
- }),
46
- abilities: new f.SchemaField({
47
- str: score(),
48
- dex: score(),
49
- con: score(),
50
- int: score(),
51
- wis: score(),
52
- cha: score(),
53
- }),
54
- health: new f.SchemaField({
55
- value: new f.NumberField({ required: true, nullable: false, integer: true, min: 0, initial: 10 }),
56
- max: new f.NumberField({ required: true, nullable: false, integer: true, min: 0, initial: 10 }),
57
- }),
58
- power: new f.SchemaField({
59
- value: new f.NumberField({ required: true, nullable: false, integer: true, min: 0, initial: 5 }),
60
- max: new f.NumberField({ required: true, nullable: false, integer: true, min: 0, initial: 5 }),
61
- }),
62
- biography: new f.HTMLField(),
63
- };
64
- }
35
+ mod: new f.NumberField({ required: true, nullable: false, integer: true, initial: 0 }),
36
+ });
65
37
 
38
+ return {
39
+ level: new f.NumberField({
40
+ required: true,
41
+ nullable: false,
42
+ integer: true,
43
+ min: 1,
44
+ max: 20,
45
+ initial: 1,
46
+ }),
47
+ speed: new f.NumberField({
48
+ required: true,
49
+ nullable: false,
50
+ integer: true,
51
+ min: 0,
52
+ initial: 30,
53
+ }),
54
+ abilities: new f.SchemaField({
55
+ str: score(),
56
+ dex: score(),
57
+ con: score(),
58
+ int: score(),
59
+ wis: score(),
60
+ cha: score(),
61
+ }),
62
+ health: new f.SchemaField({
63
+ value: new f.NumberField({ required: true, nullable: false, integer: true, min: 0, initial: 10 }),
64
+ max: new f.NumberField({ required: true, nullable: false, integer: true, min: 0, initial: 10 }),
65
+ }),
66
+ power: new f.SchemaField({
67
+ value: new f.NumberField({ required: true, nullable: false, integer: true, min: 0, initial: 5 }),
68
+ max: new f.NumberField({ required: true, nullable: false, integer: true, min: 0, initial: 5 }),
69
+ }),
70
+ biography: new f.HTMLField(),
71
+ };
72
+ };
73
+
74
+ export class CharacterData extends BaseTypeDataModel(defineCharacterSchema) {
75
+ /** @override */
66
76
  prepareDerivedData() {
67
- for (const [key, value] of Object.entries(this.abilities)) {
68
- const score = typeof value === 'number' ? value : (value?.value ?? 10);
69
- const mod = Math.floor((score - 10) / 2);
70
- this.abilities[key] = { value: score, mod };
77
+ for (const ability of Object.values(this.abilities)) {
78
+ ability.mod = Math.floor((ability.value - 10) / 2);
71
79
  }
72
80
 
73
- const conMod = this.abilities.con.mod;
74
- this.health.max = 10 + this.level + conMod;
75
-
81
+ this.health.max = 10 + this.level + this.abilities.con.mod;
76
82
  this.armorClass = 10 + this.abilities.dex.mod;
77
83
  this.initiative = this.abilities.dex.mod;
78
84
  }