Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
Use save callback
  • Loading branch information
timtmok committed Apr 1, 2024
1 parent 78cd48f commit 84dcbd8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,10 @@ img.plot-preview {
grid-area: preview;
}

.plot-save-dialog-action-bar .left,
.plot-save-dialog-action-bar .right {
display: flex;
gap: 10px;
margin-top: 15px;
.plot-save-dialog-action-bar div.ok-cancel-action-bar {
justify-content: end;
}

.button.action-bar-button {
.plot-save-dialog-action-bar .button.action-bar-button {
padding: 10px;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@ import { OKCancelActionBar } from 'vs/workbench/browser/positronComponents/posit
import { PositronModalDialog } from 'vs/workbench/browser/positronComponents/positronModalDialog/positronModalDialog';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { PositronModalReactRenderer } from 'vs/workbench/browser/positronModalReactRenderer/positronModalReactRenderer';
import { Schemas } from 'vs/base/common/network';
import { HTMLFileSystemProvider } from 'vs/platform/files/browser/htmlFileSystemProvider';
import { IFileService } from 'vs/platform/files/common/files';
import { decodeBase64 } from 'vs/base/common/buffer';

interface SavePlotOptions {
export interface SavePlotOptions {
uri: string;
path: URI;
}
Expand Down Expand Up @@ -55,9 +51,9 @@ export const showSavePlotModalDialog = async (
layoutService: IWorkbenchLayoutService,
keybindingService: IKeybindingService,
fileDialogService: IFileDialogService,
fileService: IFileService,
plotClient: PlotClientInstance,
suggestedPath?: URI
savePlotCallback: (options: SavePlotOptions) => void,
suggestedPath?: URI,
): Promise<SavePlotOptions | undefined> => {


Expand All @@ -71,14 +67,7 @@ export const showSavePlotModalDialog = async (

const plotWidth = plotClient.lastRender?.width ?? 100;
const plotHeight = plotClient.lastRender?.height ?? 100;
const getPlotUri = (plotData: string) => {
const regex = /^data:.+\/(.+);base64,(.*)$/;
const matches = plotData.match(regex);
if (!matches || matches.length !== 3) {
return null;
}
return matches;
}

renderer.render(
<SavePlotModalDialog
layoutService={layoutService}
Expand All @@ -87,20 +76,7 @@ export const showSavePlotModalDialog = async (
plotWidth={plotWidth}
plotHeight={plotHeight}
suggestedPath={suggestedPath}
savePlot={async (options) => {
const htmlFileSystemProvider = fileService.getProvider(Schemas.file) as HTMLFileSystemProvider;
const matches = getPlotUri(options.uri);

if (!matches) {
return;
}

const data = matches[2];

htmlFileSystemProvider.writeFile(options.path, decodeBase64(data).buffer, { create: true, overwrite: true, unlock: true, atomic: false })
.then(() => {
});
}}
savePlotCallback={savePlotCallback}
plotClient={plotClient}
/>
);
Expand All @@ -116,8 +92,8 @@ interface SavePlotModalDialogProps {
plotWidth: number;
plotHeight: number;
plotClient: PlotClientInstance;
savePlotCallback: (options: SavePlotOptions) => void;
suggestedPath?: URI;
savePlot: (options: SavePlotOptions) => Promise<void>;
}

const SavePlotModalDialog = (props: SavePlotModalDialogProps) => {
Expand All @@ -133,9 +109,13 @@ const SavePlotModalDialog = (props: SavePlotModalDialogProps) => {
setUri(props.plotClient.lastRender?.uri ?? '');
}, [props.plotClient.lastRender?.uri]);

const validateInput = React.useCallback((): boolean => {
return path.valid && width.valid && height.valid && dpi.valid;
}, [path, width, height, dpi]);

React.useEffect(() => {
validateInput();
}, [width, height, dpi, path]);
}, [validateInput]);

const updateWidth = (widthString: string): void => {
const newWidth = parseInt(widthString);
Expand All @@ -157,10 +137,6 @@ const SavePlotModalDialog = (props: SavePlotModalDialogProps) => {
setPath({ value: newPath, valid: !!newPath });
};

const validateInput = (): boolean => {
return path.valid && width.valid && height.valid && dpi.valid;
};

const browseHandler = async () => {
const uri = await props.fileDialogService.showSaveDialog({
title: title,
Expand All @@ -184,7 +160,7 @@ const SavePlotModalDialog = (props: SavePlotModalDialogProps) => {
const plotResult = await generatePreview();

if (plotResult) {
props.savePlot({ uri: plotResult.uri, path: path.value });
props.savePlotCallback({ uri: plotResult.uri, path: path.value });
}

setRendering(false);
Expand Down Expand Up @@ -274,5 +250,5 @@ const SavePlotModalDialog = (props: SavePlotModalDialogProps) => {
</div>

</PositronModalDialog>
)
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { IPositronIPyWidgetsService } from 'vs/workbench/services/positronIPyWid
import { Schemas } from 'vs/base/common/network';
import { IFileDialogService } from 'vs/platform/dialogs/common/dialogs';
import { decodeBase64 } from 'vs/base/common/buffer';
import { showSavePlotModalDialog } from 'vs/workbench/contrib/positronPlots/browser/modalDialogs/savePlotModalDialog';
import { SavePlotOptions, showSavePlotModalDialog } from 'vs/workbench/contrib/positronPlots/browser/modalDialogs/savePlotModalDialog';
import { IWorkbenchLayoutService } from 'vs/workbench/services/layout/browser/layoutService';
import { URI } from 'vs/base/common/uri';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
Expand Down Expand Up @@ -688,11 +688,7 @@ export class PositronPlotsService extends Disposable implements IPositronPlotsSe
this.showSavePlotDialog(uri);
} else if (plot instanceof PlotClientInstance) {
// if it's a dynamic plot, present options dialog
showSavePlotModalDialog(this._layoutService, this._keybindingService, this._fileDialogService, this._fileService, plot, suggestedPath).then(result => {
if (result) {
this.savePlotAs(result.uri, result.path);
}
});
showSavePlotModalDialog(this._layoutService, this._keybindingService, this._fileDialogService, plot, this.savePlotAs, suggestedPath);
} else {
// if it's a webview plot, do nothing
return;
Expand All @@ -701,20 +697,20 @@ export class PositronPlotsService extends Disposable implements IPositronPlotsSe
}
}

private savePlotAs(plotData: string, path: URI) {
private savePlotAs = (options: SavePlotOptions) => {
const htmlFileSystemProvider = this._fileService.getProvider(Schemas.file) as HTMLFileSystemProvider;
const matches = this.getPlotUri(plotData);
const matches = this.getPlotUri(options.uri);

if (!matches) {
return;
}

const data = matches[2];

htmlFileSystemProvider.writeFile(path, decodeBase64(data).buffer, { create: true, overwrite: true, unlock: true, atomic: false })
htmlFileSystemProvider.writeFile(options.path, decodeBase64(data).buffer, { create: true, overwrite: true, unlock: true, atomic: false })
.then(() => {
});
}
};

private getPlotUri(plotData: string) {
const regex = /^data:.+\/(.+);base64,(.*)$/;
Expand All @@ -732,7 +728,6 @@ export class PositronPlotsService extends Disposable implements IPositronPlotsSe
return;
}

const data = matches[2];
const extension = matches[1];

this._fileDialogService.showSaveDialog({
Expand All @@ -746,10 +741,7 @@ export class PositronPlotsService extends Disposable implements IPositronPlotsSe
],
}).then(result => {
if (result) {
const htmlFileSystemProvider = this._fileService.getProvider(Schemas.file) as HTMLFileSystemProvider;
htmlFileSystemProvider.writeFile(result, decodeBase64(data).buffer, { create: true, overwrite: true, unlock: true, atomic: false })
.then(() => {
});
this.savePlotAs({ path: result, uri });
}
});
}
Expand Down

0 comments on commit 84dcbd8

Please sign in to comment.