Skip to content

Commit

Permalink
Merge pull request #414 from wttech/crypto-button
Browse files Browse the repository at this point in the history
added 'Protect Text' button to script editor
  • Loading branch information
dprzybyl authored Oct 19, 2023
2 parents 0d237d5 + 7c80e86 commit 8aace96
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
this.$textArea = this.$el.find('#cqsm').eq(0);
this.$fileName = this.$el.find('#fname').eq(0);
this.$validateButton = this.$el.find('#validateButton').eq(0);
this.$cryptoButton = this.$el.find('#cryptoButton').eq(0);
this.$saveButton = this.$el.find('#saveButton').eq(0);
this.$saveAndCloseButton = this.$el.find('#saveAndCloseButton').eq(0);
this.initialValue = this.$textArea.val();
Expand Down Expand Up @@ -132,30 +133,6 @@
}
});
},
protectText: function () {
const self = this;
let range = self.editor.find(/{{((?!}}).)+}}/, {
wrap: true,
regExp: true
});
const token = range && self.editor.session.getTextRange(range);
token && $.ajax({
type: 'POST',
async: false,
url: '/bin/apm/scripts/protect',
data: {
text: token.substring(token.indexOf('{{') + 2, token.indexOf('}}'))
}
}).done(function (data) {
setTimeout(function () {
range = self.editor.find(token, {
wrap: true
});
range && self.editor.session.replace(range, data.text);
}, 10);
});
},

initEditor: function () {
const self = this;
let editor = null;
Expand All @@ -179,13 +156,8 @@
});
});

editor.session.on('change', function (delta) {
const value = editor.session.getValue();
if (value.indexOf('{{') > -1
&& value.indexOf('{{') < value.lastIndexOf('}}')
&& delta.data.action.startsWith('insert')) {
self.protectText();
}
editor.session.selection.on('changeSelection', function() {
self.$cryptoButton.attr('disabled', editor.getSelectedText().length === 0);
});

return editor;
Expand Down Expand Up @@ -237,6 +209,21 @@
});
});

this.$cryptoButton.click(function () {
let range = self.editor.getSelectionRange()
let text = self.editor.getSelectedText();
$.ajax({
type: 'POST',
async: false,
url: '/bin/apm/scripts/protect',
data: {
text: text
}
}).done(function (data) {
self.editor.session.replace(range, data.text);
});
});

this.$saveButton.click(function () {
self.fileUpload();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@
icon="spellcheck"
text="Validate"
variant="actionBar"/>
<crypto
granite:id="cryptoButton"
granite:class="foundation-collection-action"
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/button"
disabled="true"
icon="lockOn"
text="Protect Text"
variant="actionBar"/>
</primary>
<secondary jcr:primaryType="nt:unstructured">
<cancel
Expand Down

0 comments on commit 8aace96

Please sign in to comment.