Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Ivy Codemirror #14659

Merged
merged 17 commits into from
Mar 29, 2022
3 changes: 3 additions & 0 deletions changelog/14659.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
ui: Replaces the IvyCodemirror wrapper with a custom ember modifier.
```
46 changes: 10 additions & 36 deletions ui/app/components/json-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,24 @@ import { action } from '@ember/object';
* @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.
* @param {Object} [extraKeys] - to provide keyboard shortcut methods for things like saving on shift + enter.
* @param {Array} [gutters] - An array of CSS class names or class name / CSS string pairs, each of which defines a width (and optionally a background), and which will be used to draw the background of the gutters.
* @param {string} [mode] - right now we only import ruby so must mode but be ruby or defaults to javascript. If you wanted another language you need to import it into the modifier.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit on the comment wording. Also, from reading it I'm wondering since ruby and application/json are the only supported modes at this time could the arg change to useRubyMode or something like that? If true the mode is set to ruby otherwise application/json is used. What are your thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a valid question. I think however I prefer mode because it aligns with the codemirror property. It would be easy for someone to look up. I also don't want someone to think they only can use Ruby in the future. They can use lots of other options they just need to import it. They'll immediately error to if they use something other than ruby.

I'll amend the comment wording.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good so if I were to pass in php right now for example codemirror would throw an error since it's not imported?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct, it wouldn't work.

* @param {Boolean} [readOnly] - defaults to false.
* @param {String} [theme] - specify or customize the look via css.
* @param {String} [value] - value within the display.
* @param {String} [viewportMargin] - Size of viewport. Often set to "Infinity" to load/show all text regardless of length.
*/

const JSON_EDITOR_DEFAULTS = {
// IMPORTANT: `gutters` must come before `lint` since the presence of
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved comment to the modifier.

// `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,
showCursorWhenSelecting: true,
};

export default class JsonEditorComponent extends Component {
value = null;
valueUpdated = null;
onFocusOut = null;
readOnly = false;
options = null;

constructor() {
super(...arguments);
this.options = { ...JSON_EDITOR_DEFAULTS, ...this.args.options };
if (this.options.autoHeight) {
this.options.viewportMargin = Infinity;
delete this.options.autoHeight;
}
if (this.options.readOnly) {
this.options.readOnly = 'nocursor';
this.options.lineNumbers = false;
delete this.options.gutters;
}
}

get getShowToolbar() {
return this.args.showToolbar === false ? false : true;
}

@action
updateValue(...args) {
if (this.args.valueUpdated) {
onUpdate(...args) {
if (!this.args.readOnly) {
// catching a situation in which the user is not readOnly and has not provided a valueUpdated function to the instance
this.args.valueUpdated(...args);
}
}
Expand Down
68 changes: 68 additions & 0 deletions ui/app/modifiers/code-mirror.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { action } from '@ember/object';
import { bind } from '@ember/runloop';
import codemirror from 'codemirror';
import Modifier from 'ember-modifier';

import 'codemirror/addon/edit/matchbrackets';
import 'codemirror/addon/selection/active-line';
import 'codemirror/addon/lint/lint.js';
import 'codemirror/addon/lint/json-lint.js';
// right now we only use the ruby and javascript, if you use another mode you'll need to import it.
// https://codemirror.net/mode/
import 'codemirror/mode/ruby/ruby';
import 'codemirror/mode/javascript/javascript';

export default class CodeMirrorModifier extends Modifier {
didInstall() {
this._setup();
}

didUpdateArguments() {
this._editor.setOption('readOnly', this.args.named.readOnly);
if (!this.args.named.content) {
return;
}
if (this._editor.getValue() !== this.args.named.content) {
this._editor.setValue(this.args.named.content);
}
}

@action
_onChange(editor) {
this.args.named.onUpdate(editor.getValue(), this._editor);
}

@action
_onFocus(editor) {
this.args.named.onFocus(editor.getValue());
}

_setup() {
if (!this.element) {
throw new Error('CodeMirror modifier has no element');
}
const editor = codemirror(this.element, {
// IMPORTANT: `gutters` must come before `lint` since the presence of
// `gutters` is cached internally when `lint` is toggled
gutters: this.args.named.gutters || ['CodeMirror-lint-markers'],
matchBrackets: true,
lint: { lintOnChange: true },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous version of JsonEditor had lintOnChange defaulted to false. Just want to verify that we want that as true in the new version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. This was left over from me playing with the linting. I'll change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, strange. I changed back and it broke the linter. Let me investigate a little tomorrow because we are calling performLint() so we shouldn't need this.

showCursorWhenSelecting: true,
styleActiveLine: true,
tabSize: 2,
// all values we can pass into the JsonEditor
extraKeys: this.args.named.extraKeys || '',
lineNumbers: this.args.named.lineNumbers,
mode: this.args.named.mode || 'application/json',
readOnly: this.args.named.readOnly || false,
theme: this.args.named.theme || 'hashi',
value: this.args.named.content || '',
viewportMargin: this.args.named.viewportMargin || '',
});

editor.on('change', bind(this, this._onChange));
editor.on('focus', bind(this, this._onFocus));

this._editor = editor;
}
}
11 changes: 7 additions & 4 deletions ui/app/styles/components/codemirror.scss
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ $gutter-grey: #2a2f36;
.cm-s-hashi {
&.CodeMirror {
background-color: $black !important;
resize: vertical;
resize: vertical;
color: #cfd2d1 !important;
border: none;
font-family: $family-monospace;
Expand Down Expand Up @@ -168,10 +168,14 @@ $gutter-grey: #2a2f36;
}

.readonly-codemirror {
.CodeMirror-cursors {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was not working. We have it working now under a new class that only shows if the editor is readOnly.

.CodeMirror-code {
cursor: default;
}
.CodeMirror-cursor {
// https://github.com/codemirror/CodeMirror/issues/1099
display: none;
}

// css for a specific theme
.cm-s-hashi {
span {
color: $light-grey;
Expand All @@ -195,7 +199,6 @@ $gutter-grey: #2a2f36;
}
}
}

.cm-s-auto-height.CodeMirror {
height: auto;
}
Expand Down
5 changes: 4 additions & 1 deletion ui/app/templates/components/console/log-json.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
<JsonEditor
@showToolbar={{false}}
@value={{stringify this.content}}
@options={{hash readOnly=true lineNumbers=false autoHeight=true gutters=false theme="hashi auto-height"}}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all options objects were spread out so that the individual properties were sent to the json-editor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not seeing autoHeight as an arg. Is the codemirror default true and is that something we won't ever want to set to false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I removed this because before it was used to set the viewportMargin. See here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh ok I see now thanks!

@readOnly={{true}}
@viewportMargin="Infinity"
@gutters={{false}}
@theme="hashi auto-height"
/>
<HoverCopyButton @copyValue={{stringify this.content}} />
</div>
5 changes: 4 additions & 1 deletion ui/app/templates/components/control-group-success.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
data-test-json-viewer
@showToolbar={{false}}
@value={{stringify this.unwrapData}}
@options={{hash readOnly=true lineNumbers=false autoHeight=true gutters=false theme="hashi-read-only auto-height"}}
@readOnly={{true}}
@viewportMargin="Infinity"
@gutters={{false}}
@theme="hashi-read-only auto-height"
/>
<HoverCopyButton @copyValue={{stringify this.unwrapData}} />
</div>
Expand Down
24 changes: 16 additions & 8 deletions ui/app/templates/components/json-editor.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,22 @@
</Toolbar>
</div>
{{/if}}

<IvyCodemirror
@data-test-component="json-editor"
@value={{@value}}
@options={{this.options}}
@valueUpdated={{action "updateValue"}}
@onFocusOut={{action "onFocus"}}
/>
<div
{{code-mirror
content=@value
extraKeys=@extraKeys
gutters=@gutters
lineNumbers=(if @readOnly false true)
mode=@mode
readOnly=@readOnly
theme=@theme
viewportMarg=@viewportMargin
onUpdate=this.onUpdate
onFocus=this.onFocus
}}
class={{if @readOnly "readonly-codemirror"}}
data-test-component="code-mirror-modifier"
></div>

{{#if @helpText}}
<div class="box is-shadowless is-fullwidth has-short-padding">
Expand Down
2 changes: 1 addition & 1 deletion ui/app/templates/components/secret-form-show.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<JsonEditor
@title={{if @isV2 "Version Data" "Secret Data"}}
@value={{@modelForData.dataAsJSONString}}
@options={{hash readOnly=true}}
@readOnly={{true}}
/>
</div>
{{else}}
Expand Down
2 changes: 1 addition & 1 deletion ui/app/templates/components/tool-unwrap.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
{{#if (eq @unwrapActiveTab "data")}}
<div class="field">
<div class="control">
<JsonEditor @title="Unwrapped Data" @value={{stringify @unwrap_data}} @options={{hash readOnly=true}} />
<JsonEditor @title="Unwrapped Data" @value={{stringify @unwrap_data}} @readOnly={{true}} />
</div>
</div>
{{else}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<JsonEditor
@title="Ciphertext"
@valueUpdated={{action (mut @ciphertext)}}
@options={{hash mode="ruby"}}
@mode="ruby"
@data-test-transit-input="ciphertext"
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@title="Plaintext"
@value={{@plaintext}}
@valueUpdated={{action (mut @plaintext)}}
@options={{hash mode="ruby"}}
@mode="ruby"
@data-test-transit-input="plaintext"
/>
</div>
Expand Down
7 changes: 1 addition & 6 deletions ui/app/templates/components/transit-key-action/hmac.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@
<KeyVersionSelect @key={{@key}} @onVersionChange={{action (mut @key_version)}} @key_version={{@key_version}} />
<div class="field">
<div id="input-control" class="control is-relative">
<JsonEditor
@title="Input"
@valueUpdated={{action (mut @input)}}
@options={{hash mode="ruby"}}
@data-test-transit-input="input"
/>
<JsonEditor @title="Input" @valueUpdated={{action (mut @input)}} @mode="ruby" @data-test-transit-input="input" />
</div>
</div>
<div class="field">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<KeyVersionSelect @key={{@key}} @onVersionChange={{action (mut @key_version)}} @key_version={{@key_version}} />
<div class="field">
<div class="control is-expanded">
<JsonEditor @title="Ciphertext" @valueUpdated={{action (mut @ciphertext)}} @options={{hash mode="ruby"}} />
<JsonEditor @title="Ciphertext" @valueUpdated={{action (mut @ciphertext)}} @mode="ruby" />
</div>
</div>
{{#if @key.derived}}
Expand Down
2 changes: 1 addition & 1 deletion ui/app/templates/components/transit-key-action/sign.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
@title="Input"
@value={{@input}}
@valueUpdated={{action (mut @input)}}
@options={{hash mode="ruby"}}
@mode="ruby"
@data-test-transit-input="input"
/>
</div>
Expand Down
13 changes: 4 additions & 9 deletions ui/app/templates/components/transit-key-action/verify.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
@title="Input"
@value={{@input}}
@valueUpdated={{action (mut @input)}}
@options={{hash mode="ruby"}}
@mode="ruby"
@data-test-transit-input="input"
/>
</div>
Expand Down Expand Up @@ -140,12 +140,7 @@
{{#if (or (and @verification (eq @verification "HMAC")) @hmac)}}
<div class="field is-flex-column is-flex-1">
<div class="control is-flex-column is-flex-1">
<JsonEditor
@title="HMAC"
@value={{@hmac}}
@valueUpdated={{action (mut @hmac)}}
@options={{hash mode="ruby"}}
/>
<JsonEditor @title="HMAC" @value={{@hmac}} @valueUpdated={{action (mut @hmac)}} @mode="ruby" />
</div>
</div>
{{else}}
Expand All @@ -155,7 +150,7 @@
@title="Signature"
@value={{@signature}}
@valueUpdated={{action (mut @signature)}}
@options={{hash mode="ruby"}}
@mode="ruby"
/>
</div>
</div>
Expand All @@ -165,7 +160,7 @@
{{else}}
<div class="field">
<div class="control">
<JsonEditor @title="HMAC" @value={{@hmac}} @valueUpdated={{action (mut @hmac)}} @options={{hash mode="ruby"}} />
<JsonEditor @title="HMAC" @value={{@hmac}} @valueUpdated={{action (mut @hmac)}} @mode="ruby" />
</div>
</div>
<div class="field">
Expand Down
3 changes: 2 additions & 1 deletion ui/app/templates/vault/cluster/policies/create.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
@showToolbar={{false}}
@value={{this.model.policy}}
@valueUpdated={{action (mut this.model.policy)}}
@options={{hash mode="ruby" extraKeys=(hash Shift-Enter=(action "savePolicy" this.model))}}
@mode="ruby"
@extraKeys={{hash Shift-Enter=(action "savePolicy" this.model)}}
/>
{{/if}}
</div>
Expand Down
3 changes: 2 additions & 1 deletion ui/app/templates/vault/cluster/policy/edit.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
@title="Policy"
@value={{this.model.policy}}
@valueUpdated={{action (mut this.model.policy)}}
@options={{hash mode="ruby" extraKeys=(hash Shift-Enter=(action "savePolicy" this.model))}}
@mode="ruby"
@extraKeys={{hash Shift-Enter=(action "savePolicy" this.model)}}
/>
<div class="box is-shadowless is-fullwidth has-short-padding">
<p class="help-text has-text-grey-dark is-size-7">
Expand Down
3 changes: 2 additions & 1 deletion ui/app/templates/vault/cluster/policy/show.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
@title="Policy"
@subTitle={{if (eq this.policyType "acl") (concat this.uppercase this.model.format " format")}}
@value={{this.model.policy}}
@options={{hash readOnly=true mode="ruby"}}
@readOnly={{true}}
@mode="ruby"
/>
</div>
{{#if this.model.paths}}
Expand Down
7 changes: 1 addition & 6 deletions ui/ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ const appConfig = {
return `${config.rootURL.replace(/\/$/, '')}${filePath}`;
},
},
codemirror: {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

everything removed from this file was added into the modifier.

modes: ['javascript', 'ruby'],
keyMaps: ['sublime'],
},
babel: {
plugins: ['@babel/plugin-proposal-object-rest-spread', ['inline-json-import', {}]],
},
Expand Down Expand Up @@ -74,8 +70,7 @@ module.exports = function (defaults) {

app.import('node_modules/jsonlint/lib/jsonlint.js');
app.import('node_modules/codemirror/addon/lint/lint.css');
app.import('node_modules/codemirror/addon/lint/lint.js');
app.import('node_modules/codemirror/addon/lint/json-lint.js');
app.import('node_modules/codemirror/lib/codemirror.css');
app.import('node_modules/text-encoder-lite/text-encoder-lite.js');
app.import('node_modules/jsondiffpatch/dist/jsondiffpatch.umd.js');
app.import('node_modules/jsondiffpatch/dist/formatters-styles/html.css');
Expand Down
2 changes: 1 addition & 1 deletion ui/lib/core/addon/templates/components/form-field.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
this.attr.options.defaultValue
}}
@valueUpdated={{action "codemirrorUpdated" this.attr.name "string"}}
@options={{hash theme=(or this.attr.options.theme "hashi")}}
@theme={{or this.attr.options.theme "hashi"}}
@helpText={{this.attr.options.helpText}}
>
{{#if this.attr.options.allowReset}}
Expand Down
Loading