@weasel-js/modes 1.9.3 → 1.9.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.
- package/dist/index.d.ts +6 -4
- package/dist/index.js +22 -15
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -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 `
|
|
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
|
-
|
|
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
|
|
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 = [
|
|
@@ -34,7 +34,10 @@ function eligibleForMode(mode, toolTags) {
|
|
|
34
34
|
}
|
|
35
35
|
function createModeRegistry(opts) {
|
|
36
36
|
const modes = createReflectable();
|
|
37
|
-
for (const m of opts.modes)
|
|
37
|
+
for (const m of opts.modes) {
|
|
38
|
+
if (modes.get(m.id)) throw new Error(`createModeRegistry: duplicate mode id "${m.id}"`);
|
|
39
|
+
modes.set(m.id, m);
|
|
40
|
+
}
|
|
38
41
|
const initial = modes.get(opts.initial);
|
|
39
42
|
if (!initial) throw new Error(`Initial mode "${opts.initial}" not in modes list`);
|
|
40
43
|
let active = initial;
|
|
@@ -69,18 +72,24 @@ function getActiveModeFor(registry) {
|
|
|
69
72
|
return cached.value;
|
|
70
73
|
};
|
|
71
74
|
}
|
|
72
|
-
|
|
73
|
-
// src/decorations.ts
|
|
74
75
|
function createModeDecorations(opts) {
|
|
75
76
|
const { registry } = opts;
|
|
76
77
|
const painters = /* @__PURE__ */ new Map();
|
|
77
|
-
const listeners =
|
|
78
|
+
const listeners = createEmitter();
|
|
78
79
|
let registrations = 0;
|
|
80
|
+
const changed = () => {
|
|
81
|
+
registrations++;
|
|
82
|
+
listeners.emit();
|
|
83
|
+
};
|
|
79
84
|
return {
|
|
80
85
|
register(modeId, painter) {
|
|
81
86
|
painters.set(modeId, painter);
|
|
82
|
-
|
|
83
|
-
|
|
87
|
+
changed();
|
|
88
|
+
return () => {
|
|
89
|
+
if (painters.get(modeId) !== painter) return;
|
|
90
|
+
painters.delete(modeId);
|
|
91
|
+
changed();
|
|
92
|
+
};
|
|
84
93
|
},
|
|
85
94
|
paint() {
|
|
86
95
|
const painter = painters.get(registry.current().id);
|
|
@@ -89,10 +98,10 @@ function createModeDecorations(opts) {
|
|
|
89
98
|
},
|
|
90
99
|
getVersion: () => registry.getVersion() + registrations,
|
|
91
100
|
subscribe(listener) {
|
|
92
|
-
listeners.
|
|
101
|
+
const off = listeners.subscribe(listener);
|
|
93
102
|
const offMode = registry.subscribe(listener);
|
|
94
103
|
return () => {
|
|
95
|
-
|
|
104
|
+
off();
|
|
96
105
|
offMode();
|
|
97
106
|
};
|
|
98
107
|
}
|
|
@@ -106,12 +115,10 @@ function eligibleTool(reg, tool) {
|
|
|
106
115
|
function eligibleContribution(reg, entry) {
|
|
107
116
|
return eligibleForMode(reg.current(), entry.eligibility?.capabilities ?? []);
|
|
108
117
|
}
|
|
109
|
-
|
|
110
|
-
// src/scopingLayer.ts
|
|
111
118
|
function createScopingDim(opts) {
|
|
112
119
|
const { registry } = opts;
|
|
113
120
|
const dim = opts.dimAlpha ?? 0.3;
|
|
114
|
-
const listeners =
|
|
121
|
+
const listeners = createEmitter();
|
|
115
122
|
let invalidations = 0;
|
|
116
123
|
return {
|
|
117
124
|
alphaFor(id) {
|
|
@@ -126,14 +133,14 @@ function createScopingDim(opts) {
|
|
|
126
133
|
},
|
|
127
134
|
invalidate() {
|
|
128
135
|
invalidations++;
|
|
129
|
-
|
|
136
|
+
listeners.emit();
|
|
130
137
|
},
|
|
131
138
|
getVersion: () => registry.getVersion() + invalidations,
|
|
132
139
|
subscribe(listener) {
|
|
133
|
-
listeners.
|
|
140
|
+
const off = listeners.subscribe(listener);
|
|
134
141
|
const offMode = registry.subscribe(listener);
|
|
135
142
|
return () => {
|
|
136
|
-
|
|
143
|
+
off();
|
|
137
144
|
offMode();
|
|
138
145
|
};
|
|
139
146
|
}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/capabilities.ts","../src/modeDefinition.ts","../src/registry.ts","../src/decorations.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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,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,GAA0C,CAAC,YAAY;AAI7D,SAAS,gBAAgB,KAAA,EAAuC;AACrE,EAAA,OAAQ,QAAA,CAA+B,SAAS,KAAK,CAAA;AACvD;;;ACiBO,SAAS,UAAU,IAAA,EAA8B;AACtD,EAAA,OAAO,IAAA,CAAK,SAAS,IAAA,CAAK,EAAA;AAC5B;AAGO,SAAS,eAAA,CAAgB,MAAsB,QAAA,EAA6C;AACjG,EAAA,IAAI,QAAA,CAAS,MAAA,KAAW,CAAA,EAAG,OAAO,KAAA;AAClC,EAAA,MAAM,OAAA,uBAAc,GAAA,CAAmB,CAAC,GAAG,IAAA,CAAK,MAAA,EAAQ,GAAG,aAAa,CAAC,CAAA;AACzE,EAAA,KAAA,MAAW,KAAK,QAAA,EAAU;AACxB,IAAA,IAAI,OAAA,CAAQ,GAAA,CAAI,CAAC,CAAA,EAAG,OAAO,IAAA;AAAA,EAC7B;AACA,EAAA,OAAO,KAAA;AACT;AC7BO,SAAS,mBAAmB,IAAA,EAA+C;AAChF,EAAA,MAAM,QAAQ,iBAAA,EAAkC;AAChD,EAAA,KAAA,MAAW,CAAA,IAAK,KAAK,KAAA,EAAO;AAC1B,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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,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 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 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: 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 { 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"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weasel-js/modes",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.4",
|
|
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.
|
|
36
|
+
"@weasel-js/registry": "1.9.4"
|
|
37
37
|
},
|
|
38
38
|
"files": [
|
|
39
39
|
"dist",
|