@marimo-team/islands 0.26.1-dev94 → 0.26.1-dev99
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/dist/{ErrorBoundary-CAWisXTb.js → ErrorBoundary-C47z1UQO.js} +1 -1
- package/dist/{Plot-BHJLG4xm.js → Plot-gpsi2L3q.js} +76911 -88182
- package/dist/{code-visibility-ChWWAau1.js → code-visibility-CXefmoOL.js} +1 -1
- package/dist/{common-DRTRM583.js → common-DH4LyqvZ.js} +1 -1
- package/dist/{data-grid-overlay-editor-DHMjranC.js → data-grid-overlay-editor-BNw-ZqCP.js} +1 -1
- package/dist/{feedback-button-Bmx2JgkP.js → feedback-button-qQZs78cF.js} +2 -2
- package/dist/{glide-data-editor-DiX6nUKA.js → glide-data-editor-CGlZ-Lfh.js} +3 -3
- package/dist/{globals-CdAZrKjH.js → globals-DUk31nDw.js} +1 -1
- package/dist/main.js +1333 -1252
- package/dist/{number-overlay-editor-B0VveyRC.js → number-overlay-editor-Cr08x1Kh.js} +1 -1
- package/dist/{reveal-component-BWEJhJsP.js → reveal-component-DWIjRvac.js} +2 -2
- package/dist/style.css +1 -1
- package/package.json +2 -3
- package/src/plugins/impl/plotly/PlotlyPlugin.tsx +11 -4
- package/src/plugins/impl/plotly/__tests__/PlotlyPlugin.test.tsx +51 -1
- package/src/plugins/impl/plotly/__tests__/legacy-mapbox.test.ts +132 -0
- package/src/plugins/impl/plotly/__tests__/selection.test.ts +14 -35
- package/src/plugins/impl/plotly/legacy-mapbox.ts +130 -0
- package/src/plugins/impl/plotly/maplibre.css +216 -0
- package/src/plugins/impl/plotly/mapbox.css +0 -359
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@marimo-team/islands",
|
|
3
|
-
"version": "0.26.1-
|
|
3
|
+
"version": "0.26.1-dev99",
|
|
4
4
|
"main": "dist/main.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -116,7 +116,7 @@
|
|
|
116
116
|
"marked": "^15.0.12",
|
|
117
117
|
"mermaid": "^11.17.2",
|
|
118
118
|
"path-to-regexp": "^8.4.2",
|
|
119
|
-
"plotly.js": "^3.
|
|
119
|
+
"plotly.js": "^4.3.0",
|
|
120
120
|
"pyodide": "314.0.0",
|
|
121
121
|
"radix-ui": "1.8.1",
|
|
122
122
|
"react-arborist": "^3.16.0",
|
|
@@ -201,7 +201,6 @@
|
|
|
201
201
|
"@types/dompurify": "^3.2.0",
|
|
202
202
|
"@types/lodash-es": "^4.17.12",
|
|
203
203
|
"@types/node": "^24.19.2",
|
|
204
|
-
"@types/plotly.js": "^2.35.18",
|
|
205
204
|
"@types/react": "^19.3.0",
|
|
206
205
|
"@types/react-dom": "^19.3.0",
|
|
207
206
|
"@types/timestring": "^6.0.5",
|
|
@@ -4,10 +4,11 @@ import type * as Plotly from "plotly.js";
|
|
|
4
4
|
import { z } from "zod";
|
|
5
5
|
import type { IPlugin, IPluginProps, Setter } from "@/plugins/types";
|
|
6
6
|
import { Logger } from "@/utils/Logger";
|
|
7
|
+
import { migrateLegacyMapbox } from "./legacy-mapbox";
|
|
7
8
|
import type { Figure } from "./Plot";
|
|
8
9
|
|
|
9
10
|
import "./plotly.css";
|
|
10
|
-
import "./
|
|
11
|
+
import "./maplibre.css";
|
|
11
12
|
import { set } from "lodash-es";
|
|
12
13
|
import { type JSX, lazy, memo, useMemo } from "react";
|
|
13
14
|
import useEvent from "react-use-event-hook";
|
|
@@ -47,7 +48,7 @@ type T =
|
|
|
47
48
|
};
|
|
48
49
|
// These are kept in the state to persist selections across re-renders
|
|
49
50
|
// on the frontend, but likely not used in the backend.
|
|
50
|
-
selections?:
|
|
51
|
+
selections?: Plotly.Layout["selections"];
|
|
51
52
|
dragmode?: Plotly.Layout["dragmode"];
|
|
52
53
|
xaxis?: Partial<Plotly.LayoutAxis>;
|
|
53
54
|
yaxis?: Partial<Plotly.LayoutAxis>;
|
|
@@ -58,7 +59,9 @@ export class PlotlyPlugin implements IPlugin<T, Data> {
|
|
|
58
59
|
public tagName = "marimo-plotly";
|
|
59
60
|
|
|
60
61
|
public validator = z.object({
|
|
61
|
-
figure: z
|
|
62
|
+
figure: z
|
|
63
|
+
.looseObject({})
|
|
64
|
+
.transform((spec) => migrateLegacyMapbox(spec) as unknown as Figure),
|
|
62
65
|
config: z.looseObject({}),
|
|
63
66
|
});
|
|
64
67
|
|
|
@@ -135,6 +138,8 @@ export const PlotlyComponent = memo(
|
|
|
135
138
|
|
|
136
139
|
return {
|
|
137
140
|
displaylogo: false,
|
|
141
|
+
// Disable "Share chart" which uploads the figure's data to cloud.plotly.com
|
|
142
|
+
showSendToCloud: false,
|
|
138
143
|
// Prioritize user's config
|
|
139
144
|
...configMemo,
|
|
140
145
|
modeBarButtonsToAdd: mergeModeBarButtonsToAdd(
|
|
@@ -232,7 +237,9 @@ export const PlotlyComponent = memo(
|
|
|
232
237
|
setValue((prev) => ({
|
|
233
238
|
...prev,
|
|
234
239
|
selections:
|
|
235
|
-
"selections" in evt
|
|
240
|
+
"selections" in evt
|
|
241
|
+
? (evt.selections as Plotly.Layout["selections"])
|
|
242
|
+
: [],
|
|
236
243
|
points: extractPoints(evt.points),
|
|
237
244
|
indices: extractIndices(evt.points),
|
|
238
245
|
range: evt.range,
|
|
@@ -5,11 +5,12 @@ import { Suspense } from "react";
|
|
|
5
5
|
import { describe, expect, it, vi } from "vitest";
|
|
6
6
|
import { SetupMocks } from "@/__mocks__/common";
|
|
7
7
|
import type { Setter } from "@/plugins/types";
|
|
8
|
-
import { PlotlyComponent } from "../PlotlyPlugin";
|
|
8
|
+
import { PlotlyComponent, PlotlyPlugin } from "../PlotlyPlugin";
|
|
9
9
|
|
|
10
10
|
SetupMocks.resizeObserver();
|
|
11
11
|
|
|
12
12
|
type CapturedPlotProps = {
|
|
13
|
+
config?: Record<string, unknown>;
|
|
13
14
|
onClick?: (event: {
|
|
14
15
|
points: {
|
|
15
16
|
data?: { type?: string };
|
|
@@ -55,7 +56,56 @@ vi.mock("react-use-event-hook", () => ({
|
|
|
55
56
|
default: <T,>(callback: T) => callback,
|
|
56
57
|
}));
|
|
57
58
|
|
|
59
|
+
function renderPlotly(config: Record<string, unknown>) {
|
|
60
|
+
capturedPlotProps = null;
|
|
61
|
+
render(
|
|
62
|
+
<Suspense fallback={null}>
|
|
63
|
+
<PlotlyComponent
|
|
64
|
+
figure={{ data: [{ type: "scatter" }], layout: {}, frames: null }}
|
|
65
|
+
value={undefined}
|
|
66
|
+
setValue={vi.fn()}
|
|
67
|
+
host={document.createElement("div")}
|
|
68
|
+
config={config}
|
|
69
|
+
/>
|
|
70
|
+
</Suspense>,
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
58
74
|
describe("PlotlyPlugin", () => {
|
|
75
|
+
it("migrates legacy mapbox figures when validating the plugin data", () => {
|
|
76
|
+
const { figure } = new PlotlyPlugin().validator.parse({
|
|
77
|
+
figure: {
|
|
78
|
+
data: [{ type: "scattermapbox", lat: [1], lon: [2] }],
|
|
79
|
+
layout: { mapbox: { style: "open-street-map" } },
|
|
80
|
+
},
|
|
81
|
+
config: {},
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
expect(figure.data).toEqual([{ type: "scattermap", lat: [1], lon: [2] }]);
|
|
85
|
+
expect(figure.layout).toEqual({ map: { style: "open-street-map" } });
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("does not offer to upload the figure to plotly cloud by default", async () => {
|
|
89
|
+
renderPlotly({});
|
|
90
|
+
|
|
91
|
+
await waitFor(() => {
|
|
92
|
+
expect(capturedPlotProps).not.toBeNull();
|
|
93
|
+
});
|
|
94
|
+
expect(capturedPlotProps?.config).toMatchObject({
|
|
95
|
+
displaylogo: false,
|
|
96
|
+
showSendToCloud: false,
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("lets the user config opt back in to plotly cloud sharing", async () => {
|
|
101
|
+
renderPlotly({ showSendToCloud: true });
|
|
102
|
+
|
|
103
|
+
await waitFor(() => {
|
|
104
|
+
expect(capturedPlotProps).not.toBeNull();
|
|
105
|
+
});
|
|
106
|
+
expect(capturedPlotProps?.config).toMatchObject({ showSendToCloud: true });
|
|
107
|
+
});
|
|
108
|
+
|
|
59
109
|
it("clicking a bar selects that bar", async () => {
|
|
60
110
|
const setValue = vi.fn<Setter<unknown>>();
|
|
61
111
|
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
import { migrateLegacyMapbox } from "../legacy-mapbox";
|
|
5
|
+
|
|
6
|
+
describe("migrateLegacyMapbox", () => {
|
|
7
|
+
it.each([
|
|
8
|
+
["scattermapbox", "scattermap"],
|
|
9
|
+
["choroplethmapbox", "choroplethmap"],
|
|
10
|
+
["densitymapbox", "densitymap"],
|
|
11
|
+
])("renames %s traces to %s", (legacy, modern) => {
|
|
12
|
+
const figure = migrateLegacyMapbox({
|
|
13
|
+
data: [{ type: legacy, lat: [1], lon: [2] }],
|
|
14
|
+
layout: {},
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
expect(figure.data).toEqual([{ type: modern, lat: [1], lon: [2] }]);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("moves the mapbox layout and trace subplots to map, dropping the access token", () => {
|
|
21
|
+
const figure = migrateLegacyMapbox({
|
|
22
|
+
data: [
|
|
23
|
+
{ type: "scattermapbox", subplot: "mapbox" },
|
|
24
|
+
{ type: "scattermapbox", subplot: "mapbox2" },
|
|
25
|
+
],
|
|
26
|
+
layout: {
|
|
27
|
+
title: { text: "t" },
|
|
28
|
+
mapbox: { style: "open-street-map", zoom: 3, accesstoken: "secret" },
|
|
29
|
+
mapbox2: { center: { lat: 1, lon: 2 } },
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
expect(figure.data).toEqual([
|
|
34
|
+
{ type: "scattermap", subplot: "map" },
|
|
35
|
+
{ type: "scattermap", subplot: "map2" },
|
|
36
|
+
]);
|
|
37
|
+
expect(figure.layout).toEqual({
|
|
38
|
+
title: { text: "t" },
|
|
39
|
+
map: { style: "open-street-map", zoom: 3 },
|
|
40
|
+
map2: { center: { lat: 1, lon: 2 } },
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("migrates traces inside animation frames", () => {
|
|
45
|
+
const figure = migrateLegacyMapbox({
|
|
46
|
+
data: [],
|
|
47
|
+
layout: {},
|
|
48
|
+
frames: [{ name: "f0", data: [{ type: "scattermapbox" }] }],
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
expect(figure.frames).toEqual([
|
|
52
|
+
{ name: "f0", data: [{ type: "scattermap" }] },
|
|
53
|
+
]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("migrates mapbox keypaths in updatemenus and slider args", () => {
|
|
57
|
+
const figure = migrateLegacyMapbox({
|
|
58
|
+
data: [],
|
|
59
|
+
layout: {
|
|
60
|
+
updatemenus: [
|
|
61
|
+
{
|
|
62
|
+
buttons: [
|
|
63
|
+
{
|
|
64
|
+
label: "dark",
|
|
65
|
+
method: "relayout",
|
|
66
|
+
args: ["mapbox.style", "dark"],
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
label: "zoom",
|
|
70
|
+
method: "relayout",
|
|
71
|
+
args: [{ "mapbox2.zoom": 3 }],
|
|
72
|
+
},
|
|
73
|
+
{ label: "size", method: "restyle", args: ["marker.size", [10]] },
|
|
74
|
+
],
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
sliders: [
|
|
78
|
+
{ steps: [{ method: "relayout", args: ["mapbox.center.lat", 1] }] },
|
|
79
|
+
],
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
expect(figure.layout).toEqual({
|
|
84
|
+
updatemenus: [
|
|
85
|
+
{
|
|
86
|
+
buttons: [
|
|
87
|
+
{ label: "dark", method: "relayout", args: ["map.style", "dark"] },
|
|
88
|
+
{ label: "zoom", method: "relayout", args: [{ "map2.zoom": 3 }] },
|
|
89
|
+
{ label: "size", method: "restyle", args: ["marker.size", [10]] },
|
|
90
|
+
],
|
|
91
|
+
},
|
|
92
|
+
],
|
|
93
|
+
sliders: [
|
|
94
|
+
{ steps: [{ method: "relayout", args: ["map.center.lat", 1] }] },
|
|
95
|
+
],
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("does not treat inherited object keys as legacy trace types", () => {
|
|
100
|
+
const figure = migrateLegacyMapbox({
|
|
101
|
+
data: [{ type: "constructor" }],
|
|
102
|
+
layout: {},
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
expect(figure.data).toEqual([{ type: "constructor" }]);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("does not mutate its input", () => {
|
|
109
|
+
const original = {
|
|
110
|
+
data: [{ type: "scattermapbox", subplot: "mapbox" }],
|
|
111
|
+
layout: { mapbox: { accesstoken: "secret" } },
|
|
112
|
+
frames: null,
|
|
113
|
+
};
|
|
114
|
+
const snapshot = structuredClone(original);
|
|
115
|
+
|
|
116
|
+
migrateLegacyMapbox(original);
|
|
117
|
+
|
|
118
|
+
expect(original).toEqual(snapshot);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("leaves figures without legacy map traces untouched", () => {
|
|
122
|
+
const original = {
|
|
123
|
+
data: [
|
|
124
|
+
{ type: "scatter", x: [1], y: [2] },
|
|
125
|
+
{ type: "scattermap", subplot: "map", lat: [1], lon: [2] },
|
|
126
|
+
],
|
|
127
|
+
layout: { map: { zoom: 2 }, xaxis: { title: "x" } },
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
expect(migrateLegacyMapbox(original)).toEqual(original);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
@@ -13,10 +13,6 @@ import {
|
|
|
13
13
|
shouldHandleClickSelection,
|
|
14
14
|
} from "../selection";
|
|
15
15
|
|
|
16
|
-
function createTrace(trace: Partial<Plotly.PlotData>): Plotly.Data {
|
|
17
|
-
return trace as unknown as Plotly.Data;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
16
|
function createPlotDatum<T extends object>(overrides: T): Plotly.PlotDatum & T {
|
|
21
17
|
return overrides as unknown as Plotly.PlotDatum & T;
|
|
22
18
|
}
|
|
@@ -25,8 +21,8 @@ describe("hasPureLineTrace", () => {
|
|
|
25
21
|
it("detects scatter and scattergl traces that are pure lines", () => {
|
|
26
22
|
expect(
|
|
27
23
|
hasPureLineTrace([
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
{ type: "scatter", mode: "lines" },
|
|
25
|
+
{ type: "scattergl", mode: "text+lines" },
|
|
30
26
|
]),
|
|
31
27
|
).toBe(true);
|
|
32
28
|
});
|
|
@@ -34,9 +30,9 @@ describe("hasPureLineTrace", () => {
|
|
|
34
30
|
it("ignores non-line and marker traces", () => {
|
|
35
31
|
expect(
|
|
36
32
|
hasPureLineTrace([
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
33
|
+
{ type: "scatter", mode: "markers" },
|
|
34
|
+
{ type: "scatter", mode: "lines+markers" },
|
|
35
|
+
{ type: "bar" },
|
|
40
36
|
]),
|
|
41
37
|
).toBe(false);
|
|
42
38
|
});
|
|
@@ -313,61 +309,44 @@ describe("extractPoints", () => {
|
|
|
313
309
|
|
|
314
310
|
describe("hasAreaTrace", () => {
|
|
315
311
|
it("detects scatter trace with tozeroy fill", () => {
|
|
316
|
-
expect(
|
|
317
|
-
hasAreaTrace([createTrace({ type: "scatter", fill: "tozeroy" })]),
|
|
318
|
-
).toBe(true);
|
|
312
|
+
expect(hasAreaTrace([{ type: "scatter", fill: "tozeroy" }])).toBe(true);
|
|
319
313
|
});
|
|
320
314
|
|
|
321
315
|
it("detects scatter trace with tonexty fill", () => {
|
|
322
|
-
expect(
|
|
323
|
-
hasAreaTrace([createTrace({ type: "scatter", fill: "tonexty" })]),
|
|
324
|
-
).toBe(true);
|
|
316
|
+
expect(hasAreaTrace([{ type: "scatter", fill: "tonexty" }])).toBe(true);
|
|
325
317
|
});
|
|
326
318
|
|
|
327
319
|
it("detects scatter trace with stackgroup (px.area pattern)", () => {
|
|
328
320
|
expect(
|
|
329
|
-
hasAreaTrace([
|
|
330
|
-
createTrace({ type: "scatter", mode: "lines", stackgroup: "one" }),
|
|
331
|
-
]),
|
|
321
|
+
hasAreaTrace([{ type: "scatter", mode: "lines", stackgroup: "one" }]),
|
|
332
322
|
).toBe(true);
|
|
333
323
|
});
|
|
334
324
|
|
|
335
325
|
it("detects area traces with mode=none (fill-only, no visible line)", () => {
|
|
336
326
|
expect(
|
|
337
|
-
hasAreaTrace([
|
|
338
|
-
createTrace({ type: "scatter", fill: "tozeroy", mode: "none" }),
|
|
339
|
-
]),
|
|
327
|
+
hasAreaTrace([{ type: "scatter", fill: "tozeroy", mode: "none" }]),
|
|
340
328
|
).toBe(true);
|
|
341
329
|
});
|
|
342
330
|
|
|
343
331
|
it("ignores scatter traces with no fill and no stackgroup", () => {
|
|
344
332
|
expect(
|
|
345
333
|
hasAreaTrace([
|
|
346
|
-
|
|
347
|
-
|
|
334
|
+
{ type: "scatter", mode: "lines" },
|
|
335
|
+
{ type: "scatter", mode: "markers" },
|
|
348
336
|
]),
|
|
349
337
|
).toBe(false);
|
|
350
338
|
});
|
|
351
339
|
|
|
352
340
|
it("ignores scatter traces with fill=none", () => {
|
|
353
|
-
expect(hasAreaTrace([
|
|
354
|
-
false,
|
|
355
|
-
);
|
|
341
|
+
expect(hasAreaTrace([{ type: "scatter", fill: "none" }])).toBe(false);
|
|
356
342
|
});
|
|
357
343
|
|
|
358
344
|
it("ignores scatter traces with fill=empty string", () => {
|
|
359
|
-
expect(
|
|
360
|
-
hasAreaTrace([createTrace({ type: "scatter", fill: "" as "none" })]),
|
|
361
|
-
).toBe(false);
|
|
345
|
+
expect(hasAreaTrace([{ type: "scatter", fill: "" as "none" }])).toBe(false);
|
|
362
346
|
});
|
|
363
347
|
|
|
364
348
|
it("ignores non-scatter traces", () => {
|
|
365
|
-
expect(
|
|
366
|
-
hasAreaTrace([
|
|
367
|
-
createTrace({ type: "bar" }),
|
|
368
|
-
createTrace({ type: "heatmap" }),
|
|
369
|
-
]),
|
|
370
|
-
).toBe(false);
|
|
349
|
+
expect(hasAreaTrace([{ type: "bar" }, { type: "heatmap" }])).toBe(false);
|
|
371
350
|
});
|
|
372
351
|
|
|
373
352
|
it("returns false for undefined data", () => {
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
|
|
3
|
+
// plotly.js 4 removed the Mapbox GL traces. plotly.py <= 6 still emits them
|
|
4
|
+
// (deprecated), and their MapLibre-based replacements share the same schema,
|
|
5
|
+
// so we rewrite legacy figures instead of rendering a blank plot.
|
|
6
|
+
|
|
7
|
+
type Spec = Record<string, unknown>;
|
|
8
|
+
|
|
9
|
+
const LEGACY_TRACE_TYPES = new Map([
|
|
10
|
+
["scattermapbox", "scattermap"],
|
|
11
|
+
["choroplethmapbox", "choroplethmap"],
|
|
12
|
+
["densitymapbox", "densitymap"],
|
|
13
|
+
]);
|
|
14
|
+
|
|
15
|
+
// "mapbox", "mapbox2", ... -> "map", "map2", ...
|
|
16
|
+
const LEGACY_SUBPLOT_ID = /^mapbox(\d*)$/;
|
|
17
|
+
// "mapbox.style", "mapbox2.zoom", ... -> "map.style", "map2.zoom", ...
|
|
18
|
+
const LEGACY_KEYPATH = /^mapbox(\d*)(?=\.)/;
|
|
19
|
+
|
|
20
|
+
function toModernSubplotId(id: string): string {
|
|
21
|
+
return id.replace(LEGACY_SUBPLOT_ID, "map$1");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function toModernKeypath(keypath: string): string {
|
|
25
|
+
return keypath.replace(LEGACY_KEYPATH, "map$1");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isSpec(value: unknown): value is Spec {
|
|
29
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function mapItems(items: unknown, migrate: (item: unknown) => unknown) {
|
|
33
|
+
return Array.isArray(items) ? items.map(migrate) : items;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function migrateTrace(trace: unknown): unknown {
|
|
37
|
+
if (!isSpec(trace)) {
|
|
38
|
+
return trace;
|
|
39
|
+
}
|
|
40
|
+
const { type, subplot, ...rest } = trace;
|
|
41
|
+
return {
|
|
42
|
+
...rest,
|
|
43
|
+
...(typeof type === "string" && {
|
|
44
|
+
type: LEGACY_TRACE_TYPES.get(type) ?? type,
|
|
45
|
+
}),
|
|
46
|
+
...(typeof subplot === "string" && {
|
|
47
|
+
subplot: toModernSubplotId(subplot),
|
|
48
|
+
}),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Button and slider args are relayout/restyle arguments, e.g. the commonly
|
|
53
|
+
// used style switcher `["mapbox.style", "dark"]` or `[{ "mapbox.zoom": 3 }]`
|
|
54
|
+
function migrateArgs(args: unknown): unknown {
|
|
55
|
+
return mapItems(args, (arg) => {
|
|
56
|
+
if (typeof arg === "string") {
|
|
57
|
+
return toModernKeypath(arg);
|
|
58
|
+
}
|
|
59
|
+
if (isSpec(arg)) {
|
|
60
|
+
return Object.fromEntries(
|
|
61
|
+
Object.entries(arg).map(([key, value]) => [
|
|
62
|
+
toModernKeypath(key),
|
|
63
|
+
value,
|
|
64
|
+
]),
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
return arg;
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function migrateControl(control: unknown): unknown {
|
|
72
|
+
return isSpec(control)
|
|
73
|
+
? { ...control, args: migrateArgs(control.args) }
|
|
74
|
+
: control;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function migrateUpdateMenu(menu: unknown): unknown {
|
|
78
|
+
return isSpec(menu)
|
|
79
|
+
? { ...menu, buttons: mapItems(menu.buttons, migrateControl) }
|
|
80
|
+
: menu;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function migrateSlider(slider: unknown): unknown {
|
|
84
|
+
return isSpec(slider)
|
|
85
|
+
? { ...slider, steps: mapItems(slider.steps, migrateControl) }
|
|
86
|
+
: slider;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function migrateLayout(layout: unknown): unknown {
|
|
90
|
+
if (!isSpec(layout)) {
|
|
91
|
+
return layout;
|
|
92
|
+
}
|
|
93
|
+
const migrated: Spec = {};
|
|
94
|
+
for (const [key, value] of Object.entries(layout)) {
|
|
95
|
+
if (!LEGACY_SUBPLOT_ID.test(key) || !isSpec(value)) {
|
|
96
|
+
migrated[key] = value;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
// The access token is meaningless for MapLibre
|
|
100
|
+
const { accesstoken: _accesstoken, ...subplot } = value;
|
|
101
|
+
migrated[toModernSubplotId(key)] = subplot;
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
...migrated,
|
|
105
|
+
...("updatemenus" in migrated && {
|
|
106
|
+
updatemenus: mapItems(migrated.updatemenus, migrateUpdateMenu),
|
|
107
|
+
}),
|
|
108
|
+
...("sliders" in migrated && {
|
|
109
|
+
sliders: mapItems(migrated.sliders, migrateSlider),
|
|
110
|
+
}),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function migrateFrame(frame: unknown): unknown {
|
|
115
|
+
return isSpec(frame)
|
|
116
|
+
? { ...frame, data: mapItems(frame.data, migrateTrace) }
|
|
117
|
+
: frame;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Rewrites `scattermapbox`-style figures to their `scattermap` equivalents. */
|
|
121
|
+
export function migrateLegacyMapbox(figure: Spec): Spec {
|
|
122
|
+
return {
|
|
123
|
+
...figure,
|
|
124
|
+
data: mapItems(figure.data, migrateTrace),
|
|
125
|
+
layout: migrateLayout(figure.layout),
|
|
126
|
+
...("frames" in figure && {
|
|
127
|
+
frames: mapItems(figure.frames, migrateFrame),
|
|
128
|
+
}),
|
|
129
|
+
};
|
|
130
|
+
}
|