Skip to content

Commit

Permalink
working on yaml editor (how to prevent from being a focus trap - no s…
Browse files Browse the repository at this point in the history
…olution yet)
  • Loading branch information
aalves08 committed Jan 15, 2025
1 parent f623499 commit 1beaa7a
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 68 deletions.
27 changes: 26 additions & 1 deletion pkg/rancher-components/src/components/Card/Card.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import { defineComponent, PropType } from 'vue';
import { createFocusTrap, FocusTrap } from 'focus-trap';
export default defineComponent({
name: 'Card',
Expand Down Expand Up @@ -50,12 +51,36 @@ export default defineComponent({
type: Boolean,
default: false,
},
}
triggerFocusTrap: {
type: Boolean,
default: false,
},
},
data() {
return { focusTrapInstance: {} as FocusTrap };
},
mounted() {
if (this.triggerFocusTrap) {
this.focusTrapInstance = createFocusTrap(this.$refs.cardContainer as HTMLElement, {
escapeDeactivates: true,
allowOutsideClick: true,
});
this.$nextTick(() => {
this.focusTrapInstance.activate();
});
}
},
beforeUnmount() {
if (this.focusTrapInstance && this.triggerFocusTrap) {
this.focusTrapInstance.deactivate();
}
},
});
</script>

<template>
<div
ref="cardContainer"
class="card-container"
:class="{'highlight-border': showHighlightBorder, 'card-sticky': sticky}"
data-testid="card"
Expand Down
15 changes: 14 additions & 1 deletion shell/components/AppModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,20 @@ export default defineComponent({
},
handleEscapeKey(event: KeyboardEvent) {
if (this.clickToClose && event.key === 'Escape') {
this.$emit('close');
console.warn('ESCAPE KEY PRESSED ON MODAL...');

Check warning on line 106 in shell/components/AppModal.vue

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
// this.$emit('close');
const yamlEditorEl = document.querySelector('#yaml-editor-code-mirror');
const currActiveEl = document.activeElement;
console.log('******* document.activeElement ********', document.activeElement);

Check warning on line 111 in shell/components/AppModal.vue

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
// console.log('******* this.$refs.cm ********', this.$refs.cm);
// console.log('******* "document.querySelector ********', document.querySelector('#yaml-editor-code-mirror'));
console.log('******* contains ********', document.querySelector('#yaml-editor-code-mirror')?.contains(document.activeElement));

Check warning on line 114 in shell/components/AppModal.vue

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
if (!yamlEditorEl || (yamlEditorEl && !yamlEditorEl.contains(currActiveEl))) {
console.error('CLOSING!!!');

Check warning on line 117 in shell/components/AppModal.vue

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
this.$emit('close');
}
}
},
isValidWidth(value: number | string) {
Expand Down
2 changes: 2 additions & 0 deletions shell/components/CodeMirror.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export default {
const theme = this.$store.getters['prefs/theme'];
const keymap = this.$store.getters['prefs/get'](KEYMAP);
console.log('keymap', keymap);

Check warning on line 55 in shell/components/CodeMirror.vue

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
const out = {
// codemirror default options
tabSize: 2,
Expand Down
9 changes: 8 additions & 1 deletion shell/components/Import.vue
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export default {
v-else
:show-highlight-border="false"
data-testid="import-yaml"
:trigger-focus-trap="true"
>
<template #title>
<div style="display: block; width: 100%;">
Expand All @@ -136,7 +137,7 @@ export default {
<FileSelector
class="btn role-secondary pull-left"
:label="t('generic.readFromFile')"
tabindex="0"
:aria-label="t('generic.readFromFile')"
@selected="onFileSelected"
/>
</div>
Expand Down Expand Up @@ -193,6 +194,8 @@ export default {
type="button"
class="btn role-primary"
data-testid="import-yaml-close"
role="button"
:aria-label="t('generic.close')"
@click="close"
>
{{ t('generic.close') }}
Expand All @@ -207,6 +210,8 @@ export default {
type="button"
class="btn role-secondary mr-10"
data-testid="import-yaml-cancel"
role="button"
:aria-label="t('generic.cancel')"
@click="close"
>
{{ t('generic.cancel') }}
Expand All @@ -216,6 +221,8 @@ export default {
mode="import"
:disabled="!currentYaml.length"
data-testid="import-yaml-import-action"
role="button"
:aria-label="t('import.title')"
@click="importYaml"
/>
</div>
Expand Down
38 changes: 33 additions & 5 deletions shell/components/YamlEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ export default {
return [EDITOR_MODES.EDIT_CODE, EDITOR_MODES.VIEW_CODE].includes(this.editorMode);
},
},
watch: {
showUploadPrompt(neu) {
if (neu) {
Expand All @@ -156,10 +155,31 @@ export default {
},
},
mounted() {
document.addEventListener('keydown', this.handleEscapeKey);
},
beforeUnmount() {
document.removeEventListener('keydown', this.handleEscapeKey);
},
methods: {
focus() {
if ( this.$refs.cm ) {
this.$refs.cm.focus();
// focus() {
// if ( this.$refs.cm ) {
// this.$refs.cm.focus();
// }
// },
handleEscapeKey(e) {
if (e.key === 'Escape') {
console.log('******* document.activeElement ON YAML EDITOR ********', document.activeElement);

Check warning on line 174 in shell/components/YamlEditor.vue

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
const yamlEditorEl = this.$refs.cm.$el;
const currActiveEl = document.activeElement;
if (yamlEditorEl.contains(currActiveEl)) {
console.warn('MOVING FOCUS TO OUTER ELEMENT!');

Check warning on line 180 in shell/components/YamlEditor.vue

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
this.$refs.outerYamlEditorElement.focus();
}
}
},
Expand Down Expand Up @@ -204,7 +224,10 @@ export default {
</script>

<template>
<div class="yaml-editor">
<div
ref="outerYamlEditorElement"
class="yaml-editor"
>
<div class="text-right">
<span
v-if="isPreview && !hidePreviewButtons"
Expand All @@ -227,6 +250,7 @@ export default {
</div>
<CodeMirror
v-if="showCodeEditor"
id="yaml-editor-code-mirror"
ref="cm"
:class="{fill: true, scrolling: scrolling}"
:value="curValue"
Expand Down Expand Up @@ -255,6 +279,10 @@ export default {
display: flex;
flex-direction: column;
&:focus {
@include focus-outline;
}
.fill {
flex: 1;
}
Expand Down
4 changes: 4 additions & 0 deletions shell/components/form/LabeledSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,10 @@ export default {
// LabeledSelect.
padding-bottom: 1px;
&:focus-visible {
@include focus-outline;
}
&.no-label.compact-input {
:deep() .vs__actions:after {
top: -2px;
Expand Down
3 changes: 1 addition & 2 deletions shell/components/nav/Header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -542,13 +542,12 @@ export default {
tabindex="0"
:aria-label="t('nav.import')"
@click="openImport()"
@keyup.enter="openImport()"
@keyup.space="openImport()"
>
<i class="icon icon-upload icon-lg" />
</button>
<app-modal
:visibility="showImportModal"
v-if="showImportModal"
class="import-modal"
name="importModal"
width="75%"
Expand Down
29 changes: 0 additions & 29 deletions shell/directives/focus-trap.js

This file was deleted.

2 changes: 0 additions & 2 deletions shell/initialize/install-directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import cleanTooltipDirective from '@shell/directives/clean-tooltip';
import positiveIntNumberDirective from '@shell/directives/positive-int-number.js';
import trimWhitespaceDirective from '@shell/directives/trim-whitespace';
import intNumberDirective from '@shell/directives/int-number';
import focusTrap from '@shell/directives/focus-trap';

/**
* Prevent extensions from overriding existing directives
Expand Down Expand Up @@ -48,7 +47,6 @@ function addDirectives(vueApp) {
vueApp.directive('focus', focusDirective);
vueApp.directive('intNumber', intNumberDirective);
vueApp.directive('positiveIntNumber', positiveIntNumberDirective);
vueApp.directive('trap', focusTrap);
}

/**
Expand Down
33 changes: 8 additions & 25 deletions shell/mixins/vue-select-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,22 @@ export default {
// Defaults found at - https://github.com/sagalbot/vue-select/blob/master/src/components/Select.vue#L947
const out = { ...map };

// tab
(out[9] = (e) => {
// user esc'd
if (!vm.open) {
return;
}

e.preventDefault();

const optsLen = vm.filteredOptions.length;
const typeAheadPointer = vm.typeAheadPointer;

if (e.shiftKey) {
if (typeAheadPointer === 0) {
return vm.onEscape();
}

return vm.typeAheadUp();
}
if (typeAheadPointer + 1 === optsLen) {
return vm.onEscape();
// escape key
(out[27] = (e) => {
// this will prevent propagation to global escape key handler
// that is responsible for closing a modal
// probably we can gate it to a prop...
if (vm.open) {
e.stopPropagation();
}

return vm.typeAheadDown();
});

(out[27] = (e) => {
vm.open = false;
vm.search = '';

return false;
});

// enter key
(out[13] = (e, opt) => {
if (!vm.open) {
vm.open = true;
Expand Down
5 changes: 3 additions & 2 deletions shell/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@
"babel-preset-vue": "2.0.2",
"cache-loader": "4.1.0",
"clipboard-polyfill": "4.0.1",
"color": "4.2.3",
"codemirror": ">=5.64.0 <6",
"codemirror-editor-vue3": "2.7.1",
"color": "4.2.3",
"cookie": "0.7.0",
"cookie-universal": "2.2.2",
"core-js": "3.25.3",
Expand Down Expand Up @@ -90,6 +90,7 @@
"express": "4.17.1",
"file-saver": "2.0.2",
"floating-vue": "5.2.2",
"focus-trap": "^7.6.4",
"frontmatter-markdown-loader": "3.7.0",
"identicon.js": "2.3.3",
"intl-messageformat": "7.8.4",
Expand All @@ -108,9 +109,9 @@
"jszip": "3.8.0",
"lodash": "4.17.21",
"marked": "4.0.17",
"node-polyfill-webpack-plugin": "3.0.0",
"nodemon": "2.0.22",
"nyc": "15.1.0",
"node-polyfill-webpack-plugin": "3.0.0",
"papaparse": "5.3.0",
"portal-vue": "~3.0.0",
"sass": "1.51.0",
Expand Down
12 changes: 12 additions & 0 deletions shell/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7894,6 +7894,13 @@ [email protected]:
"@floating-ui/dom" "~1.1.1"
vue-resize "^2.0.0-alpha.1"

focus-trap@^7.6.4:
version "7.6.4"
resolved "https://registry.npmjs.org/focus-trap/-/focus-trap-7.6.4.tgz#455ec5c51fee5ae99604ca15142409ffbbf84db9"
integrity sha512-xx560wGBk7seZ6y933idtjJQc1l+ck+pI3sKvhKozdBV1dRZoKhkW5xoCaFv9tQiX5RH1xfSxjuNu6g+lmN/gw==
dependencies:
tabbable "^6.2.0"

[email protected], follow-redirects@^1.0.0, follow-redirects@^1.14.0:
version "1.15.2"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13"
Expand Down Expand Up @@ -12648,6 +12655,11 @@ symbol-tree@^3.2.4:
resolved "https://registry.yarnpkg.com/symbol-tree/-/symbol-tree-3.2.4.tgz#430637d248ba77e078883951fb9aa0eed7c63fa2"
integrity sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw==

tabbable@^6.2.0:
version "6.2.0"
resolved "https://registry.npmjs.org/tabbable/-/tabbable-6.2.0.tgz#732fb62bc0175cfcec257330be187dcfba1f3b97"
integrity sha512-Cat63mxsVJlzYvN51JmVXIgNoUokrIaT2zLclCXjRd8boZ0004U4KCs/sToJ75C6sdlByWxpYnb5Boif1VSFew==

table@^6.0.9:
version "6.8.2"
resolved "https://registry.yarnpkg.com/table/-/table-6.8.2.tgz#c5504ccf201213fa227248bdc8c5569716ac6c58"
Expand Down

0 comments on commit 1beaa7a

Please sign in to comment.