@weasel-js/modes 1.9.3 → 1.10.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.
@@ -7,8 +7,15 @@ declare const ALL_TAGS: readonly ["navigation", "creates-selection", "creates-pa
7
7
  * set this package ships. */
8
8
  type CapabilityTag = (typeof ALL_TAGS)[number] | (string & {});
9
9
  /** Tags that are implicitly allowed in every mode — never listed per-mode.
10
- * A tool tagged with any of these is always eligible. */
10
+ * A tool carrying only these is eligible everywhere. */
11
11
  declare const IMPLICIT_TAGS: readonly CapabilityTag[];
12
+ /**
13
+ * Whether a tool or contribution carrying `tags` may be used while `allowed`
14
+ * is the set of tags the active mode permits. Every tag has to be allowed: a
15
+ * tag names something the entry does, and one refused thing refuses the entry.
16
+ * An entry with no tags asks for nothing and is allowed everywhere.
17
+ */
18
+ declare function capabilitiesAllowed(allowed: ReadonlySet<string>, tags: readonly CapabilityTag[] | undefined): boolean;
12
19
  /** Whether a string is one of the shipped `ALL_TAGS`. An app-defined tag is
13
20
  * still a valid `CapabilityTag` and will answer `false` here. */
14
21
  declare function isCapabilityTag(value: string): value is CapabilityTag;
@@ -95,4 +102,4 @@ declare const DEFAULT_MODES: readonly ModeDefinition[];
95
102
  * app's own mode set, use the registry's `byId` instead. */
96
103
  declare function byId(id: string): ModeDefinition;
97
104
 
98
- export { ALL_TAGS as A, type CapabilityTag as C, DEFAULT_MODES as D, FREE_TRANSFORM as F, IMPLICIT_TAGS as I, type ModeDefinition as M, NORMAL as N, PATH_EDIT as P, TEXT_EDIT as T, type WorkspaceVisual as W, CROP as a, ISOLATION as b, byId as c, eligibleForMode as e, isCapabilityTag as i, modeLabel as m };
105
+ export { ALL_TAGS as A, type CapabilityTag as C, DEFAULT_MODES as D, FREE_TRANSFORM as F, IMPLICIT_TAGS as I, type ModeDefinition as M, NORMAL as N, PATH_EDIT as P, TEXT_EDIT as T, type WorkspaceVisual as W, CROP as a, ISOLATION as b, byId as c, capabilitiesAllowed as d, eligibleForMode as e, isCapabilityTag as i, modeLabel as m };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { M as ModeDefinition, C as CapabilityTag } from './default-BJzHWrIR.js';
2
- export { A as ALL_TAGS, a as CROP, D as DEFAULT_MODES, F as FREE_TRANSFORM, I as IMPLICIT_TAGS, b as ISOLATION, N as NORMAL, P as PATH_EDIT, T as TEXT_EDIT, W as WorkspaceVisual, c as byId, e as eligibleForMode, i as isCapabilityTag, m as modeLabel } from './default-BJzHWrIR.js';
1
+ import { M as ModeDefinition, C as CapabilityTag } from './default-6A7txdga.js';
2
+ export { A as ALL_TAGS, a as CROP, D as DEFAULT_MODES, F as FREE_TRANSFORM, I as IMPLICIT_TAGS, b as ISOLATION, N as NORMAL, P as PATH_EDIT, T as TEXT_EDIT, W as WorkspaceVisual, c as byId, d as capabilitiesAllowed, e as eligibleForMode, i as isCapabilityTag, m as modeLabel } from './default-6A7txdga.js';
3
3
  import { Reflection } from '@weasel-js/registry';
4
4
 
5
5
  /** Options for `createModeRegistry`: the modes to register and which one is
@@ -24,7 +24,8 @@ interface ModeRegistry {
24
24
  * also advances on `setMode`. */
25
25
  readonly reflection: Reflection<ModeDefinition>;
26
26
  }
27
- /** Build a mode registry. Throws if `initial` does not name one of `modes`. */
27
+ /** Build a mode registry. Throws if two of `modes` share an id, or if
28
+ * `initial` does not name one of them. */
28
29
  declare function createModeRegistry(opts: CreateModeRegistryOptions): ModeRegistry;
29
30
  /** The active mode as a canvas gates on it: its id, and every capability tag
30
31
  * a tool may carry to be usable in it — the mode's `allows` plus
@@ -54,12 +55,13 @@ interface CreateModeDecorationsOptions {
54
55
  * for as long as the mode is active and belong to the mode rather than to any
55
56
  * tool — path-edit's anchor dots and handle lines are the motivating case. */
56
57
  interface ModeDecorations {
57
- /** Register (or replace) the painter for a mode id. */
58
- register(modeId: string, painter: ModeDecorationPainter): void;
58
+ /** Register (or replace) the painter for a mode id. Returns a release that
59
+ * removes it, and does nothing once another painter has replaced it. */
60
+ register(modeId: string, painter: ModeDecorationPainter): () => void;
59
61
  /** Paint the active mode's decorations. Returns an empty array if no
60
62
  * painter is registered for the active mode. */
61
63
  paint(): ModeDrawCommand[];
62
- /** Monotonic version. Bumps on mode change or painter registration. */
64
+ /** Monotonic version. Bumps on mode change, painter registration, or release. */
63
65
  getVersion(): number;
64
66
  /** Notified whenever `getVersion` bumps — what a canvas repaints on. */
65
67
  subscribe(listener: () => void): () => void;
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export { CROP, DEFAULT_MODES, FREE_TRANSFORM, ISOLATION, NORMAL, PATH_EDIT, TEXT_EDIT, byId } from './chunk-DPP5MVDO.js';
2
- import { createReflectable } from '@weasel-js/registry';
2
+ import { createReflectable, createEmitter } from '@weasel-js/registry';
3
3
 
4
4
  // src/capabilities.ts
5
5
  var ALL_TAGS = [
@@ -16,6 +16,9 @@ var ALL_TAGS = [
16
16
  "edits-page"
17
17
  ];
18
18
  var IMPLICIT_TAGS = ["navigation"];
19
+ function capabilitiesAllowed(allowed, tags) {
20
+ return tags === void 0 || tags.every((tag) => allowed.has(tag));
21
+ }
19
22
  function isCapabilityTag(value) {
20
23
  return ALL_TAGS.includes(value);
21
24
  }
@@ -25,16 +28,17 @@ function modeLabel(mode) {
25
28
  return mode.label ?? mode.id;
26
29
  }
27
30
  function eligibleForMode(mode, toolTags) {
28
- if (toolTags.length === 0) return false;
29
- const allowed = /* @__PURE__ */ new Set([...mode.allows, ...IMPLICIT_TAGS]);
30
- for (const t of toolTags) {
31
- if (allowed.has(t)) return true;
32
- }
33
- return false;
31
+ return capabilitiesAllowed(allowedCapabilitiesOf(mode), toolTags);
32
+ }
33
+ function allowedCapabilitiesOf(mode) {
34
+ return /* @__PURE__ */ new Set([...mode.allows, ...IMPLICIT_TAGS]);
34
35
  }
35
36
  function createModeRegistry(opts) {
36
37
  const modes = createReflectable();
37
- for (const m of opts.modes) modes.set(m.id, m);
38
+ for (const m of opts.modes) {
39
+ if (modes.get(m.id)) throw new Error(`createModeRegistry: duplicate mode id "${m.id}"`);
40
+ modes.set(m.id, m);
41
+ }
38
42
  const initial = modes.get(opts.initial);
39
43
  if (!initial) throw new Error(`Initial mode "${opts.initial}" not in modes list`);
40
44
  let active = initial;
@@ -59,7 +63,7 @@ function createModeRegistry(opts) {
59
63
  };
60
64
  }
61
65
  function activeModeOf(mode) {
62
- return { id: mode.id, allowedCapabilities: /* @__PURE__ */ new Set([...mode.allows, ...IMPLICIT_TAGS]) };
66
+ return { id: mode.id, allowedCapabilities: allowedCapabilitiesOf(mode) };
63
67
  }
64
68
  function getActiveModeFor(registry) {
65
69
  let cached = null;
@@ -69,18 +73,24 @@ function getActiveModeFor(registry) {
69
73
  return cached.value;
70
74
  };
71
75
  }
72
-
73
- // src/decorations.ts
74
76
  function createModeDecorations(opts) {
75
77
  const { registry } = opts;
76
78
  const painters = /* @__PURE__ */ new Map();
77
- const listeners = /* @__PURE__ */ new Set();
79
+ const listeners = createEmitter();
78
80
  let registrations = 0;
81
+ const changed = () => {
82
+ registrations++;
83
+ listeners.emit();
84
+ };
79
85
  return {
80
86
  register(modeId, painter) {
81
87
  painters.set(modeId, painter);
82
- registrations++;
83
- for (const l of [...listeners]) l();
88
+ changed();
89
+ return () => {
90
+ if (painters.get(modeId) !== painter) return;
91
+ painters.delete(modeId);
92
+ changed();
93
+ };
84
94
  },
85
95
  paint() {
86
96
  const painter = painters.get(registry.current().id);
@@ -89,10 +99,10 @@ function createModeDecorations(opts) {
89
99
  },
90
100
  getVersion: () => registry.getVersion() + registrations,
91
101
  subscribe(listener) {
92
- listeners.add(listener);
102
+ const off = listeners.subscribe(listener);
93
103
  const offMode = registry.subscribe(listener);
94
104
  return () => {
95
- listeners.delete(listener);
105
+ off();
96
106
  offMode();
97
107
  };
98
108
  }
@@ -106,12 +116,10 @@ function eligibleTool(reg, tool) {
106
116
  function eligibleContribution(reg, entry) {
107
117
  return eligibleForMode(reg.current(), entry.eligibility?.capabilities ?? []);
108
118
  }
109
-
110
- // src/scopingLayer.ts
111
119
  function createScopingDim(opts) {
112
120
  const { registry } = opts;
113
121
  const dim = opts.dimAlpha ?? 0.3;
114
- const listeners = /* @__PURE__ */ new Set();
122
+ const listeners = createEmitter();
115
123
  let invalidations = 0;
116
124
  return {
117
125
  alphaFor(id) {
@@ -126,20 +134,20 @@ function createScopingDim(opts) {
126
134
  },
127
135
  invalidate() {
128
136
  invalidations++;
129
- for (const l of [...listeners]) l();
137
+ listeners.emit();
130
138
  },
131
139
  getVersion: () => registry.getVersion() + invalidations,
132
140
  subscribe(listener) {
133
- listeners.add(listener);
141
+ const off = listeners.subscribe(listener);
134
142
  const offMode = registry.subscribe(listener);
135
143
  return () => {
136
- listeners.delete(listener);
144
+ off();
137
145
  offMode();
138
146
  };
139
147
  }
140
148
  };
141
149
  }
142
150
 
143
- export { ALL_TAGS, IMPLICIT_TAGS, activeModeOf, createModeDecorations, createModeRegistry, createScopingDim, eligibleContribution, eligibleForMode, eligibleTool, getActiveModeFor, isCapabilityTag, modeLabel };
151
+ export { ALL_TAGS, IMPLICIT_TAGS, activeModeOf, capabilitiesAllowed, createModeDecorations, createModeRegistry, createScopingDim, eligibleContribution, eligibleForMode, eligibleTool, getActiveModeFor, isCapabilityTag, modeLabel };
144
152
  //# sourceMappingURL=index.js.map
145
153
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/capabilities.ts","../src/modeDefinition.ts","../src/registry.ts","../src/decorations.ts","../src/eligibility.ts","../src/scopingLayer.ts"],"names":[],"mappings":";;;;AAGO,IAAM,QAAA,GAAW;AAAA,EACtB,YAAA;AAAA,EACA,mBAAA;AAAA,EACA,eAAA;AAAA,EACA,gBAAA;AAAA,EACA,cAAA;AAAA,EACA,eAAA;AAAA,EACA,YAAA;AAAA,EACA,sBAAA;AAAA,EACA,eAAA;AAAA,EACA,cAAA;AAAA,EACA;AACF;AASO,IAAM,aAAA,GAA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AA,IAAY,GAAA;AAC7B,EAAA,MAAM,SAAA,uBAAgB,GAAA,EAAgB;AACtC,EAAA,IAAI,aAAA,GAAgB,CAAA;AACpB,EAAA,OAAO;AAAA,IACL,SAAS,EAAA,EAAI;AACX,MAAA,MAAM,IAAA,GAAO,SAAS,OAAA,EAAQ;AAC9B,MAAA,IAAI,CAAC,IAAA,CAAK,OAAA,EAAS,OAAO,CAAA;AAC1B,MAAA,OAAO,KAAK,YAAA,EAAa,CAAE,GAAA,CAAI,EAAE,IAAI,CAAA,GAAI,GAAA;AAAA,IAC3C,CAAA;AAAA,IACA,qBAAqB,EAAA,EAAI;AACvB,MAAA,MAAM,IAAA,GAAO,SAAS,OAAA,EAAQ;AAC9B,MAAA,IAAI,CAAC,IAAA,CAAK,OAAA,EAAS,OAAO,IAAA;AAC1B,MAAA,OAAO,IAAA,CAAK,YAAA,EAAa,CAAE,GAAA,CAAI,EAAE,CAAA;AAAA,IACnC,CAAA;AAAA,IACA,UAAA,GAAa;AACX,MAAA,aAAA,EAAA;AACA,MAAA,KAAA,MAAW,CAAA,IAAK,CAAC,GAAG,SAAS,GAAG,CAAA,EAAE;AAAA,IACpC,CAAA;AAAA,IACA,UAAA,EAAY,MAAM,QAAA,CAAS,UAAA,EAAW,GAAI,aAAA;AAAA,IAC1C,UAAU,QAAA,EAAU;AAClB,MAAA,SAAA,CAAU,IAAI,QAAQ,CAAA;AACtB,MAAA,MAAM,OAAA,GAAU,QAAA,CAAS,SAAA,CAAU,QAAQ,CAAA;AAC3C,MAAA,OAAO,MAAM;AACX,QAAA,SAAA,CAAU,OAAO,QAAQ,CAAA;AACzB,QAAA,OAAA,EAAQ;AAAA,MACV,CAAA;AAAA,IACF;AAAA,GACF;AACF","file":"index.js","sourcesContent":["/** The full vocabulary of capability tags shipped in the default preset.\n * Apps and other consumers can add their own tags; this list is what\n * `weasel-modes` itself uses. */\nexport const ALL_TAGS = [\n 'navigation',\n 'creates-selection',\n 'creates-paths',\n 'creates-shapes',\n 'creates-text',\n 'edits-anchors',\n 'edits-text',\n 'transforms-selection',\n 'samples-color',\n 'applies-fill',\n 'edits-page',\n] as const;\n\n/** One capability a tool or contribution declares, and a mode allows. Any\n * string is accepted so apps can add tags of their own; `ALL_TAGS` is the\n * set this package ships. */\nexport type CapabilityTag = (typeof ALL_TAGS)[number] | (string & {});\n\n/** Tags that are implicitly allowed in every mode — never listed per-mode.\n * A tool tagged with any of these is always eligible. */\nexport const IMPLICIT_TAGS: readonly CapabilityTag[] = ['navigation'];\n\n/** Whether a string is one of the shipped `ALL_TAGS`. An app-defined tag is\n * still a valid `CapabilityTag` and will answer `false` here. */\nexport function isCapabilityTag(value: string): value is CapabilityTag {\n return (ALL_TAGS as readonly string[]).includes(value);\n}\n","import { IMPLICIT_TAGS, type CapabilityTag } from './capabilities';\n\n/** How a mode tints the workspace — the area around the page — so the user\n * can see at a glance which mode is active. */\nexport interface WorkspaceVisual {\n tint?: string;\n gradient?: 'top-down' | 'bottom-up';\n intensity?: number;\n}\n\n/**\n * A mode: an app-level editing context that narrows which tools are usable and\n * how the workspace looks. Tools live inside modes; a tool is never \"in\" one.\n *\n * `kind` picks the lifecycle. A `soft` mode (path-edit, isolation, text-edit)\n * is a scoped context with no commit ceremony — every edit inside it is\n * independently undoable and `exit` is non-destructive. A `strict` mode\n * (free-transform, crop) is a transaction: the whole session collapses to one\n * undoable step and leaving requires an explicit `commit` or `cancel`.\n *\n * The four shortcut fields are key chords (`'Escape'`, `'Meta+T'`) that\n * `modeShortcuts` in `@weasel-js/routing` turns into dispatcher bindings,\n * each live only while the mode it acts on is (or, for `entry`, is not).\n */\nexport interface ModeDefinition {\n id: string;\n /** Human-readable name for chrome — a breadcrumb, a status bar. Falls back\n * to `id`; read it through {@link modeLabel}. */\n label?: string;\n /** One sentence on what the mode is for, for a tooltip or a hint line. */\n description?: string;\n kind: 'soft' | 'strict';\n /** Capability tags this mode allows beyond IMPLICIT_TAGS. */\n allows: CapabilityTag[];\n /** When true, out-of-target objects dim at the renderer layer. */\n scoping: boolean;\n workspace?: WorkspaceVisual;\n entry?: { shortcut?: string; trigger?: 'double-click-target' };\n exit?: { shortcut?: string };\n commit?: { shortcut?: string };\n cancel?: { shortcut?: string };\n /** Leave a soft mode and throw its session away, where `exit` keeps it to\n * resume. */\n discard?: { shortcut?: string };\n}\n\n/** A mode's display name: its `label`, else its `id`. */\nexport function modeLabel(mode: ModeDefinition): string {\n return mode.label ?? mode.id;\n}\n\n/** True iff a tool carrying `toolTags` is eligible for `mode`. */\nexport function eligibleForMode(mode: ModeDefinition, toolTags: readonly CapabilityTag[]): boolean {\n if (toolTags.length === 0) return false;\n const allowed = new Set<CapabilityTag>([...mode.allows, ...IMPLICIT_TAGS]);\n for (const t of toolTags) {\n if (allowed.has(t)) return true;\n }\n return false;\n}\n","import { createReflectable, type Reflection } from '@weasel-js/registry';\nimport { IMPLICIT_TAGS } from './capabilities';\nimport type { ModeDefinition } from './modeDefinition';\n\n/** Options for `createModeRegistry`: the modes to register and which one is\n * active at startup. */\nexport interface CreateModeRegistryOptions {\n modes: readonly ModeDefinition[];\n initial: string;\n}\n\n/** Holds the set of available modes and which one is active, and notifies\n * subscribers when that changes. `getVersion` is a monotonic counter for\n * render-cache invalidation. Unknown mode ids throw rather than being\n * ignored. */\nexport interface ModeRegistry {\n current(): ModeDefinition;\n /** Every registered mode, in registration order. */\n list(): readonly ModeDefinition[];\n setMode(id: string): void;\n byId(id: string): ModeDefinition;\n getVersion(): number;\n subscribe(listener: () => void): () => void;\n /** The registered modes keyed by id. Shares this registry's version, so it\n * also advances on `setMode`. */\n readonly reflection: Reflection<ModeDefinition>;\n}\n\n/** Build a mode registry. Throws if `initial` does not name one of `modes`. */\nexport function createModeRegistry(opts: CreateModeRegistryOptions): ModeRegistry {\n const modes = createReflectable<ModeDefinition>();\n for (const m of opts.modes) modes.set(m.id, m);\n const initial = modes.get(opts.initial);\n if (!initial) throw new Error(`Initial mode \"${opts.initial}\" not in modes list`);\n\n let active: ModeDefinition = initial;\n\n return {\n current: () => active,\n list: () => opts.modes,\n setMode(id: string): void {\n const next = modes.get(id);\n if (!next) throw new Error(`Unknown mode id: ${id}`);\n if (next === active) return;\n active = next;\n modes.bump();\n },\n byId(id: string): ModeDefinition {\n const m = modes.get(id);\n if (!m) throw new Error(`Unknown mode id: ${id}`);\n return m;\n },\n getVersion: modes.getVersion,\n subscribe: modes.subscribe,\n reflection: modes.reflection,\n };\n}\n\n/** The active mode as a canvas gates on it: its id, and every capability tag\n * a tool may carry to be usable in it — the mode's `allows` plus\n * `IMPLICIT_TAGS`. The shape `<SceneCanvas modes>` gates on. */\nexport interface ActiveMode {\n id: string;\n allowedCapabilities: ReadonlySet<string>;\n}\n\n/** A mode definition as a canvas gates on it. */\nexport function activeModeOf(mode: ModeDefinition): ActiveMode {\n return { id: mode.id, allowedCapabilities: new Set<string>([...mode.allows, ...IMPLICIT_TAGS]) };\n}\n\n/** A reader for the registry's active mode, as `<SceneCanvas modes>` builds\n * one. It returns the same object until the mode changes. */\nexport function getActiveModeFor(registry: ModeRegistry): () => ActiveMode {\n let cached: { version: number; value: ActiveMode } | null = null;\n return () => {\n const version = registry.getVersion();\n if (cached?.version !== version) cached = { version, value: activeModeOf(registry.current()) };\n return cached.value;\n };\n}\n","import type { ModeRegistry } from './registry';\n\n/** Loose draw-command type — the kit's `DrawCommand` union shape is over in\n * `src/renderer`. We keep this opaque to avoid a cross-package import in\n * the type surface; consumers cast to the real DrawCommand at use site. */\nexport type ModeDrawCommand = unknown;\n\n/** Produces the draw commands for one mode's persistent decorations. Called\n * once per paint while that mode is active. */\nexport type ModeDecorationPainter = () => ModeDrawCommand[];\n\n/** Options for `createModeDecorations`. */\nexport interface CreateModeDecorationsOptions {\n registry: ModeRegistry;\n}\n\n/** Per-mode decoration painters, keyed by mode id. Mode decorations are drawn\n * for as long as the mode is active and belong to the mode rather than to any\n * tool — path-edit's anchor dots and handle lines are the motivating case. */\nexport interface ModeDecorations {\n /** Register (or replace) the painter for a mode id. */\n register(modeId: string, painter: ModeDecorationPainter): void;\n /** Paint the active mode's decorations. Returns an empty array if no\n * painter is registered for the active mode. */\n paint(): ModeDrawCommand[];\n /** Monotonic version. Bumps on mode change or painter registration. */\n getVersion(): number;\n /** Notified whenever `getVersion` bumps — what a canvas repaints on. */\n subscribe(listener: () => void): () => void;\n}\n\n/** Build a decoration registry bound to a mode registry, so switching modes\n * switches which painter runs. */\nexport function createModeDecorations(opts: CreateModeDecorationsOptions): ModeDecorations {\n const { registry } = opts;\n const painters = new Map<string, ModeDecorationPainter>();\n const listeners = new Set<() => void>();\n let registrations = 0;\n\n return {\n register(modeId: string, painter: ModeDecorationPainter): void {\n painters.set(modeId, painter);\n registrations++;\n for (const l of [...listeners]) l();\n },\n paint(): ModeDrawCommand[] {\n const painter = painters.get(registry.current().id);\n if (!painter) return [];\n return painter();\n },\n getVersion: () => registry.getVersion() + registrations,\n subscribe(listener: () => void): () => void {\n listeners.add(listener);\n const offMode = registry.subscribe(listener);\n return () => {\n listeners.delete(listener);\n offMode();\n };\n },\n };\n}\n","import type { ModeRegistry } from './registry';\nimport type { CapabilityTag } from './capabilities';\nimport { eligibleForMode } from './modeDefinition';\n\n/** The part of a tool this package needs in order to judge eligibility: its\n * id and the capabilities it claims. Structural, so a real `ToolDef`\n * satisfies it without this package depending on the kit. */\nexport interface ToolLike {\n id: string;\n capabilities?: readonly CapabilityTag[];\n}\n\n/** True iff `tool` is usable in the registry's currently active mode. */\nexport function eligibleTool(reg: ModeRegistry, tool: ToolLike): boolean {\n return eligibleForMode(reg.current(), tool.capabilities ?? []);\n}\n\n/** True iff a contribution is usable in the registry's active mode. */\nexport function eligibleContribution(\n reg: ModeRegistry,\n entry: { id: string; eligibility?: { capabilities?: readonly CapabilityTag[] } },\n): boolean {\n return eligibleForMode(reg.current(), entry.eligibility?.capabilities ?? []);\n}\n","import type { ModeRegistry } from './registry';\n\n/** Options for `createScopingDim`. */\nexport interface CreateScopingDimOptions {\n registry: ModeRegistry;\n /** Returns the set of node ids that are *in* scope for the current mode.\n * The app computes this — typically: selection-only for path-edit,\n * isolated subtree for isolation, empty for non-scoping modes. */\n getTargetIds: () => ReadonlySet<string>;\n /** Alpha for out-of-scope nodes. Default 0.3 (spec). */\n dimAlpha?: number;\n}\n\n/** Answers, per node, how a scoping mode should treat it: dimmed or not,\n * hit-testable or not. Consult it from the renderer and the hit-tester —\n * it computes nothing itself. Its answers change when the mode does, or when\n * the app says the target set moved (`invalidate`); both notify\n * subscribers, so a canvas can repaint on `subscribe` (`<SceneCanvas\n * redrawOn>`). */\nexport interface ScopingDim {\n /** Multiplier in [0, 1] to apply to a node's render alpha. */\n alphaFor(id: string): number;\n /** Whether the node should respond to pointer hits. */\n isPointerInteractive(id: string): boolean;\n /** Tell subscribers the target set changed without a mode switch. */\n invalidate(): void;\n /** Monotonic; bumps on a mode switch and on `invalidate`. */\n getVersion(): number;\n subscribe(listener: () => void): () => void;\n}\n\n/** Build the scoping lookup for a mode registry. In a mode with\n * `scoping: false` every node is full-alpha and interactive, so this is safe\n * to consult unconditionally. */\nexport function createScopingDim(opts: CreateScopingDimOptions): ScopingDim {\n const { registry } = opts;\n const dim = opts.dimAlpha ?? 0.3;\n const listeners = new Set<() => void>();\n let invalidations = 0;\n return {\n alphaFor(id) {\n const mode = registry.current();\n if (!mode.scoping) return 1;\n return opts.getTargetIds().has(id) ? 1 : dim;\n },\n isPointerInteractive(id) {\n const mode = registry.current();\n if (!mode.scoping) return true;\n return opts.getTargetIds().has(id);\n },\n invalidate() {\n invalidations++;\n for (const l of [...listeners]) l();\n },\n getVersion: () => registry.getVersion() + invalidations,\n subscribe(listener) {\n listeners.add(listener);\n const offMode = registry.subscribe(listener);\n return () => {\n listeners.delete(listener);\n offMode();\n };\n },\n };\n}\n"]}
1
+ {"version":3,"sources":["../src/capabilities.ts","../src/modeDefinition.ts","../src/registry.ts","../src/decorations.ts","../src/eligibility.ts","../src/scopingLayer.ts"],"names":["createEmitter"],"mappings":";;;;AAGO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A,UAAA,GAAa;AACX,MAAA,aAAA,EAAA;AACA,MAAA,SAAA,CAAU,IAAA,EAAK;AAAA,IACjB,CAAA;AAAA,IACA,UAAA,EAAY,MAAM,QAAA,CAAS,UAAA,EAAW,GAAI,aAAA;AAAA,IAC1C,UAAU,QAAA,EAAU;AAClB,MAAA,MAAM,GAAA,GAAM,SAAA,CAAU,SAAA,CAAU,QAAQ,CAAA;AACxC,MAAA,MAAM,OAAA,GAAU,QAAA,CAAS,SAAA,CAAU,QAAQ,CAAA;AAC3C,MAAA,OAAO,MAAM;AACX,QAAA,GAAA,EAAI;AACJ,QAAA,OAAA,EAAQ;AAAA,MACV,CAAA;AAAA,IACF;AAAA,GACF;AACF","file":"index.js","sourcesContent":["/** The full vocabulary of capability tags shipped in the default preset.\n * Apps and other consumers can add their own tags; this list is what\n * `weasel-modes` itself uses. */\nexport const ALL_TAGS = [\n 'navigation',\n 'creates-selection',\n 'creates-paths',\n 'creates-shapes',\n 'creates-text',\n 'edits-anchors',\n 'edits-text',\n 'transforms-selection',\n 'samples-color',\n 'applies-fill',\n 'edits-page',\n] as const;\n\n/** One capability a tool or contribution declares, and a mode allows. Any\n * string is accepted so apps can add tags of their own; `ALL_TAGS` is the\n * set this package ships. */\nexport type CapabilityTag = (typeof ALL_TAGS)[number] | (string & {});\n\n/** Tags that are implicitly allowed in every mode — never listed per-mode.\n * A tool carrying only these is eligible everywhere. */\nexport const IMPLICIT_TAGS: readonly CapabilityTag[] = ['navigation'];\n\n/**\n * Whether a tool or contribution carrying `tags` may be used while `allowed`\n * is the set of tags the active mode permits. Every tag has to be allowed: a\n * tag names something the entry does, and one refused thing refuses the entry.\n * An entry with no tags asks for nothing and is allowed everywhere.\n */\nexport function capabilitiesAllowed(allowed: ReadonlySet<string>, tags: readonly CapabilityTag[] | undefined): boolean {\n return tags === undefined || tags.every((tag) => allowed.has(tag));\n}\n\n/** Whether a string is one of the shipped `ALL_TAGS`. An app-defined tag is\n * still a valid `CapabilityTag` and will answer `false` here. */\nexport function isCapabilityTag(value: string): value is CapabilityTag {\n return (ALL_TAGS as readonly string[]).includes(value);\n}\n","import { capabilitiesAllowed, IMPLICIT_TAGS, type CapabilityTag } from './capabilities';\n\n/** How a mode tints the workspace — the area around the page — so the user\n * can see at a glance which mode is active. */\nexport interface WorkspaceVisual {\n tint?: string;\n gradient?: 'top-down' | 'bottom-up';\n intensity?: number;\n}\n\n/**\n * A mode: an app-level editing context that narrows which tools are usable and\n * how the workspace looks. Tools live inside modes; a tool is never \"in\" one.\n *\n * `kind` picks the lifecycle. A `soft` mode (path-edit, isolation, text-edit)\n * is a scoped context with no commit ceremony — every edit inside it is\n * independently undoable and `exit` is non-destructive. A `strict` mode\n * (free-transform, crop) is a transaction: the whole session collapses to one\n * undoable step and leaving requires an explicit `commit` or `cancel`.\n *\n * The four shortcut fields are key chords (`'Escape'`, `'Meta+T'`) that\n * `modeShortcuts` in `@weasel-js/routing` turns into dispatcher bindings,\n * each live only while the mode it acts on is (or, for `entry`, is not).\n */\nexport interface ModeDefinition {\n id: string;\n /** Human-readable name for chrome — a breadcrumb, a status bar. Falls back\n * to `id`; read it through {@link modeLabel}. */\n label?: string;\n /** One sentence on what the mode is for, for a tooltip or a hint line. */\n description?: string;\n kind: 'soft' | 'strict';\n /** Capability tags this mode allows beyond IMPLICIT_TAGS. */\n allows: CapabilityTag[];\n /** When true, out-of-target objects dim at the renderer layer. */\n scoping: boolean;\n workspace?: WorkspaceVisual;\n entry?: { shortcut?: string; trigger?: 'double-click-target' };\n exit?: { shortcut?: string };\n commit?: { shortcut?: string };\n cancel?: { shortcut?: string };\n /** Leave a soft mode and throw its session away, where `exit` keeps it to\n * resume. */\n discard?: { shortcut?: string };\n}\n\n/** A mode's display name: its `label`, else its `id`. */\nexport function modeLabel(mode: ModeDefinition): string {\n return mode.label ?? mode.id;\n}\n\n/** True iff a tool carrying `toolTags` is eligible for `mode`. */\nexport function eligibleForMode(mode: ModeDefinition, toolTags: readonly CapabilityTag[]): boolean {\n return capabilitiesAllowed(allowedCapabilitiesOf(mode), toolTags);\n}\n\n/** Every tag `mode` permits: its `allows` plus `IMPLICIT_TAGS`. */\nexport function allowedCapabilitiesOf(mode: ModeDefinition): Set<string> {\n return new Set<string>([...mode.allows, ...IMPLICIT_TAGS]);\n}\n","import { createReflectable, type Reflection } from '@weasel-js/registry';\nimport { allowedCapabilitiesOf, type ModeDefinition } from './modeDefinition';\n\n/** Options for `createModeRegistry`: the modes to register and which one is\n * active at startup. */\nexport interface CreateModeRegistryOptions {\n modes: readonly ModeDefinition[];\n initial: string;\n}\n\n/** Holds the set of available modes and which one is active, and notifies\n * subscribers when that changes. `getVersion` is a monotonic counter for\n * render-cache invalidation. Unknown mode ids throw rather than being\n * ignored. */\nexport interface ModeRegistry {\n current(): ModeDefinition;\n /** Every registered mode, in registration order. */\n list(): readonly ModeDefinition[];\n setMode(id: string): void;\n byId(id: string): ModeDefinition;\n getVersion(): number;\n subscribe(listener: () => void): () => void;\n /** The registered modes keyed by id. Shares this registry's version, so it\n * also advances on `setMode`. */\n readonly reflection: Reflection<ModeDefinition>;\n}\n\n/** Build a mode registry. Throws if two of `modes` share an id, or if\n * `initial` does not name one of them. */\nexport function createModeRegistry(opts: CreateModeRegistryOptions): ModeRegistry {\n const modes = createReflectable<ModeDefinition>();\n for (const m of opts.modes) {\n if (modes.get(m.id)) throw new Error(`createModeRegistry: duplicate mode id \"${m.id}\"`);\n modes.set(m.id, m);\n }\n const initial = modes.get(opts.initial);\n if (!initial) throw new Error(`Initial mode \"${opts.initial}\" not in modes list`);\n\n let active: ModeDefinition = initial;\n\n return {\n current: () => active,\n list: () => opts.modes,\n setMode(id: string): void {\n const next = modes.get(id);\n if (!next) throw new Error(`Unknown mode id: ${id}`);\n if (next === active) return;\n active = next;\n modes.bump();\n },\n byId(id: string): ModeDefinition {\n const m = modes.get(id);\n if (!m) throw new Error(`Unknown mode id: ${id}`);\n return m;\n },\n getVersion: modes.getVersion,\n subscribe: modes.subscribe,\n reflection: modes.reflection,\n };\n}\n\n/** The active mode as a canvas gates on it: its id, and every capability tag\n * a tool may carry to be usable in it — the mode's `allows` plus\n * `IMPLICIT_TAGS`. The shape `<SceneCanvas modes>` gates on. */\nexport interface ActiveMode {\n id: string;\n allowedCapabilities: ReadonlySet<string>;\n}\n\n/** A mode definition as a canvas gates on it. */\nexport function activeModeOf(mode: ModeDefinition): ActiveMode {\n return { id: mode.id, allowedCapabilities: allowedCapabilitiesOf(mode) };\n}\n\n/** A reader for the registry's active mode, as `<SceneCanvas modes>` builds\n * one. It returns the same object until the mode changes. */\nexport function getActiveModeFor(registry: ModeRegistry): () => ActiveMode {\n let cached: { version: number; value: ActiveMode } | null = null;\n return () => {\n const version = registry.getVersion();\n if (cached?.version !== version) cached = { version, value: activeModeOf(registry.current()) };\n return cached.value;\n };\n}\n","import { createEmitter } from '@weasel-js/registry';\nimport type { ModeRegistry } from './registry';\n\n/** Loose draw-command type — the kit's `DrawCommand` union shape is over in\n * `src/renderer`. We keep this opaque to avoid a cross-package import in\n * the type surface; consumers cast to the real DrawCommand at use site. */\nexport type ModeDrawCommand = unknown;\n\n/** Produces the draw commands for one mode's persistent decorations. Called\n * once per paint while that mode is active. */\nexport type ModeDecorationPainter = () => ModeDrawCommand[];\n\n/** Options for `createModeDecorations`. */\nexport interface CreateModeDecorationsOptions {\n registry: ModeRegistry;\n}\n\n/** Per-mode decoration painters, keyed by mode id. Mode decorations are drawn\n * for as long as the mode is active and belong to the mode rather than to any\n * tool — path-edit's anchor dots and handle lines are the motivating case. */\nexport interface ModeDecorations {\n /** Register (or replace) the painter for a mode id. Returns a release that\n * removes it, and does nothing once another painter has replaced it. */\n register(modeId: string, painter: ModeDecorationPainter): () => void;\n /** Paint the active mode's decorations. Returns an empty array if no\n * painter is registered for the active mode. */\n paint(): ModeDrawCommand[];\n /** Monotonic version. Bumps on mode change, painter registration, or release. */\n getVersion(): number;\n /** Notified whenever `getVersion` bumps — what a canvas repaints on. */\n subscribe(listener: () => void): () => void;\n}\n\n/** Build a decoration registry bound to a mode registry, so switching modes\n * switches which painter runs. */\nexport function createModeDecorations(opts: CreateModeDecorationsOptions): ModeDecorations {\n const { registry } = opts;\n const painters = new Map<string, ModeDecorationPainter>();\n const listeners = createEmitter();\n let registrations = 0;\n const changed = (): void => {\n registrations++;\n listeners.emit();\n };\n\n return {\n register(modeId: string, painter: ModeDecorationPainter): () => void {\n painters.set(modeId, painter);\n changed();\n return () => {\n if (painters.get(modeId) !== painter) return;\n painters.delete(modeId);\n changed();\n };\n },\n paint(): ModeDrawCommand[] {\n const painter = painters.get(registry.current().id);\n if (!painter) return [];\n return painter();\n },\n getVersion: () => registry.getVersion() + registrations,\n subscribe(listener: () => void): () => void {\n const off = listeners.subscribe(listener);\n const offMode = registry.subscribe(listener);\n return () => {\n off();\n offMode();\n };\n },\n };\n}\n","import type { ModeRegistry } from './registry';\nimport type { CapabilityTag } from './capabilities';\nimport { eligibleForMode } from './modeDefinition';\n\n/** The part of a tool this package needs in order to judge eligibility: its\n * id and the capabilities it claims. Structural, so a real `ToolDef`\n * satisfies it without this package depending on the kit. */\nexport interface ToolLike {\n id: string;\n capabilities?: readonly CapabilityTag[];\n}\n\n/** True iff `tool` is usable in the registry's currently active mode. */\nexport function eligibleTool(reg: ModeRegistry, tool: ToolLike): boolean {\n return eligibleForMode(reg.current(), tool.capabilities ?? []);\n}\n\n/** True iff a contribution is usable in the registry's active mode. */\nexport function eligibleContribution(\n reg: ModeRegistry,\n entry: { id: string; eligibility?: { capabilities?: readonly CapabilityTag[] } },\n): boolean {\n return eligibleForMode(reg.current(), entry.eligibility?.capabilities ?? []);\n}\n","import { createEmitter } from '@weasel-js/registry';\nimport type { ModeRegistry } from './registry';\n\n/** Options for `createScopingDim`. */\nexport interface CreateScopingDimOptions {\n registry: ModeRegistry;\n /** Returns the set of node ids that are *in* scope for the current mode.\n * The app computes this — typically: selection-only for path-edit,\n * isolated subtree for isolation, empty for non-scoping modes. */\n getTargetIds: () => ReadonlySet<string>;\n /** Alpha for out-of-scope nodes. Default 0.3 (spec). */\n dimAlpha?: number;\n}\n\n/** Answers, per node, how a scoping mode should treat it: dimmed or not,\n * hit-testable or not. Consult it from the renderer and the hit-tester —\n * it computes nothing itself. Its answers change when the mode does, or when\n * the app says the target set moved (`invalidate`); both notify\n * subscribers, so a canvas can repaint on `subscribe` (`<SceneCanvas\n * redrawOn>`). */\nexport interface ScopingDim {\n /** Multiplier in [0, 1] to apply to a node's render alpha. */\n alphaFor(id: string): number;\n /** Whether the node should respond to pointer hits. */\n isPointerInteractive(id: string): boolean;\n /** Tell subscribers the target set changed without a mode switch. */\n invalidate(): void;\n /** Monotonic; bumps on a mode switch and on `invalidate`. */\n getVersion(): number;\n subscribe(listener: () => void): () => void;\n}\n\n/** Build the scoping lookup for a mode registry. In a mode with\n * `scoping: false` every node is full-alpha and interactive, so this is safe\n * to consult unconditionally. */\nexport function createScopingDim(opts: CreateScopingDimOptions): ScopingDim {\n const { registry } = opts;\n const dim = opts.dimAlpha ?? 0.3;\n const listeners = createEmitter();\n let invalidations = 0;\n return {\n alphaFor(id) {\n const mode = registry.current();\n if (!mode.scoping) return 1;\n return opts.getTargetIds().has(id) ? 1 : dim;\n },\n isPointerInteractive(id) {\n const mode = registry.current();\n if (!mode.scoping) return true;\n return opts.getTargetIds().has(id);\n },\n invalidate() {\n invalidations++;\n listeners.emit();\n },\n getVersion: () => registry.getVersion() + invalidations,\n subscribe(listener) {\n const off = listeners.subscribe(listener);\n const offMode = registry.subscribe(listener);\n return () => {\n off();\n offMode();\n };\n },\n };\n}\n"]}
@@ -1 +1 @@
1
- export { a as CROP, D as DEFAULT_MODES, F as FREE_TRANSFORM, b as ISOLATION, N as NORMAL, P as PATH_EDIT, T as TEXT_EDIT, c as byId } from '../default-BJzHWrIR.js';
1
+ export { a as CROP, D as DEFAULT_MODES, F as FREE_TRANSFORM, b as ISOLATION, N as NORMAL, P as PATH_EDIT, T as TEXT_EDIT, c as byId } from '../default-6A7txdga.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weasel-js/modes",
3
- "version": "1.9.3",
3
+ "version": "1.10.0",
4
4
  "description": "App-level modality primitives: capability tags, mode definitions, mode registry, and decoration layer.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -33,7 +33,7 @@
33
33
  "node": ">=22"
34
34
  },
35
35
  "dependencies": {
36
- "@weasel-js/registry": "1.9.3"
36
+ "@weasel-js/registry": "1.10.0"
37
37
  },
38
38
  "files": [
39
39
  "dist",