Skip to content

Commit 5021038

Browse files
authored
Merge pull request #160 from mongoosejs/codex/add-copy-document-option-to-menu
Add 'Copy Document' option to document menu
2 parents 97e52de + de79b36 commit 5021038

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

frontend/src/document/document.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@
113113
>
114114
Add Field
115115
</button>
116+
<button
117+
@click="copyDocument(); desktopMenuOpen = false"
118+
type="button"
119+
class="flex items-center px-4 py-2 text-sm text-gray-700 hover:bg-blue-100"
120+
>
121+
Copy Document
122+
</button>
116123
<button
117124
@click="shouldShowDeleteModal=true; desktopMenuOpen = false"
118125
:disabled="!canManipulate"
@@ -201,6 +208,13 @@
201208
>
202209
Add Field
203210
</button>
211+
<button
212+
@click="copyDocument(); mobileMenuOpen = false"
213+
type="button"
214+
class="flex items-center px-4 py-2 text-sm text-gray-700 hover:bg-blue-100"
215+
>
216+
Copy Document
217+
</button>
204218
<button
205219
@click="shouldShowDeleteModal=true; mobileMenuOpen = false"
206220
:disabled="!canManipulate"

frontend/src/document/document.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,43 @@ module.exports = app => app.component('document', {
275275
this.changes = {};
276276
}
277277
},
278+
copyDocument() {
279+
if (!this.document) {
280+
return;
281+
}
282+
283+
const textToCopy = JSON.stringify(this.document, null, 2);
284+
const fallbackCopy = () => {
285+
if (typeof document === 'undefined') {
286+
return;
287+
}
288+
const textArea = document.createElement('textarea');
289+
textArea.value = textToCopy;
290+
textArea.setAttribute('readonly', '');
291+
textArea.style.position = 'absolute';
292+
textArea.style.left = '-9999px';
293+
document.body.appendChild(textArea);
294+
textArea.select();
295+
try {
296+
document.execCommand('copy');
297+
} finally {
298+
document.body.removeChild(textArea);
299+
}
300+
this.$toast.success('Document copied!');
301+
};
302+
303+
if (typeof navigator !== 'undefined' && navigator.clipboard && navigator.clipboard.writeText) {
304+
navigator.clipboard.writeText(textToCopy)
305+
.then(() => {
306+
this.$toast.success('Document copied!');
307+
})
308+
.catch(() => {
309+
fallbackCopy();
310+
});
311+
} else {
312+
fallbackCopy();
313+
}
314+
},
278315
goBack() {
279316
// Preserve query parameters when going back to models page
280317
this.$router.push({

0 commit comments

Comments
 (0)