@gallerist/plugin-pins 0.0.0-stage → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,130 @@
1
+ import { ViewerApi, Group, Item, Plugin } from '@gallerist/core';
2
+
3
+ type PinPresentation = 'comment' | 'tag' | 'feature';
4
+ interface PinReply {
5
+ author?: string;
6
+ /** a secondary line, e.g. a timestamp */
7
+ meta?: string;
8
+ body: string;
9
+ }
10
+ interface Pin {
11
+ id: string;
12
+ /** position on the image: 0..1 of its width/height (`normalized`, default) or image pixels */
13
+ x: number;
14
+ y: number;
15
+ space?: 'normalized' | 'natural';
16
+ /** short name: the product, the feature, or the comment's author line; also the pin's label */
17
+ label?: string;
18
+ /** the card's text: a comment, a description */
19
+ body?: string;
20
+ /** a secondary line: a price, a timestamp, a dimension */
21
+ meta?: string;
22
+ /** a link from the card (a product page) */
23
+ href?: string;
24
+ /** the link's text; defaults to `strings.open` */
25
+ linkLabel?: string;
26
+ /** comment threads: the replies under the first comment */
27
+ replies?: PinReply[];
28
+ /** comment threads: drawn muted and marked as resolved */
29
+ resolved?: boolean;
30
+ }
31
+ interface PinContext {
32
+ viewer: ViewerApi;
33
+ group: Group;
34
+ item: Item;
35
+ }
36
+ /** `onOpenChange`'s context: the pin's item, and the pin whose card was open before this change. */
37
+ interface PinOpenChange extends PinContext {
38
+ /** the pin whose card was open until now (the one that closed), or `null` if none was */
39
+ previous: Pin | null;
40
+ }
41
+ /** Options for `open()`. */
42
+ interface PinOpenOptions {
43
+ /** move focus: into the card when it opens (once it is painted), to the pin when it closes */
44
+ focus?: boolean;
45
+ }
46
+ interface PinsStrings {
47
+ /** the toolbar toggle's label */
48
+ toggle: string;
49
+ /** the "place a comment" button's label */
50
+ add: string;
51
+ /** shown while placing a comment */
52
+ placing: string;
53
+ /** a pin's accessible name: its number (1-based) and label */
54
+ pin: (n: number, label?: string) => string;
55
+ /** the card's close button */
56
+ close: string;
57
+ /** default link text */
58
+ open: string;
59
+ /** a resolved thread's badge */
60
+ resolved: string;
61
+ }
62
+ declare const DEFAULT_PINS_STRINGS: PinsStrings;
63
+ interface PinsOptions {
64
+ /** how pins look and what their card holds. Default `tag`. */
65
+ presentation?: PinPresentation;
66
+ /** the pins of an item. Default: `item.meta.pins`. */
67
+ pins?: (item: Item, group: Group) => readonly Pin[] | undefined;
68
+ /** whether pins start shown (default true); the toolbar toggle and the `p` key flip it */
69
+ visible?: boolean;
70
+ /** add the show/hide toggle to the toolbar (default true) */
71
+ toggle?: boolean;
72
+ /**
73
+ * Replace the card's contents for a pin; `null` (or `undefined`) keeps the default card
74
+ * (`defaultPinCard`) for that pin. Called each time the card opens, and again when an open card
75
+ * is redrawn (`refresh()`, new data); the card is empty while closed. Returning the same node
76
+ * again (one cached per pin) is supported. If it throws, the pin gets the default card and the
77
+ * error is reported (`reportError`, else `console.error`). Don't call `open()` from here.
78
+ */
79
+ renderCard?: (pin: Pin, ctx: PinContext) => Node | null | undefined;
80
+ /**
81
+ * A node `renderCard` returned was dropped: its card closed (by the user, `open()`, moving to
82
+ * another item, the pin leaving the data), was redrawn, or the plugin or viewer was destroyed.
83
+ * Called once per returned node, after it left the card, so a host can unmount what it rendered
84
+ * there (a React root or portal). Not called for the default card, nor for a node `renderCard`
85
+ * returned again for the redrawn card (it hasn't left; it is handed back when it does). Runs once
86
+ * the change has settled, before `onOpenChange`; calling `open()` from it is fine.
87
+ */
88
+ onCardRemove?: (pin: Pin, node: Node) => void;
89
+ /**
90
+ * A pin's card opened (`pin`) or closed (`null`). `ctx.previous` is the pin whose card was open
91
+ * until now: the one that closed, also when another pin's card replaced it. When a callback
92
+ * changes the card again (`open()`), an earlier call may describe a state already replaced: the
93
+ * last one is current.
94
+ */
95
+ onOpenChange?: (pin: Pin | null, ctx: PinOpenChange) => void;
96
+ /**
97
+ * Turns on placing: an "Add a comment" toolbar button (and the `c` key) arms a one-shot click on
98
+ * the image, and this is called with the point in normalized image coordinates. Add the pin to
99
+ * your data and call `refresh()` (or update the collection) to show it.
100
+ */
101
+ onPlace?: (at: {
102
+ x: number;
103
+ y: number;
104
+ }, ctx: PinContext) => void;
105
+ strings?: Partial<PinsStrings>;
106
+ }
107
+ /** The plugin, plus controls for the host. */
108
+ interface PinsPlugin extends Plugin {
109
+ /** re-read every pin (after the host changed pin data in place) */
110
+ refresh(): void;
111
+ /** show or hide all pins */
112
+ setVisible(visible: boolean): void;
113
+ /** open a pin's card on the current item by id; `null` closes it. `{ focus: true }` moves focus
114
+ * into the card (or, closing, to its pin). */
115
+ open(id: string | null, opts?: PinOpenOptions): void;
116
+ }
117
+ /** What `defaultPinCard` draws: the presentation and the strings it uses (`open`, `resolved`). */
118
+ interface PinCardOptions {
119
+ /** default `tag` */
120
+ presentation?: PinPresentation;
121
+ strings?: Partial<PinsStrings>;
122
+ }
123
+ /**
124
+ * The card contents `pins()` draws for a pin by default, for a `renderCard` that wraps or extends
125
+ * them (text is set as text, never parsed as HTML).
126
+ */
127
+ declare function defaultPinCard(pin: Pin, opts?: PinCardOptions): DocumentFragment;
128
+ declare function pins(opts?: PinsOptions): PinsPlugin;
129
+
130
+ export { DEFAULT_PINS_STRINGS, type Pin, type PinCardOptions, type PinContext, type PinOpenChange, type PinOpenOptions, type PinPresentation, type PinReply, type PinsOptions, type PinsPlugin, type PinsStrings, defaultPinCard, pins };
@@ -0,0 +1,130 @@
1
+ import { ViewerApi, Group, Item, Plugin } from '@gallerist/core';
2
+
3
+ type PinPresentation = 'comment' | 'tag' | 'feature';
4
+ interface PinReply {
5
+ author?: string;
6
+ /** a secondary line, e.g. a timestamp */
7
+ meta?: string;
8
+ body: string;
9
+ }
10
+ interface Pin {
11
+ id: string;
12
+ /** position on the image: 0..1 of its width/height (`normalized`, default) or image pixels */
13
+ x: number;
14
+ y: number;
15
+ space?: 'normalized' | 'natural';
16
+ /** short name: the product, the feature, or the comment's author line; also the pin's label */
17
+ label?: string;
18
+ /** the card's text: a comment, a description */
19
+ body?: string;
20
+ /** a secondary line: a price, a timestamp, a dimension */
21
+ meta?: string;
22
+ /** a link from the card (a product page) */
23
+ href?: string;
24
+ /** the link's text; defaults to `strings.open` */
25
+ linkLabel?: string;
26
+ /** comment threads: the replies under the first comment */
27
+ replies?: PinReply[];
28
+ /** comment threads: drawn muted and marked as resolved */
29
+ resolved?: boolean;
30
+ }
31
+ interface PinContext {
32
+ viewer: ViewerApi;
33
+ group: Group;
34
+ item: Item;
35
+ }
36
+ /** `onOpenChange`'s context: the pin's item, and the pin whose card was open before this change. */
37
+ interface PinOpenChange extends PinContext {
38
+ /** the pin whose card was open until now (the one that closed), or `null` if none was */
39
+ previous: Pin | null;
40
+ }
41
+ /** Options for `open()`. */
42
+ interface PinOpenOptions {
43
+ /** move focus: into the card when it opens (once it is painted), to the pin when it closes */
44
+ focus?: boolean;
45
+ }
46
+ interface PinsStrings {
47
+ /** the toolbar toggle's label */
48
+ toggle: string;
49
+ /** the "place a comment" button's label */
50
+ add: string;
51
+ /** shown while placing a comment */
52
+ placing: string;
53
+ /** a pin's accessible name: its number (1-based) and label */
54
+ pin: (n: number, label?: string) => string;
55
+ /** the card's close button */
56
+ close: string;
57
+ /** default link text */
58
+ open: string;
59
+ /** a resolved thread's badge */
60
+ resolved: string;
61
+ }
62
+ declare const DEFAULT_PINS_STRINGS: PinsStrings;
63
+ interface PinsOptions {
64
+ /** how pins look and what their card holds. Default `tag`. */
65
+ presentation?: PinPresentation;
66
+ /** the pins of an item. Default: `item.meta.pins`. */
67
+ pins?: (item: Item, group: Group) => readonly Pin[] | undefined;
68
+ /** whether pins start shown (default true); the toolbar toggle and the `p` key flip it */
69
+ visible?: boolean;
70
+ /** add the show/hide toggle to the toolbar (default true) */
71
+ toggle?: boolean;
72
+ /**
73
+ * Replace the card's contents for a pin; `null` (or `undefined`) keeps the default card
74
+ * (`defaultPinCard`) for that pin. Called each time the card opens, and again when an open card
75
+ * is redrawn (`refresh()`, new data); the card is empty while closed. Returning the same node
76
+ * again (one cached per pin) is supported. If it throws, the pin gets the default card and the
77
+ * error is reported (`reportError`, else `console.error`). Don't call `open()` from here.
78
+ */
79
+ renderCard?: (pin: Pin, ctx: PinContext) => Node | null | undefined;
80
+ /**
81
+ * A node `renderCard` returned was dropped: its card closed (by the user, `open()`, moving to
82
+ * another item, the pin leaving the data), was redrawn, or the plugin or viewer was destroyed.
83
+ * Called once per returned node, after it left the card, so a host can unmount what it rendered
84
+ * there (a React root or portal). Not called for the default card, nor for a node `renderCard`
85
+ * returned again for the redrawn card (it hasn't left; it is handed back when it does). Runs once
86
+ * the change has settled, before `onOpenChange`; calling `open()` from it is fine.
87
+ */
88
+ onCardRemove?: (pin: Pin, node: Node) => void;
89
+ /**
90
+ * A pin's card opened (`pin`) or closed (`null`). `ctx.previous` is the pin whose card was open
91
+ * until now: the one that closed, also when another pin's card replaced it. When a callback
92
+ * changes the card again (`open()`), an earlier call may describe a state already replaced: the
93
+ * last one is current.
94
+ */
95
+ onOpenChange?: (pin: Pin | null, ctx: PinOpenChange) => void;
96
+ /**
97
+ * Turns on placing: an "Add a comment" toolbar button (and the `c` key) arms a one-shot click on
98
+ * the image, and this is called with the point in normalized image coordinates. Add the pin to
99
+ * your data and call `refresh()` (or update the collection) to show it.
100
+ */
101
+ onPlace?: (at: {
102
+ x: number;
103
+ y: number;
104
+ }, ctx: PinContext) => void;
105
+ strings?: Partial<PinsStrings>;
106
+ }
107
+ /** The plugin, plus controls for the host. */
108
+ interface PinsPlugin extends Plugin {
109
+ /** re-read every pin (after the host changed pin data in place) */
110
+ refresh(): void;
111
+ /** show or hide all pins */
112
+ setVisible(visible: boolean): void;
113
+ /** open a pin's card on the current item by id; `null` closes it. `{ focus: true }` moves focus
114
+ * into the card (or, closing, to its pin). */
115
+ open(id: string | null, opts?: PinOpenOptions): void;
116
+ }
117
+ /** What `defaultPinCard` draws: the presentation and the strings it uses (`open`, `resolved`). */
118
+ interface PinCardOptions {
119
+ /** default `tag` */
120
+ presentation?: PinPresentation;
121
+ strings?: Partial<PinsStrings>;
122
+ }
123
+ /**
124
+ * The card contents `pins()` draws for a pin by default, for a `renderCard` that wraps or extends
125
+ * them (text is set as text, never parsed as HTML).
126
+ */
127
+ declare function defaultPinCard(pin: Pin, opts?: PinCardOptions): DocumentFragment;
128
+ declare function pins(opts?: PinsOptions): PinsPlugin;
129
+
130
+ export { DEFAULT_PINS_STRINGS, type Pin, type PinCardOptions, type PinContext, type PinOpenChange, type PinOpenOptions, type PinPresentation, type PinReply, type PinsOptions, type PinsPlugin, type PinsStrings, defaultPinCard, pins };