Skip to content

Instantly share code, notes, and snippets.

@lsmith77
Created January 10, 2023 22:07
Show Gist options
  • Save lsmith77/01e9049a70f6c984887488e96fb7c6aa to your computer and use it in GitHub Desktop.
Save lsmith77/01e9049a70f6c984887488e96fb7c6aa to your computer and use it in GitHub Desktop.
<script>
const copyListener = (e) => {
e.preventDefault();
};
document.addEventListener("copy", copyListener);
document.addEventListener("cut", copyListener);
function copyToClipboard()
{
console.log(this.selection);
// get the html for the current selection
// but this only seems to get the text for the current selection
html = this.selection.get();
addToClipboard(html);
}
function cutToClipboard()
{
html = this.selection.get();
addToClipboard(html);
// remove the selected html from the editor
// this.selection.delete is not a function
this.selection.delete();
}
function copyAllToClipboard()
{
html = this.html.get();
addToClipboard(html);
}
function addToClipboard(html)
{
html+= @json(__('content.witty_editor_viral_copy_text'));
let type = "text/html";
let blob = new Blob([html], { type });
let data = [new ClipboardItem({ [type]: blob })];
navigator.clipboard.write(data).then(
function () {
/* success */
},
function () {
/* failure */
}
);
}
FroalaEditor.DefineIcon('copy', {template: 'svg', PATH: 'M 9 0 C 7.039062 0 5.371094 1.25 4.757812 3 L 3 3 C 1.34375 3 0 4.34375 0 6 L 0 21 C 0 22.65625 1.34375 24 3 24 L 15 24 C 16.65625 24 18 22.65625 18 21 L 18 6 C 18 4.34375 16.65625 3 15 3 L 13.242188 3 C 12.628906 1.25 10.960938 0 9 0 Z M 9 3 C 9.828125 3 10.5 3.671875 10.5 4.5 C 10.5 5.328125 9.828125 6 9 6 C 8.171875 6 7.5 5.328125 7.5 4.5 C 7.5 3.671875 8.171875 3 9 3 Z M 5.25 9 L 12.75 9 C 13.164062 9 13.5 9.335938 13.5 9.75 C 13.5 10.164062 13.164062 10.5 12.75 10.5 L 5.25 10.5 C 4.835938 10.5 4.5 10.164062 4.5 9.75 C 4.5 9.335938 4.835938 9 5.25 9 Z M 5.25 9 '});
FroalaEditor.RegisterCommand('copy', {
title: @json(__('content.witty_editor_copy_button')),
focus: false,
undo: false,
refreshAfterCallback: false,
callback: copyAllToClipboard,
});
new FroalaEditor('#witty_editor', {
key: @json(config('app.froala_key')),
language: @json(config('app.locale')),
attribution: false,
autofocus: true,
documentReady: true,
spellcheck: false,
placeholderText: @json(__('content.witty_editor_placeholder_text')),
toolbarButtons: ['fullscreen', 'bold', 'italic', 'underline', 'strikeThrough', 'fontFamily', 'fontSize', 'color', 'paragraphFormat', 'align', 'formatOL', 'formatUL', 'insertLink', 'clearFormatting', 'undo', 'redo', 'trackChanges', 'copy', 'share', 'help'],
toolbarButtonsSM: ['fullscreen', 'bold', 'italic', 'underline', 'strikeThrough', 'fontFamily', 'fontSize', 'insertLink', 'trackChanges', 'copy', 'share'],
toolbarButtonsXS: ['fullscreen', 'bold', 'italic', 'underline', 'strikeThrough', 'fontSize', 'copy', 'share'],
events: {
'window.cut': cutToClipboard,
'window.copy': copyToClipboard,
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment