@formicoidea/labre-core 0.51.0 → 0.52.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.
@@ -4,6 +4,7 @@ export { SurfaceElementModel, SurfaceGroupLikeModel, } from './element-model/bas
4
4
  export { CanvasElementType } from './element-model/index.js';
5
5
  export { CanvasRenderer } from './renderer/canvas-renderer.js';
6
6
  export { DomRenderer } from './renderer/dom-renderer.js';
7
+ export { isPainted } from './renderer/paint-predicate.js';
7
8
  export { ElementHighlightOverlay } from './renderer/element-highlight-overlay.js';
8
9
  export type { ElementRenderer } from './renderer/elements/index.js';
9
10
  export * from './renderer/elements/type.js';
@@ -6,6 +6,7 @@ export { SurfaceElementModel, SurfaceGroupLikeModel, } from './element-model/bas
6
6
  export { CanvasElementType } from './element-model/index.js';
7
7
  export { CanvasRenderer } from './renderer/canvas-renderer.js';
8
8
  export { DomRenderer } from './renderer/dom-renderer.js';
9
+ export { isPainted } from './renderer/paint-predicate.js';
9
10
  export { ElementHighlightOverlay } from './renderer/element-highlight-overlay.js';
10
11
  export * from './renderer/elements/type.js';
11
12
  export { Overlay, OverlayIdentifier } from './renderer/overlay.js';
@@ -38,6 +38,7 @@ export * from './quick-search-service.js';
38
38
  export * from './selection-pane-service.js';
39
39
  export * from './telemetry-service/index.js';
40
40
  export * from './theme-service.js';
41
+ export * from './thread-marks-service.js';
41
42
  export * from './title-band-service.js';
42
43
  export * from './toolbar-service/index.js';
43
44
  export * from './translation-service/index.js';
@@ -38,6 +38,7 @@ export * from './quick-search-service.js';
38
38
  export * from './selection-pane-service.js';
39
39
  export * from './telemetry-service/index.js';
40
40
  export * from './theme-service.js';
41
+ export * from './thread-marks-service.js';
41
42
  export * from './title-band-service.js';
42
43
  export * from './toolbar-service/index.js';
43
44
  export * from './translation-service/index.js';
@@ -0,0 +1,69 @@
1
+ import type { ExtensionType } from '../../store/index.js';
2
+ import type { ReadonlySignal } from '@preact/signals-core';
3
+ /**
4
+ * What a mark sits on (ADR 0038): a block, in either view, or a canvas element
5
+ * no block backs (a shape, a connector, a group, a framework board). The id is
6
+ * the one the library already gives it: a block id, an element id.
7
+ */
8
+ export type ThreadMarkTargetType = 'block' | 'element';
9
+ /**
10
+ * One marked target of the mounted document, as the host computes it. Ids and
11
+ * counters only: the library never learns what the host attaches to a target,
12
+ * who wrote it or what it says.
13
+ *
14
+ * `count` is how many the host has on that target (a mark at `0` or below is
15
+ * not drawn); `unread` turns the mark to the accent colour.
16
+ */
17
+ export interface ThreadMark {
18
+ targetType: ThreadMarkTargetType;
19
+ targetId: string;
20
+ count: number;
21
+ unread: boolean;
22
+ }
23
+ /**
24
+ * What the library hands the host when someone clicks a mark or "Discuss"
25
+ * (`com.labre.thread.discuss`) in a block or element toolbar.
26
+ *
27
+ * `excerpt` is computed here because the library knows the content: the
28
+ * block's text (its descendants' for a container such as a note), a canvas
29
+ * element's label as the selection pane reads it, else the kind of target.
30
+ * Whitespace is collapsed and it is cut to at most 280 characters, an
31
+ * ellipsis included (`THREAD_EXCERPT_MAX`). It is a snapshot for the host to
32
+ * store beside its anchor; the library keeps no copy.
33
+ */
34
+ export interface ThreadOpenRequest {
35
+ docId: string;
36
+ targetType: ThreadMarkTargetType;
37
+ targetId: string;
38
+ excerpt: string;
39
+ }
40
+ /** The longest excerpt `openThread` is ever handed, in code points. */
41
+ export declare const THREAD_EXCERPT_MAX = 280;
42
+ /**
43
+ * The "has a thread" seam (ADR 0038): the host owns the discussions, the
44
+ * library marks what the host says is marked and asks the host to open one.
45
+ *
46
+ * Nothing is written to the document, ever: no prop, no id in the content, no
47
+ * write on load. That is why it is not `CommentProvider`, which writes
48
+ * `block.props.comments` and ignores canvas elements.
49
+ *
50
+ * Absent or `null`: no mark is drawn and no toolbar offers "Discuss".
51
+ */
52
+ export interface ThreadMarksService {
53
+ /**
54
+ * The marks of the mounted document. Read reactively: a new array
55
+ * repaints the marks. An id the editor cannot find (a deleted block, an
56
+ * element of the other view) is ignored, never an error.
57
+ */
58
+ marks$: ReadonlySignal<readonly ThreadMark[]>;
59
+ /** Open (or start) the discussion on a target. Writes nothing here. */
60
+ openThread(request: ThreadOpenRequest): void;
61
+ }
62
+ export declare const ThreadMarksProvider: import("../../global/di/types.js").ServiceIdentifier<ThreadMarksService> & (<U extends ThreadMarksService = ThreadMarksService>(variant: import("../../global/di/types.js").ServiceVariant) => import("../../global/di/types.js").ServiceIdentifier<U>);
63
+ /**
64
+ * Host seam: the host's thread marks, or `null` to say there are none.
65
+ *
66
+ * `di.override`, like every panel seam, so a later registration wins and
67
+ * registering twice never throws at boot.
68
+ */
69
+ export declare function ThreadMarksExtension(service: ThreadMarksService | null): ExtensionType;
@@ -0,0 +1,20 @@
1
+ import { createIdentifier } from '../../global/di/index.js';
2
+ /** The longest excerpt `openThread` is ever handed, in code points. */
3
+ export const THREAD_EXCERPT_MAX = 280;
4
+ export const ThreadMarksProvider = createIdentifier('LabreThreadMarks');
5
+ /**
6
+ * Host seam: the host's thread marks, or `null` to say there are none.
7
+ *
8
+ * `di.override`, like every panel seam, so a later registration wins and
9
+ * registering twice never throws at boot.
10
+ */
11
+ export function ThreadMarksExtension(service) {
12
+ return {
13
+ setup: di => {
14
+ // `null` rides through the factory on purpose: `getOptional` then
15
+ // reports the seam as absent, which is the whole "no mark, no button"
16
+ // story.
17
+ di.override(ThreadMarksProvider, () => service);
18
+ },
19
+ };
20
+ }
@@ -0,0 +1 @@
1
+ export declare function effects(): void;
@@ -0,0 +1,4 @@
1
+ import { AFFINE_THREAD_MARKS_WIDGET, ThreadMarksWidget } from './widget.js';
2
+ export function effects() {
3
+ customElements.define(AFFINE_THREAD_MARKS_WIDGET, ThreadMarksWidget);
4
+ }
@@ -0,0 +1,19 @@
1
+ import { type ThreadMarkTargetType } from '../affine-shared/services/index.js';
2
+ import type { BlockStdScope } from '../std/index.js';
3
+ /** A block or a canvas element, by the id the library gave it. */
4
+ export interface ThreadTarget {
5
+ targetType: ThreadMarkTargetType;
6
+ targetId: string;
7
+ }
8
+ /**
9
+ * Collapse whitespace and cut to {@link THREAD_EXCERPT_MAX} code points, the
10
+ * ellipsis included. Code points, not UTF-16 units, so an emoji is never cut
11
+ * in half and a host counting characters (Postgres `char_length`) agrees.
12
+ */
13
+ export declare function truncateThreadExcerpt(raw: string): string;
14
+ /**
15
+ * The excerpt handed to `openThread` (ADR 0038): what the target says, as a
16
+ * reader would recognise it, at most {@link THREAD_EXCERPT_MAX} characters.
17
+ * Read only: nothing is written, nothing is cached.
18
+ */
19
+ export declare function threadExcerpt(std: BlockStdScope, target: ThreadTarget): string;
@@ -0,0 +1,68 @@
1
+ import { getSurfaceBlock } from '../affine-block-surface/index.js';
2
+ import { THREAD_EXCERPT_MAX, } from '../affine-shared/services/index.js';
3
+ import { selectionPaneRowLabel, selectionPaneTypeLabel, } from '../affine-widget-edgeless-toolbar/index.js';
4
+ import { GfxBlockElementModel } from '../std/gfx/index.js';
5
+ const ELLIPSIS = '…';
6
+ /**
7
+ * Collapse whitespace and cut to {@link THREAD_EXCERPT_MAX} code points, the
8
+ * ellipsis included. Code points, not UTF-16 units, so an emoji is never cut
9
+ * in half and a host counting characters (Postgres `char_length`) agrees.
10
+ */
11
+ export function truncateThreadExcerpt(raw) {
12
+ const text = raw.replace(/\s+/g, ' ').trim();
13
+ const chars = Array.from(text);
14
+ if (chars.length <= THREAD_EXCERPT_MAX)
15
+ return text;
16
+ return (chars
17
+ .slice(0, THREAD_EXCERPT_MAX - 1)
18
+ .join('')
19
+ .trimEnd() + ELLIPSIS);
20
+ }
21
+ /**
22
+ * A block's own text, then its descendants' in document order (a note, a
23
+ * callout, a list item with children), until there is more than an excerpt
24
+ * can hold.
25
+ */
26
+ function blockText(model) {
27
+ const parts = [];
28
+ let length = 0;
29
+ const visit = (block) => {
30
+ if (length > THREAD_EXCERPT_MAX)
31
+ return;
32
+ const own = block.text?.toString() ?? '';
33
+ if (own.trim()) {
34
+ parts.push(own);
35
+ length += own.length + 1;
36
+ }
37
+ for (const child of block.children)
38
+ visit(child);
39
+ };
40
+ visit(model);
41
+ return parts.join(' ');
42
+ }
43
+ function rawExcerpt(std, target) {
44
+ if (target.targetType === 'block') {
45
+ const model = std.store.getModelById(target.targetId);
46
+ if (!model)
47
+ return '';
48
+ const text = blockText(model);
49
+ if (text.trim())
50
+ return text;
51
+ // A canvas block with no text reads as the selection pane names it: a
52
+ // frame by its title, an image or a note by its kind.
53
+ if (model instanceof GfxBlockElementModel) {
54
+ return selectionPaneRowLabel(std, model);
55
+ }
56
+ return selectionPaneTypeLabel(std, model.flavour);
57
+ }
58
+ const element = getSurfaceBlock(std.store)?.getElementById(target.targetId);
59
+ return element ? selectionPaneRowLabel(std, element) : '';
60
+ }
61
+ /**
62
+ * The excerpt handed to `openThread` (ADR 0038): what the target says, as a
63
+ * reader would recognise it, at most {@link THREAD_EXCERPT_MAX} characters.
64
+ * Read only: nothing is written, nothing is cached.
65
+ */
66
+ export function threadExcerpt(std, target) {
67
+ return truncateThreadExcerpt(rawExcerpt(std, target));
68
+ }
@@ -0,0 +1,5 @@
1
+ export * from './excerpt.js';
2
+ export * from './toolbar.js';
3
+ export * from './translations.js';
4
+ export * from './widget.js';
5
+ export declare const threadMarksWidget: import("../store/index.js").ExtensionType;
@@ -0,0 +1,8 @@
1
+ import { WidgetViewExtension } from '../std/index.js';
2
+ import { literal, unsafeStatic } from 'lit/static-html.js';
3
+ import { AFFINE_THREAD_MARKS_WIDGET } from './widget.js';
4
+ export * from './excerpt.js';
5
+ export * from './toolbar.js';
6
+ export * from './translations.js';
7
+ export * from './widget.js';
8
+ export const threadMarksWidget = WidgetViewExtension('affine:page', AFFINE_THREAD_MARKS_WIDGET, literal `${unsafeStatic(AFFINE_THREAD_MARKS_WIDGET)}`);
@@ -0,0 +1,49 @@
1
+ import { type ToolbarContext } from '../affine-shared/services/index.js';
2
+ import { type BlockStdScope } from '../std/index.js';
3
+ import type { ExtensionType } from '../store/index.js';
4
+ import { type ThreadTarget } from './excerpt.js';
5
+ /**
6
+ * The ONE target a toolbar row speaks for, or `null` when it speaks for
7
+ * several (a discussion anchors to one block or one element, labre ADR-0048).
8
+ *
9
+ * - On the canvas, exactly one selected model: a canvas element (shape,
10
+ * connector, group, framework board) is an `element`, a canvas block (note,
11
+ * frame, image, embed) a `block`.
12
+ * - A text selection within one block: that block (the grain is the block,
13
+ * never a range of text).
14
+ * - One selected block, or the block a hover toolbar is drawn for.
15
+ */
16
+ export declare function threadTargetOf(ctx: ToolbarContext): ThreadTarget | null;
17
+ /**
18
+ * Ask the host to open the discussion of `target`, with the excerpt the
19
+ * library computes. Nothing is written: no prop, no id in the document.
20
+ * Without the seam, nothing happens.
21
+ */
22
+ export declare function openThreadOn(std: BlockStdScope, target: ThreadTarget): void;
23
+ export declare const THREAD_DISCUSS_ACTION_ID = "g.thread-discuss";
24
+ /**
25
+ * "Discuss" in the toolbar of every block and every canvas element (ADR 0038).
26
+ * Shown only when the host registered the seam and the row speaks for one
27
+ * target. It is not AFFiNE's `blockCommentToolbarButton`: that one writes the
28
+ * comment id into the block.
29
+ */
30
+ export declare const threadDiscussToolbarConfig: {
31
+ readonly when: (ctx: ToolbarContext) => boolean;
32
+ readonly actions: [{
33
+ readonly id: "g.thread-discuss";
34
+ readonly icon: import("lit-html").TemplateResult<1>;
35
+ readonly labelWording: import("../affine-shared/services/index.js").ChromeWording;
36
+ readonly tooltipWording: import("../affine-shared/services/index.js").ChromeWording;
37
+ readonly run: (ctx: ToolbarContext) => void;
38
+ }];
39
+ };
40
+ /**
41
+ * Registered on the two wildcard rows, each under an owner key: the bare
42
+ * `affine:surface:*` is the root's built-in canvas module, and an owner key
43
+ * never collides with a bare one. `affine:*` is merged into every row the
44
+ * toolbar draws; `affine:surface:*` names the canvas rows explicitly, so a
45
+ * row that stopped merging `affine:*` would still offer it. Where both apply,
46
+ * the toolbar merges entries by id, so the row shows "Discuss" once. A new
47
+ * block or framework inherits it by doing nothing.
48
+ */
49
+ export declare const threadDiscussToolbarExtensions: ExtensionType[];
@@ -0,0 +1,108 @@
1
+ import { CommentIcon } from '@blocksuite/icons/lit';
2
+ import { ToolbarModuleExtension, toolbarModuleKey, ThreadMarksProvider, } from '../affine-shared/services/index.js';
3
+ import { BlockFlavourIdentifier, BlockSelection, TextSelection, } from '../std/index.js';
4
+ import { GfxPrimitiveElementModel } from '../std/gfx/index.js';
5
+ import { threadExcerpt } from './excerpt.js';
6
+ import { THREAD_DISCUSS } from './translations.js';
7
+ /** Containers a discussion never anchors to: the page and the canvas itself. */
8
+ const NOT_A_TARGET = new Set(['affine:page', 'affine:surface']);
9
+ function blockTarget(id, ctx) {
10
+ if (!id)
11
+ return null;
12
+ const model = ctx.store.getModelById(id);
13
+ if (!model || NOT_A_TARGET.has(model.flavour))
14
+ return null;
15
+ return { targetType: 'block', targetId: model.id };
16
+ }
17
+ /**
18
+ * The ONE target a toolbar row speaks for, or `null` when it speaks for
19
+ * several (a discussion anchors to one block or one element, labre ADR-0048).
20
+ *
21
+ * - On the canvas, exactly one selected model: a canvas element (shape,
22
+ * connector, group, framework board) is an `element`, a canvas block (note,
23
+ * frame, image, embed) a `block`.
24
+ * - A text selection within one block: that block (the grain is the block,
25
+ * never a range of text).
26
+ * - One selected block, or the block a hover toolbar is drawn for.
27
+ */
28
+ export function threadTargetOf(ctx) {
29
+ if (ctx.hasSelectedSurfaceModels) {
30
+ const models = ctx.getSurfaceModels();
31
+ if (models.length !== 1)
32
+ return null;
33
+ const model = models[0];
34
+ if (!model)
35
+ return null;
36
+ return model instanceof GfxPrimitiveElementModel
37
+ ? { targetType: 'element', targetId: model.id }
38
+ : blockTarget(model.id, ctx);
39
+ }
40
+ const text = ctx.selection.find(TextSelection);
41
+ if (text) {
42
+ const from = text.from.blockId;
43
+ const to = text.to?.blockId ?? from;
44
+ return from === to ? blockTarget(from, ctx) : null;
45
+ }
46
+ if (ctx.selection.filter(BlockSelection).length > 1)
47
+ return null;
48
+ return blockTarget(ctx.getCurrentModelBy(BlockSelection)?.id, ctx);
49
+ }
50
+ /**
51
+ * Ask the host to open the discussion of `target`, with the excerpt the
52
+ * library computes. Nothing is written: no prop, no id in the document.
53
+ * Without the seam, nothing happens.
54
+ */
55
+ export function openThreadOn(std, target) {
56
+ const service = std.getOptional(ThreadMarksProvider);
57
+ if (!service)
58
+ return;
59
+ service.openThread({
60
+ docId: std.store.id,
61
+ targetType: target.targetType,
62
+ targetId: target.targetId,
63
+ excerpt: threadExcerpt(std, target),
64
+ });
65
+ }
66
+ export const THREAD_DISCUSS_ACTION_ID = 'g.thread-discuss';
67
+ /**
68
+ * "Discuss" in the toolbar of every block and every canvas element (ADR 0038).
69
+ * Shown only when the host registered the seam and the row speaks for one
70
+ * target. It is not AFFiNE's `blockCommentToolbarButton`: that one writes the
71
+ * comment id into the block.
72
+ */
73
+ export const threadDiscussToolbarConfig = {
74
+ when: ctx => Boolean(ctx.std.getOptional(ThreadMarksProvider)) &&
75
+ threadTargetOf(ctx) !== null,
76
+ actions: [
77
+ {
78
+ id: THREAD_DISCUSS_ACTION_ID,
79
+ icon: CommentIcon(),
80
+ labelWording: THREAD_DISCUSS,
81
+ tooltipWording: THREAD_DISCUSS,
82
+ run: ctx => {
83
+ const target = threadTargetOf(ctx);
84
+ if (target)
85
+ openThreadOn(ctx.std, target);
86
+ },
87
+ },
88
+ ],
89
+ };
90
+ /**
91
+ * Registered on the two wildcard rows, each under an owner key: the bare
92
+ * `affine:surface:*` is the root's built-in canvas module, and an owner key
93
+ * never collides with a bare one. `affine:*` is merged into every row the
94
+ * toolbar draws; `affine:surface:*` names the canvas rows explicitly, so a
95
+ * row that stopped merging `affine:*` would still offer it. Where both apply,
96
+ * the toolbar merges entries by id, so the row shows "Discuss" once. A new
97
+ * block or framework inherits it by doing nothing.
98
+ */
99
+ export const threadDiscussToolbarExtensions = [
100
+ ToolbarModuleExtension({
101
+ id: BlockFlavourIdentifier(toolbarModuleKey('affine:*', 'thread')),
102
+ config: threadDiscussToolbarConfig,
103
+ }),
104
+ ToolbarModuleExtension({
105
+ id: BlockFlavourIdentifier(toolbarModuleKey('affine:surface:*', 'thread')),
106
+ config: threadDiscussToolbarConfig,
107
+ }),
108
+ ];
@@ -0,0 +1,9 @@
1
+ import type { ChromeWording } from '../affine-shared/services/index.js';
2
+ /**
3
+ * "Discuss": the toolbar action that asks the host to open the discussion of
4
+ * a block or a canvas element, and the accessible name of the mark drawn on
5
+ * what already has one (ADR 0038).
6
+ */
7
+ export declare const THREAD_DISCUSS: ChromeWording;
8
+ /** Every wording this package declares, in the order it renders them. */
9
+ export declare const THREAD_MARKS_WORDINGS: readonly ChromeWording[];
@@ -0,0 +1,11 @@
1
+ /**
2
+ * "Discuss": the toolbar action that asks the host to open the discussion of
3
+ * a block or a canvas element, and the accessible name of the mark drawn on
4
+ * what already has one (ADR 0038).
5
+ */
6
+ export const THREAD_DISCUSS = [
7
+ 'com.labre.thread.discuss',
8
+ 'Discuss',
9
+ ];
10
+ /** Every wording this package declares, in the order it renders them. */
11
+ export const THREAD_MARKS_WORDINGS = [THREAD_DISCUSS];
@@ -0,0 +1,12 @@
1
+ import { type ViewExtensionContext, ViewExtensionProvider } from '../affine-ext-loader/index.js';
2
+ /**
3
+ * The "has a thread" seam's view side (ADR 0038): the marks widget and the
4
+ * "Discuss" toolbar module, in both views. Registered unconditionally: the
5
+ * seam, not a flag, decides whether anything shows (no flag gates a
6
+ * discussion, labre P5).
7
+ */
8
+ export declare class ThreadMarksViewExtension extends ViewExtensionProvider {
9
+ name: string;
10
+ effect(): void;
11
+ setup(context: ViewExtensionContext): void;
12
+ }
@@ -0,0 +1,25 @@
1
+ import { ViewExtensionProvider, } from '../affine-ext-loader/index.js';
2
+ import { effects } from './effects.js';
3
+ import { threadMarksWidget } from './index.js';
4
+ import { threadDiscussToolbarExtensions } from './toolbar.js';
5
+ /**
6
+ * The "has a thread" seam's view side (ADR 0038): the marks widget and the
7
+ * "Discuss" toolbar module, in both views. Registered unconditionally: the
8
+ * seam, not a flag, decides whether anything shows (no flag gates a
9
+ * discussion, labre P5).
10
+ */
11
+ export class ThreadMarksViewExtension extends ViewExtensionProvider {
12
+ constructor() {
13
+ super(...arguments);
14
+ this.name = 'affine-thread-marks-widget';
15
+ }
16
+ effect() {
17
+ super.effect();
18
+ effects();
19
+ }
20
+ setup(context) {
21
+ super.setup(context);
22
+ context.register(threadMarksWidget);
23
+ context.register(threadDiscussToolbarExtensions);
24
+ }
25
+ }
@@ -0,0 +1,51 @@
1
+ import { type ThreadMark } from '../affine-shared/services/index.js';
2
+ import { WidgetComponent } from '../std/index.js';
3
+ export declare const AFFINE_THREAD_MARKS_WIDGET = "affine-thread-marks-widget";
4
+ /**
5
+ * Where a mark sits, relative to the widget's own origin: beside a block of
6
+ * the page (`side`, in the right gutter, at the block's top), or on the
7
+ * top-right corner of a canvas model or of a block inside a canvas note
8
+ * (`corner`, centred on it).
9
+ */
10
+ export interface ThreadMarkPlace {
11
+ x: number;
12
+ y: number;
13
+ anchor: 'side' | 'corner';
14
+ }
15
+ export interface PlacedThreadMark extends ThreadMarkPlace {
16
+ mark: ThreadMark;
17
+ }
18
+ /**
19
+ * The marks to draw, from what the host says and where the editor finds each
20
+ * target. Pure, so the "ignore what is absent" rule is held by a test:
21
+ *
22
+ * - a target `locate` cannot find (deleted, of the other view, hidden for this
23
+ * viewer) is skipped, never an error: an orphan anchor has no mark;
24
+ * - a mark at `count <= 0` is not drawn;
25
+ * - two marks on one target are one mark: counts add up, unread wins.
26
+ */
27
+ export declare function placeThreadMarks(marks: readonly ThreadMark[], locate: (mark: ThreadMark) => ThreadMarkPlace | null): PlacedThreadMark[];
28
+ /**
29
+ * The "has a thread" marks (ADR 0038), one widget for both views, mounted on
30
+ * the root like `remote-selection`: in the page view beside each marked block,
31
+ * on the canvas on the corner of each marked element or block. A click asks
32
+ * the host to open the discussion, exactly as "Discuss" does.
33
+ *
34
+ * It reads the seam and the layout, and writes nothing. Positions are
35
+ * client rects taken relative to the widget's own box and recomputed, once a
36
+ * frame at most, when the marks, the document, the canvas or the window
37
+ * change. Without the seam it renders nothing.
38
+ */
39
+ export declare class ThreadMarksWidget extends WidgetComponent {
40
+ static styles: import("lit").CSSResult;
41
+ private readonly _refresh;
42
+ private get _edgeless();
43
+ connectedCallback(): void;
44
+ private _locate;
45
+ render(): unknown;
46
+ }
47
+ declare global {
48
+ interface HTMLElementTagNameMap {
49
+ [AFFINE_THREAD_MARKS_WIDGET]: ThreadMarksWidget;
50
+ }
51
+ }
@@ -0,0 +1,214 @@
1
+ import { CommentIcon } from '@blocksuite/icons/lit';
2
+ import { isPainted } from '../affine-block-surface/index.js';
3
+ import { ThreadMarksProvider, translateKey, } from '../affine-shared/services/index.js';
4
+ import { isInsideEdgelessEditor, requestThrottledConnectedFrame, } from '../affine-shared/utils/index.js';
5
+ import { WidgetComponent } from '../std/index.js';
6
+ import { GfxBlockElementModel, GfxControllerIdentifier, GfxPrimitiveElementModel, } from '../std/gfx/index.js';
7
+ import { effect } from '@preact/signals-core';
8
+ import { cssVarV2 } from '@toeverything/theme/v2';
9
+ import { css, html, nothing, unsafeCSS } from 'lit';
10
+ import { repeat } from 'lit/directives/repeat.js';
11
+ import { styleMap } from 'lit/directives/style-map.js';
12
+ import { openThreadOn } from './toolbar.js';
13
+ import { THREAD_DISCUSS } from './translations.js';
14
+ export const AFFINE_THREAD_MARKS_WIDGET = 'affine-thread-marks-widget';
15
+ /**
16
+ * The marks to draw, from what the host says and where the editor finds each
17
+ * target. Pure, so the "ignore what is absent" rule is held by a test:
18
+ *
19
+ * - a target `locate` cannot find (deleted, of the other view, hidden for this
20
+ * viewer) is skipped, never an error: an orphan anchor has no mark;
21
+ * - a mark at `count <= 0` is not drawn;
22
+ * - two marks on one target are one mark: counts add up, unread wins.
23
+ */
24
+ export function placeThreadMarks(marks, locate) {
25
+ const merged = new Map();
26
+ for (const mark of marks) {
27
+ if (!(mark.count > 0))
28
+ continue;
29
+ const key = `${mark.targetType}:${mark.targetId}`;
30
+ const seen = merged.get(key);
31
+ merged.set(key, seen
32
+ ? {
33
+ ...seen,
34
+ count: seen.count + mark.count,
35
+ unread: seen.unread || mark.unread,
36
+ }
37
+ : mark);
38
+ }
39
+ const placed = [];
40
+ for (const mark of merged.values()) {
41
+ const place = locate(mark);
42
+ if (place)
43
+ placed.push({ ...place, mark });
44
+ }
45
+ return placed;
46
+ }
47
+ /**
48
+ * The "has a thread" marks (ADR 0038), one widget for both views, mounted on
49
+ * the root like `remote-selection`: in the page view beside each marked block,
50
+ * on the canvas on the corner of each marked element or block. A click asks
51
+ * the host to open the discussion, exactly as "Discuss" does.
52
+ *
53
+ * It reads the seam and the layout, and writes nothing. Positions are
54
+ * client rects taken relative to the widget's own box and recomputed, once a
55
+ * frame at most, when the marks, the document, the canvas or the window
56
+ * change. Without the seam it renders nothing.
57
+ */
58
+ export class ThreadMarksWidget extends WidgetComponent {
59
+ constructor() {
60
+ super(...arguments);
61
+ this._refresh = requestThrottledConnectedFrame(() => {
62
+ this.requestUpdate();
63
+ }, this);
64
+ }
65
+ static { this.styles = css `
66
+ :host {
67
+ position: absolute;
68
+ top: 0;
69
+ left: 0;
70
+ width: 0;
71
+ height: 0;
72
+ overflow: visible;
73
+ pointer-events: none;
74
+ z-index: 1;
75
+ }
76
+
77
+ .thread-mark {
78
+ position: absolute;
79
+ top: 0;
80
+ left: 0;
81
+ gap: 2px;
82
+ padding: 0 6px 0 4px;
83
+ border-radius: 12px;
84
+ border: 1px solid ${unsafeCSS(cssVarV2('layer/insideBorder/border'))};
85
+ background: ${unsafeCSS(cssVarV2('layer/background/overlayPanel'))};
86
+ box-shadow: var(--affine-shadow-1);
87
+ color: ${unsafeCSS(cssVarV2('text/secondary'))};
88
+ pointer-events: auto;
89
+ }
90
+
91
+ .thread-mark[data-unread] {
92
+ color: ${unsafeCSS(cssVarV2('text/emphasis'))};
93
+ }
94
+
95
+ @media (pointer: coarse) {
96
+ .thread-mark::after {
97
+ content: '';
98
+ position: absolute;
99
+ inset: -10px;
100
+ }
101
+ }
102
+ `; }
103
+ get _edgeless() {
104
+ return isInsideEdgelessEditor(this.std.host);
105
+ }
106
+ connectedCallback() {
107
+ super.connectedCallback();
108
+ const { _disposables, std } = this;
109
+ // The seam is fixed for the editor's life: without it, follow nothing,
110
+ // so a host that has no threads pays nothing on a pan or a keystroke.
111
+ if (!std.getOptional(ThreadMarksProvider))
112
+ return;
113
+ _disposables.add(std.store.slots.blockUpdated.subscribe(this._refresh));
114
+ // A block mounts after the widget: a mark on it can only be placed then.
115
+ _disposables.add(std.view.viewUpdated.subscribe(this._refresh));
116
+ _disposables.addFromEvent(window, 'resize', this._refresh);
117
+ // Capture: the page view scrolls in a container, not the window.
118
+ _disposables.addFromEvent(window, 'scroll', this._refresh, true);
119
+ const gfx = std.getOptional(GfxControllerIdentifier);
120
+ if (gfx) {
121
+ _disposables.add(gfx.viewport.viewportUpdated.subscribe(this._refresh));
122
+ // The surface may load after the widget connects: follow it.
123
+ _disposables.add(effect(() => {
124
+ const surface = gfx.surface$.value;
125
+ if (!surface)
126
+ return;
127
+ this._refresh();
128
+ const subscriptions = [
129
+ surface.elementAdded.subscribe(this._refresh),
130
+ surface.elementRemoved.subscribe(this._refresh),
131
+ surface.elementUpdated.subscribe(this._refresh),
132
+ ];
133
+ return () => subscriptions.forEach(s => s.unsubscribe());
134
+ }));
135
+ }
136
+ }
137
+ _locate(mark, origin) {
138
+ const edgeless = this._edgeless;
139
+ if (edgeless) {
140
+ const gfx = this.std.get(GfxControllerIdentifier);
141
+ const model = gfx.getElementById(mark.targetId);
142
+ if (model instanceof GfxPrimitiveElementModel ||
143
+ model instanceof GfxBlockElementModel) {
144
+ // What this viewer does not see carries no mark (ADR 0031): the
145
+ // renderers' own predicate for an element, the local hide for a block.
146
+ const painted = model instanceof GfxPrimitiveElementModel
147
+ ? isPainted(model, gfx.localVisibility)
148
+ : !gfx.localVisibility.isHidden(model);
149
+ if (!painted)
150
+ return null;
151
+ const bound = model.elementBound;
152
+ const [x, y] = gfx.viewport.toViewCoord(bound.maxX, bound.y);
153
+ return {
154
+ x: x + gfx.viewport.left - origin.left,
155
+ y: y + gfx.viewport.top - origin.top,
156
+ anchor: 'corner',
157
+ };
158
+ }
159
+ }
160
+ if (mark.targetType !== 'block')
161
+ return null;
162
+ const block = this.std.view.getBlock(mark.targetId);
163
+ if (!block)
164
+ return null;
165
+ const rect = block.getBoundingClientRect();
166
+ if (rect.width === 0 && rect.height === 0)
167
+ return null;
168
+ return {
169
+ x: rect.right - origin.left,
170
+ y: rect.top - origin.top,
171
+ anchor: edgeless ? 'corner' : 'side',
172
+ };
173
+ }
174
+ render() {
175
+ const service = this.std.getOptional(ThreadMarksProvider);
176
+ if (!service)
177
+ return nothing;
178
+ const marks = service.marks$.value;
179
+ if (marks.length === 0)
180
+ return nothing;
181
+ const origin = this.getBoundingClientRect();
182
+ const placed = placeThreadMarks(marks, mark => this._locate(mark, origin));
183
+ if (placed.length === 0)
184
+ return nothing;
185
+ const label = translateKey(this.std, ...THREAD_DISCUSS);
186
+ return repeat(placed, ({ mark }) => `${mark.targetType}:${mark.targetId}`, ({ mark, x, y, anchor }) => html `<icon-button
187
+ class="thread-mark"
188
+ data-testid="thread-mark"
189
+ data-target-type=${mark.targetType}
190
+ data-target-id=${mark.targetId}
191
+ ?data-unread=${mark.unread}
192
+ width="auto"
193
+ height="24px"
194
+ .active=${mark.unread}
195
+ .text=${String(mark.count)}
196
+ aria-label=${label}
197
+ title=${label}
198
+ style=${styleMap({
199
+ transform: anchor === 'side'
200
+ ? `translate(${x + 8}px, ${y}px)`
201
+ : `translate(${x}px, ${y}px) translate(-50%, -50%)`,
202
+ })}
203
+ @pointerdown=${(event) => event.stopPropagation()}
204
+ @click=${(event) => {
205
+ event.stopPropagation();
206
+ openThreadOn(this.std, {
207
+ targetType: mark.targetType,
208
+ targetId: mark.targetId,
209
+ });
210
+ }}
211
+ >${CommentIcon({ width: '16px', height: '16px' })}</icon-button
212
+ >`);
213
+ }
214
+ }
@@ -54,6 +54,7 @@ import { PageDraggingAreaViewExtension } from '../_pkgs/affine-widget-page-dragg
54
54
  import { RemoteSelectionViewExtension } from '../_pkgs/affine-widget-remote-selection/view.js';
55
55
  import { ScrollAnchoringViewExtension } from '../_pkgs/affine-widget-scroll-anchoring/view.js';
56
56
  import { SlashMenuViewExtension } from '../_pkgs/affine-widget-slash-menu/view.js';
57
+ import { ThreadMarksViewExtension } from '../_pkgs/affine-widget-thread-marks/view.js';
57
58
  import { ToolbarViewExtension } from '../_pkgs/affine-widget-toolbar/view.js';
58
59
  import { ViewportOverlayViewExtension } from '../_pkgs/affine-widget-viewport-overlay/view.js';
59
60
  import { isBlockEnabled, isCapabilityEnabled, } from '../flags.js';
@@ -158,6 +159,7 @@ export function getInternalViewExtensions(flags) {
158
159
  KeyboardToolbarViewExtension,
159
160
  LinkedDocViewExtension,
160
161
  RemoteSelectionViewExtension,
162
+ ThreadMarksViewExtension,
161
163
  ScrollAnchoringViewExtension,
162
164
  SlashMenuViewExtension,
163
165
  ToolbarViewExtension,
@@ -58,6 +58,9 @@ export { type SelectionPaneNode, selectionPaneTree, } from './_pkgs/affine-block
58
58
  export { frameCommands, type ReorderFramesParams, reorderFramesParams, } from './_pkgs/affine-block-frame/index.js';
59
59
  export { formatPropertyValue, isPropertyValueEmpty, } from './_pkgs/affine-fragment-doc-title/index.js';
60
60
  export type { PropertiesPanelRequest, PropertiesPanelService, TitleBandNotShownPage, TitleBandProperty, TitleBandService, TitleBandSheetState, } from './_pkgs/affine-shared/services/index.js';
61
+ export { ThreadMarksExtension } from './_pkgs/affine-shared/services/index.js';
62
+ export type { ThreadMark, ThreadMarksService, ThreadMarkTargetType, ThreadOpenRequest, } from './_pkgs/affine-shared/services/index.js';
63
+ export { type ThreadTarget, threadExcerpt, } from './_pkgs/affine-widget-thread-marks/index.js';
61
64
  /**
62
65
  * The document's frames in presentation order, first slide first — the order
63
66
  * `canvas.frame.reorder` writes and the presentation walks. Works in page mode
@@ -70,6 +70,17 @@ export { frameCommands, reorderFramesParams, } from './_pkgs/affine-block-frame/
70
70
  * value.
71
71
  */
72
72
  export { formatPropertyValue, isPropertyValueEmpty, } from './_pkgs/affine-fragment-doc-title/index.js';
73
+ /*
74
+ * The "has a thread" seam (ADR 0038). The editor marks what the host says
75
+ * has a discussion and asks the host to open one, with an excerpt it
76
+ * computes; the host registers `ThreadMarksExtension({ marks$, openThread })`
77
+ * among its editor extensions. Gathered here with its types so a host's
78
+ * discussion module imports one entry; `threadExcerpt` is the excerpt the
79
+ * editor hands `openThread`, for a host that starts a discussion from its
80
+ * own UI.
81
+ */
82
+ export { ThreadMarksExtension } from './_pkgs/affine-shared/services/index.js';
83
+ export { threadExcerpt, } from './_pkgs/affine-widget-thread-marks/index.js';
73
84
  /**
74
85
  * The document's frames in presentation order, first slide first — the order
75
86
  * `canvas.frame.reorder` writes and the presentation walks. Works in page mode
@@ -41,6 +41,7 @@ import { EDGELESS_TOOLBAR_WORDINGS } from './_pkgs/affine-widget-edgeless-toolba
41
41
  import { ZOOM_TOOLBAR_WORDINGS } from './_pkgs/affine-widget-edgeless-zoom-toolbar/translations.js';
42
42
  import { LINKED_DOC_WORDINGS } from './_pkgs/affine-widget-linked-doc/translations.js';
43
43
  import { REMOTE_SELECTION_WORDINGS } from './_pkgs/affine-widget-remote-selection/translations.js';
44
+ import { THREAD_MARKS_WORDINGS } from './_pkgs/affine-widget-thread-marks/translations.js';
44
45
  import { SLASH_MENU_WORDINGS } from './_pkgs/affine-widget-slash-menu/translations.js';
45
46
  import { TOOLBAR_WIDGET_WORDINGS } from './_pkgs/affine-widget-toolbar/translations.js';
46
47
  import { collectTranslationKeys, commandCategoryTranslationEntries, commandTranslationEntries, mergeTranslationEntries, } from './_pkgs/std/index.js';
@@ -309,6 +310,7 @@ const PACKAGE_WORDINGS = [
309
310
  ZOOM_TOOLBAR_WORDINGS,
310
311
  LINKED_DOC_WORDINGS,
311
312
  REMOTE_SELECTION_WORDINGS,
313
+ THREAD_MARKS_WORDINGS,
312
314
  TOOLBAR_WIDGET_WORDINGS,
313
315
  COMPONENTS_WORDINGS,
314
316
  INLINE_LATEX_WORDINGS,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@formicoidea/labre-core",
3
3
  "description": "Labre editor — the full BlockSuite-derived editor as one package.",
4
- "version": "0.51.0",
4
+ "version": "0.52.0",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "author": "lajola",