Skip to content

Commit

Permalink
v-clipboard refactoring (#550)
Browse files Browse the repository at this point in the history
  • Loading branch information
GusevPM authored Aug 6, 2023
1 parent 2652cfc commit c43814c
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 60 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"portal-vue": "^2.1.7",
"redoc": "^2.0.0-rc.65",
"sanitize-html": "^2.7.0",
"v-clipboard": "3.0.0-next.1",
"v-clipboard": "^3.0.0-next.1",
"vue": "^2.6.10",
"vue-codemirror": "^4.0.6",
"vue-codemirror-lite": "^1.0.4",
Expand Down
31 changes: 19 additions & 12 deletions src/components/Dialogs/RawJsonViewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
v-bind="attrs"
v-on="on"
v-else
v-clipboard="handleCopyClick"
@click="() => {
copy(handleCopyClick())
}"
class="mr-4 text--secondary"
>
<v-icon small class="mr-1">mdi-content-copy</v-icon>Copy
Expand All @@ -54,9 +56,9 @@
class="px-4"
v-for="(item, index) in ['parameter', 'storage', 'code', 'all']"
:key="index"
@click="() => {}"
v-clipboard="() => getSection(item)"
v-clipboard:success="showClipboardOK"
@click="() => {
copy(getSection(item))
}"
>
<v-list-item-title class="text-capitalize">{{
item
Expand All @@ -67,8 +69,9 @@
<v-btn
v-else
class="mr-4 text--secondary"
v-clipboard="() => JSON.stringify(data, null, ' ')"
v-clipboard:success="showClipboardOK"
@click="() => {
copy(JSON.stringify(data, null, ' '))
}"
text
>
<v-icon small class="mr-1">mdi-content-copy</v-icon>Copy
Expand Down Expand Up @@ -108,6 +111,7 @@
import { mapActions } from "vuex";
import VueJsonPretty from "vue-json-pretty";
import {keysToCamel} from "../../utils/object";
import { copyToClipboard } from "../../utils/clipboard";
import '@/styles/vue-json-pretty.css';
const BIG_SIZE_JSON_SYMBOLS = 10000;
Expand Down Expand Up @@ -151,7 +155,7 @@ export default {
renderingStep: 1,
}),
methods: {
...mapActions(["showError", "showClipboardOK"]),
...mapActions(["showError", "showClipboardOK", "showClipboardFail"]),
close() {
this.$emit("input", false);
},
Expand All @@ -160,6 +164,9 @@ export default {
return this.raw
}
},
copy(text, successMessage, failMessage) {
copyToClipboard(text, this.showClipboardOK.bind(null, successMessage), this.showClipboardFail.bind(null, failMessage));
},
isTokenMetadata() {
return this.type.toLowerCase() === 'token metadata';
},
Expand All @@ -170,11 +177,11 @@ export default {
if (section && Array.isArray(this.data)) {
for (var i = 0; i < this.data.length; i++) {
if (this.data[i].prim === section) {
return this.data[i];
return JSON.stringify(this.data[i]);
}
}
}
return this.data;
return JSON.stringify(this.data);
},
parseData(data) {
return this.isTokenMetadata() ? keysToCamel(data) : data;
Expand Down Expand Up @@ -213,9 +220,9 @@ export default {
}
},
updated() {
if (this.show) {
this.$refs.rawJsonDialog.show();
}
if (this.show) {
this.$refs.rawJsonDialog.show();
}
},
watch: {
show(newValue) {
Expand Down
32 changes: 15 additions & 17 deletions src/components/ValueInspector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
readonly
:label="label"
:value="value"
@click="copyText(value)"
@click="() => {
copy(value)
}"
></v-textarea>
<v-text-field
v-else
Expand All @@ -19,14 +21,18 @@
readonly
filled
:label="label"
@click="copyText(value)"
@click="() => {
copy(value)
}"
></v-text-field>

<v-btn
text
small
link
@click.prevent.stop="copyText(value)"
@click.prevent.stop="() => {
copy(value)
}"
:class="isPreFormatted ? 'pl-0 mt-4' : ''"
>
<v-icon small class="mr-1">mdi-content-copy</v-icon>
Expand Down Expand Up @@ -80,10 +86,11 @@
</template>

<script>
import Michelson from "@/components/Michelson.vue";
import {mapActions} from "vuex";
import isIpfs from "is-ipfs";
import Michelson from "@/components/Michelson.vue";
import { copyToClipboard } from "@/utils/clipboard";
import { checkAddress, matchAddress, isSrAddress } from "@/utils/tz.js";
import {mapActions} from "vuex";
export default {
name: "ValueInspector",
Expand Down Expand Up @@ -122,18 +129,9 @@ export default {
},
},
methods: {
...mapActions(["showClipboardOK"]),
copyText(text) {
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(text).then(() => this.showClipboardOK());
} else {
try {
window.clipboardData.setData("Text", text);
this.showClipboardOK();
} catch (e) {
//
}
}
...mapActions(["showClipboardOK", "showClipboardFail"]),
copy(text, successMessage, failMessage) {
copyToClipboard(text, this.showClipboardOK.bind(null, successMessage), this.showClipboardFail.bind(null, failMessage));
},
handleAddress(span) {
const address = matchAddress(span);
Expand Down
12 changes: 10 additions & 2 deletions src/components/schema/schemaDialog/SchemaCmdLine.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
<v-spacer></v-spacer>
<v-btn
class="mr-4 text--secondary"
v-clipboard="() => cmdLine"
v-clipboard:success="showClipboardOk"
@click="() => {
copy(cmdLine)
}"
text
>
<v-icon small class="mr-1">mdi-content-copy</v-icon>Copy
Expand All @@ -34,6 +35,9 @@
</template>

<script>
import {mapActions} from "vuex";
import { copyToClipboard } from "@/utils/clipboard";
export default {
name: "SchemaCmdLine",
props: {
Expand Down Expand Up @@ -81,6 +85,10 @@ export default {
}
},
methods: {
...mapActions(["showClipboardOK", "showClipboardFail"]),
copy(text, successMessage, failMessage) {
copyToClipboard(text, this.showClipboardOK.bind(null, successMessage), this.showClipboardFail.bind(null, failMessage));
},
isNumber(evt) {
const charCode = (evt.which) ? evt.which : evt.keyCode;
if ((charCode > 31 && (charCode < 48 || charCode > 57)) && charCode !== 46) {
Expand Down
25 changes: 17 additions & 8 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,23 @@ export default new Vuex.Store({
showWarning({ commit }, value) {
commit('setWarning', value);
},
showClipboardWarning({ commit }) {
commit('setWarning', 'Copied! Text might be truncated due to its size');
},
showClipboardOK({ commit }) {
commit('setSuccess', 'Copied to the clipboard')
},
showClipboardFail({ dispatch}) {
dispatch('showError', 'Failed to save to the clipboard')
showClipboardWarning({ commit }, text) {
if (!text) {
text = 'Copied! Text might be truncated due to its size!'
}
commit('setWarning', text);
},
showClipboardOK({ commit }, text) {
if (!text) {
text = 'Copied to the clipboard!'
}
commit('setSuccess', text)
},
showClipboardFail({ dispatch}, text) {
if (!text) {
text = 'Failed to save to the clipboard!'
}
dispatch('showError', text)
},
showSuccess({ commit }, value) {
commit('setSuccess', value)
Expand Down
9 changes: 9 additions & 0 deletions src/utils/clipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function copyToClipboard(text, showOK, showFail) {
navigator.clipboard
.writeText(text)
.then(() => showOK())
.catch((err) => {
console.log(err);
showFail();
});
}
19 changes: 12 additions & 7 deletions src/views/constant/ConstantTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
<v-spacer></v-spacer>
<v-btn
class="mr-1 text--secondary"
v-clipboard="getValueToCopy"
v-clipboard:success="showSuccessCopy"
@click="() => {
copy(getValueToCopy())
}"
small
text
>
Expand Down Expand Up @@ -49,11 +50,12 @@
<script>
import {mapActions} from "vuex";
import ConstantInfo from "./ConstantInfo";
import ReferenceContract from "@/views/constant/ReferenceContract";
import Michelson from "@/components/Michelson";
import {toTitleCase} from "@/utils/string";
import RawJsonViewer from "@/components/Dialogs/RawJsonViewer.vue";
import { copyToClipboard } from "@/utils/clipboard";
import {downloadFileFormContent} from "@/utils/download";
import {toTitleCase} from "@/utils/string";
import ReferenceContract from "@/views/constant/ReferenceContract";
export default {
name: "NetworkTab",
Expand Down Expand Up @@ -87,18 +89,21 @@ export default {
}
},
methods: {
...mapActions(["showClipboardOK", "showClipboardWarning"]),
...mapActions(["showClipboardOK", "showClipboardFail"]),
async getConstantInfo() {
this.loading = true;
this.constantInfo = await this.api.getConstant(this.$route.params.network, this.$route.params.address);
this.loading = false;
},
copy(text, successMessage, failMessage) {
copyToClipboard(text, this.showSuccessCopy.bind(null, successMessage), this.showClipboardFail.bind(null, failMessage));
},
getValueToCopy() {
return this.constantInfo.code;
},
showSuccessCopy() {
showSuccessCopy(successMessage) {
if (this.constantInfo.code.length) {
this.showClipboardOK();
this.showClipboardOK(successMessage);
}
},
downloadFile() {
Expand Down
15 changes: 10 additions & 5 deletions src/views/contract/CodeTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@

<v-btn
class="mr-1 text--secondary"
v-clipboard="getValueToCopy"
v-clipboard:success="showSuccessCopy"
@click="() => {
copy(getValueToCopy())
}"
small
text
>
Expand Down Expand Up @@ -73,6 +74,7 @@ import ErrorState from "@/components/ErrorState.vue";
import RawJsonViewer from "@/components/Dialogs/RawJsonViewer.vue";
import {downloadFileFormContent} from "@/utils/download";
import UsingConstantsDialog from "@/components/UsingConstantsDialog";
import { copyToClipboard } from "@/utils/clipboard";
export default {
props: {
Expand Down Expand Up @@ -130,10 +132,13 @@ export default {
clearInterval(this.renderingInterval);
},
methods: {
...mapActions(["showError", "showClipboardOK", "showClipboardWarning"]),
showSuccessCopy() {
...mapActions(["showError", "showClipboardOK", "showClipboardWarning", "showClipboardFail"]),
copy(text, successMessage, failMessage) {
copyToClipboard(text, this.showSuccessCopy.bind(null, successMessage), this.showClipboardFail.bind(null, failMessage));
},
showSuccessCopy(successMessage) {
if (this.selectedCode.length <= this.freezingAmount) {
this.showClipboardOK();
this.showClipboardOK(successMessage);
}
},
getValueToCopy() {
Expand Down
15 changes: 8 additions & 7 deletions src/views/contract/StorageTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,9 @@
</v-tooltip>
<v-btn
v-if="rawStorage && raw"
@click="
() => {
$clipboard(rawStorage);
showClipboardOK();
}
"
@click="() => {
copy(rawStorage)
}"
class="ml-2"
small
text
Expand Down Expand Up @@ -137,6 +134,7 @@ import RawJsonViewer from "@/components/Dialogs/RawJsonViewer.vue";
import MiguelTreeView from "@/components/MiguelTreeView.vue";
import TypeDef from "@/views/contract/TypeDef.vue";
import {shortcutOnly} from "../../utils/tz";
import { copyToClipboard } from "../../utils/clipboard";
export default {
name: "StorageTab",
Expand Down Expand Up @@ -213,7 +211,10 @@ export default {
},
},
methods: {
...mapActions(["showError", "showClipboardOK"]),
...mapActions(["showError", "showClipboardOK", "showClipboardFail"]),
copy(text, successMessage, failMessage) {
copyToClipboard(text, this.showClipboardOK.bind(null, successMessage), this.showClipboardFail.bind(null, failMessage));
},
getStorage(force = false) {
if (this.storage) {
this.raw = false;
Expand Down

0 comments on commit c43814c

Please sign in to comment.