Skip to content
Merged
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
185 changes: 113 additions & 72 deletions src/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,65 +184,15 @@ const App: React.FC<Props> = (props) => {
[setExample, setEmptySpec, setGist],
);

useEffect(() => {
const handleMessage = (evt: MessageEvent) => {
const data = evt.data as MessageData;
if (!data.spec) {
return;
}
setState((s) => ({...s, baseUrl: evt.origin}));
console.info('[Vega-Editor] Received Message', evt.origin, data);

if (data.config) {
setState((s) => ({...s, configEditorString: stringify(data.config)}));
}

if (data.spec || data.file) {
// FIXME: remove any
(evt as any).source.postMessage(true, '*');
}
if (data.spec) {
setState((s) => ({...s, editorString: data.spec}));
}
if (data.renderer) {
setState((s) => ({...s, renderer: data.renderer}));
}
};

window.addEventListener('message', handleMessage, false);

if (params?.mode) {
const mode = params.mode.toLowerCase();
if (mode === 'vega' || mode === 'vega-lite') {
setState((s) => ({...s, mode: mode as Mode}));
}
}
setSpecInUrl(params);
// Modular parsers
const parseVegaLiteSpec = useCallback(
(editorString: string, configEditorString: string, logLevel: number) => {
const currLogger = new LocalLogger();
currLogger.level(logLevel);

return () => {
window.removeEventListener('message', handleMessage, false);
};
}, [params, setState, setSpecInUrl]);

// Parse Logic
useEffect(() => {
if (state.manualParse) {
if (!state.parse) {
return;
}
} else {
if (!state.editorString || state.editorString.trim() === '') {
return;
}
}

const currLogger = new LocalLogger();
currLogger.level(state.logLevel);

try {
if (state.mode === Mode.VegaLite) {
const vegaLiteSpec: vegaLite.TopLevelSpec = parseJSONCOrThrow(state.editorString);
const config: Config = parseJSONCOrThrow(state.configEditorString);
try {
const vegaLiteSpec: vegaLite.TopLevelSpec = parseJSONCOrThrow(editorString);
const config: Config = parseJSONCOrThrow(configEditorString);

const options = {
config,
Expand All @@ -264,7 +214,7 @@ const App: React.FC<Props> = (props) => {
validateVegaLite(vegaLiteSpec, currLogger);

const compileResult =
state.editorString !== '{}' ? vegaLite.compile(vegaLiteSpec, options) : {spec: {}, normalized: {}};
editorString !== '{}' ? vegaLite.compile(vegaLiteSpec, options) : {spec: {}, normalized: {}};
const normalizedSpec = compileResult.normalized;

setState((s) => ({
Expand All @@ -279,8 +229,28 @@ const App: React.FC<Props> = (props) => {
debugs: currLogger.debugs,
error: null,
}));
} else {
const spec = parseJSONCOrThrow(state.editorString);
} catch (error: any) {
setState((s) => ({
...s,
error: {message: error.message},
parse: false,
errors: currLogger.errors,
warns: currLogger.warns,
infos: currLogger.infos,
debugs: currLogger.debugs,
}));
}
},
[setState],
);

const parseVegaSpec = useCallback(
(editorString: string, logLevel: number) => {
const currLogger = new LocalLogger();
currLogger.level(logLevel);

try {
const spec = parseJSONCOrThrow(editorString);
if (spec.$schema) {
try {
const parsed = schemaParser(spec.$schema);
Expand All @@ -305,19 +275,90 @@ const App: React.FC<Props> = (props) => {
debugs: currLogger.debugs,
error: null,
}));
} catch (error: any) {
setState((s) => ({
...s,
error: {message: error.message},
parse: false,
errors: currLogger.errors,
warns: currLogger.warns,
infos: currLogger.infos,
debugs: currLogger.debugs,
}));
}
},
[setState],
);

useEffect(() => {
const handleMessage = (evt: MessageEvent) => {
const data = evt.data as MessageData;
if (!data.spec) {
return;
}
setState((s) => ({...s, baseUrl: evt.origin}));
console.info('[Vega-Editor] Received Message', evt.origin, data);

if (data.config) {
setState((s) => ({...s, configEditorString: stringify(data.config)}));
}

if (data.spec || data.file) {
// FIXME: remove any
(evt as any).source.postMessage(true, '*');
}
if (data.spec) {
setState((s) => ({...s, editorString: data.spec}));
}
} catch (error) {
setState((s) => ({
...s,
error: {message: error.message},
parse: false,
errors: currLogger.errors,
warns: currLogger.warns,
infos: currLogger.infos,
debugs: currLogger.debugs,
}));
if (data.renderer) {
setState((s) => ({...s, renderer: data.renderer}));
}
};

window.addEventListener('message', handleMessage, false);

if (params?.mode) {
const mode = params.mode.toLowerCase();
if (mode === 'vega' || mode === 'vega-lite') {
setState((s) => ({...s, mode: mode as Mode}));
}
}
setSpecInUrl(params);

return () => {
window.removeEventListener('message', handleMessage, false);
};
}, [params, setState, setSpecInUrl]);

// Parse Logic
useEffect(() => {
if (state.manualParse) {
if (!state.parse) {
return;
}
} else {
if (!state.editorString || state.editorString.trim() === '') {
return;
}
}

if (state.mode === Mode.VegaLite) {
parseVegaLiteSpec(state.editorString, state.configEditorString, state.logLevel);
} else {
parseVegaSpec(state.editorString, state.logLevel);
}
}, [state.editorString, state.mode, state.parse, state.config, state.logLevel, setState]);
}, [
state.editorString,
state.mode,
state.parse,
state.config,
state.logLevel,
state.manualParse,
state.configEditorString,
setState,
parseVegaLiteSpec,
parseVegaSpec,
]);

useEffect(() => {
if (state.mergeConfigSpec) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/header/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
const [open, setOpen] = useState(false);
const [scrollPosition, setScrollPos] = useState(0);
const [showVega, setShowVega] = useState(mode === Mode.Vega);
const [helpModalOpen, setHelpModalOpen] = useState(false);

Check warning on line 34 in src/components/header/renderer.tsx

View workflow job for this annotation

GitHub Actions / Lint and Build

'helpModalOpen' is assigned a value but never used
const [exportModalOpen, setExportModalOpen] = useState(false);

Check warning on line 35 in src/components/header/renderer.tsx

View workflow job for this annotation

GitHub Actions / Lint and Build

'exportModalOpen' is assigned a value but never used
const [shareModalOpen, setShareModalOpen] = useState(false);

Check warning on line 36 in src/components/header/renderer.tsx

View workflow job for this annotation

GitHub Actions / Lint and Build

'shareModalOpen' is assigned a value but never used
const [gistModalOpen, setGistModalOpen] = useState(false);

Check warning on line 37 in src/components/header/renderer.tsx

View workflow job for this annotation

GitHub Actions / Lint and Build

'gistModalOpen' is assigned a value but never used
const [examplesModalOpen, setExamplesModalOpen] = useState(showExample);

const scrollHandlers = useRef(new WeakMap());
Expand Down Expand Up @@ -533,7 +533,7 @@
className="header-button"
id="run-button"
onClick={() => {
setState((s) => ({...s, parse: true}));
if (manualParse) setState((s) => ({...s, parse: true}));
}}
>
<Play className="header-icon" />
Expand Down Expand Up @@ -620,7 +620,7 @@
const exportContent = <ExportModal />;
const shareContent = <ShareModal />;

const handleExamplesModalOpen = useCallback(() => {

Check warning on line 623 in src/components/header/renderer.tsx

View workflow job for this annotation

GitHub Actions / Lint and Build

'handleExamplesModalOpen' is assigned a value but never used
setExamplesModalOpen(true);

setTimeout(() => {
Expand All @@ -641,7 +641,7 @@
}, 0);
}, [lastPosition]);

const handleExamplesModalClose = useCallback(() => {

Check warning on line 644 in src/components/header/renderer.tsx

View workflow job for this annotation

GitHub Actions / Lint and Build

'handleExamplesModalClose' is assigned a value but never used
setExamplesModalOpen(false);

if (examplePortal.current) {
Expand All @@ -659,10 +659,10 @@
setShowVega(isVega);
}, []);

const handleExportModalOpen = () => setExportModalOpen(true);

Check warning on line 662 in src/components/header/renderer.tsx

View workflow job for this annotation

GitHub Actions / Lint and Build

'handleExportModalOpen' is assigned a value but never used
const handleExportModalClose = () => setExportModalOpen(false);

Check warning on line 663 in src/components/header/renderer.tsx

View workflow job for this annotation

GitHub Actions / Lint and Build

'handleExportModalClose' is assigned a value but never used

const handleShareModalOpen = () => setShareModalOpen(true);

Check warning on line 665 in src/components/header/renderer.tsx

View workflow job for this annotation

GitHub Actions / Lint and Build

'handleShareModalOpen' is assigned a value but never used
const handleShareModalClose = () => setShareModalOpen(false);

const handleGistModalOpen = () => setGistModalOpen(true);
Expand Down
8 changes: 3 additions & 5 deletions src/components/input-panel/spec-editor/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,14 @@ const EditorWithNavigation: React.FC<{

if (parsedMode === Mode.Vega) {
props.updateVegaSpec(spec, config);
} else if (parsedMode === Mode.VegaLite) {
props.updateVegaLiteSpec(spec, config);
} else {
props.updateEditorString(spec);
props.updateVegaLiteSpec(spec, config);
}
},
[mode, props.updateVegaSpec, props.updateVegaLiteSpec, props.updateEditorString],
[mode, props.updateVegaSpec, props.updateVegaLiteSpec],
);

const debouncedUpdateSpec = useCallback(debounce(700, updateSpec), [updateSpec]);
const debouncedUpdateSpec = useCallback(debounce(1200, updateSpec), [updateSpec]);

useEffect(() => {
if (compressed) {
Expand Down
21 changes: 13 additions & 8 deletions src/components/renderer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ const RendererContainer: React.FC = () => {
const {state, setState} = useAppContext();
const {setRuntime, recordPulse} = useDataflowActions();

const resetLogs = React.useCallback(
() =>
setState((s) => ({
...s,
errors: [],
warns: [],
infos: [],
debugs: [],
})),
[setState],
);

const props = {
baseURL: state.baseURL,
config: state.config,
Expand All @@ -23,14 +35,7 @@ const RendererContainer: React.FC = () => {
backgroundColor: state.backgroundColor,
expressionInterpreter: state.expressionInterpreter,
setError: (error: {message: string} | null) => setState((s) => ({...s, error})),
resetLogs: () =>
setState((s) => ({
...s,
errors: [],
warns: [],
infos: [],
debugs: [],
})),
resetLogs,
};

return (
Expand Down
1 change: 1 addition & 0 deletions src/components/renderer/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export default function Renderer(props: RendererProps) {
}
}, [location.pathname, navigate]);

// fullscreen keyboard shortcuts
useEffect(() => {
const onKey = (e: KeyboardEvent) => {
if (e.keyCode === KEYCODES.ESCAPE && size.fullscreen) {
Expand Down
35 changes: 19 additions & 16 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ export class DispatchingLogger implements vega.LoggerInterface {
this.setState = setState;
}

private updateMessageArray(state: any, messageArrayKey: string, message: string): any {
const lastMessage = state[messageArrayKey][state[messageArrayKey].length - 1];
if (lastMessage === message) {
return state;
}
return {
...state,
[messageArrayKey]: [...state[messageArrayKey], message],
};
}

public updateCurrentState(state: any) {
this.currentState = state;
}
Expand Down Expand Up @@ -98,10 +109,8 @@ export class DispatchingLogger implements vega.LoggerInterface {
if (this.level() >= vega.Warn) {
console.warn(...args);

this.setState((state) => ({
...state,
warns: [...state.warns, args.join(' ')],
}));
const message = args.join(' ');
this.setState((state) => this.updateMessageArray(state, 'warns', message));
}

return this;
Expand All @@ -118,10 +127,8 @@ export class DispatchingLogger implements vega.LoggerInterface {
if (this.level() >= vega.Info) {
console.info(...args);

this.setState((state) => ({
...state,
infos: [...state.infos, args.join(' ')],
}));
const message = args.join(' ');
this.setState((state) => this.updateMessageArray(state, 'infos', message));
}

return this;
Expand All @@ -138,10 +145,8 @@ export class DispatchingLogger implements vega.LoggerInterface {
if (this.level() >= vega.Debug) {
console.debug(...args);

this.setState((state) => ({
...state,
debugs: [...state.debugs, args.join(' ')],
}));
const message = args.join(' ');
this.setState((state) => this.updateMessageArray(state, 'debugs', message));
}

return this;
Expand All @@ -157,10 +162,8 @@ export class DispatchingLogger implements vega.LoggerInterface {
// Always log errors regardless of level
console.error(...args);

this.setState((state) => ({
...state,
errors: [...state.errors, args.join(' ')],
}));
const message = args.join(' ');
this.setState((state) => this.updateMessageArray(state, 'errors', message));

return this;
};
Expand Down
Loading