Skip to content

Commit

Permalink
Implement Structure Definition Saving
Browse files Browse the repository at this point in the history
  • Loading branch information
LightFLP committed Jan 15, 2024
1 parent 036f117 commit c283f9d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
18 changes: 18 additions & 0 deletions src/extension/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class EditorProvider implements vscode.CustomTextEditorProvider {
_token: vscode.CancellationToken
): Promise<void> {
const rulesFile = `${document.fileName}.rules`;
const structureDefinitionFile = `${document.fileName}.structure`;

// Setup initial content for the webview
webviewPanel.webview.options = {
Expand All @@ -35,6 +36,7 @@ export class EditorProvider implements vscode.CustomTextEditorProvider {
type: message_type,
text: document.getText(),
rules: fs.existsSync(rulesFile) ? JSON.parse(fs.readFileSync(rulesFile, {encoding: 'utf8'})) : [],
structureDefinitionFile: fs.existsSync(structureDefinitionFile) ? JSON.parse(fs.readFileSync(structureDefinitionFile, {encoding: 'utf8'})) : []
});
}

Expand All @@ -59,11 +61,27 @@ export class EditorProvider implements vscode.CustomTextEditorProvider {

// Receive message from the webview.
webviewPanel.webview.onDidReceiveMessage(e => {

if (e.type === 'readFile') {
updateWebview('readFile');
} else if (e.type === 'saveRules') {
fs.writeFileSync(rulesFile, JSON.stringify(e.rules));
}
else if (e.type === 'saveStructureDefinition') {

const options: vscode.SaveDialogOptions = {
title: 'Save Structure Definition',
defaultUri: vscode.Uri.joinPath(document.uri, structureDefinitionFile),
filters: {
'Stucture files': ['structure']
}
};
vscode.window.showSaveDialog(options).then(fileUri => {
if (fileUri) {
fs.writeFileSync(fileUri.fsPath, e.structureDefinition);
}
});
}
else if (e.type === 'exportData') {
const filename = document.fileName.split(".tracy")[0].split("_Tracy_export_")[0]
const _date = new Date().toISOString().slice(0,10).replace(/-/g, "");
Expand Down
8 changes: 8 additions & 0 deletions src/viewer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ export default class App extends React.Component<Props, State> {
}
}

handleSavingStuctureDefinition(structureDefinition: string) {
console.log("Saving definition - in App");
// console.log(structureDefinition);
this.vscode.postMessage({ type: "saveStructureDefinition", structureDefinition: structureDefinition});
}

handleRowCollapse(rowIndex: number, isRendered: boolean) {
const newRowProps = this.state.rowProperties;
newRowProps[rowIndex].isRendered = isRendered;
Expand Down Expand Up @@ -795,6 +801,8 @@ export default class App extends React.Component<Props, State> {
onClose={() => this.handleStructureDialog(true)}
onStructureUpdate={() => this.handleStructureUpdate(false)}
onMatchStructure={(expression) => this.handleStructureMatching(expression)}
onStructureDefinitionSave={(structureDefinition) => this.handleSavingStuctureDefinition(structureDefinition)}
onStructureDefinitionLoad={() => {}}
onDefineSegment={(expression) => this.handleSegmentation(expression)}
onNavigateStructureMatches={(isGoingForward) =>
this.handleNavigation(isGoingForward, true)
Expand Down
25 changes: 22 additions & 3 deletions src/viewer/structures/StructureDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import isEqual from "react-fast-compare";
import cloneDeep from "lodash/cloneDeep";
import ContextMenu from "../contextMenu/contextMenu";
import { styled } from "@mui/material/styles";
import StructureSettingsDropdown from "./StructureSettingsDropdown";
import { StructureSettingsDropdown } from "./StructureSettingsDropdown";

interface Props {
logHeaderColumns: Header[];
Expand All @@ -44,6 +44,8 @@ interface Props {
onStructureUpdate: () => void;
onNavigateStructureMatches: (isGoingForward: boolean) => void;
onMatchStructure: (expression: string) => void;
onStructureDefinitionSave:(structureDefinition: string) => void;
onStructureDefinitionLoad:() => void;
onExportStructureMatches: () => void;
onDefineSegment: (expression: string) => void;
}
Expand All @@ -57,6 +59,7 @@ interface State {
}

export default class StructureDialog extends React.Component<Props, State> {

constructor(props: Props) {
super(props);
const { logHeaderColumnsTypes, logSelectedRows } = this.props;
Expand All @@ -71,8 +74,9 @@ export default class StructureDialog extends React.Component<Props, State> {
wildcards: [],
};

//bind context for all functions used by the context menu:
//bind context for all functions used by the context and dropdown menus:
this.createWildcard = this.createWildcard.bind(this);
this.saveStructureDefinition = this.saveStructureDefinition.bind(this);
}

componentDidMount(): void {
Expand Down Expand Up @@ -464,6 +468,21 @@ export default class StructureDialog extends React.Component<Props, State> {
}
}

saveStructureDefinition() {
console.log("Saving structure definition");
const { structureEntries, wildcards} = this.state;
const {logHeaderColumns, onStructureDefinitionSave} = this.props;

let structureDefiniton = { headerColumns: logHeaderColumns, entries: structureEntries, wildcards: wildcards};
let structureDefinitonJSON = JSON.stringify(structureDefiniton);

onStructureDefinitionSave(structureDefinitonJSON);
}

loadStructureDefinition() {
console.log("Loading structure definition");
}

render() {
const { structureEntries, wildcards, isRemovingStructureEntries, isStructureMatching } =
this.state;
Expand Down Expand Up @@ -553,7 +572,7 @@ export default class StructureDialog extends React.Component<Props, State> {
>
<i className="codicon codicon-question" />
</CustomWidthTooltip>
<StructureSettingsDropdown/>
<StructureSettingsDropdown onStructureDefinitionSave={this.saveStructureDefinition} onStructureDefinitionLoad={this.loadStructureDefinition}/>
<IconButton
id="close-button"
aria-label="close"
Expand Down
20 changes: 15 additions & 5 deletions src/viewer/structures/StructureSettingsDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,23 @@ import SaveIcon from '@mui/icons-material/Save';
import FileOpenIcon from '@mui/icons-material/FileOpen';
import SettingsIcon from '@mui/icons-material/Settings';

export default function BasicMenu() {
interface StructureSettingsDropdownProps {
onStructureDefinitionSave: () => void;
onStructureDefinitionLoad: () => void;
}

export const StructureSettingsDropdown: React.FunctionComponent<StructureSettingsDropdownProps> = ({onStructureDefinitionSave, onStructureDefinitionLoad}) => {
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
const open = Boolean(anchorEl);
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
setAnchorEl(event.currentTarget);
};
const handleClose = () => {
const handleClose = (action: string) => {
if (action === 'save')
onStructureDefinitionSave();
else if (action === 'load')
onStructureDefinitionLoad();

setAnchorEl(null);
};

Expand All @@ -35,19 +45,19 @@ export default function BasicMenu() {
id="basic-menu"
anchorEl={anchorEl}
open={open}
onClose={handleClose}
onClose={() => handleClose('')}
MenuListProps={{
'aria-labelledby': 'basic-button',
}}
>
{/* <MenuItem onClick={handleClose}>Profile</MenuItem> */}
<MenuItem onClick={handleClose}>
<MenuItem onClick={() => handleClose('save')}>
<ListItemIcon>
<SaveIcon fontSize="small" />
</ListItemIcon>
<ListItemText>Save Structure Definition</ListItemText>
</MenuItem>
<MenuItem onClick={handleClose}>
<MenuItem onClick={() => handleClose('load')}>
<ListItemIcon>
<FileOpenIcon fontSize="small" />
</ListItemIcon>
Expand Down

0 comments on commit c283f9d

Please sign in to comment.