@gloom-sh/terminal 0.20.22 → 0.20.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/account/heard-from-question.tsx +6 -4
- package/src/account/heard-from.ts +3 -4
- package/src/api-client/exchange-announcements.ts +2 -2
- package/src/api-client/heard-from.ts +5 -7
- package/src/api-client/ownership-disclosures.ts +38 -5
- package/src/api-client/types/ticker-data.ts +3 -1
- package/src/architecture/dom-cell-metrics-allowlist.json +2 -2
- package/src/cli/commands/market.ts +5 -3
- package/src/cli/commands/overview.ts +7 -9
- package/src/cli/listing-arg.ts +45 -18
- package/src/cli/pane-functions/catalog-snapshot.json +2 -2
- package/src/cli/pane-functions/data.ts +3 -1
- package/src/cli/pane-functions/freshness.ts +6 -1
- package/src/cli/pane-functions/function-help.ts +0 -2
- package/src/cli/pane-functions/market-session.ts +12 -2
- package/src/cli/pane-functions/screenshot-payload.ts +3 -1
- package/src/cli/symbol-list.ts +52 -14
- package/src/cli/ticker-resolution.ts +133 -38
- package/src/components/chart/composite/composite-chart.tsx +12 -0
- package/src/components/chart/composite/types.ts +18 -0
- package/src/components/chart/composite/use-composite-chart-keyboard.ts +38 -15
- package/src/components/chart-table/header.tsx +11 -5
- package/src/components/chart-table/selection.ts +20 -1
- package/src/components/data-table/view.tsx +2 -0
- package/src/components/feed-data-table/stack-view.tsx +85 -5
- package/src/components/layout/pane/header.tsx +12 -30
- package/src/components/ticker/badge/index.tsx +9 -4
- package/src/components/ticker/badge/list.tsx +3 -1
- package/src/components/ui/data-table/index.tsx +7 -1
- package/src/components/ui/data-table/muted-row.tsx +47 -0
- package/src/components/ui/data-table/opentui/index.tsx +42 -11
- package/src/components/ui/data-table/spill.ts +36 -0
- package/src/components/ui/data-table/types.ts +17 -1
- package/src/components/ui/data-table/use-column-order.ts +11 -0
- package/src/components/ui/icon.tsx +50 -3
- package/src/components/ui/list-view.tsx +2 -2
- package/src/components/ui/table-layout.ts +32 -0
- package/src/components/ui/terminal-hover-label.tsx +49 -0
- package/src/components/use-pinned-moment.ts +222 -13
- package/src/core/state/app/state.ts +11 -2
- package/src/core/state/app/types.ts +2 -1
- package/src/data/sqlite/access.ts +14 -1
- package/src/data/sqlite/checkpointer.ts +2 -2
- package/src/data/sqlite/database.ts +4 -0
- package/src/docs/snapshot.json +180 -26
- package/src/i18n/es.ts +1 -0
- package/src/i18n/ja.ts +1 -0
- package/src/i18n/ko.ts +1 -0
- package/src/i18n/zh-cn.ts +1 -0
- package/src/i18n/zh-tw.ts +1 -0
- package/src/market-data/market/freshness.ts +2 -2
- package/src/market-data/market/globex-day.ts +14 -0
- package/src/market-data/published-us-sessions.ts +3 -2
- package/src/market-data/quotes/live-valuation.ts +4 -2
- package/src/market-data/quotes/metadata.ts +48 -5
- package/src/market-data/quotes/regular-session-valuation.ts +12 -1
- package/src/plugins/builtin/alerts/history-pane.tsx +19 -3
- package/src/plugins/builtin/broker-trading/orders.tsx +4 -2
- package/src/plugins/builtin/chat/attachments/desktop.tsx +46 -12
- package/src/plugins/builtin/chat/channels.ts +5 -2
- package/src/plugins/builtin/chat/content/composer-runtime.ts +64 -65
- package/src/plugins/builtin/chat/content/footer.tsx +17 -7
- package/src/plugins/builtin/chat/content/index.tsx +108 -36
- package/src/plugins/builtin/chat/content/new-dm-dialog.tsx +21 -8
- package/src/plugins/builtin/chat/content/selection-runtime.ts +16 -0
- package/src/plugins/builtin/chat/content/shortcuts.ts +3 -0
- package/src/plugins/builtin/chat/content/snapshot.ts +37 -13
- package/src/plugins/builtin/chat/content/transcript.tsx +12 -0
- package/src/plugins/builtin/chat/content/types.ts +2 -0
- package/src/plugins/builtin/chat/controller/channels.ts +15 -15
- package/src/plugins/builtin/chat/controller/index.ts +140 -34
- package/src/plugins/builtin/chat/controller/lifecycle.ts +8 -12
- package/src/plugins/builtin/chat/controller/persistence.ts +32 -4
- package/src/plugins/builtin/chat/controller/send.ts +41 -17
- package/src/plugins/builtin/chat/controller/session-runtime.ts +28 -27
- package/src/plugins/builtin/chat/controller/state.ts +17 -8
- package/src/plugins/builtin/chat/controller/storage.ts +41 -4
- package/src/plugins/builtin/chat/controller/view.ts +6 -3
- package/src/plugins/builtin/chat/index.tsx +1 -1
- package/src/plugins/builtin/chat/layout.ts +8 -1
- package/src/plugins/builtin/chat/message/desktop.tsx +15 -11
- package/src/plugins/builtin/chat/message/header.tsx +112 -30
- package/src/plugins/builtin/chat/message/reactions.tsx +10 -11
- package/src/plugins/builtin/chat/message/render-state.ts +4 -2
- package/src/plugins/builtin/chat/message/terminal.tsx +13 -15
- package/src/plugins/builtin/chat/message/types.ts +5 -0
- package/src/plugins/builtin/cloud-tweets/table.tsx +25 -2
- package/src/plugins/builtin/company-kpis/model.ts +11 -3
- package/src/plugins/builtin/correlation/compute.ts +6 -1
- package/src/plugins/builtin/custom-view/loader.ts +95 -34
- package/src/plugins/builtin/custom-view/pane.tsx +31 -41
- package/src/plugins/builtin/custom-view/table.ts +201 -0
- package/src/plugins/builtin/dividend-yield/client.ts +14 -17
- package/src/plugins/builtin/dividend-yield/headless.ts +0 -1
- package/src/plugins/builtin/dividend-yield/model.ts +45 -6
- package/src/plugins/builtin/dividend-yield/pane.tsx +70 -7
- package/src/plugins/builtin/dividend-yield/types.ts +2 -3
- package/src/plugins/builtin/dividend-yield/view.ts +22 -2
- package/src/plugins/builtin/econ/calendar-model.ts +135 -0
- package/src/plugins/builtin/econ/columns.ts +132 -0
- package/src/plugins/builtin/econ/index.tsx +25 -65
- package/src/plugins/builtin/holders/disclosures-view.tsx +9 -6
- package/src/plugins/builtin/holders/disclosures.ts +20 -6
- package/src/plugins/builtin/holders/headless.ts +2 -2
- package/src/plugins/builtin/ipo-calendar/model.ts +30 -1
- package/src/plugins/builtin/ipo-calendar/pane.tsx +7 -3
- package/src/plugins/builtin/kelly-sizer/headless.ts +9 -4
- package/src/plugins/builtin/kelly-sizer/pane.tsx +19 -9
- package/src/plugins/builtin/kelly-sizer/units.ts +26 -0
- package/src/plugins/builtin/market-halts/pane.tsx +14 -2
- package/src/plugins/builtin/news/wire/news/row.ts +5 -12
- package/src/plugins/builtin/news/wire/news/table.tsx +32 -41
- package/src/plugins/builtin/pe-band/headless.ts +1 -1
- package/src/plugins/builtin/pe-band/model.ts +41 -18
- package/src/plugins/builtin/pe-band/pane.tsx +6 -5
- package/src/plugins/builtin/perps/equity-row.tsx +4 -6
- package/src/plugins/builtin/perps/markets-section.tsx +19 -15
- package/src/plugins/builtin/perps/model.ts +10 -2
- package/src/plugins/builtin/portfolio-list/cli/import-positions.ts +6 -18
- package/src/plugins/builtin/portfolio-list/cli/portfolio-command.ts +1 -1
- package/src/plugins/builtin/portfolio-list/pane/purchase-date-fx.ts +1 -1
- package/src/plugins/builtin/portfolio-list/positions-import/index.tsx +1 -1
- package/src/plugins/builtin/portfolio-list/positions-import/lookup.ts +28 -57
- package/src/plugins/builtin/research/event-data.ts +3 -1
- package/src/plugins/builtin/research/event-model.ts +17 -15
- package/src/plugins/builtin/research/relative-valuation-columns.ts +19 -6
- package/src/plugins/builtin/research/relative-valuation-headless.ts +1 -1
- package/src/plugins/builtin/scanner/flow-pane.tsx +10 -0
- package/src/plugins/builtin/sec/exchange-filings.ts +32 -7
- package/src/plugins/builtin/sec/headless.ts +8 -8
- package/src/plugins/builtin/sec/index.tsx +69 -29
- package/src/plugins/builtin/shared/report-freshness.ts +4 -1
- package/src/plugins/builtin/ticker-detail/financials/aggregation.ts +1 -2
- package/src/plugins/builtin/ticker-detail/financials/exchange-table.tsx +61 -0
- package/src/plugins/builtin/ticker-detail/financials/model.ts +38 -4
- package/src/plugins/builtin/ticker-detail/financials/ratio-table.tsx +9 -2
- package/src/plugins/builtin/ticker-detail/financials/tab.tsx +68 -39
- package/src/plugins/builtin/ticker-detail/headless.ts +4 -1
- package/src/plugins/builtin/ticker-detail/overview/model.ts +61 -15
- package/src/plugins/builtin/ticker-detail/overview-tab.tsx +5 -0
- package/src/plugins/builtin/time-sales/model.ts +16 -9
- package/src/plugins/builtin/time-sales/pane.tsx +27 -24
- package/src/plugins/builtin/treasury-daily/client.ts +43 -9
- package/src/plugins/builtin/treasury-daily/pane.tsx +48 -27
- package/src/plugins/builtin/treasury-daily/resource.ts +33 -0
- package/src/portfolio/positions-import/parse.ts +14 -6
- package/src/portfolio/positions-import/plan.ts +3 -2
- package/src/react/async-resource.ts +3 -1
- package/src/renderers/dom/data-table/index.tsx +40 -0
- package/src/renderers/dom/data-table/row.tsx +39 -3
- package/src/renderers/dom/desktop/icons.tsx +147 -3
- package/src/renderers/dom/desktop/popover.tsx +2 -2
- package/src/renderers/dom/host/media-surface.tsx +204 -45
- package/src/renderers/dom/host/tabs.tsx +1 -0
- package/src/renderers/dom/styles.css +14 -2
- package/src/renderers/electrobun/bun/desktop/backend.ts +2 -0
- package/src/renderers/electrobun/bun/desktop/rpc-timing.ts +9 -3
- package/src/renderers/electrobun/shared/protocol.ts +2 -0
- package/src/renderers/electrobun/view/backend-rpc.ts +5 -2
- package/src/renderers/electrobun/view/rpc-timeout.ts +44 -3
- package/src/sources/gloomberb-cloud/index.ts +8 -7
- package/src/sources/gloomberb-cloud/normalizers.ts +7 -2
- package/src/sources/listing-symbols.ts +7 -1
- package/src/sources/provider-router/cache.ts +5 -4
- package/src/sources/provider-router/financials.ts +40 -18
- package/src/state/app/tab-cursor.ts +47 -6
- package/src/telemetry/stall-clocks.ts +3 -0
- package/src/telemetry/stall-reports.ts +12 -3
- package/src/telemetry/windows-process-activity.ts +41 -0
- package/src/telemetry/windows-time.ts +32 -0
- package/src/test-support/scoped-earnings.ts +24 -0
- package/src/tickers/terminal-codes.ts +1 -6
- package/src/time-series/daily-valuation.ts +13 -6
- package/src/time-series/filed-trailing-eps.ts +16 -3
- package/src/time-series/fundamentals/current-valuation.ts +8 -0
- package/src/time-series/fundamentals/historical-valuation.ts +20 -0
- package/src/time-series/fundamentals/series.ts +13 -1
- package/src/time-series/valuation-currency.ts +2 -0
- package/src/types/financials.ts +46 -10
- package/src/ui/dialog-context.ts +8 -0
- package/src/utils/datetime-format.ts +23 -1
- package/src/utils/declared-dividends.ts +12 -2
- package/src/utils/exchange-announcements.ts +4 -1
- package/src/utils/exchange-notation.generated.ts +3 -1
- package/src/utils/exchanges.ts +2 -1
- package/src/utils/fundamentals.ts +1 -0
- package/src/utils/main-thread-stall.ts +9 -1
- package/src/utils/rpc-timeout-error.ts +36 -11
- package/src/utils/sec.ts +3 -1
- package/src/utils/share-multiples.ts +1 -1
- package/src/utils/stall-process-activity.ts +70 -0
- package/src/utils/statement-scope.ts +103 -0
- package/src/utils/trailing-eps.ts +1 -1
- package/src/utils/zoned-date-time.ts +1 -0
|
@@ -9,6 +9,8 @@ import type {
|
|
|
9
9
|
DataTableSectionHeader,
|
|
10
10
|
} from "../../../components/ui/data-table";
|
|
11
11
|
import { tableColumnLeadGap } from "../../../components/ui/table-layout";
|
|
12
|
+
import { tableCellSpill } from "../../../components/ui/data-table/spill";
|
|
13
|
+
import { displayWidth } from "../../../utils/format";
|
|
12
14
|
import { WebIcon } from "../desktop/icons";
|
|
13
15
|
import { useFrozenColumnInsets } from "./frozen-column";
|
|
14
16
|
import { WEB_CELL_HEIGHT, WEB_CELL_WIDTH } from "../../../theme/font-scale";
|
|
@@ -219,6 +221,7 @@ function WebDataTableRowInner<
|
|
|
219
221
|
freezeFirstColumn,
|
|
220
222
|
scrollLeft = 0,
|
|
221
223
|
viewportWidth = 0,
|
|
224
|
+
cutColumns = null,
|
|
222
225
|
columnGap,
|
|
223
226
|
horizontalPadding,
|
|
224
227
|
focusPane,
|
|
@@ -246,6 +249,8 @@ function WebDataTableRowInner<
|
|
|
246
249
|
freezeFirstColumn?: boolean;
|
|
247
250
|
scrollLeft?: number;
|
|
248
251
|
viewportWidth?: number;
|
|
252
|
+
/** The columns the body's edges cut, by index; null while every column shows whole. */
|
|
253
|
+
cutColumns?: readonly boolean[] | null;
|
|
249
254
|
columnGap: number;
|
|
250
255
|
horizontalPadding: number;
|
|
251
256
|
focusPane: () => void;
|
|
@@ -367,6 +372,16 @@ function WebDataTableRowInner<
|
|
|
367
372
|
const rowBg = selected
|
|
368
373
|
? CSS_SELECTED
|
|
369
374
|
: rowBackgroundColor ?? CSS_BG;
|
|
375
|
+
const cells: DataTableCell[] = columns.map((column) => renderCell(item, column, index, rowState));
|
|
376
|
+
// A cut name reads on into the blank of the figure after it in its row (tableCellSpill), between two columns that
|
|
377
|
+
// both show whole: the frozen column, one sliding under it and one the body's edge cuts keep their cells. Its cell
|
|
378
|
+
// spans the figure's column too, its text stopping a blank before the figure, which draws over it on no fill of its own.
|
|
379
|
+
const spills = columns.map((column, columnIndex) => (
|
|
380
|
+
(freezeFirstColumn && columnIndex === 0) || insets[columnIndex] || insets[columnIndex + 1]
|
|
381
|
+
|| cutColumns?.[columnIndex] || cutColumns?.[columnIndex + 1]
|
|
382
|
+
? false : tableCellSpill(column, cells[columnIndex]!, columns[columnIndex + 1], cells[columnIndex + 1], columnGap) > 0
|
|
383
|
+
));
|
|
384
|
+
const spilled = spills.includes(true);
|
|
370
385
|
|
|
371
386
|
return (
|
|
372
387
|
<div
|
|
@@ -401,7 +416,9 @@ function WebDataTableRowInner<
|
|
|
401
416
|
}}
|
|
402
417
|
>
|
|
403
418
|
{columns.map((column, columnIndex) => {
|
|
404
|
-
const cell
|
|
419
|
+
const cell = cells[columnIndex]!;
|
|
420
|
+
const spillsOn = spills[columnIndex] === true;
|
|
421
|
+
const spilledInto = columnIndex > 0 && spills[columnIndex - 1] === true;
|
|
405
422
|
return (
|
|
406
423
|
<div
|
|
407
424
|
key={column.id}
|
|
@@ -414,16 +431,19 @@ function WebDataTableRowInner<
|
|
|
414
431
|
data-gloom-cell-may-be-blank={cell.mayBeBlank ? "true" : undefined}
|
|
415
432
|
style={{
|
|
416
433
|
minWidth: 0,
|
|
434
|
+
// A row with a spill places each cell in its own column, the spilling one across the next too.
|
|
435
|
+
...spilled ? { gridRow: 1, gridColumn: spillsOn ? `${columnIndex + 1} / span 2` : `${columnIndex + 1}` } : {},
|
|
417
436
|
position: freezeFirstColumn && columnIndex === 0 ? "sticky" : undefined,
|
|
418
437
|
left: freezeFirstColumn && columnIndex === 0 ? inlinePaddingPx(horizontalPadding) : undefined,
|
|
419
438
|
zIndex: freezeFirstColumn && columnIndex === 0 ? 1 : undefined,
|
|
420
439
|
marginLeft: leadGapCss(columns, columnIndex, columnGap),
|
|
421
440
|
paddingLeft: columnIndex > 0 ? insets[columnIndex] : undefined,
|
|
441
|
+
paddingRight: spillsOn ? spillStopCss(cells[columnIndex + 1]!) : undefined,
|
|
422
442
|
boxSizing: "border-box",
|
|
423
443
|
height: WEB_CELL_HEIGHT,
|
|
424
444
|
overflow: "hidden",
|
|
425
445
|
cursor: cell.onMouseDown ? "pointer" : undefined,
|
|
426
|
-
backgroundColor: cell.backgroundColor ?? rowBg,
|
|
446
|
+
backgroundColor: spilledInto ? "transparent" : cell.backgroundColor ?? rowBg,
|
|
427
447
|
boxShadow: freezeFirstColumn && columnIndex === 0 ? `-${inlinePaddingPx(horizontalPadding)}px 0 0 ${cell.backgroundColor ?? rowBg}, ${columnGap * WEB_CELL_WIDTH}px 0 0 ${cell.backgroundColor ?? rowBg}` : undefined,
|
|
428
448
|
}}
|
|
429
449
|
onMouseDown={(event) => {
|
|
@@ -472,6 +492,8 @@ function WebDataTableRowInner<
|
|
|
472
492
|
<CellText
|
|
473
493
|
cell={cell}
|
|
474
494
|
column={column}
|
|
495
|
+
// The figure's text alone takes the pointer, so its title is not shown over the name before it.
|
|
496
|
+
spilledInto={spilledInto}
|
|
475
497
|
color={selected
|
|
476
498
|
? selectedRowCellColor(cell.color, selectedTextOverridesCellColor, cell.keepColorWhenSelected, colors) ?? CSS_SELECTED_TEXT
|
|
477
499
|
: cell.color ?? CSS_TEXT}
|
|
@@ -484,8 +506,22 @@ function WebDataTableRowInner<
|
|
|
484
506
|
);
|
|
485
507
|
}
|
|
486
508
|
|
|
487
|
-
|
|
509
|
+
/**
|
|
510
|
+
* Where a spilling name's text stops in the cell it spans: a blank before the
|
|
511
|
+
* figure the next column draws at its right end.
|
|
512
|
+
*/
|
|
513
|
+
function spillStopCss(next: DataTableCell): CSSProperties["paddingRight"] {
|
|
514
|
+
return `calc(${displayWidth(next.text.trim()) + 1} * var(--cell-w))`;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
function CellText({ cell, column, color, spilledInto = false }: {
|
|
518
|
+
cell: DataTableCell;
|
|
519
|
+
column: DataTableColumn;
|
|
520
|
+
color: string;
|
|
521
|
+
spilledInto?: boolean;
|
|
522
|
+
}) {
|
|
488
523
|
const style = clippedCellTextStyle(column, color, cell.attributes ?? TextAttributes.NONE);
|
|
524
|
+
if (spilledInto) return <CellLabel cell={cell} column={column} style={{ ...style, width: "fit-content", marginLeft: "auto" }} />;
|
|
489
525
|
if (cell.note) {
|
|
490
526
|
const text = (
|
|
491
527
|
<div
|
|
@@ -2,8 +2,12 @@
|
|
|
2
2
|
import { releasePointerFocus } from "../host/focus-scope";
|
|
3
3
|
import { formatShortcutForHost } from "../../../app/keybindings/labels";
|
|
4
4
|
import { ariaKeyShortcut } from "./dialog-actions";
|
|
5
|
-
import
|
|
6
|
-
import
|
|
5
|
+
import { PANE_SELECTOR, placementEdges } from "./popover";
|
|
6
|
+
import { useEffect, useLayoutEffect, useRef, useState, type ReactNode, type RefObject } from "react";
|
|
7
|
+
import { createPortal } from "react-dom";
|
|
8
|
+
import { ICON_GROUP_ATTRIBUTE, type IconButtonProps, type IconName, type IconProps } from "../../../components/ui/icon";
|
|
9
|
+
import { TOOLTIP_SURFACE_STYLE } from "../../../components/ui/frame";
|
|
10
|
+
import { usePaneFocused } from "../../../state/app/activity";
|
|
7
11
|
|
|
8
12
|
/** The desktop icon set, drawn on a 12-unit grid in the current colour. */
|
|
9
13
|
const ICONS: Record<IconName, ReactNode> = {
|
|
@@ -189,6 +193,22 @@ const ICONS: Record<IconName, ReactNode> = {
|
|
|
189
193
|
<path d="M2 3.4 6 6.5l4-3.1" stroke="currentColor" strokeWidth="1.1" strokeLinecap="round" strokeLinejoin="round" />
|
|
190
194
|
</>
|
|
191
195
|
),
|
|
196
|
+
// A reply: an arrow that turns back to the left, as a mail reply's does.
|
|
197
|
+
reply: (
|
|
198
|
+
<>
|
|
199
|
+
<path d="M4.6 2.6 1.8 5.4l2.8 2.8" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round" />
|
|
200
|
+
<path d="M2.2 5.4h4.4a3.4 3.4 0 0 1 3.4 3.4v.8" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" />
|
|
201
|
+
</>
|
|
202
|
+
),
|
|
203
|
+
// A reaction: a smiling face, outlined like the other glyphs.
|
|
204
|
+
react: (
|
|
205
|
+
<>
|
|
206
|
+
<circle cx="6" cy="6" r="4.5" stroke="currentColor" strokeWidth="1.2" />
|
|
207
|
+
<circle cx="4.5" cy="5" r=".75" fill="currentColor" />
|
|
208
|
+
<circle cx="7.5" cy="5" r=".75" fill="currentColor" />
|
|
209
|
+
<path d="M4.1 7.2a2.2 2.2 0 0 0 3.8 0" stroke="currentColor" strokeWidth="1.1" strokeLinecap="round" />
|
|
210
|
+
</>
|
|
211
|
+
),
|
|
192
212
|
restore: (
|
|
193
213
|
<>
|
|
194
214
|
<rect x="2" y="4" width="6" height="6" rx="0.8" stroke="currentColor" strokeWidth="1.1" />
|
|
@@ -230,28 +250,146 @@ function pressEventFrom(
|
|
|
230
250
|
};
|
|
231
251
|
}
|
|
232
252
|
|
|
253
|
+
/** Between a focus label and the icons it names: clear of the focus ring, as a popover is of its trigger. */
|
|
254
|
+
const FOCUS_LABEL_GAP = 6;
|
|
255
|
+
|
|
256
|
+
/** How far past a list's edge a group may reach and still count as shown: a rounded pixel, never a row. */
|
|
257
|
+
const SCROLL_CLIP_SLACK = 1;
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Whether a box between the button and its pane cuts into `group` (a list the
|
|
261
|
+
* row was scrolled out of, or any other box that clips what is in it). A list
|
|
262
|
+
* keeps rows past its edges in the page, so the group still has a place
|
|
263
|
+
* there: behind whatever is beside the list, such as a chat's composer.
|
|
264
|
+
*/
|
|
265
|
+
function clippedInsidePane(button: HTMLElement, group: DOMRect): boolean {
|
|
266
|
+
const pane = button.closest(PANE_SELECTOR);
|
|
267
|
+
for (let box = button.parentElement; box && box !== pane; box = box.parentElement) {
|
|
268
|
+
const { display, overflowX, overflowY } = window.getComputedStyle(box);
|
|
269
|
+
// A box that draws nothing of its own (display: contents) clips nothing.
|
|
270
|
+
if (display === "contents" || (overflowX === "visible" && overflowY === "visible")) continue;
|
|
271
|
+
const shown = box.getBoundingClientRect();
|
|
272
|
+
if (overflowY !== "visible" && (group.top < shown.top - SCROLL_CLIP_SLACK || group.bottom > shown.bottom + SCROLL_CLIP_SLACK)) return true;
|
|
273
|
+
if (overflowX !== "visible" && (group.left < shown.left - SCROLL_CLIP_SLACK || group.right > shown.right + SCROLL_CLIP_SLACK)) return true;
|
|
274
|
+
}
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Where a focus label goes, in viewport pixels: after the button's group if
|
|
280
|
+
* it fits in the pane, else before it, else under it (over it when that
|
|
281
|
+
* leaves the pane); always clear of the group's icons. Null while the group
|
|
282
|
+
* is scrolled out of the pane, or any way out of a list inside it, when there
|
|
283
|
+
* is nothing to name: the label is on the page, where no list would clip it.
|
|
284
|
+
*/
|
|
285
|
+
function focusLabelPlace(button: HTMLElement, label: { width: number; height: number }): { left: number; top: number } | null {
|
|
286
|
+
const group = (button.closest(`[${ICON_GROUP_ATTRIBUTE}]`) ?? button).getBoundingClientRect();
|
|
287
|
+
const edges = placementEdges(button, "pane");
|
|
288
|
+
const middle = group.top + group.height / 2;
|
|
289
|
+
if (middle < edges.top || middle > edges.bottom || clippedInsidePane(button, group)) return null;
|
|
290
|
+
const beside = Math.max(edges.top, Math.min(group.top + (group.height - label.height) / 2, edges.bottom - label.height));
|
|
291
|
+
const after = group.right + FOCUS_LABEL_GAP;
|
|
292
|
+
if (after + label.width <= edges.right) return { left: after, top: beside };
|
|
293
|
+
const before = group.left - FOCUS_LABEL_GAP - label.width;
|
|
294
|
+
if (before >= edges.left) return { left: before, top: beside };
|
|
295
|
+
const left = Math.max(edges.left, Math.min(group.right - label.width, edges.right - label.width));
|
|
296
|
+
const below = group.bottom + FOCUS_LABEL_GAP;
|
|
297
|
+
return { left, top: below + label.height <= edges.bottom ? below : group.top - FOCUS_LABEL_GAP - label.height };
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* The name of a focused icon button, for the keyboard: what its tooltip says
|
|
302
|
+
* under the pointer. Portalled to the page so no row or list clips it, kept
|
|
303
|
+
* on its group wherever the group goes, hidden while a list hides the group,
|
|
304
|
+
* and never in the way of a press.
|
|
305
|
+
*/
|
|
306
|
+
function IconFocusLabel({ button, text }: { button: RefObject<HTMLButtonElement | null>; text: string }) {
|
|
307
|
+
const ref = useRef<HTMLDivElement | null>(null);
|
|
308
|
+
useLayoutEffect(() => {
|
|
309
|
+
const target = button.current;
|
|
310
|
+
const label = ref.current;
|
|
311
|
+
if (!target || !label) return;
|
|
312
|
+
let frame = 0;
|
|
313
|
+
let drawn: string | null = null;
|
|
314
|
+
// Placed each frame, for as long as the keyboard is on the icon: a row
|
|
315
|
+
// moves on a scroll, and also when a message arrives, a row above it
|
|
316
|
+
// changes height or the list is given less room, which no event reports.
|
|
317
|
+
// Written to the element itself, so the label is drawn where its group
|
|
318
|
+
// is in that same frame, and is gone in the frame a list hides the group.
|
|
319
|
+
const follow = () => {
|
|
320
|
+
const place = focusLabelPlace(target, { width: label.offsetWidth, height: label.offsetHeight });
|
|
321
|
+
const next = place ? `${place.left}px ${place.top}px` : "";
|
|
322
|
+
if (next !== drawn) {
|
|
323
|
+
drawn = next;
|
|
324
|
+
label.style.visibility = place ? "visible" : "hidden";
|
|
325
|
+
if (place) {
|
|
326
|
+
label.style.left = `${place.left}px`;
|
|
327
|
+
label.style.top = `${place.top}px`;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
frame = window.requestAnimationFrame(follow);
|
|
331
|
+
};
|
|
332
|
+
follow();
|
|
333
|
+
return () => window.cancelAnimationFrame(frame);
|
|
334
|
+
}, [button, text]);
|
|
335
|
+
return createPortal(
|
|
336
|
+
<div
|
|
337
|
+
ref={ref}
|
|
338
|
+
aria-hidden="true"
|
|
339
|
+
data-gloom-role="icon-button-focus-label"
|
|
340
|
+
className="gloom-icon-focus-label"
|
|
341
|
+
style={{
|
|
342
|
+
...TOOLTIP_SURFACE_STYLE,
|
|
343
|
+
// One line high, as the icons are, so it stays on their row.
|
|
344
|
+
padding: "0 var(--gloom-space-sm)",
|
|
345
|
+
// Hidden until it is placed; its place is then kept on the element (see the effect above).
|
|
346
|
+
left: 0,
|
|
347
|
+
top: 0,
|
|
348
|
+
visibility: "hidden",
|
|
349
|
+
}}
|
|
350
|
+
>
|
|
351
|
+
{text}
|
|
352
|
+
</div>,
|
|
353
|
+
document.body,
|
|
354
|
+
);
|
|
355
|
+
}
|
|
356
|
+
|
|
233
357
|
export function WebIconButton({
|
|
234
358
|
icon,
|
|
235
359
|
label,
|
|
236
360
|
title,
|
|
237
361
|
shortcut,
|
|
238
362
|
hasPopup,
|
|
363
|
+
expanded,
|
|
239
364
|
onPress,
|
|
240
365
|
pressed,
|
|
241
366
|
color,
|
|
242
367
|
disabled = false,
|
|
243
368
|
size = 12,
|
|
244
369
|
stopPropagation = true,
|
|
370
|
+
onHoverChange,
|
|
371
|
+
focusLabel = false,
|
|
245
372
|
}: IconButtonProps) {
|
|
373
|
+
const ref = useRef<HTMLButtonElement | null>(null);
|
|
374
|
+
const [keyboardFocused, setKeyboardFocused] = useState(false);
|
|
375
|
+
const name = title ?? (shortcut ? `${label} (${formatShortcutForHost(shortcut, "platform")})` : label);
|
|
376
|
+
const paneFocused = usePaneFocused();
|
|
377
|
+
// Tab and Shift-Tab move the app's focus to another pane: the icon lets go
|
|
378
|
+
// of the page's focus too, so neither its label nor Enter stays behind here.
|
|
379
|
+
useEffect(() => {
|
|
380
|
+
if (focusLabel && keyboardFocused && paneFocused === false) ref.current?.blur();
|
|
381
|
+
}, [focusLabel, keyboardFocused, paneFocused]);
|
|
246
382
|
return (
|
|
247
383
|
<button
|
|
384
|
+
ref={ref}
|
|
248
385
|
type="button"
|
|
249
386
|
className="gloom-icon-button"
|
|
250
387
|
aria-label={label}
|
|
251
388
|
aria-pressed={pressed}
|
|
252
389
|
aria-keyshortcuts={ariaKeyShortcut(shortcut)}
|
|
253
390
|
aria-haspopup={hasPopup}
|
|
254
|
-
|
|
391
|
+
aria-expanded={expanded}
|
|
392
|
+
title={name}
|
|
255
393
|
disabled={disabled || !onPress}
|
|
256
394
|
data-gloom-interactive={onPress && !disabled ? "true" : undefined}
|
|
257
395
|
data-gloom-role="icon-button"
|
|
@@ -259,6 +397,11 @@ export function WebIconButton({
|
|
|
259
397
|
style={color ? { color } : undefined}
|
|
260
398
|
onMouseDown={(event) => { if (stopPropagation) event.stopPropagation(); }}
|
|
261
399
|
onMouseUp={(event) => { if (stopPropagation) event.stopPropagation(); }}
|
|
400
|
+
onMouseEnter={onHoverChange ? () => onHoverChange(true) : undefined}
|
|
401
|
+
onMouseLeave={onHoverChange ? () => onHoverChange(false) : undefined}
|
|
402
|
+
// Focus from a press draws no ring and names nothing; the keyboard's does both.
|
|
403
|
+
onFocus={focusLabel ? (event) => setKeyboardFocused(event.currentTarget.matches(":focus-visible")) : undefined}
|
|
404
|
+
onBlur={focusLabel ? () => setKeyboardFocused(false) : undefined}
|
|
262
405
|
onClick={(event) => {
|
|
263
406
|
if (stopPropagation) event.stopPropagation();
|
|
264
407
|
if (disabled) return;
|
|
@@ -278,6 +421,7 @@ export function WebIconButton({
|
|
|
278
421
|
}}
|
|
279
422
|
>
|
|
280
423
|
<WebIcon name={icon} size={size} />
|
|
424
|
+
{focusLabel && keyboardFocused ? <IconFocusLabel button={ref} text={name} /> : null}
|
|
281
425
|
</button>
|
|
282
426
|
);
|
|
283
427
|
}
|
|
@@ -9,7 +9,7 @@ const VIEWPORT_MARGIN = 10;
|
|
|
9
9
|
/** How far a popover kept inside its pane stays off the pane's edge. */
|
|
10
10
|
const PANE_MARGIN = 4;
|
|
11
11
|
const POPOVER_GAP = 6;
|
|
12
|
-
const PANE_SELECTOR = '[data-gloom-role="pane-window"], [data-gloom-role="detached-pane-window"]';
|
|
12
|
+
export const PANE_SELECTOR = '[data-gloom-role="pane-window"], [data-gloom-role="detached-pane-window"]';
|
|
13
13
|
/** `.gloom-popover` in styles.css: above dialogs and toasts. */
|
|
14
14
|
const POPOVER_Z_INDEX = 10_001;
|
|
15
15
|
|
|
@@ -43,7 +43,7 @@ function anchorEdges(anchor: BoxRenderable): Edges | null {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
/** The area a popover must stay in: the window, and for `pane` the pane that holds its trigger too. */
|
|
46
|
-
function placementEdges(wrapper: Element | null, boundary: HostPopoverProps["boundary"]): Edges {
|
|
46
|
+
export function placementEdges(wrapper: Element | null, boundary: HostPopoverProps["boundary"]): Edges {
|
|
47
47
|
const viewport = {
|
|
48
48
|
left: VIEWPORT_MARGIN,
|
|
49
49
|
top: VIEWPORT_MARGIN,
|
|
@@ -4,8 +4,24 @@ import { forwardRef, useEffect, useImperativeHandle, useRef, useState, type CSSP
|
|
|
4
4
|
import type { MediaSurfaceHandle, MediaSurfaceProps } from "../../../ui/host";
|
|
5
5
|
import { cleanDomProps, commonStyle } from "./style";
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
type PlaybackState = Parameters<NonNullable<MediaSurfaceProps["onPlaybackStateChange"]>>[0];
|
|
8
|
+
const YOUTUBE_ATTEMPT_TIMEOUT_MS = 15_000;
|
|
9
|
+
const YOUTUBE_WIDGET_ID = "gloom-media";
|
|
10
|
+
const YOUTUBE_EVENTS = ["onStateChange", "onError", "onAutoplayBlocked"];
|
|
11
|
+
const YOUTUBE_UNAVAILABLE = "The embedded player could not play this stream.";
|
|
12
|
+
|
|
13
|
+
type YoutubeControls = { play(): void; pause(): void; mute(): void };
|
|
14
|
+
|
|
15
|
+
function youtubePlaybackState(value: unknown): PlaybackState | "ended" | undefined {
|
|
16
|
+
switch (value) {
|
|
17
|
+
case 0: return "ended";
|
|
18
|
+
case 1: return "playing";
|
|
19
|
+
case 2: return "paused";
|
|
20
|
+
case 3: return "loading";
|
|
21
|
+
case -1:
|
|
22
|
+
case 5: return "idle";
|
|
23
|
+
default: return undefined;
|
|
24
|
+
}
|
|
9
25
|
}
|
|
10
26
|
|
|
11
27
|
export const WebMediaSurface = forwardRef<HTMLVideoElement, MediaSurfaceProps>(function WebMediaSurface(rawProps, forwardedRef) {
|
|
@@ -29,50 +45,55 @@ export const WebMediaSurface = forwardRef<HTMLVideoElement, MediaSurfaceProps>(f
|
|
|
29
45
|
autoPlay?: boolean;
|
|
30
46
|
muted?: boolean;
|
|
31
47
|
mediaHandleRef?: MediaSurfaceProps["mediaHandleRef"];
|
|
32
|
-
onPlaybackStateChange?: (state:
|
|
48
|
+
onPlaybackStateChange?: (state: PlaybackState) => void;
|
|
33
49
|
onMutedChange?: (muted: boolean) => void;
|
|
34
50
|
onError?: (message: string) => void;
|
|
35
51
|
};
|
|
36
52
|
const videoRef = useRef<HTMLVideoElement | null>(null);
|
|
37
53
|
const iframeRef = useRef<HTMLIFrameElement | null>(null);
|
|
38
54
|
const youtubeMutedRef = useRef(muted);
|
|
39
|
-
const youtubePlayingRef = useRef(
|
|
40
|
-
const
|
|
55
|
+
const youtubePlayingRef = useRef(false);
|
|
56
|
+
const youtubeControls = useRef<YoutubeControls | null>(null);
|
|
57
|
+
const retryYoutubeSrc = useRef<string | null>(null);
|
|
58
|
+
const callbacks = useRef({ onPlaybackStateChange, onError });
|
|
59
|
+
callbacks.current = { onPlaybackStateChange, onError };
|
|
60
|
+
const [failedSrc, setFailedSrc] = useState<string | null>(null);
|
|
41
61
|
const mediaSrc = typeof src === "string" ? src.trim() : "";
|
|
42
|
-
const
|
|
62
|
+
const failed = failedSrc === mediaSrc;
|
|
63
|
+
const embedUrl = /^https?:\/\/(?:www\.)?youtube(?:-nocookie)?\.com\/embed\//.test(mediaSrc) ? new URL(mediaSrc) : null;
|
|
64
|
+
if (embedUrl) {
|
|
65
|
+
embedUrl.protocol = "https:";
|
|
66
|
+
if (embedUrl.searchParams.get("enablejsapi") !== "1") embedUrl.searchParams.set("enablejsapi", "1");
|
|
67
|
+
}
|
|
68
|
+
const youtubeOrigin = embedUrl?.origin;
|
|
43
69
|
const baseStyle = commonStyle(props);
|
|
44
70
|
|
|
45
71
|
useImperativeHandle(forwardedRef, () => videoRef.current as HTMLVideoElement, []);
|
|
46
72
|
useImperativeHandle(mediaHandleRef, (): MediaSurfaceHandle => ({
|
|
47
73
|
async play() {
|
|
48
|
-
if (
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
74
|
+
if (youtubeOrigin) {
|
|
75
|
+
if (failed) {
|
|
76
|
+
retryYoutubeSrc.current = mediaSrc;
|
|
77
|
+
setFailedSrc(null);
|
|
78
|
+
} else youtubeControls.current?.play();
|
|
52
79
|
return;
|
|
53
80
|
}
|
|
54
81
|
if (videoRef.current) await videoRef.current.play();
|
|
55
82
|
},
|
|
56
83
|
pause() {
|
|
57
|
-
if (
|
|
58
|
-
|
|
59
|
-
youtubePlayingRef.current = false;
|
|
60
|
-
onPlaybackStateChange?.("paused");
|
|
84
|
+
if (youtubeOrigin) {
|
|
85
|
+
youtubeControls.current?.pause();
|
|
61
86
|
return;
|
|
62
87
|
}
|
|
63
88
|
videoRef.current?.pause();
|
|
64
89
|
},
|
|
65
90
|
async toggle() {
|
|
66
|
-
if (
|
|
67
|
-
if (
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
postYoutubeCommand(iframeRef.current, "playVideo");
|
|
73
|
-
youtubePlayingRef.current = true;
|
|
74
|
-
onPlaybackStateChange?.("playing");
|
|
75
|
-
}
|
|
91
|
+
if (youtubeOrigin) {
|
|
92
|
+
if (failed) {
|
|
93
|
+
retryYoutubeSrc.current = mediaSrc;
|
|
94
|
+
setFailedSrc(null);
|
|
95
|
+
} else if (youtubePlayingRef.current) youtubeControls.current?.pause();
|
|
96
|
+
else youtubeControls.current?.play();
|
|
76
97
|
return;
|
|
77
98
|
}
|
|
78
99
|
const video = videoRef.current;
|
|
@@ -84,9 +105,9 @@ export const WebMediaSurface = forwardRef<HTMLVideoElement, MediaSurfaceProps>(f
|
|
|
84
105
|
}
|
|
85
106
|
},
|
|
86
107
|
toggleMuted() {
|
|
87
|
-
if (
|
|
108
|
+
if (youtubeOrigin) {
|
|
88
109
|
youtubeMutedRef.current = !youtubeMutedRef.current;
|
|
89
|
-
|
|
110
|
+
youtubeControls.current?.mute();
|
|
90
111
|
onMutedChange?.(youtubeMutedRef.current);
|
|
91
112
|
return youtubeMutedRef.current;
|
|
92
113
|
}
|
|
@@ -95,13 +116,13 @@ export const WebMediaSurface = forwardRef<HTMLVideoElement, MediaSurfaceProps>(f
|
|
|
95
116
|
video.muted = !video.muted;
|
|
96
117
|
return video.muted;
|
|
97
118
|
},
|
|
98
|
-
}), [muted, onMutedChange,
|
|
119
|
+
}), [failed, mediaSrc, muted, onMutedChange, youtubeOrigin]);
|
|
99
120
|
|
|
100
121
|
// Keys typed into a cross-origin frame never reach the app, so a click on
|
|
101
122
|
// the player would take Tab, the command bar and every pane key with it.
|
|
102
123
|
// Its controls work by pointer without focus; hand the keyboard back.
|
|
103
124
|
useEffect(() => {
|
|
104
|
-
if (!
|
|
125
|
+
if (!youtubeOrigin) return;
|
|
105
126
|
let timer: ReturnType<typeof setTimeout> | null = null;
|
|
106
127
|
const releaseFrameFocus = () => {
|
|
107
128
|
timer = null;
|
|
@@ -121,30 +142,172 @@ export const WebMediaSurface = forwardRef<HTMLVideoElement, MediaSurfaceProps>(f
|
|
|
121
142
|
window.removeEventListener("blur", handleWindowBlur);
|
|
122
143
|
if (timer !== null) clearTimeout(timer);
|
|
123
144
|
};
|
|
124
|
-
}, [
|
|
145
|
+
}, [youtubeOrigin]);
|
|
125
146
|
|
|
126
147
|
useEffect(() => {
|
|
127
148
|
youtubeMutedRef.current = muted;
|
|
128
|
-
if (
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
149
|
+
if (youtubeOrigin) onMutedChange?.(muted);
|
|
150
|
+
}, [mediaSrc, muted, onMutedChange, youtubeOrigin]);
|
|
151
|
+
|
|
152
|
+
useEffect(() => setFailedSrc(null), [mediaSrc]);
|
|
153
|
+
|
|
154
|
+
useEffect(() => {
|
|
155
|
+
const frame = iframeRef.current;
|
|
156
|
+
if (!youtubeOrigin || failed || !frame) return;
|
|
157
|
+
let last: PlaybackState | undefined;
|
|
158
|
+
let ended = false;
|
|
159
|
+
let connected = false;
|
|
160
|
+
let hasStateTransition = false;
|
|
161
|
+
let deadline: ReturnType<typeof setTimeout> | undefined;
|
|
162
|
+
let queuedPlayback: "playVideo" | "pauseVideo" | undefined;
|
|
163
|
+
let queuedMute = false;
|
|
164
|
+
let pauseRequested = false;
|
|
165
|
+
const active = () => !ended && iframeRef.current === frame;
|
|
166
|
+
const post = (payload: object) => {
|
|
167
|
+
if (active()) frame.contentWindow?.postMessage(JSON.stringify({ ...payload, id: YOUTUBE_WIDGET_ID, channel: "widget" }), youtubeOrigin);
|
|
168
|
+
};
|
|
169
|
+
const command = (func: string, args: string[] = []) => post({ event: "command", func, args });
|
|
170
|
+
const update = (state: PlaybackState) => {
|
|
171
|
+
if (state === last) return;
|
|
172
|
+
last = state;
|
|
173
|
+
youtubePlayingRef.current = state === "playing";
|
|
174
|
+
callbacks.current.onPlaybackStateChange?.(state);
|
|
175
|
+
};
|
|
176
|
+
const stopAttempt = () => {
|
|
177
|
+
clearTimeout(deadline);
|
|
178
|
+
deadline = undefined;
|
|
179
|
+
};
|
|
180
|
+
const stopHandshake = () => {
|
|
181
|
+
clearInterval(listenAgain);
|
|
182
|
+
clearTimeout(handshakeDeadline);
|
|
183
|
+
};
|
|
184
|
+
const fail = () => {
|
|
185
|
+
if (!active()) return;
|
|
186
|
+
ended = true;
|
|
187
|
+
stopHandshake();
|
|
188
|
+
stopAttempt();
|
|
189
|
+
setFailedSrc(mediaSrc);
|
|
190
|
+
update("error");
|
|
191
|
+
callbacks.current.onError?.(YOUTUBE_UNAVAILABLE);
|
|
192
|
+
};
|
|
193
|
+
const startAttempt = () => {
|
|
194
|
+
if (!active() || deadline !== undefined) return;
|
|
195
|
+
const timer = setTimeout(() => { if (deadline === timer) fail(); }, YOUTUBE_ATTEMPT_TIMEOUT_MS);
|
|
196
|
+
deadline = timer;
|
|
197
|
+
update("loading");
|
|
198
|
+
};
|
|
199
|
+
const setPlayback = (func: "playVideo" | "pauseVideo") => {
|
|
200
|
+
if (connected) command(func);
|
|
201
|
+
else queuedPlayback = func;
|
|
202
|
+
};
|
|
203
|
+
const setMute = () => command(youtubeMutedRef.current ? "mute" : "unMute");
|
|
204
|
+
const controls: YoutubeControls = {
|
|
205
|
+
play() {
|
|
206
|
+
if (pauseRequested || last !== "playing") startAttempt();
|
|
207
|
+
pauseRequested = false;
|
|
208
|
+
setPlayback("playVideo");
|
|
209
|
+
},
|
|
210
|
+
pause() {
|
|
211
|
+
pauseRequested = last === "loading" || last === "playing";
|
|
212
|
+
stopAttempt();
|
|
213
|
+
setPlayback("pauseVideo");
|
|
214
|
+
},
|
|
215
|
+
mute() {
|
|
216
|
+
if (connected) setMute();
|
|
217
|
+
else queuedMute = true;
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
const connect = () => {
|
|
221
|
+
if (connected) return;
|
|
222
|
+
connected = true;
|
|
223
|
+
stopHandshake();
|
|
224
|
+
for (const kind of YOUTUBE_EVENTS) command("addEventListener", [kind]);
|
|
225
|
+
if (queuedMute) setMute();
|
|
226
|
+
if (queuedPlayback) command(queuedPlayback);
|
|
227
|
+
if (deadline === undefined && last === "loading") update("idle");
|
|
228
|
+
};
|
|
229
|
+
const message = (event: MessageEvent) => {
|
|
230
|
+
const source = frame.contentWindow;
|
|
231
|
+
if (!active() || !source || event.source !== source || event.origin !== youtubeOrigin || typeof event.data !== "string") return;
|
|
232
|
+
let payload: { event?: unknown; id?: unknown; info?: unknown } | null;
|
|
233
|
+
try {
|
|
234
|
+
payload = JSON.parse(event.data);
|
|
235
|
+
} catch {
|
|
236
|
+
// Intentional ignore: malformed player messages are not playback events.
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
if (!payload || typeof payload !== "object" || payload.id !== YOUTUBE_WIDGET_ID) return;
|
|
240
|
+
const { event: kind, info } = payload;
|
|
241
|
+
const snapshot = info && typeof info === "object" ? info : undefined;
|
|
242
|
+
if (kind === "onReady" || kind === "alreadyInitialized" || (kind === "initialDelivery" && snapshot)) connect();
|
|
243
|
+
if (kind === "onError" && typeof info === "number" && Number.isInteger(info) && info > 0) {
|
|
244
|
+
fail();
|
|
245
|
+
} else if (kind === "onAutoplayBlocked") {
|
|
246
|
+
pauseRequested = false;
|
|
247
|
+
stopAttempt();
|
|
248
|
+
update("idle");
|
|
249
|
+
} else {
|
|
250
|
+
const isTransition = kind === "onStateChange";
|
|
251
|
+
if (hasStateTransition && !isTransition) return;
|
|
252
|
+
const state = isTransition ? youtubePlaybackState(info)
|
|
253
|
+
: (kind === "initialDelivery" || kind === "infoDelivery") && snapshot && "playerState" in snapshot
|
|
254
|
+
? youtubePlaybackState(snapshot.playerState) : undefined;
|
|
255
|
+
if (state === undefined) return;
|
|
256
|
+
if (isTransition) hasStateTransition = true;
|
|
257
|
+
if (state === "ended") {
|
|
258
|
+
if (deadline !== undefined && !isTransition) return;
|
|
259
|
+
pauseRequested = false;
|
|
260
|
+
stopAttempt();
|
|
261
|
+
update("idle");
|
|
262
|
+
} else if (state === "playing") {
|
|
263
|
+
if (pauseRequested) return;
|
|
264
|
+
stopAttempt();
|
|
265
|
+
update(state);
|
|
266
|
+
} else if (state === "loading") {
|
|
267
|
+
if (!pauseRequested && (isTransition || last === "playing")) startAttempt();
|
|
268
|
+
} else if (state === "paused" && (isTransition || pauseRequested)) {
|
|
269
|
+
pauseRequested = false;
|
|
270
|
+
stopAttempt();
|
|
271
|
+
update(state);
|
|
272
|
+
} else if (connected && deadline === undefined) {
|
|
273
|
+
update(state);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
const listen = () => post({ event: "listening" });
|
|
278
|
+
const listenAgain = setInterval(listen, 250);
|
|
279
|
+
const handshakeDeadline = setTimeout(() => { if (!connected) fail(); }, YOUTUBE_ATTEMPT_TIMEOUT_MS);
|
|
280
|
+
window.addEventListener("message", message);
|
|
281
|
+
youtubeControls.current = controls;
|
|
282
|
+
update("loading");
|
|
283
|
+
if (autoPlay) startAttempt();
|
|
284
|
+
if (retryYoutubeSrc.current === mediaSrc) controls.play();
|
|
285
|
+
retryYoutubeSrc.current = null;
|
|
286
|
+
listen();
|
|
287
|
+
return () => {
|
|
288
|
+
ended = true;
|
|
289
|
+
stopHandshake();
|
|
290
|
+
stopAttempt();
|
|
291
|
+
youtubeControls.current = null;
|
|
292
|
+
window.removeEventListener("message", message);
|
|
293
|
+
};
|
|
294
|
+
}, [autoPlay, failed, mediaSrc, youtubeOrigin]);
|
|
295
|
+
|
|
296
|
+
useEffect(() => {
|
|
297
|
+
if (youtubeOrigin) return;
|
|
134
298
|
const video = videoRef.current;
|
|
135
299
|
if (!video || !mediaSrc) {
|
|
136
|
-
onPlaybackStateChange?.("idle");
|
|
300
|
+
if (!mediaSrc) onPlaybackStateChange?.("idle");
|
|
137
301
|
return;
|
|
138
302
|
}
|
|
139
303
|
|
|
140
|
-
setFailed(false);
|
|
141
304
|
onPlaybackStateChange?.("loading");
|
|
142
305
|
let hls: Hls | null = null;
|
|
143
306
|
|
|
144
307
|
let active = true;
|
|
145
308
|
const fail = (message: string) => {
|
|
146
309
|
if (!active) return;
|
|
147
|
-
|
|
310
|
+
setFailedSrc(mediaSrc);
|
|
148
311
|
onPlaybackStateChange?.("error");
|
|
149
312
|
onError?.(message);
|
|
150
313
|
};
|
|
@@ -211,7 +374,7 @@ export const WebMediaSurface = forwardRef<HTMLVideoElement, MediaSurfaceProps>(f
|
|
|
211
374
|
video.removeAttribute("src");
|
|
212
375
|
video.load();
|
|
213
376
|
};
|
|
214
|
-
}, [autoPlay, mediaSrc, onError, onMutedChange, onPlaybackStateChange,
|
|
377
|
+
}, [autoPlay, mediaSrc, onError, onMutedChange, onPlaybackStateChange, youtubeOrigin]);
|
|
215
378
|
|
|
216
379
|
return (
|
|
217
380
|
<div
|
|
@@ -224,19 +387,15 @@ export const WebMediaSurface = forwardRef<HTMLVideoElement, MediaSurfaceProps>(f
|
|
|
224
387
|
...(props.style as CSSProperties | undefined),
|
|
225
388
|
}}
|
|
226
389
|
>
|
|
227
|
-
{
|
|
390
|
+
{!failed && embedUrl ? (
|
|
228
391
|
<iframe
|
|
229
392
|
key={mediaSrc}
|
|
230
393
|
ref={iframeRef}
|
|
231
394
|
title={title || "Live TV stream"}
|
|
232
|
-
src={
|
|
395
|
+
src={embedUrl.href}
|
|
233
396
|
referrerPolicy="strict-origin-when-cross-origin"
|
|
234
397
|
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
|
|
235
398
|
allowFullScreen
|
|
236
|
-
onLoad={() => {
|
|
237
|
-
iframeRef.current?.contentWindow?.postMessage(JSON.stringify({ event: "listening", id: 1 }), "*");
|
|
238
|
-
onPlaybackStateChange?.(autoPlay ? "playing" : "paused");
|
|
239
|
-
}}
|
|
240
399
|
style={{
|
|
241
400
|
width: "100%",
|
|
242
401
|
height: "100%",
|