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

KVv2 json cursor jumps on "enter" #27569

Merged
merged 37 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
ee027c2
it works...but does it break everything else?
Monkeychip Jun 21, 2024
3bf188c
Update code-mirror.js
Monkeychip Jun 21, 2024
7b2413f
Update code-mirror.js
Monkeychip Jun 21, 2024
9d3351c
return to original
Monkeychip Jun 21, 2024
033e8b7
Merge branch 'main' into ui/VAULT-28328/kv-cursor-jump-saga-cont
Monkeychip Jun 21, 2024
877abc1
changelog
Monkeychip Jun 21, 2024
e47758f
Merge branch 'main' into ui/VAULT-28328/kv-cursor-jump-saga-cont
Monkeychip Jun 25, 2024
c081fe1
Merge branch 'main' into ui/VAULT-28328/kv-cursor-jump-saga-cont
Monkeychip Jun 27, 2024
b058b82
different approach to move onto parse at create and edit. it breaks t…
Monkeychip Jul 1, 2024
1c63043
use onBlur event on codemirrror
Monkeychip Jul 1, 2024
f64de9f
maybe? lets run the tests and find out
Monkeychip Jul 1, 2024
731db27
update comments
Monkeychip Jul 1, 2024
a861f36
wip for conditional to only compare on kvv2
Monkeychip Jul 2, 2024
8998b98
remove onblur leftovers
Monkeychip Jul 2, 2024
fc08596
missed two
Monkeychip Jul 2, 2024
16ddc25
clean up
Monkeychip Jul 2, 2024
54ffc48
test coverage
Monkeychip Jul 2, 2024
5c0bea6
try catch logical operator instead
Monkeychip Jul 3, 2024
74e4e96
stringify helper and not native json stringify to maintain object shape
Monkeychip Jul 3, 2024
5e5c7cb
remove comment
Monkeychip Jul 3, 2024
f721028
Update json-editor.js
Monkeychip Jul 3, 2024
229ca6a
Update json-editor.js
Monkeychip Jul 3, 2024
243d378
Update json-editor.js
Monkeychip Jul 3, 2024
3b19547
Merge branch 'main' into ui/VAULT-28328/kv-cursor-jump-saga-cont
Monkeychip Jul 3, 2024
c1e902b
Test fix
Monkeychip Jul 3, 2024
c2a7627
maybe
Monkeychip Jul 3, 2024
a732021
more specific cursor test
Monkeychip Jul 3, 2024
29fd0e6
json-editor test cleanup
Monkeychip Jul 3, 2024
0c7cc7e
Delete ui/testrun1.txt
Monkeychip Jul 3, 2024
1851165
Delete ui/testrun2.txt
Monkeychip Jul 3, 2024
8dcb08c
Merge branch 'main' into ui/VAULT-28328/kv-cursor-jump-saga-cont
Monkeychip Jul 3, 2024
5a905c0
remove non json test it doesn't test anything
Monkeychip Jul 3, 2024
5d57ed3
update test and comment for how it's testing non-json content
Monkeychip Jul 5, 2024
5dc9165
test fix
Monkeychip Jul 5, 2024
a2dcf28
put shape of json blob back:
Monkeychip Jul 5, 2024
bffd06c
send in original without parsing or stringify
Monkeychip Jul 5, 2024
a049d43
welp friday things
Monkeychip Jul 5, 2024
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
3 changes: 3 additions & 0 deletions changelog/27569.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
ui: Fix cursor jump on KVv2 json editor that would occur after pressing ENTER.
```
17 changes: 15 additions & 2 deletions ui/lib/core/addon/modifiers/code-mirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { action } from '@ember/object';
import { bind } from '@ember/runloop';
import codemirror from 'codemirror';
import Modifier from 'ember-modifier';
import { stringify } from 'core/helpers/stringify';

import 'codemirror/addon/edit/matchbrackets';
import 'codemirror/addon/selection/active-line';
Expand All @@ -26,8 +27,20 @@ export default class CodeMirrorModifier extends Modifier {
} else {
// this hook also fires any time there is a change to tracked state
this._editor.setOption('readOnly', namedArgs.readOnly);
if (namedArgs.content && this._editor.getValue() !== namedArgs.content) {
this._editor.setValue(namedArgs.content);
let value = this._editor.getValue();
let content = namedArgs.content;
if (!content) return;
try {
// First parse json to make white space and line breaks consistent between the two items,
Monkeychip marked this conversation as resolved.
Show resolved Hide resolved
// then stringify so they can be compared.
// We use the stringify helper so we do not flatten the json object
value = stringify([JSON.parse(value)], {});
content = stringify([JSON.parse(content)], {});
} catch {
// this catch will occur for non-json content when the mode is not javascript (e.g. ruby).
}
if (value !== content) {
this._editor.setValue(content);
}
}
}
Expand Down
Monkeychip marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@
/* eslint-disable no-useless-escape */
import { module, test } from 'qunit';
import { v4 as uuidv4 } from 'uuid';
import { click, currentURL, fillIn, findAll, setupOnerror, typeIn, visit } from '@ember/test-helpers';
import {
click,
currentURL,
fillIn,
findAll,
setupOnerror,
typeIn,
visit,
triggerKeyEvent,
} from '@ember/test-helpers';
import { setupApplicationTest } from 'vault/tests/helpers';
import authPage from 'vault/tests/pages/auth';
import {
Expand All @@ -24,6 +33,7 @@ import {
} from 'vault/tests/helpers/kv/policy-generator';
import { clearRecords, writeSecret, writeVersionedSecret } from 'vault/tests/helpers/kv/kv-run-commands';
import { FORM, PAGE } from 'vault/tests/helpers/kv/kv-selectors';
import { GENERAL } from 'vault/tests/helpers/general-selectors';
import codemirror from 'vault/tests/helpers/codemirror';

/**
Expand Down Expand Up @@ -309,6 +319,19 @@ module('Acceptance | kv-v2 workflow | edge cases', function (hooks) {
assert.false(codemirror().getValue().includes('*'), 'Values are not obscured on edit view');
});

test('on enter the JSON editor cursor goes to the next line', async function (assert) {
Monkeychip marked this conversation as resolved.
Show resolved Hide resolved
// see issue here: https://github.com/hashicorp/vault/issues/27524
const predictedCursorPosition = JSON.stringify({ line: 3, ch: 0, sticky: null });
await visit(`/vault/secrets/${this.backend}/kv/create`);
await fillIn(FORM.inputByAttr('path'), 'json jump');

await click(FORM.toggleJson);
codemirror().setCursor({ line: 2, ch: 1 });
await triggerKeyEvent(GENERAL.codemirrorTextarea, 'keydown', 'Enter');
const actualCursorPosition = JSON.stringify(codemirror().getCursor());
assert.strictEqual(actualCursorPosition, predictedCursorPosition, 'the cursor stayed on the next line');
});

test('viewing advanced secret data versions displays the correct version data', async function (assert) {
assert.expect(2);
const obscuredDataV1 = `{
Expand Down
2 changes: 2 additions & 0 deletions ui/tests/helpers/general-selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,6 @@ export const GENERAL = {
cancelButton: '[data-test-cancel]',
saveButton: '[data-test-save]',
maskedInput: (name: string) => `[data-test-textarea="${name}"]`,
codemirror: `[data-test-component="code-mirror-modifier"]`,
codemirrorTextarea: `[data-test-component="code-mirror-modifier"] textarea`,
};
28 changes: 22 additions & 6 deletions ui/tests/integration/components/json-editor-test.js
Monkeychip marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ import hbs from 'htmlbars-inline-precompile';
import jsonEditor from '../../pages/components/json-editor';
import sinon from 'sinon';
import { setRunOptions } from 'ember-a11y-testing/test-support';
import codemirror from 'vault/tests/helpers/codemirror';

const component = create(jsonEditor);

module('Integration | Component | json-editor', function (hooks) {
setupRenderingTest(hooks);

const JSON_BLOB = `{
"test": "test"
}`;
"test": "test"
}`;
const BAD_JSON_BLOB = `{
"test": test
}`;
"test": test
}`;

hooks.beforeEach(function () {
this.set('valueUpdated', sinon.spy());
Expand Down Expand Up @@ -122,12 +123,27 @@ module('Integration | Component | json-editor', function (hooks) {
assert.dom('.CodeMirror-code').hasText(`{ "test": "********"}`, 'shows data with obscured values');
assert.dom('[data-test-toggle-input="revealValues"]').isNotChecked('reveal values toggle is unchecked');
await click('[data-test-toggle-input="revealValues"]');
assert.dom('.CodeMirror-code').hasText(JSON_BLOB, 'shows data with real values');
assert.strictEqual(codemirror().getValue(), JSON_BLOB, 'shows data with real values');
Monkeychip marked this conversation as resolved.
Show resolved Hide resolved
assert.dom('[data-test-toggle-input="revealValues"]').isChecked('reveal values toggle is checked');
// turn obscure back on to ensure readonly overrides reveal setting
await click('[data-test-toggle-input="revealValues"]');
this.set('readOnly', false);
assert.dom('[data-test-toggle-input="revealValues"]').doesNotExist('reveal values toggle is hidden');
assert.dom('.CodeMirror-code').hasText(JSON_BLOB, 'shows data with real values on edit mode');
assert.strictEqual(codemirror().getValue(), JSON_BLOB, 'shows data with real values on edit mode');
});

test('code-mirror modifier sets value correctly on non json object', async function (assert) {
this.value = '#A comment';
await render(hbs`
<JsonEditor
@value={{this.value}}
@example={{this.example}}
@mode="ruby"
@valueUpdated={{fn (mut this.value)}}
/>
`);
assert.strictEqual(codemirror().getValue(), `#A comment`, 'shows initial non json value');
codemirror().setValue('#Another comment');
Monkeychip marked this conversation as resolved.
Show resolved Hide resolved
assert.strictEqual(codemirror().getValue(), `#Another comment`, 'shows updated non json value');
});
});
Loading