Skip to content

Commit 7947363

Browse files
authored
Merge pull request #1461 from vega/cameron.yick/vega-interpreter-control
feat(editor): Add setting for using editor with `expressionInterpreter` enabled
2 parents e034278 + a6ea2ca commit 7947363

File tree

9 files changed

+602
-539
lines changed

9 files changed

+602
-539
lines changed

package-lock.json

+525-523
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,12 @@
6363
"redux-localstorage": "^0.4.1",
6464
"redux-thunk": "^2.4.2",
6565
"tippy.js": "^6.3.7",
66-
"vega-cli": "5.30.0",
6766
"tslib": "^2.7.0",
67+
"vega-cli": "5.30.0",
6868
"vega-datasets": "^2.9.0",
69-
"vega-lite": "5.21.0",
7069
"vega-embed": "^6.26.0",
70+
"vega-interpreter": "^1.1.0",
71+
"vega-lite": "5.21.0",
7172
"vega-schema-url-parser": "^2.2.0",
7273
"vega-themes": "^2.15.0",
7374
"vega-tooltip": "^0.34.0",

src/actions/editor.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export const SET_DECORATION = 'SET_DECORATION' as const;
4646
export const SET_COMPILED_EDITOR_REFERENCE = 'SET_COMPILED_EDITOR_REFERENCE' as const;
4747
export const SET_EDITOR_FOCUS = 'SET_EDITOR_FOCUS' as const;
4848
export const SET_BACKGROUND_COLOR = 'SET_BACKGROUND_COLOR' as const;
49+
export const SET_EXPRESSION_INTERPRETER = 'SET_EXPRESSION_INTERPRETER' as const;
4950

5051
export const ERROR = 'ERROR' as const;
5152
export const WARN = 'WARN' as const;
@@ -100,7 +101,8 @@ export type Action =
100101
| AddError
101102
| AddWarning
102103
| AddInfo
103-
| AddDebug;
104+
| AddDebug
105+
| SetExpressionInterpreter;
104106

105107
export function setMode(mode: Mode) {
106108
return {
@@ -511,3 +513,12 @@ export function addDebug(debug: string) {
511513
}
512514

513515
export type AddDebug = ReturnType<typeof addDebug>;
516+
517+
export function setExpressionInterpreter(value: boolean) {
518+
return {
519+
type: SET_EXPRESSION_INTERPRETER,
520+
expressionInterpreter: value,
521+
};
522+
}
523+
524+
export type SetExpressionInterpreter = ReturnType<typeof setExpressionInterpreter>;

src/components/renderer/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export function mapStateToProps(state: State) {
2121
vegaSpec: state.vegaSpec,
2222
view: state.view,
2323
backgroundColor: state.backgroundColor,
24+
expressionInterpreter: state.expressionInterpreter,
2425
};
2526
}
2627

src/components/renderer/renderer.tsx

+22-13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import addProjections from '../../utils/addProjections';
1212
import {dispatchingLogger} from '../../utils/logger';
1313
import {Popup} from '../popup';
1414
import './index.css';
15+
import {expressionInterpreter as vegaInterpreter} from 'vega-interpreter';
1516

1617
// Add additional projections
1718
addProjections(vega.projection);
@@ -126,16 +127,23 @@ class Editor extends React.PureComponent<Props, State> {
126127
}
127128

128129
public initView() {
129-
const {vegaSpec, vegaLiteSpec, normalizedVegaLiteSpec, config, baseURL, mode, setView, setRuntime, hoverEnable} =
130-
this.props;
131-
132-
let runtime: vega.Runtime;
133-
if (mode === Mode.VegaLite) {
134-
// In vl mode, we compile Vega-Lite spec along with config to Vega spec
135-
runtime = vega.parse(vegaSpec);
136-
} else {
137-
runtime = vega.parse(vegaSpec, config as VgConfig);
138-
}
130+
const {
131+
vegaSpec,
132+
vegaLiteSpec,
133+
normalizedVegaLiteSpec,
134+
config,
135+
baseURL,
136+
mode,
137+
setView,
138+
setRuntime,
139+
hoverEnable,
140+
expressionInterpreter,
141+
} = this.props;
142+
143+
const parseOptions = expressionInterpreter ? {ast: true} : {};
144+
145+
// In vl mode, we compile Vega-Lite spec along with config to Vega spec
146+
const runtime = vega.parse(vegaSpec, mode === Mode.VegaLite ? {} : (config as VgConfig), parseOptions);
139147

140148
const loader = vega.loader();
141149
const originalLoad = loader.load.bind(loader);
@@ -163,15 +171,15 @@ class Editor extends React.PureComponent<Props, State> {
163171

164172
this.props.view.finalize();
165173
}
166-
167174
const hover = typeof hoverEnable === 'boolean' ? hoverEnable : mode === Mode.Vega;
168175
const view = new vega.View(runtime, {
169176
hover,
170177
loader,
178+
expr: expressionInterpreter ? vegaInterpreter : undefined,
171179
});
172180

173181
view.runAfter(this.runAfter, true);
174-
(view as any).logger(dispatchingLogger);
182+
view.logger(dispatchingLogger);
175183

176184
const debug = (window as any).VEGA_DEBUG;
177185
debug.view = view;
@@ -284,7 +292,8 @@ class Editor extends React.PureComponent<Props, State> {
284292
!deepEqual(prevProps.logLevel, this.props.logLevel) ||
285293
!deepEqual(prevProps.mode, this.props.mode) ||
286294
!deepEqual(prevProps.hoverEnable, this.props.hoverEnable) ||
287-
!deepEqual(prevProps.tooltipEnable, this.props.tooltipEnable)
295+
!deepEqual(prevProps.tooltipEnable, this.props.tooltipEnable) ||
296+
prevProps.expressionInterpreter !== this.props.expressionInterpreter
288297
) {
289298
this.initView();
290299
}

src/components/sidebar/index.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export function mapStateToProps(state: State) {
1111
renderer: state.renderer,
1212
tooltipEnable: state.tooltipEnable,
1313
backgroundColor: state.backgroundColor,
14+
expressionInterpreter: state.expressionInterpreter,
1415
};
1516
}
1617

@@ -23,6 +24,7 @@ export function mapDispatchToProps(dispatch: Dispatch<EditorActions.Action>) {
2324
setSettingsState: EditorActions.setSettingsState,
2425
setTooltip: EditorActions.setTooltip,
2526
setBackgroundColor: EditorActions.setBackgroundColor,
27+
setExpressionInterpreter: EditorActions.setExpressionInterpreter,
2628
},
2729
dispatch,
2830
);

src/components/sidebar/renderer.tsx

+25
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ class Sidebar extends Component<any, any> {
103103
setLogLevel,
104104
setTooltip,
105105
tooltipEnable,
106+
expressionInterpreter,
107+
setExpressionInterpreter,
106108
} = this.props;
107109

108110
const hover = typeof this.props.hoverEnable !== 'boolean' ? 'Auto' : this.props.hoverEnable ? 'On' : 'Off';
@@ -200,6 +202,29 @@ class Sidebar extends Component<any, any> {
200202
</a>{' '}
201203
handler.
202204
</p>
205+
<div className="expression-interpreter">
206+
<label>
207+
<input
208+
onChange={(e) => setExpressionInterpreter(e.target.checked)}
209+
type="checkbox"
210+
name=""
211+
id="expressionInterpreter"
212+
checked={expressionInterpreter}
213+
/>
214+
Expression Interpreter
215+
</label>
216+
</div>
217+
<p className="settings-description">
218+
Enables the{' '}
219+
<a href="https://vega.github.io/vega/usage/interpreter/" rel="noopener noreferrer" target="_blank">
220+
Expression Interpreter
221+
</a>
222+
, which is{' '}
223+
<a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP" rel="noopener noreferrer" target="_blank">
224+
CSP
225+
</a>{' '}
226+
compliant.
227+
</p>
203228
</div>
204229
);
205230
}

src/constants/default-state.ts

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ export type State = {
6262
debugs: string[];
6363
themeName: string;
6464
backgroundColor: string;
65+
/** https://vega.github.io/vega/usage/interpreter/ */
66+
expressionInterpreter: boolean;
6567
} & typeof dataflowInitialState;
6668

6769
export const DEFAULT_STATE: State = {
@@ -110,5 +112,6 @@ export const DEFAULT_STATE: State = {
110112
debugs: [],
111113
infos: [],
112114
backgroundColor: '#ffffff',
115+
expressionInterpreter: false,
113116
...dataflowInitialState,
114117
};

src/reducers/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
SET_VEGA_EXAMPLE,
4444
SET_VEGA_LITE_EXAMPLE,
4545
SET_VIEW,
46+
SET_EXPRESSION_INTERPRETER,
4647
SHOW_LOGS,
4748
TOGGLE_AUTO_PARSE,
4849
TOGGLE_COMPILED_VEGA_SPEC,
@@ -328,6 +329,7 @@ export default (state: State = DEFAULT_STATE, action: Action): State => {
328329
warns: [],
329330
infos: [],
330331
debugs: [],
332+
expressionInterpreter: false,
331333
};
332334
case SET_MODE_ONLY:
333335
return {
@@ -528,6 +530,13 @@ export default (state: State = DEFAULT_STATE, action: Action): State => {
528530
...state,
529531
editorFocus: action.editorFocus,
530532
};
533+
534+
case SET_EXPRESSION_INTERPRETER:
535+
return {
536+
...state,
537+
expressionInterpreter: action.expressionInterpreter,
538+
};
539+
531540
case RECEIVE_CURRENT_USER:
532541
return {
533542
...state,

0 commit comments

Comments
 (0)