Skip to content

[FIX] Chart: Fix flicker when copying chart as image #6103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/helpers/figures/charts/chart_ui_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export async function chartToImageFile(
const div = document.createElement("div");
div.style.width = `${figure.width}px`;
div.style.height = `${figure.height}px`;
div.style.position = "fixed";
div.style.opacity = "0";
const canvas = document.createElement("canvas");
div.append(canvas);
canvas.setAttribute("width", figure.width.toString());
Expand Down
7 changes: 6 additions & 1 deletion src/registries/figure_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,15 @@ function getChartMenu(
const innerHTML = `<img src="${xmlEscape(imageUrl)}" />`;
const blob = await chartToImageFile(runtime, figure, chartType)!;

env.clipboard.write({
await env.clipboard.write({
"text/html": innerHTML,
"image/png": blob,
});
env.notifyUser({
text: _t("The chart was copied to your clipboard"),
sticky: false,
type: "info",
});
},
},
{
Expand Down
15 changes: 8 additions & 7 deletions tests/figures/chart/charts_component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { BACKGROUND_CHART_COLOR, DEBOUNCE_TIME } from "../../../src/constants";
import { toHex, toZone } from "../../../src/helpers";
import { ScorecardChart } from "../../../src/helpers/figures/charts";
import { getChartColorsGenerator } from "../../../src/helpers/figures/charts/runtime";
import { ClipboardPlugin } from "../../../src/plugins/ui_stateful";
import {
CHART_TYPES,
ChartDefinition,
Expand Down Expand Up @@ -54,7 +53,6 @@ import {
} from "../../test_helpers/dom_helper";
import { getCellContent } from "../../test_helpers/getters_helpers";
import {
getPlugin,
mockChart,
mockGeoJsonService,
mountComponentWithPortalTarget,
Expand Down Expand Up @@ -346,7 +344,8 @@ describe("charts", () => {
test.each([TEST_CHART_TYPES])(
"Copy a chart as a figure pushes it in the clipboard as a File",
async (chartType) => {
await mountSpreadsheet();
const spyNotify = jest.fn();
({ env, model, fixture } = await mountSpreadsheetHelper({ model, notifyUser: spyNotify }));
createTestChart(chartType);
await nextTick();
await simulateClick(".o-figure");
Expand All @@ -358,16 +357,18 @@ describe("charts", () => {
throw new Error("Clipboard read failed");
}
const clipboardContent = clipboard.content;

const cbPlugin = getPlugin(model, ClipboardPlugin);
//@ts-ignore
const clipboardHtmlData = JSON.stringify(cbPlugin.getSheetData());
const imgData = new window.Chart("test", mockChartData).toBase64Image();

expect(clipboardContent).toMatchObject({
"text/html": `<img src="${xmlEscape(imgData)}" />`,
"image/png": expect.any(Blob),
});

expect(spyNotify).toHaveBeenCalledWith({
text: "The chart was copied to your clipboard",
sticky: false,
type: "info",
});
}
);

Expand Down