Skip to content

Commit

Permalink
replace $copyText with copyTextToClipboard function
Browse files Browse the repository at this point in the history
Signed-off-by: andy.lee <andy.lee@suse.com>
(cherry picked from commit dc75bb9)
  • Loading branch information
a110605 committed Sep 12, 2024
1 parent 950139e commit 6cd01a5
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 65 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"ansi_up": "5.0.0",
"babel-plugin-module-resolver": "4.0.0",
"browser-env": "3.3.0",
"clipboard-polyfill": "4.0.1",
"cookie": "0.5.0",
"cookie-universal-nuxt": "2.1.4",
"cron-validator": "1.2.0",
Expand Down Expand Up @@ -113,7 +114,6 @@
"url-parse": "1.5.10",
"v-tooltip": "2.0.3",
"vue-client-only": "2.1.0",
"vue-clipboard2": "0.3.1",
"vue-codemirror": "4.0.6",
"vue-js-modal": "1.3.35",
"vue-meta": "2.4.0",
Expand Down
8 changes: 6 additions & 2 deletions shell/components/CopyCode.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<script>
import { isArray } from '@shell/utils/array';
import { copyTextToClipboard } from '@shell/utils/clipboard';
import { exceptionToErrorsArray } from '@shell/utils/error';
function flatten(node) {
if ( node.text ) {
Expand Down Expand Up @@ -27,14 +29,16 @@ export default {
const content = flatten(this.$slots.default).trim();
this.$copyText(content).then(() => {
copyTextToClipboard(content).then(() => {
this.copied = true;
setTimeout(() => {
this.copied = false;
}, 2000);
this.$emit('copied');
}).catch((e) => {
this.$emit('error', exceptionToErrorsArray(e));
});
this.$emit('copied');
},
},
Expand Down
3 changes: 2 additions & 1 deletion shell/components/CopyToClipboard.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script>
import AsyncButton from '@shell/components/AsyncButton';
import { copyTextToClipboard } from '@shell/utils/clipboard';
export default {
components: { AsyncButton },
Expand All @@ -18,7 +19,7 @@ export default {
methods: {
clicked(buttonCb) {
this.$copyText(this.text).then(() => {
copyTextToClipboard(this.text).then(() => {
buttonCb(true);
}).catch(() => {
buttonCb(false);
Expand Down
23 changes: 14 additions & 9 deletions shell/components/CopyToClipboardText.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<script>
import { copyTextToClipboard } from '@shell/utils/clipboard';
import { exceptionToErrorsArray } from '@shell/utils/error';
export default {
props: {
text: {
Expand All @@ -20,17 +22,20 @@ export default {
clicked(event) {
if (!this.copied) {
event.preventDefault();
this.$copyText(this.text);
this.copied = true;
copyTextToClipboard(this.text).then(() => {
this.copied = true;
let t = event.target;
let t = event.target;
if (t.tagName === 'I') {
t = t.parentElement || t;
}
setTimeout(() => {
this.copied = false;
}, 500);
if (t.tagName === 'I') {
t = t.parentElement || t;
}
setTimeout(() => {
this.copied = false;
}, 500);
}).catch((e) => {
this.$emit('error', exceptionToErrorsArray(e));
});
}
},
}
Expand Down
7 changes: 6 additions & 1 deletion shell/components/StatusTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {
LAST_UPDATED, TYPE, REASON, MESSAGE, STATUS
} from '@shell/config/table-headers';
import SortableTable from '@shell/components/SortableTable';
import { copyTextToClipboard } from '@shell/utils/clipboard';
import { exceptionToErrorsArray } from '@shell/utils/error';
export default {
components: { SortableTable },
props: {
Expand Down Expand Up @@ -31,12 +34,14 @@ export default {
$event.stopPropagation();
$event.preventDefault();
this.$copyText(this.$slots.default[0].text).then(() => {
copyTextToClipboard(this.$slots.default[0].text).then(() => {
this.copied = true;
setTimeout(() => {
this.copied = false;
}, 2000);
}).catch((e) => {
this.$emit('error', exceptionToErrorsArray(e));
});
},
}
Expand Down
9 changes: 5 additions & 4 deletions shell/components/__tests__/CopyCode.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { mount } from '@vue/test-utils';
import CopyCode from '@shell/components/CopyCode.vue';

jest.mock('@shell/utils/clipboard', () => {
return { copyTextToClipboard: jest.fn(() => Promise.resolve({})) };
});

describe('component: CopyCode', () => {
it('should emit copied after click', async() => {
const wrapper = mount(CopyCode, {
mocks: { $copyText: () => new Promise(() => undefined) },
slots: { default: '<div></div>' }
});
const wrapper = mount(CopyCode, { slots: { default: '<div></div>' } });

await wrapper.find('code').trigger('click');

Expand Down
4 changes: 3 additions & 1 deletion shell/components/form/Password.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { mapGetters } from 'vuex';
import { LabeledInput } from '@components/Form/LabeledInput';
import { CHARSET, randomStr } from '@shell/utils/string';
import { _CREATE } from '@shell/config/query-params';
import { copyTextToClipboard } from '@shell/utils/clipboard';
export default {
components: { LabeledInput },
Expand Down Expand Up @@ -80,6 +81,7 @@ export default {
}
},
methods: {
copyTextToClipboard,
generatePassword() {
this.password = randomStr(16, CHARSET.ALPHA_NUM);
},
Expand Down Expand Up @@ -115,7 +117,7 @@ export default {
>
<a
href="#"
@click.prevent.stop="$copyText(password)"
@click.prevent.stop="copyTextToClipboard(password)"
>{{ t('action.copy') }}</a>
</div>
<div
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { mount } from '@vue/test-utils';
import CustomCommand from '@shell/edit/provisioning.cattle.io.cluster/CustomCommand.vue';

jest.mock('@shell/utils/clipboard', () => {
return { copyTextToClipboard: jest.fn(() => Promise.resolve({})) };
});

describe('component: CustomCommand', () => {
it('should return linux commands with the right flags based on cluster information', () => {
const token = 'MY_TOKEN';
Expand Down
1 change: 0 additions & 1 deletion shell/initialize/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import pluginsLoader from '../core/plugins-loader.js';
import axiosShell from '../plugins/axios';
import '../plugins/tooltip';
import '../plugins/clean-tooltip-directive';
import '../plugins/vue-clipboard2';
import '../plugins/v-select';
import '../plugins/directives';
import '../plugins/clean-html-directive';
Expand Down
10 changes: 7 additions & 3 deletions shell/models/management.cattle.io.cluster.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Vue from 'vue';
import { CATALOG, CLUSTER_BADGE } from '@shell/config/labels-annotations';
import { NODE, FLEET, MANAGEMENT, CAPI } from '@shell/config/types';
import { insertAt } from '@shell/utils/array';
Expand All @@ -14,6 +13,7 @@ import { isHarvesterCluster } from '@shell/utils/cluster';
import HybridModel from '@shell/plugins/steve/hybrid-class';
import { LINUX, WINDOWS } from '@shell/store/catalog';
import { KONTAINER_TO_DRIVER } from './management.cattle.io.kontainerdriver';
import { copyTextToClipboard } from '@shell/utils/clipboard';

// See translation file cluster.providers for list of providers
// If the logo is not named with the provider name, add an override here
Expand Down Expand Up @@ -396,9 +396,13 @@ export default class MgmtCluster extends HybridModel {
}

async copyKubeConfig() {
const config = await this.generateKubeConfig();
try {
const config = await this.generateKubeConfig();

Vue.prototype.$copyText(config);
if (config) {
await copyTextToClipboard(config);
}
} catch {}
}

async fetchNodeMetrics() {
Expand Down
2 changes: 1 addition & 1 deletion shell/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"babel-plugin-module-resolver": "4.0.0",
"babel-preset-vue": "2.0.2",
"browser-env": "3.3.0",
"clipboard-polyfill": "4.0.1",
"cookie": "0.5.0",
"cookie-universal-nuxt": "2.1.4",
"core-js": "3.21.1",
Expand Down Expand Up @@ -122,7 +123,6 @@
"url-parse": "1.5.10",
"v-tooltip": "2.0.3",
"vue": "2.7.14",
"vue-clipboard2": "0.3.1",
"vue-codemirror": "4.0.6",
"vue-js-modal": "1.3.35",
"vue-resize": "0.4.5",
Expand Down
4 changes: 0 additions & 4 deletions shell/plugins/vue-clipboard2.js

This file was deleted.

5 changes: 5 additions & 0 deletions shell/utils/clipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as Clipboard from 'clipboard-polyfill';

export async function copyTextToClipboard(text) {
await Clipboard.writeText(text);
}
41 changes: 4 additions & 37 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5024,14 +5024,10 @@ cli-width@^3.0.0:
resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-3.0.0.tgz#a2f48437a2caa9a22436e794bf071ec9e61cedf6"
integrity sha512-FxqpkPPwu1HjuN93Omfm4h8uIanXofW0RxVEW3k5RKx+mJJYSthzNhp32Kzxxy3YAEZ/Dc/EWN1vZRY0+kOhbw==

clipboard@^2.0.0:
version "2.0.11"
resolved "https://registry.yarnpkg.com/clipboard/-/clipboard-2.0.11.tgz#62180360b97dd668b6b3a84ec226975762a70be5"
integrity sha512-C+0bbOqkezLIsmWSvlsXS0Q0bmkugu7jcfMIACB+RDEntIzQIkdr148we28AfSloQLRdZlYL/QYyrq05j/3Faw==
dependencies:
good-listener "^1.2.2"
select "^1.1.2"
tiny-emitter "^2.0.0"
clipboard-polyfill@4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/clipboard-polyfill/-/clipboard-polyfill-4.0.1.tgz#8bff9da4b37565d4fac9dd85249c65bd24f3d1a8"
integrity sha512-oOxooaJd9dzy78jk1fw4IL+J0SiBn+W24KqUYUwsr0OWEsb5aZlKAWglr9Fg/XEFQMT3ZmJ2AnRoRMZZOMcOQQ==

clipboardy@^2.3.0:
version "2.3.0"
Expand Down Expand Up @@ -6582,11 +6578,6 @@ delayed-stream@~1.0.0:
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==

delegate@^3.1.2:
version "3.2.0"
resolved "https://registry.yarnpkg.com/delegate/-/delegate-3.2.0.tgz#b66b71c3158522e8ab5744f720d8ca0c2af59166"
integrity sha512-IofjkYBZaZivn0V8nnsMJGBr4jVLxHDheKSW88PyxS5QC4Vo9ZbZVvhzlSxY87fVq3STR6r+4cGepyHkcWOQSw==

depd@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/depd/-/depd-2.0.0.tgz#b696163cc757560d09cf22cc8fad1571b79e76df"
Expand Down Expand Up @@ -8486,13 +8477,6 @@ globby@^9.2.0:
pify "^4.0.1"
slash "^2.0.0"

good-listener@^1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/good-listener/-/good-listener-1.2.2.tgz#d53b30cdf9313dffb7dc9a0d477096aa6d145c50"
integrity sha512-goW1b+d9q/HIwbVYZzZ6SsTr4IgE+WA44A0GmPIQstuOrgsFcT7VEJ48nmr9GaRtNu0XTKacFLGnBPAM6Afouw==
dependencies:
delegate "^3.1.2"

gopd@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/gopd/-/gopd-1.0.1.tgz#29ff76de69dac7489b7c0918a5788e56477c332c"
Expand Down Expand Up @@ -13533,11 +13517,6 @@ select-hose@^2.0.0:
resolved "https://registry.yarnpkg.com/select-hose/-/select-hose-2.0.0.tgz#625d8658f865af43ec962bfc376a37359a4994ca"
integrity sha512-mEugaLK+YfkijB4fx0e6kImuJdCIt2LxCRcbEYPqRGCs4F2ogyfZU5IAZRdjCP8JPq2AtdNoC/Dux63d9Kiryg==

select@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/select/-/select-1.1.2.tgz#0e7350acdec80b1108528786ec1d4418d11b396d"
integrity sha512-OwpTSOfy6xSs1+pwcNrv0RBMOzI39Lp3qQKUTPVVPRjCdNa5JH/oPRiqsesIskK8TVgmRiHwO4KXlV2Li9dANA==

selfsigned@^1.10.8:
version "1.10.14"
resolved "https://registry.yarnpkg.com/selfsigned/-/selfsigned-1.10.14.tgz#ee51d84d9dcecc61e07e4aba34f229ab525c1574"
Expand Down Expand Up @@ -14544,11 +14523,6 @@ timsort@^0.3.0:
resolved "https://registry.yarnpkg.com/timsort/-/timsort-0.3.0.tgz#405411a8e7e6339fe64db9a234de11dc31e02bd4"
integrity sha512-qsdtZH+vMoCARQtyod4imc2nIJwg9Cc7lPRrw9CzF8ZKR0khdr8+2nX80PBhET3tcyTtJDxAffGh2rXH4tyU8A==

tiny-emitter@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/tiny-emitter/-/tiny-emitter-2.1.0.tgz#1d1a56edfc51c43e863cbb5382a72330e3555423"
integrity sha512-NB6Dk1A9xgQPMoGqC5CVXn123gWyte215ONT5Pp5a0yt4nlEoO1ZWeCwpncaekPHXO60i47ihFnZPiRPjRMq4Q==

tmp@^0.0.33:
version "0.0.33"
resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.0.33.tgz#6d34335889768d21b2bcda0aa277ced3b1bfadf9"
Expand Down Expand Up @@ -15173,13 +15147,6 @@ vue-client-only@2.1.0:
resolved "https://registry.yarnpkg.com/vue-client-only/-/vue-client-only-2.1.0.tgz#1a67a47b8ecacfa86d75830173fffee3bf8a4ee3"
integrity sha512-vKl1skEKn8EK9f8P2ZzhRnuaRHLHrlt1sbRmazlvsx6EiC3A8oWF8YCBrMJzoN+W3OnElwIGbVjsx6/xelY1AA==

vue-clipboard2@0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/vue-clipboard2/-/vue-clipboard2-0.3.1.tgz#6e551fb7bd384889b28b0da3b12289ed6bca4894"
integrity sha512-H5S/agEDj0kXjUb5GP2c0hCzIXWRBygaWLN3NEFsaI9I3uWin778SFEMt8QRXiPG+7anyjqWiw2lqcxWUSfkYg==
dependencies:
clipboard "^2.0.0"

vue-codemirror@4.0.6:
version "4.0.6"
resolved "https://registry.yarnpkg.com/vue-codemirror/-/vue-codemirror-4.0.6.tgz#b786bb80d8d762a93aab8e46f79a81006f0437c4"
Expand Down

0 comments on commit 6cd01a5

Please sign in to comment.