@formicoidea/labre-core-database 2.1.1 → 2.4.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.
@@ -1,3 +1,4 @@
1
+ import { viewDatabaseId, } from '@formicoidea/labre-core/blocks/database';
1
2
  import { DataViewEventTraceKey, } from '@formicoidea/labre-core/data-view';
2
3
  import { propertyOptions, sourceProperties } from './import-view.js';
3
4
  import { defaultMapping, remapView } from './remap-view.js';
@@ -11,6 +12,11 @@ import { defaultMapping, remapView } from './remap-view.js';
11
12
  * column, filter, sort and grouping under its new id, and one the target
12
13
  * lacks leaves them. `openIn`, `colorRules` and any other key are carried
13
14
  * as `remapView` carries them.
15
+ *
16
+ * A source of another database (ADR 0035 amendment of 2026-10-30, under a
17
+ * host with `crossDatabaseViews`) is named by its `databaseId` too, stored
18
+ * on the view; going back to the block's own database removes it, so a
19
+ * view reading its block's database stores exactly what it did in 2.3.
14
20
  */
15
21
  /** A source's schema block as a correspondence reads it (`props.columns`). */
16
22
  export function schemaProperties(schema) {
@@ -22,25 +28,36 @@ export function schemaProperties(schema) {
22
28
  }));
23
29
  }
24
30
  /** The keys `remapView` leaves to the caller: never cleared here. */
25
- const CALLER_KEYS = new Set(['id', 'name', 'sourceId']);
31
+ const CALLER_KEYS = new Set(['id', 'name', 'sourceId', 'databaseId']);
26
32
  /**
27
- * Point view `viewId` of `dataSource` at `sourceId`, as one undo step, and
28
- * report `DatabaseViewSourceChanged { source: added }` once. `false`, with
29
- * nothing written, when the view already reads that source, the page is
30
- * read-only (re-read here: a source created by the host took a round trip),
31
- * the view is drawn through a fallback preset (its stored data belongs to a
32
- * preset this client does not have) or `resolveSource` knows no such source.
33
+ * Point view `viewId` of `dataSource` at `sourceId` of `databaseId` (absent
34
+ * or the block's own: the block's database), as one undo step, and report
35
+ * `DatabaseViewSourceChanged { source: added }` once. `false`, with nothing
36
+ * written, when the view already reads that source of that database, the
37
+ * page is read-only (re-read here: a source created by the host took a
38
+ * round trip), the view is drawn through a fallback preset (its stored data
39
+ * belongs to a preset this client does not have) or `resolveSource` knows
40
+ * no such source. The same `sourceId` in another database is another
41
+ * source.
33
42
  */
34
- export function changeViewSource(dataSource, viewId, sourceId, added) {
43
+ export function changeViewSource(dataSource, viewId, sourceId, added, databaseId) {
35
44
  const view = dataSource.viewDataGet(viewId);
36
- if (!view || view.sourceId === sourceId)
45
+ if (!view)
37
46
  return false;
47
+ const home = dataSource.hostModel.props.externalSourceId;
48
+ // `undefined`: the block's database, however the caller named it.
49
+ const targetDatabase = databaseId === undefined || databaseId === home ? undefined : databaseId;
50
+ const storedDatabase = viewDatabaseId(view);
51
+ const fromDatabase = storedDatabase === home ? undefined : storedDatabase;
52
+ if (view.sourceId === sourceId && fromDatabase === targetDatabase) {
53
+ return false;
54
+ }
38
55
  if (dataSource.readonly$.peek())
39
56
  return false;
40
57
  if (dataSource.viewManager.viewGet(viewId)?.shownAsFallback$.peek()) {
41
58
  return false;
42
59
  }
43
- const target = dataSource.sourceOf(sourceId);
60
+ const target = dataSource.sourceOf(sourceId, targetDatabase);
44
61
  if (!target)
45
62
  return false;
46
63
  // The data source answers for the current view's source; another view's
@@ -48,7 +65,7 @@ export function changeViewSource(dataSource, viewId, sourceId, added) {
48
65
  const storedSource = typeof view.sourceId === 'string' ? view.sourceId : undefined;
49
66
  const fromSchema = viewId === dataSource.viewManager.currentViewId$.peek()
50
67
  ? null
51
- : dataSource.sourceOf(storedSource)?.schema;
68
+ : dataSource.sourceOf(storedSource, fromDatabase)?.schema;
52
69
  const from = fromSchema
53
70
  ? schemaProperties(fromSchema)
54
71
  : sourceProperties(dataSource);
@@ -62,11 +79,19 @@ export function changeViewSource(dataSource, viewId, sourceId, added) {
62
79
  if (!CALLER_KEYS.has(key) && !(key in data))
63
80
  cleared[key] = undefined;
64
81
  }
82
+ // Back home, the view's `databaseId` goes (an own `undefined`, as above);
83
+ // a view that never had one gets no key at all.
84
+ const database = targetDatabase !== undefined
85
+ ? { databaseId: targetDatabase }
86
+ : 'databaseId' in view
87
+ ? { databaseId: undefined }
88
+ : {};
65
89
  dataSource.hostModel.store.captureSync();
66
90
  dataSource.viewDataUpdate(viewId, () => ({
67
91
  ...cleared,
68
92
  ...data,
69
93
  sourceId,
94
+ ...database,
70
95
  }));
71
96
  dataSource.serviceGet(DataViewEventTraceKey)?.('DatabaseViewSourceChanged', {
72
97
  source: added,
@@ -18,11 +18,13 @@ export declare function databaseIdOf(dataSource: DataSource): string | undefined
18
18
  /** Whether the picker can import a view into this data source now. */
19
19
  export declare function canImportView(picker: DataSourcePickerService | null | undefined, dataSource: DataSource): boolean;
20
20
  /**
21
- * Add `imported` as a new view of `dataSource`, on `sourceId`, and show it;
22
- * its id, or `null` when nothing was added. Re-checks read-only (the host's
23
- * dialog took time) and refuses a layout this database does not add.
21
+ * Add `imported` as a new view of `dataSource`, on `sourceId` (of
22
+ * `databaseId`, a source of another database, ADR 0035 amendment of
23
+ * 2026-10-30), and show it; its id, or `null` when nothing was added.
24
+ * Re-checks read-only (the host's dialog took time) and refuses a layout
25
+ * this database does not add.
24
26
  */
25
- export declare function addImportedView(dataSource: DataSource, sourceId: string | undefined, imported: DataSourcePickerImportedView): string | null;
27
+ export declare function addImportedView(dataSource: DataSource, sourceId: string | undefined, imported: DataSourcePickerImportedView, databaseId?: string): string | null;
26
28
  /**
27
29
  * Ask the host to import a view into `dataSource` (the full page's "Import a
28
30
  * view", the "+ view" third entry). Nothing without the picker's verb, on a
@@ -1,4 +1,4 @@
1
- import { HostedDataSourceBase } from '@formicoidea/labre-core/blocks/database';
1
+ import { HostedDataSourceBase, viewDatabaseId, } from '@formicoidea/labre-core/blocks/database';
2
2
  import { DataViewEventTraceKey, } from '@formicoidea/labre-core/data-view';
3
3
  import { nanoid } from '@formicoidea/labre-core/store';
4
4
  import { remapView, viewTemplateData } from './remap-view.js';
@@ -45,11 +45,13 @@ export function canImportView(picker, dataSource) {
45
45
  databaseIdOf(dataSource) !== undefined);
46
46
  }
47
47
  /**
48
- * Add `imported` as a new view of `dataSource`, on `sourceId`, and show it;
49
- * its id, or `null` when nothing was added. Re-checks read-only (the host's
50
- * dialog took time) and refuses a layout this database does not add.
48
+ * Add `imported` as a new view of `dataSource`, on `sourceId` (of
49
+ * `databaseId`, a source of another database, ADR 0035 amendment of
50
+ * 2026-10-30), and show it; its id, or `null` when nothing was added.
51
+ * Re-checks read-only (the host's dialog took time) and refuses a layout
52
+ * this database does not add.
51
53
  */
52
- export function addImportedView(dataSource, sourceId, imported) {
54
+ export function addImportedView(dataSource, sourceId, imported, databaseId) {
53
55
  if (dataSource.readonly$.peek())
54
56
  return null;
55
57
  const mode = imported.view.mode;
@@ -69,6 +71,7 @@ export function addImportedView(dataSource, sourceId, imported) {
69
71
  name,
70
72
  mode,
71
73
  ...(sourceId === undefined ? {} : { sourceId }),
74
+ ...(databaseId === undefined ? {} : { databaseId }),
72
75
  });
73
76
  dataSource.viewManager.setCurrentView(id);
74
77
  dataSource.serviceGet(DataViewEventTraceKey)?.('DatabaseViewAdded', {
@@ -84,6 +87,17 @@ function currentSourceId(dataSource) {
84
87
  const sourceId = view?.sourceId;
85
88
  return typeof sourceId === 'string' ? sourceId : undefined;
86
89
  }
90
+ /**
91
+ * The current view's stored `databaseId` when it reads a source of another
92
+ * database than its block's (ADR 0035 amendment of 2026-10-30); absent,
93
+ * the block's.
94
+ */
95
+ function currentForeignDatabase(dataSource, home) {
96
+ const viewId = dataSource.viewManager.currentViewId$.peek();
97
+ const view = viewId ? dataSource.viewDataGet(viewId) : undefined;
98
+ const databaseId = viewDatabaseId(view);
99
+ return databaseId === home ? undefined : databaseId;
100
+ }
87
101
  /**
88
102
  * Ask the host to import a view into `dataSource` (the full page's "Import a
89
103
  * view", the "+ view" third entry). Nothing without the picker's verb, on a
@@ -97,12 +111,14 @@ export function requestViewImport(picker, dataSource) {
97
111
  if (dataSource.readonly$.peek())
98
112
  return;
99
113
  const sourceId = currentSourceId(dataSource);
114
+ // The copy reads the current view's source, in that view's database.
115
+ const foreign = currentForeignDatabase(dataSource, databaseId);
100
116
  const request = {
101
- databaseId,
117
+ databaseId: foreign ?? databaseId,
102
118
  ...(sourceId === undefined ? {} : { sourceId }),
103
119
  properties: sourceProperties(dataSource),
104
120
  layouts: dataSource.viewMetas.map(meta => meta.type),
105
- add: imported => addImportedView(dataSource, sourceId, imported),
121
+ add: imported => addImportedView(dataSource, sourceId, imported, foreign),
106
122
  };
107
123
  importView.call(picker, request);
108
124
  }
@@ -115,7 +131,7 @@ export function currentViewTemplate(dataSource) {
115
131
  return null;
116
132
  const sourceId = currentSourceId(dataSource);
117
133
  return {
118
- databaseId,
134
+ databaseId: currentForeignDatabase(dataSource, databaseId) ?? databaseId,
119
135
  ...(sourceId === undefined ? {} : { sourceId }),
120
136
  name: view.name,
121
137
  view: viewTemplateData(view),
@@ -15,7 +15,10 @@ export interface RemapDropped {
15
15
  colorRules: number;
16
16
  }
17
17
  export interface RemappedView {
18
- /** The view's data for this database, without `id`, `name` or `sourceId`. */
18
+ /**
19
+ * The view's data for this database, without `id`, `name`, `sourceId` or
20
+ * `databaseId`.
21
+ */
19
22
  data: Record<string, unknown>;
20
23
  dropped: RemapDropped;
21
24
  }
@@ -42,8 +45,10 @@ type Json = Record<string, unknown>;
42
45
  * but a rule that loses any condition is removed whole and counted: a
43
46
  * rule with fewer conditions would colour rows its author never meant;
44
47
  * its colour is the shared palette's and is kept as is;
45
- * - `id`, `name` and `sourceId` are left to the caller; any other key is
46
- * copied as is.
48
+ * - `id`, `name`, `sourceId` and `databaseId` (the database of a source of
49
+ * another database, ADR 0035 amendment of 2026-10-30) are left to the
50
+ * caller: a copy names the source it reads where it lands; any other key
51
+ * is copied as is.
47
52
  *
48
53
  * A mapping onto a property that `to` does not have counts as ignored.
49
54
  */
@@ -36,8 +36,10 @@ function isRecord(value) {
36
36
  * but a rule that loses any condition is removed whole and counted: a
37
37
  * rule with fewer conditions would colour rows its author never meant;
38
38
  * its colour is the shared palette's and is kept as is;
39
- * - `id`, `name` and `sourceId` are left to the caller; any other key is
40
- * copied as is.
39
+ * - `id`, `name`, `sourceId` and `databaseId` (the database of a source of
40
+ * another database, ADR 0035 amendment of 2026-10-30) are left to the
41
+ * caller: a copy names the source it reads where it lands; any other key
42
+ * is copied as is.
41
43
  *
42
44
  * A mapping onto a property that `to` does not have counts as ignored.
43
45
  */
@@ -46,6 +48,7 @@ export function remapView(view, from, to, mapping) {
46
48
  delete data.id;
47
49
  delete data.name;
48
50
  delete data.sourceId;
51
+ delete data.databaseId;
49
52
  const dropped = {
50
53
  filters: 0,
51
54
  sorts: 0,
@@ -252,6 +255,7 @@ export function viewTemplateData(view) {
252
255
  const data = plainCopy(view);
253
256
  delete data.id;
254
257
  delete data.sourceId;
258
+ delete data.databaseId;
255
259
  delete data.sort;
256
260
  if (isRecord(data.filter))
257
261
  data.filter = { ...data.filter, conditions: [] };
@@ -0,0 +1,48 @@
1
+ import type { DataSourcePickerDatabase, DataSourcePickerService } from '@formicoidea/labre-core/shared/services';
2
+ import { type TemplateResult } from 'lit';
3
+ /**
4
+ * One source list for the whole workspace (ADR 0035 amendment of
5
+ * 2026-10-30, labre « Base de données v3 »): under a host with
6
+ * `crossDatabaseViews`, a view's settings › "Source" and "+ view" list the
7
+ * sources of every database the picker lists, not only the block's. Pure,
8
+ * so the order is held by a unit spec without a DOM.
9
+ *
10
+ * - The block's own database first, in the host's order (as in 2.3), then
11
+ * the other databases' sources by name (accents and case folded, numbers
12
+ * in numeric order), a tie by database title then by id, so the order
13
+ * never depends on the host's list of databases.
14
+ * - A source of another database carries its database (`databaseId`,
15
+ * `databaseTitle`): the entry labels it, a pick stores it on the view.
16
+ * - Without `crossDatabaseViews` only the block's database is listed: the
17
+ * 2.3 list, whatever the picker returns.
18
+ */
19
+ export interface WorkspaceSource {
20
+ /** What a view stores as its `sourceId`. */
21
+ sourceId: string;
22
+ title: string;
23
+ /** Set for a source of ANOTHER database than the block's; absent, the block's. */
24
+ databaseId?: string;
25
+ /** That database's title, for the entry's label. */
26
+ databaseTitle?: string;
27
+ }
28
+ /** Whether the host lets a view read a source of another database. */
29
+ export declare function crossDatabaseViews(picker: Pick<DataSourcePickerService, 'crossDatabaseViews'> | null | undefined): boolean;
30
+ export declare function workspaceSources(databases: readonly DataSourcePickerDatabase[], homeDatabaseId: string | undefined, acrossDatabases: boolean): WorkspaceSource[];
31
+ /**
32
+ * A source's database as a trailing argument, only when it is another one:
33
+ * a call about the block's own database is made exactly as in 2.3.
34
+ */
35
+ export declare const inDatabase: (databaseId: string | undefined) => [] | [string];
36
+ /**
37
+ * Whether `source` is the one a view reads, by database AND source: the same
38
+ * `sourceId` in another database is another source. `viewDatabaseId`
39
+ * absent is the block's database, as `source.databaseId` absent is.
40
+ */
41
+ export declare function isViewSource(source: WorkspaceSource, viewSourceId: string | undefined, viewDatabaseId: string | undefined): boolean;
42
+ /**
43
+ * The postfix of a source of another database: its database's title, grey,
44
+ * on the right (and the check of the current source after it, since a
45
+ * postfix replaces the menu's own check). `undefined` for the block's own
46
+ * sources, so their entries stay the 2.3 ones.
47
+ */
48
+ export declare function databaseLabel(source: WorkspaceSource, untitled: string, selected?: boolean): TemplateResult | undefined;
@@ -0,0 +1,60 @@
1
+ import { DoneIcon } from '@blocksuite/icons/lit';
2
+ import { html } from 'lit';
3
+ /** Whether the host lets a view read a source of another database. */
4
+ export function crossDatabaseViews(picker) {
5
+ return picker?.crossDatabaseViews === true;
6
+ }
7
+ const collator = new Intl.Collator(undefined, {
8
+ sensitivity: 'base',
9
+ numeric: true,
10
+ });
11
+ export function workspaceSources(databases, homeDatabaseId, acrossDatabases) {
12
+ const home = databases.find(database => database.id === homeDatabaseId);
13
+ const own = (home?.sources ?? []).map(source => ({
14
+ sourceId: source.id,
15
+ title: source.title,
16
+ }));
17
+ if (!acrossDatabases)
18
+ return own;
19
+ const others = databases
20
+ .filter(database => database.id !== homeDatabaseId)
21
+ .flatMap(database => database.sources.map(source => ({
22
+ sourceId: source.id,
23
+ title: source.title,
24
+ databaseId: database.id,
25
+ databaseTitle: database.title,
26
+ })));
27
+ others.sort((a, b) => collator.compare(a.title, b.title) ||
28
+ collator.compare(a.databaseTitle ?? '', b.databaseTitle ?? '') ||
29
+ (a.databaseId ?? '').localeCompare(b.databaseId ?? '') ||
30
+ a.sourceId.localeCompare(b.sourceId));
31
+ return [...own, ...others];
32
+ }
33
+ /**
34
+ * A source's database as a trailing argument, only when it is another one:
35
+ * a call about the block's own database is made exactly as in 2.3.
36
+ */
37
+ export const inDatabase = (databaseId) => databaseId === undefined ? [] : [databaseId];
38
+ /**
39
+ * Whether `source` is the one a view reads, by database AND source: the same
40
+ * `sourceId` in another database is another source. `viewDatabaseId`
41
+ * absent is the block's database, as `source.databaseId` absent is.
42
+ */
43
+ export function isViewSource(source, viewSourceId, viewDatabaseId) {
44
+ return (source.sourceId === viewSourceId && source.databaseId === viewDatabaseId);
45
+ }
46
+ /**
47
+ * The postfix of a source of another database: its database's title, grey,
48
+ * on the right (and the check of the current source after it, since a
49
+ * postfix replaces the menu's own check). `undefined` for the block's own
50
+ * sources, so their entries stay the 2.3 ones.
51
+ */
52
+ export function databaseLabel(source, untitled, selected = false) {
53
+ if (source.databaseId === undefined)
54
+ return undefined;
55
+ return html `<span
56
+ data-testid="source-database-label"
57
+ style="margin-left: auto; padding-left: 8px; font-size: 12px; color: var(--affine-text-secondary-color); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 160px;"
58
+ >${source.databaseTitle || untitled}</span
59
+ >${selected ? DoneIcon() : undefined}`;
60
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@formicoidea/labre-core-database",
3
3
  "description": "Labre core-database editing chrome for @formicoidea/labre-core.",
4
- "version": "2.1.1",
4
+ "version": "2.4.0",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "author": "lajola",
@@ -27,7 +27,7 @@
27
27
  "dist"
28
28
  ],
29
29
  "dependencies": {
30
- "@formicoidea/labre-core": "2.1.1",
30
+ "@formicoidea/labre-core": "2.4.0",
31
31
  "@blocksuite/icons": "^2.2.12",
32
32
  "@preact/signals-core": "^1.8.0",
33
33
  "lit": "^3.2.0"