From b06cb26edc1d36a6b0a392c07c59d7b3382e16fa Mon Sep 17 00:00:00 2001 From: antelle Date: Sat, 14 Sep 2019 17:06:38 +0200 Subject: [PATCH] fix #743: copying entry fields to clipboard --- app/scripts/comp/copy-paste.js | 17 ++++++++++++++ app/scripts/comp/kdbx-to-html.js | 4 ++++ app/scripts/locales/base.json | 1 + app/scripts/models/entry-model.js | 5 ++++ app/scripts/views/details/details-view.js | 12 ++++++++++ app/templates/export/entry.hbs | 28 +++++++++++++---------- release-notes.md | 1 + 7 files changed, 56 insertions(+), 12 deletions(-) diff --git a/app/scripts/comp/copy-paste.js b/app/scripts/comp/copy-paste.js index 427906b9..355285b2 100644 --- a/app/scripts/comp/copy-paste.js +++ b/app/scripts/comp/copy-paste.js @@ -48,6 +48,23 @@ const CopyPaste = { hiddenInput.remove(); } }); + }, + + copyHtml(html) { + const el = document.createElement('div'); + el.innerHTML = html; + document.body.appendChild(el); + + const range = document.createRange(); + range.selectNodeContents(el); + const sel = window.getSelection(); + sel.removeAllRanges(); + sel.addRange(range); + + const result = document.execCommand('copy'); + + el.remove(); + return result; } }; diff --git a/app/scripts/comp/kdbx-to-html.js b/app/scripts/comp/kdbx-to-html.js index 702cc0e4..e24acdd5 100644 --- a/app/scripts/comp/kdbx-to-html.js +++ b/app/scripts/comp/kdbx-to-html.js @@ -105,6 +105,10 @@ const KdbxToHtml = { appVersion: RuntimeInfo.version, content }); + }, + + entryToHtml(db, entry) { + return walkEntry(db, entry, []); } }; diff --git a/app/scripts/locales/base.json b/app/scripts/locales/base.json index f4f02e3a..92e54335 100644 --- a/app/scripts/locales/base.json +++ b/app/scripts/locales/base.json @@ -273,6 +273,7 @@ "detMenuCopyUser": "Copy user", "detSetupOtp": "One-time passwords", "detClone": "Make a copy", + "detCopyEntryToClipboard": "Copy values to clipboard", "detClonedName": "Copy", "detAutoType": "Auto-type", "detAutoTypeSettings": "Auto-type settings", diff --git a/app/scripts/models/entry-model.js b/app/scripts/models/entry-model.js index 41b58505..3c946fad 100644 --- a/app/scripts/models/entry-model.js +++ b/app/scripts/models/entry-model.js @@ -6,6 +6,7 @@ const IconUrl = require('../util/icon-url'); const Otp = require('../util/otp'); const kdbxweb = require('kdbxweb'); const Ranking = require('../util/ranking'); +const KdbxToHtml = require('../comp/kdbx-to-html'); const EntryModel = Backbone.Model.extend({ defaults: {}, @@ -704,6 +705,10 @@ const EntryModel = Backbone.Model.extend({ const fieldWeight = fieldWeights[fieldName] || defaultFieldWeight; return rank + stringRank * fieldWeight; }, 0); + }, + + getHtml() { + return KdbxToHtml.entryToHtml(this.file.db, this.entry); } }); diff --git a/app/scripts/views/details/details-view.js b/app/scripts/views/details/details-view.js index 5ece6f33..33e6760c 100644 --- a/app/scripts/views/details/details-view.js +++ b/app/scripts/views/details/details-view.js @@ -453,6 +453,11 @@ const DetailsView = Backbone.View.extend({ }); } moreOptions.push({ value: 'clone', icon: 'clone', text: Locale.detClone }); + moreOptions.push({ + value: 'copy-to-clipboard', + icon: 'copy', + text: Locale.detCopyEntryToClipboard + }); const rect = this.moreView.labelEl[0].getBoundingClientRect(); dropdownView.render({ position: { top: rect.bottom, left: rect.left }, @@ -485,6 +490,9 @@ const DetailsView = Backbone.View.extend({ case 'clone': this.clone(); break; + case 'copy-to-clipboard': + this.copyToClipboard(); + break; default: if (e.item.lastIndexOf('add:', 0) === 0) { const fieldName = e.item.substr(4); @@ -967,6 +975,10 @@ const DetailsView = Backbone.View.extend({ Backbone.trigger('select-entry', newEntry); }, + copyToClipboard() { + CopyPaste.copyHtml(this.model.getHtml()); + }, + deleteFromTrash() { Alerts.yesno({ header: Locale.detDelFromTrash, diff --git a/app/templates/export/entry.hbs b/app/templates/export/entry.hbs index ff88ca0c..64d805a6 100644 --- a/app/templates/export/entry.hbs +++ b/app/templates/export/entry.hbs @@ -20,21 +20,25 @@ {{tags}} {{/if}} - - {{Res 'group'}} - {{path}} - - {{#if attachments.length}} + {{#if path}} - {{res 'detAttachments'}} - - {{#each attachments as |attachment|}} - {{attachment.name}} - {{~#unless @last}}, {{/unless}} - {{/each}} - + {{Res 'group'}} + {{path}} {{/if}} + {{#if attachments}} + {{#if attachments.length}} + + {{res 'detAttachments'}} + + {{#each attachments as |attachment|}} + {{attachment.name}} + {{~#unless @last}}, {{/unless}} + {{/each}} + + + {{/if}} + {{/if}} {{res 'detCreated'}} {{created}} diff --git a/release-notes.md b/release-notes.md index 3ce21227..1202d443 100644 --- a/release-notes.md +++ b/release-notes.md @@ -7,6 +7,7 @@ Release notes `+` #1255: file format version and kdf selection in settings `*` #502: increased the default value of encryption rounds `+` #348: configurable system-wide shortcuts +`+` #743: copying entry fields to clipboard `*` devtools are now opened with alt-cmd-I `-` fix #764: multiple attachments display `-` fix multi-line fields display in history