@bevel-software/platform-core-frontend 0.33.0 → 0.34.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.
Files changed (59) hide show
  1. package/package.json +2 -2
  2. package/src/lib/__tests__/api.test.ts +28 -1
  3. package/src/lib/api.ts +4 -2
  4. package/src/modules/access/__tests__/ManageAccessDialog.add-row-picking.test.tsx +53 -1
  5. package/src/modules/access/api.ts +3 -4
  6. package/src/modules/access/components/ManageAccessDialog.tsx +7 -7
  7. package/src/modules/admin/__tests__/DirectoryGroupsPage.test.tsx +51 -0
  8. package/src/modules/admin/components/DirectoryGroupsPage.tsx +42 -5
  9. package/src/modules/admin/services/groups.api.ts +11 -1
  10. package/src/modules/agents/__tests__/useConnectAsChoice.test.ts +56 -0
  11. package/src/modules/agents/components/ConnectAsChoice.tsx +81 -0
  12. package/src/modules/agents/connect-as.ts +15 -0
  13. package/src/modules/agents/index.ts +5 -0
  14. package/src/modules/agents/services/agent-choices.api.ts +39 -0
  15. package/src/modules/agents/useConnectAsChoice.ts +66 -0
  16. package/src/modules/audit/__tests__/AuditLogPage.test.tsx +13 -0
  17. package/src/modules/audit/components/AuditLogPage.tsx +9 -0
  18. package/src/modules/audit/services/audit.api.ts +5 -0
  19. package/src/modules/auth/__tests__/AccountPage.test.tsx +21 -0
  20. package/src/modules/auth/__tests__/account.api.test.ts +69 -0
  21. package/src/modules/auth/components/AccountPage.tsx +8 -2
  22. package/src/modules/auth/services/account.api.ts +18 -2
  23. package/src/modules/git/__tests__/BranchSwitcher.test.tsx +0 -14
  24. package/src/modules/git/__tests__/git.api.test.ts +2 -2
  25. package/src/modules/git/hooks/__tests__/useGitState.deleteBranch.test.tsx +1 -1
  26. package/src/modules/library/__tests__/SkillPage.test.tsx +229 -24
  27. package/src/modules/library/components/skill-page/SkillFilePane.tsx +57 -9
  28. package/src/modules/library/components/skill-page/SkillPage.tsx +65 -25
  29. package/src/modules/review/components/MarkdownDiffViewer.tsx +23 -1
  30. package/src/modules/review/components/ReviewPanel.tsx +8 -2
  31. package/src/modules/review/components/__tests__/MarkdownDiffViewer.test.tsx +31 -0
  32. package/src/modules/secrets-vault/components/ConnectToolsPage.tsx +79 -16
  33. package/src/modules/secrets-vault/components/__tests__/ConnectToolsPage.test.tsx +112 -2
  34. package/src/modules/secrets-vault/services/connect.api.ts +20 -5
  35. package/src/modules/toolbar/components/ExternalAgentAccessPage.tsx +38 -4
  36. package/src/modules/toolbar/components/__tests__/ExternalAgentAccessPage.test.tsx +91 -2
  37. package/src/modules/toolbar/services/external-api-keys.api.ts +5 -2
  38. package/src/modules/workspace/components/__tests__/FileExplorer.test.tsx +26 -0
  39. package/src/modules/workspace/components/renderers/HtmlRenderer.tsx +40 -7
  40. package/src/modules/workspace/components/renderers/KbAppLink.tsx +56 -0
  41. package/src/modules/workspace/components/renderers/KbMarkdownView.tsx +30 -10
  42. package/src/modules/workspace/components/renderers/MarkdownRenderer.tsx +37 -40
  43. package/src/modules/workspace/components/renderers/__tests__/HtmlRenderer.links.test.tsx +185 -0
  44. package/src/modules/workspace/components/renderers/__tests__/MarkdownRenderer.sectionReclick.test.tsx +88 -0
  45. package/src/modules/workspace/components/renderers/__tests__/MarkdownRenderer.test.tsx +199 -29
  46. package/src/modules/workspace/components/renderers/__tests__/bridgeHarness.ts +37 -4
  47. package/src/modules/workspace/components/renderers/__tests__/htmlSandbox.test.ts +59 -0
  48. package/src/modules/workspace/components/renderers/__tests__/htmlViewsGuide.test.ts +15 -1
  49. package/src/modules/workspace/components/renderers/__tests__/liveDataReferencePage.test.ts +397 -0
  50. package/src/modules/workspace/components/renderers/htmlSandbox.ts +48 -15
  51. package/src/modules/workspace/components/renderers/htmlToolBridge.ts +19 -0
  52. package/src/modules/workspace/components/renderers/kbMarkdownPipeline.tsx +35 -28
  53. package/src/modules/workspace/components/renderers/scrollToHeading.ts +35 -0
  54. package/src/modules/workspace/hooks/__tests__/useLinksOut.test.ts +6 -0
  55. package/src/modules/workspace/hooks/useLinksOut.ts +12 -6
  56. package/src/modules/workspace/index.ts +21 -2
  57. package/src/modules/workspace/routing/__tests__/kb-routes.test.tsx +262 -25
  58. package/src/modules/workspace/routing/kb-routes.ts +221 -53
  59. package/src/modules/workspace/utils/fileTree.ts +1 -1
@@ -1,7 +1,7 @@
1
1
  import { describe, it, expect, vi } from 'vitest';
2
2
  import { render, screen, waitFor, fireEvent } from '@testing-library/react';
3
3
  import userEvent from '@testing-library/user-event';
4
- import { MemoryRouter } from 'react-router-dom';
4
+ import { MemoryRouter, useLocation } from 'react-router-dom';
5
5
  import { WorkspaceContext, type WorkspaceContextValue } from '../../../state/workspace.context';
6
6
  import { makeWorkspaceFixture } from '../../../__tests__/testFixtures';
7
7
  import { GitContext, type GitContextValue } from '../../../../git/state/git.context';
@@ -16,6 +16,17 @@ vi.mock('react-router-dom', async (importActual) => ({
16
16
  useNavigate: () => navigateMock,
17
17
  }));
18
18
 
19
+ /** The router's location, as text — what a link inside the file must not change. */
20
+ function LocationProbe() {
21
+ const location = useLocation();
22
+ return <output data-testid="location">{location.pathname + location.search + location.hash}</output>;
23
+ }
24
+
25
+ /** `window.open` answering with a tab, as a browser does when it opens one. */
26
+ function stubWindowOpen() {
27
+ return vi.spyOn(window, 'open').mockImplementation(() => ({ opener: window }) as unknown as Window);
28
+ }
29
+
19
30
  function makeGit(): GitContextValue {
20
31
  return {
21
32
  status: { branch: 'alice/draft', hasUpstream: true, unmergedFromUpstream: false },
@@ -195,71 +206,230 @@ describe('MarkdownRenderer', () => {
195
206
 
196
207
  // --- Link rendering in preview (readOnly) mode ---
197
208
 
198
- function renderPreview(content: string, filePath: string) {
209
+ // `at` is the address this tab is on — a file page, or the same page in
210
+ // full screen. The probe shows the router's location, which a link inside
211
+ // the file must leave as it was.
212
+ function renderPreview(content: string, filePath: string, at = `/workspace/alice%2Fdraft/${filePath}`) {
199
213
  return render(
200
- <MemoryRouter>
214
+ <MemoryRouter initialEntries={[at]}>
201
215
  <WorkspaceContext.Provider value={makeWorkspace()}>
202
216
  <GitContext.Provider value={makeGit()}>
203
217
  <MarkdownRenderer content={content} filePath={filePath} onSave={async () => {}} readOnly />
218
+ <LocationProbe />
204
219
  </GitContext.Provider>
205
220
  </WorkspaceContext.Provider>
206
221
  </MemoryRouter>,
207
222
  );
208
223
  }
209
224
 
210
- it('renders a nodeType frontmatter value as a clickable link that navigates', async () => {
225
+ /**
226
+ * A link inside the file opened `url` in a new tab, and this tab went
227
+ * nowhere. The anchor names the same address with `target="_blank"`, so a
228
+ * middle-click or the context menu opens what the click opened.
229
+ */
230
+ async function expectOpensInNewTab(name: string, url: string) {
211
231
  navigateMock.mockClear();
212
- const user = userEvent.setup();
232
+ const open = stubWindowOpen();
233
+ const before = screen.getByTestId('location').textContent;
234
+ const link = screen.getByRole('link', { name });
235
+ expect(link).toHaveAttribute('href', url);
236
+ expect(link).toHaveAttribute('target', '_blank');
237
+ expect(link).toHaveAttribute('rel', 'noopener');
238
+ await userEvent.setup().click(link);
239
+ expect(open).toHaveBeenCalledWith(url, '_blank');
240
+ expect(navigateMock).not.toHaveBeenCalled();
241
+ expect(screen.getByTestId('location').textContent).toBe(before);
242
+ open.mockRestore();
243
+ }
244
+
245
+ it('opens a nodeType frontmatter link in a new tab', async () => {
213
246
  renderPreview(
214
247
  '---\nnodeType: "[Process](../NodeTypes/Process.md)"\n---\n\n# Body',
215
248
  'Knowledge/Sub/Foo.md',
216
249
  );
217
- const link = screen.getByRole('link', { name: 'Process' });
218
- await user.click(link);
219
- expect(navigateMock).toHaveBeenCalledWith('/workspace/alice%2Fdraft/Knowledge/NodeTypes/Process.md');
250
+ await expectOpensInNewTab('Process', '/workspace/alice%2Fdraft/Knowledge/NodeTypes/Process.md');
220
251
  });
221
252
 
222
- it('renders a bare body link whose path contains spaces as a working link', async () => {
223
- navigateMock.mockClear();
224
- const user = userEvent.setup();
253
+ it('opens a bare body link whose path contains spaces in a new tab', async () => {
225
254
  renderPreview('[Open](Some File.md)', 'Knowledge/Foo.md');
226
- const link = screen.getByRole('link', { name: 'Open' });
227
- await user.click(link);
228
- expect(navigateMock).toHaveBeenCalledWith('/workspace/alice%2Fdraft/Knowledge/Some%20File.md');
255
+ await expectOpensInNewTab('Open', '/workspace/alice%2Fdraft/Knowledge/Some%20File.md');
229
256
  });
230
257
 
231
- it('renders an angle-bracketed body link with spaces as a working link', async () => {
232
- navigateMock.mockClear();
233
- const user = userEvent.setup();
258
+ it('opens an angle-bracketed body link with spaces in a new tab', async () => {
234
259
  renderPreview('[Open](<Some File.md>)', 'Knowledge/Foo.md');
235
- const link = screen.getByRole('link', { name: 'Open' });
236
- await user.click(link);
237
- expect(navigateMock).toHaveBeenCalledWith('/workspace/alice%2Fdraft/Knowledge/Some%20File.md');
260
+ await expectOpensInNewTab('Open', '/workspace/alice%2Fdraft/Knowledge/Some%20File.md');
238
261
  });
239
262
 
240
263
  // The link a paste wraps a Copy path in: root-anchored, so it opens the same
241
264
  // file however deep the linking page sits.
242
265
  it('opens a pasted root-anchored link from a file in a different folder', async () => {
243
- navigateMock.mockClear();
244
- const user = userEvent.setup();
245
266
  renderPreview(
246
267
  '[subfile](</knowledge-base/KnowledgeBase/File Type examples/subfile.md>)',
247
268
  'knowledge-base/KnowledgeBase/Other/Deep/parent.md',
248
269
  );
249
- await user.click(screen.getByRole('link', { name: 'subfile' }));
250
- expect(navigateMock).toHaveBeenCalledWith(
270
+ await expectOpensInNewTab(
271
+ 'subfile',
251
272
  '/workspace/alice%2Fdraft/knowledge-base/KnowledgeBase/File%20Type%20examples/subfile.md',
252
273
  );
253
274
  });
254
275
 
255
276
  it('keeps the branch of an absolute citation URL instead of resolving it against the file', async () => {
256
- navigateMock.mockClear();
257
- const user = userEvent.setup();
258
277
  renderPreview('[Cited](/workspace/target-company-state/knowledge-base/GTM/Bundle.md#status)', 'Knowledge/Foo.md');
259
- await user.click(screen.getByRole('link', { name: 'Cited' }));
260
- expect(navigateMock).toHaveBeenCalledWith(
261
- '/workspace/target-company-state/knowledge-base/GTM/Bundle.md#status',
278
+ await expectOpensInNewTab('Cited', '/workspace/target-company-state/knowledge-base/GTM/Bundle.md#status');
279
+ });
280
+
281
+ // Once the hook resolves targets, a workspace link that is not a `.md` is
282
+ // the app's too: left to the browser it was a relative URL that replaced
283
+ // the page being read.
284
+ it('opens a link to an HTML page of the workspace in a new tab', async () => {
285
+ renderPreview('[Board](../Dashboards/Board.html)', 'Knowledge/Foo.md');
286
+ await expectOpensInNewTab('Board', '/workspace/alice%2Fdraft/Dashboards/Board.html');
287
+ });
288
+
289
+ // A scheme is what makes a link external, not a name that starts `http`:
290
+ // judged by prefix, this page of the workspace was left to the browser as a
291
+ // relative URL that replaced the page being read.
292
+ it('opens a relative link to a file whose name starts with http in a new tab', async () => {
293
+ renderPreview('[Notes](http-notes.md)', 'Knowledge/Foo.md');
294
+ await expectOpensInNewTab('Notes', '/workspace/alice%2Fdraft/Knowledge/http-notes.md');
295
+ });
296
+
297
+ it('opens an id-link in a new tab at the node URL, heading kept', async () => {
298
+ renderPreview('[Plan](plan-node#goal)', 'Knowledge/Foo.md');
299
+ await expectOpensInNewTab('Plan', '/workspace/alice%2Fdraft/plan-node#goal');
300
+ });
301
+
302
+ // Full screen is this tab's view, asked for by its query. The new tab gets
303
+ // the target's plain address, and this one keeps `?fullscreen=1`.
304
+ it('in full screen, opens the plain address in a new tab and stays full screen', async () => {
305
+ renderPreview('[Open](Other.md)', 'Knowledge/Foo.md', '/workspace/alice%2Fdraft/Knowledge/Foo.md?fullscreen=1');
306
+ expect(screen.getByTestId('location')).toHaveTextContent('?fullscreen=1');
307
+ await expectOpensInNewTab('Open', '/workspace/alice%2Fdraft/Knowledge/Other.md');
308
+ expect(screen.getByTestId('location')).toHaveTextContent('?fullscreen=1');
309
+ });
310
+
311
+ it('in full screen, opens an id-link in a new tab at the plain node URL', async () => {
312
+ renderPreview('[Plan](plan-node)', 'Knowledge/Foo.md', '/workspace/alice%2Fdraft/foo-node?fullscreen=1');
313
+ await expectOpensInNewTab('Plan', '/workspace/alice%2Fdraft/plan-node');
314
+ });
315
+
316
+ // A section link is the browser's: a plain fragment anchor, no new tab.
317
+ it('leaves a same-file anchor to scroll in place', async () => {
318
+ renderPreview('[Goal](#goal)', 'Knowledge/Foo.md');
319
+ const open = stubWindowOpen();
320
+ const link = screen.getByRole('link', { name: 'Goal' });
321
+ expect(link).toHaveAttribute('href', '#goal');
322
+ expect(link).not.toHaveAttribute('target');
323
+ await userEvent.setup().click(link);
324
+ expect(open).not.toHaveBeenCalled();
325
+ open.mockRestore();
326
+ });
327
+
328
+ // The same section written as a path to this file: this tab moves by its
329
+ // fragment only, on its own address and query — full screen stays.
330
+ it('scrolls a link to a section of this file in place, keeping the address and query', async () => {
331
+ navigateMock.mockClear();
332
+ renderPreview('[Goal](Foo.md#goal)', 'Knowledge/Foo.md', '/workspace/alice%2Fdraft/foo-node?fullscreen=1');
333
+ const open = stubWindowOpen();
334
+ const link = screen.getByRole('link', { name: 'Goal' });
335
+ expect(link).toHaveAttribute('href', '/workspace/alice%2Fdraft/foo-node?fullscreen=1#goal');
336
+ expect(link).not.toHaveAttribute('target');
337
+ await userEvent.setup().click(link);
338
+ expect(navigateMock).toHaveBeenCalledWith('/workspace/alice%2Fdraft/foo-node?fullscreen=1#goal');
339
+ expect(open).not.toHaveBeenCalled();
340
+ open.mockRestore();
341
+ });
342
+
343
+ // ...and on that address the page scrolls the heading the fragment names
344
+ // into view: the other half of "a section link scrolls in place".
345
+ it('scrolls the heading a section address names into view, in full screen too', async () => {
346
+ const original = Element.prototype.scrollIntoView;
347
+ const scrolled: string[] = [];
348
+ Element.prototype.scrollIntoView = function (this: Element) {
349
+ scrolled.push(this.id);
350
+ };
351
+ try {
352
+ renderPreview('# Intro\n\n## Goal\n\nText', 'Knowledge/Foo.md', '/workspace/alice%2Fdraft/foo-node?fullscreen=1#goal');
353
+ await waitFor(() => expect(scrolled).toEqual(['goal']));
354
+ expect(screen.getByTestId('location').textContent).toBe('/workspace/alice%2Fdraft/foo-node?fullscreen=1#goal');
355
+ } finally {
356
+ Element.prototype.scrollIntoView = original;
357
+ }
358
+ });
359
+
360
+ // Ctrl/⌘/Shift-click on a new-tab link is the browser's to answer, as for
361
+ // any `target="_blank"` anchor: the app neither cancels it nor opens a tab.
362
+ it('leaves a modifier-click on a link to the browser', () => {
363
+ renderPreview('[Open](Other.md)', 'Knowledge/Foo.md');
364
+ const open = stubWindowOpen();
365
+ const link = screen.getByRole('link', { name: 'Open' });
366
+ // Read once the app's handlers have all had the click, then cancelled so
367
+ // the test DOM does not go and fetch the link itself.
368
+ const leftToBrowser: boolean[] = [];
369
+ const after = (e: Event) => {
370
+ leftToBrowser.push(!e.defaultPrevented);
371
+ e.preventDefault();
372
+ };
373
+ window.addEventListener('click', after);
374
+ for (const modifier of [{ ctrlKey: true }, { metaKey: true }, { shiftKey: true }]) {
375
+ link.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, ...modifier }));
376
+ }
377
+ window.removeEventListener('click', after);
378
+ expect(leftToBrowser).toEqual([true, true, true]);
379
+ expect(open).not.toHaveBeenCalled();
380
+ open.mockRestore();
381
+ });
382
+
383
+ // A section link carries this tab's own address too, so a Ctrl-click or a
384
+ // middle-click on it is the browser's: it opens that section where the
385
+ // reader asked, and this tab does not move.
386
+ it('leaves a modified or middle click on a section link to the browser', () => {
387
+ navigateMock.mockClear();
388
+ renderPreview('[Goal](Foo.md#goal)', 'Knowledge/Foo.md', '/workspace/alice%2Fdraft/foo-node?fullscreen=1');
389
+ const open = stubWindowOpen();
390
+ const link = screen.getByRole('link', { name: 'Goal' });
391
+ const leftToBrowser: boolean[] = [];
392
+ const after = (e: Event) => {
393
+ leftToBrowser.push(!e.defaultPrevented);
394
+ e.preventDefault();
395
+ };
396
+ window.addEventListener('click', after);
397
+ for (const init of [{ ctrlKey: true }, { metaKey: true }, { shiftKey: true }, { button: 1 }]) {
398
+ link.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, ...init }));
399
+ }
400
+ window.removeEventListener('click', after);
401
+ expect(leftToBrowser).toEqual([true, true, true, true]);
402
+ expect(navigateMock).not.toHaveBeenCalled();
403
+ expect(open).not.toHaveBeenCalled();
404
+ open.mockRestore();
405
+ });
406
+
407
+ // The node on screen is known by its id, not read off the address: a tab
408
+ // still on the file's PATH URL (not canonicalized yet, or a library frame
409
+ // that never canonicalizes) keeps an id-link to this node in place.
410
+ it('scrolls an id-link to the node on screen in place while the tab is on its path URL', async () => {
411
+ navigateMock.mockClear();
412
+ const filePath = 'Knowledge/OnPath.md';
413
+ const fetchSpy = vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) =>
414
+ String(input).includes('/resolve-path?')
415
+ ? new Response(JSON.stringify({ id: 'on-path-node' }), { status: 200 })
416
+ : new Response('', { status: 404 }),
262
417
  );
418
+ try {
419
+ renderPreview('[Goal](on-path-node#goal) [Plan](plan-node)', filePath, `/workspace/alice%2Fdraft/${filePath}?fullscreen=1`);
420
+ const here = `/workspace/alice%2Fdraft/${filePath}?fullscreen=1#goal`;
421
+ await waitFor(() => expect(screen.getByRole('link', { name: 'Goal' })).toHaveAttribute('href', here));
422
+ expect(screen.getByRole('link', { name: 'Goal' })).not.toHaveAttribute('target');
423
+ const open = stubWindowOpen();
424
+ await userEvent.setup().click(screen.getByRole('link', { name: 'Goal' }));
425
+ expect(navigateMock).toHaveBeenCalledWith(here);
426
+ expect(open).not.toHaveBeenCalled();
427
+ open.mockRestore();
428
+ // Another node is still another tab.
429
+ await expectOpensInNewTab('Plan', '/workspace/alice%2Fdraft/plan-node');
430
+ } finally {
431
+ fetchSpy.mockRestore();
432
+ }
263
433
  });
264
434
 
265
435
  // --- Images in preview mode ---
@@ -16,9 +16,22 @@ export interface BridgeRun {
16
16
  * was cancelled. `alreadyPrevented` is a click a handler of the page's own
17
17
  * cancelled before it bubbled up to the bridge. `noAnchor` is a click on
18
18
  * something that is not inside an `<a>` at all — a button, a div — which
19
- * the bridge must leave alone.
19
+ * the bridge must leave alone. `ctrlKey`/`metaKey`/`shiftKey`/`altKey`
20
+ * are the keys held; `button` 1 is the middle button, which a browser
21
+ * dispatches as an `auxclick` (never a `click`), and so does this.
20
22
  */
21
- click: (href: string, opts?: { alreadyPrevented?: boolean; noAnchor?: boolean }) => { prevented: boolean };
23
+ click: (
24
+ href: string,
25
+ opts?: {
26
+ alreadyPrevented?: boolean;
27
+ noAnchor?: boolean;
28
+ ctrlKey?: boolean;
29
+ metaKey?: boolean;
30
+ shiftKey?: boolean;
31
+ altKey?: boolean;
32
+ button?: number;
33
+ },
34
+ ) => { prevented: boolean };
22
35
  /** `window.bevel` as the page's own scripts see it. */
23
36
  bevel: Record<string, unknown>;
24
37
  /** Messages posted to the parent. */
@@ -59,8 +72,19 @@ export function runBridge(
59
72
  const out = buildSandboxedHtml({ title: 't', libModuleSources: [], bodyHtml: '', toolBridge: opts.toolBridge });
60
73
  const scriptBody = out.match(/<script type="module">([\s\S]*?)<\/script>/)![1];
61
74
 
62
- type ClickEvent = { target: { closest: () => unknown }; defaultPrevented: boolean; preventDefault: () => void };
75
+ type ClickEvent = {
76
+ type: string;
77
+ button: number;
78
+ ctrlKey: boolean;
79
+ metaKey: boolean;
80
+ shiftKey: boolean;
81
+ altKey: boolean;
82
+ target: { closest: () => unknown };
83
+ defaultPrevented: boolean;
84
+ preventDefault: () => void;
85
+ };
63
86
  const handlers: ((e: ClickEvent) => void)[] = [];
87
+ const auxHandlers: ((e: ClickEvent) => void)[] = [];
64
88
  const posted: unknown[] = [];
65
89
  const scrolled: string[] = [];
66
90
  const scrolledTags: string[] = [];
@@ -108,6 +132,7 @@ export function runBridge(
108
132
  document: {
109
133
  addEventListener: (type: string, fn: (e: ClickEvent) => void) => {
110
134
  if (type === 'click') handlers.push(fn);
135
+ if (type === 'auxclick') auxHandlers.push(fn);
111
136
  },
112
137
  getElementById: (id: string) => (ids.has(id) ? element(id) : null),
113
138
  // In document order a control comes first here, so a fallback that
@@ -130,7 +155,15 @@ export function runBridge(
130
155
  click: (href: string, opts = {}) => {
131
156
  let prevented = opts.alreadyPrevented ?? false;
132
157
  const anchor = opts.noAnchor ? null : { getAttribute: () => href };
133
- handlers[0]({
158
+ const button = opts.button ?? 0;
159
+ const type = button === 0 ? 'click' : 'auxclick';
160
+ for (const fn of type === 'click' ? handlers : auxHandlers) fn({
161
+ type,
162
+ button,
163
+ ctrlKey: opts.ctrlKey ?? false,
164
+ metaKey: opts.metaKey ?? false,
165
+ shiftKey: opts.shiftKey ?? false,
166
+ altKey: opts.altKey ?? false,
134
167
  target: { closest: () => anchor },
135
168
  defaultPrevented: prevented,
136
169
  preventDefault: () => (prevented = true),
@@ -482,6 +482,51 @@ describe('buildSandboxedHtml', () => {
482
482
  expect(bridge.scrolled).toEqual(['goal']);
483
483
  });
484
484
 
485
+ /**
486
+ * A click the reader asked a new tab of. The frame's sandbox has no
487
+ * allow-popups, so the browser cannot open one from inside it: the bridge
488
+ * cancels the click and asks the parent, saying so (`newTab: true`), for
489
+ * every destination, a fragment of this page included. A plain click and
490
+ * an Alt click are followed as before.
491
+ */
492
+ describe('a modified or middle click asks the parent for a new tab', () => {
493
+ it('posts newTab for Ctrl, Cmd, Shift and the middle button, to another file and to a section of this page', () => {
494
+ const bridge = runBridge({ ids: ['totals'] });
495
+ expect(bridge.click('Board.html', { ctrlKey: true })).toEqual({ prevented: true });
496
+ expect(bridge.click('Board.html#q3', { metaKey: true })).toEqual({ prevented: true });
497
+ expect(bridge.click(' https://example.com/docs ', { shiftKey: true })).toEqual({ prevented: true });
498
+ expect(bridge.click('#totals', { button: 1 })).toEqual({ prevented: true });
499
+ expect(bridge.posted).toEqual([
500
+ { type: 'bevel.navigate', href: 'Board.html', newTab: true },
501
+ { type: 'bevel.navigate', href: 'Board.html#q3', newTab: true },
502
+ { type: 'bevel.navigate', href: 'https://example.com/docs', newTab: true },
503
+ { type: 'bevel.navigate', href: '#totals', newTab: true },
504
+ ]);
505
+ // A section asked for in a new tab does not scroll this one.
506
+ expect(bridge.scrolled).toEqual([]);
507
+ });
508
+
509
+ it('follows a plain or an Alt click as before, and leaves the right button alone', () => {
510
+ const bridge = runBridge({ ids: ['totals'] });
511
+ bridge.click('Board.html');
512
+ bridge.click('Board.html', { altKey: true });
513
+ bridge.click('#totals');
514
+ expect(bridge.click('Board.html', { button: 2 })).toEqual({ prevented: false });
515
+ expect(bridge.posted).toEqual([
516
+ { type: 'bevel.navigate', href: 'Board.html' },
517
+ { type: 'bevel.navigate', href: 'Board.html' },
518
+ ]);
519
+ expect(bridge.scrolled).toEqual(['totals']);
520
+ });
521
+
522
+ it('leaves a modified click the page already cancelled to the page', () => {
523
+ const bridge = runBridge();
524
+ bridge.click('Board.html', { ctrlKey: true, alreadyPrevented: true });
525
+ bridge.click('Board.html', { button: 1, alreadyPrevented: true });
526
+ expect(bridge.posted).toEqual([]);
527
+ });
528
+ });
529
+
485
530
  /**
486
531
  * A bare fragment is an in-page jump by either route. Left to the browser, a
487
532
  * `srcdoc` frame resolves `#totals` against the APP's URL and navigates
@@ -592,6 +637,20 @@ describe('buildSandboxedHtml', () => {
592
637
  ]);
593
638
  expect(bridge.scrolled).toEqual([]);
594
639
  });
640
+
641
+ it('a fragment the parent hands back (a link to this page by its own name) scrolls as a bare one does', async () => {
642
+ const bridge = runBridge({ ids: ['q3'] });
643
+ await bridge.deliver({ type: 'bevel.jump', hash: '#q3' });
644
+ expect(bridge.scrolled).toEqual(['q3']);
645
+ expect(bridge.posted).toEqual([]);
646
+ });
647
+
648
+ it('takes a jump from the parent only: a message from anywhere else scrolls nothing', async () => {
649
+ const bridge = runBridge({ ids: ['q3'] });
650
+ await bridge.deliver({ type: 'bevel.jump', hash: '#q3' }, { fromParent: false });
651
+ await bridge.deliver({ type: 'bevel.jump', hash: 'q3' });
652
+ expect(bridge.scrolled).toEqual([]);
653
+ });
595
654
  });
596
655
 
597
656
  it('embeds the body HTML verbatim (sanitization happens upstream)', () => {
@@ -119,6 +119,20 @@ describe('the html-views guide section agrees with the renderer', () => {
119
119
  expect(bridge.posted).toEqual([]);
120
120
  });
121
121
 
122
+ it('says a modified or middle click opens a new tab, and the bridge asks the app for one', () => {
123
+ expect(flat).toContain(
124
+ 'A click on a link with Ctrl, Cmd or Shift held, or with the middle button, opens its destination in a new tab, as a browser does with a link: a bare fragment opens the page\'s own file at that section in a new tab, and the page stays where it is.',
125
+ );
126
+ const bridge = runBridge({ ids: ['totals'] });
127
+ bridge.click('#totals', { ctrlKey: true });
128
+ bridge.click('../Knowledge/Alice.md', { button: 1 });
129
+ expect(bridge.scrolled).toEqual([]);
130
+ expect(bridge.posted).toEqual([
131
+ { type: 'bevel.navigate', href: '#totals', newTab: true },
132
+ { type: 'bevel.navigate', href: '../Knowledge/Alice.md', newTab: true },
133
+ ]);
134
+ });
135
+
122
136
  it('names the addresses only a script may open, and the app opens exactly those beyond the markup', () => {
123
137
  expect(flat).toContain(
124
138
  'A call from a script may also open `tel:`, `sms:`, `geo:` and a protocol-relative `//host/path` address in a new tab, though a link written in the markup cannot keep one.',
@@ -144,7 +158,7 @@ describe('the html-views guide section agrees with the renderer', () => {
144
158
  const named = new Set([...guide.matchAll(/window\.bevel\??\.(\w+)/g)].map((m) => m[1]!));
145
159
  expect([...named].sort()).toEqual(Object.keys(runBridge().bevel).sort());
146
160
  expect([...named].sort()).toEqual(Object.keys(runBridge({}, { toolBridge: true }).bevel).sort());
147
- expect([...named].sort()).toEqual(['navigate', 'openNode', 'requireTools', 'tools']);
161
+ expect([...named].sort()).toEqual(['branch', 'navigate', 'openNode', 'requireTools', 'tools']);
148
162
  expect(flat).toContain('A distribution may add data members of its own to `window.bevel`');
149
163
  });
150
164