2021-12-01 18:41:49 +00:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import { action } from '@ember/object';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @module JsonEditor
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```js
|
|
|
|
* <JsonEditor @title="Policy" @value={{codemirror.string}} @valueUpdated={{ action "codemirrorUpdate"}} />
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param {string} [title] - Name above codemirror view
|
|
|
|
* @param {string} value - a specific string the comes from codemirror. It's the value inside the codemirror display
|
|
|
|
* @param {Function} [valueUpdated] - action to preform when you edit the codemirror value.
|
|
|
|
* @param {Function} [onFocusOut] - action to preform when you focus out of codemirror.
|
|
|
|
* @param {string} [helpText] - helper text.
|
|
|
|
* @param {object} [options] - option object that overrides codemirror default options such as the styling.
|
|
|
|
*/
|
2021-05-06 16:59:15 +00:00
|
|
|
|
2018-04-03 14:16:57 +00:00
|
|
|
const JSON_EDITOR_DEFAULTS = {
|
|
|
|
// IMPORTANT: `gutters` must come before `lint` since the presence of
|
|
|
|
// `gutters` is cached internally when `lint` is toggled
|
|
|
|
gutters: ['CodeMirror-lint-markers'],
|
|
|
|
tabSize: 2,
|
|
|
|
mode: 'application/json',
|
|
|
|
lineNumbers: true,
|
|
|
|
lint: { lintOnChange: false },
|
|
|
|
theme: 'hashi',
|
|
|
|
readOnly: false,
|
2018-08-16 16:07:19 +00:00
|
|
|
showCursorWhenSelecting: true,
|
2018-04-03 14:16:57 +00:00
|
|
|
};
|
|
|
|
|
2021-12-01 18:41:49 +00:00
|
|
|
export default class JsonEditorComponent extends Component {
|
|
|
|
value = null;
|
|
|
|
valueUpdated = null;
|
|
|
|
onFocusOut = null;
|
|
|
|
readOnly = false;
|
|
|
|
options = null;
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2021-12-01 18:41:49 +00:00
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
this.options = { ...JSON_EDITOR_DEFAULTS, ...this.args.options };
|
2021-05-06 16:59:15 +00:00
|
|
|
if (this.options.autoHeight) {
|
|
|
|
this.options.viewportMargin = Infinity;
|
|
|
|
delete this.options.autoHeight;
|
2018-04-03 14:16:57 +00:00
|
|
|
}
|
2021-05-06 16:59:15 +00:00
|
|
|
if (this.options.readOnly) {
|
|
|
|
this.options.readOnly = 'nocursor';
|
|
|
|
this.options.lineNumbers = false;
|
|
|
|
delete this.options.gutters;
|
|
|
|
}
|
2021-12-01 18:41:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get getShowToolbar() {
|
|
|
|
return this.args.showToolbar === false ? false : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
updateValue(...args) {
|
|
|
|
if (this.args.valueUpdated) {
|
|
|
|
this.args.valueUpdated(...args);
|
|
|
|
}
|
|
|
|
}
|
2021-05-06 16:59:15 +00:00
|
|
|
|
2021-12-01 18:41:49 +00:00
|
|
|
@action
|
|
|
|
onFocus(...args) {
|
|
|
|
if (this.args.onFocusOut) {
|
|
|
|
this.args.onFocusOut(...args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|